-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(balh): [workaround] 泰区番剧信息优先使用 season_id 获取 (#1298)
* perf(balh): [workaround] 泰区番剧信息优先使用 season_id 获取 pchpub/BiliRoaming-Rust-Server#122 * 保存 DB 实例 * wip: db变量改成Promise, 保证只打开一次 * wip: IndexedDb->BalhDb --------- Co-authored-by: ipcjs <gipcjs@gmail.com>
- Loading branch information
1 parent
2bc87a7
commit 5692a08
Showing
3 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const DB_NAME = 'balh'; | ||
const DB_VERSION = 1; | ||
|
||
let dbPromise: Promise<IDBDatabase> | undefined | ||
export function openDb() { | ||
return new Promise<IDBDatabase>((resolve, reject) => { | ||
var req = indexedDB.open(DB_NAME, DB_VERSION); | ||
req.onsuccess = function (evt) { | ||
resolve(this.result) | ||
} | ||
req.onerror = function (evt) { | ||
reject(evt) | ||
} | ||
|
||
req.onupgradeneeded = (evt: IDBVersionChangeEvent) => { | ||
var storeEPIDCache = (evt.currentTarget as IDBOpenDBRequest)?.result.createObjectStore( | ||
'ep_id_season_id', { keyPath: 'ep_id' }) | ||
storeEPIDCache.createIndex('season_id', 'season_id', { unique: false }) | ||
} | ||
}) | ||
} | ||
|
||
function getDb() { | ||
return dbPromise ??= openDb() | ||
} | ||
|
||
async function getObjectStore(store_name: string, mode: IDBTransactionMode) { | ||
const db = await getDb() | ||
var tx = db.transaction(store_name, mode); | ||
return tx.objectStore(store_name); | ||
} | ||
|
||
export namespace BalhDb { | ||
export async function setSsId(ep_id: number, season_id: string) { | ||
var store = await getObjectStore('ep_id_season_id', 'readwrite') | ||
store.put({ ep_id: ep_id, season_id: season_id }) | ||
} | ||
|
||
export function getSsId(ep_id: number): Promise<string> { | ||
return new Promise(async (resolve, reject) => { | ||
var store = await getObjectStore('ep_id_season_id', 'readonly'); | ||
var req: IDBRequest = store.get(ep_id) | ||
req.onsuccess = () => { | ||
if (!req.result) | ||
resolve('') | ||
else | ||
resolve(req.result.season_id) | ||
} | ||
req.onerror = (e) => { | ||
reject(e) | ||
} | ||
}) | ||
} | ||
} |