Skip to content

Commit

Permalink
保存 DB 实例
Browse files Browse the repository at this point in the history
  • Loading branch information
Howard20181 authored and ipcjs committed Jan 6, 2024
1 parent cd6926a commit f61149f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BiliBiliApi } from "../../api/bilibili"
import { BiliPlusApi } from "../../api/biliplus"
import { getObjectStore, openDb } from "../../util/IndexedDB"
import { setSsId } from "../../util/IndexedDB"
import { Converters } from "../../util/converters"
import { cookieStorage } from "../../util/cookie"
import { util_init } from "../../util/initiator"
Expand Down Expand Up @@ -130,7 +130,6 @@ async function fixThailandSeason(ep_id: string, season_id: string) {

origin.result.episodes = []
if (origin.result.modules.length > 0) {
var store = getObjectStore(await openDb(), 'ep_id_season_id', 'readwrite')
origin.result.modules[0].data.episodes.forEach((ep) => {
ep.episode_status = ep.status
ep.ep_id = ep.id
Expand All @@ -139,7 +138,7 @@ async function fixThailandSeason(ep_id: string, season_id: string) {
origin.result.episodes?.push(ep)
if (season_id !== '5551')
try {
store.put({ ep_id: ep.id, season_id: season_id })
setSsId(ep.id, season_id)
} catch (e) {
log('addSsEpId error', e)
}
Expand Down
24 changes: 7 additions & 17 deletions packages/unblock-area-limit/src/util/IndexedDB.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const DB_NAME = 'balh';
const DB_VERSION = 1;

let db: IDBDatabase | null = null;
export function openDb() {
return new Promise<IDBDatabase>((resolve, reject) => {
var req = indexedDB.open(DB_NAME, DB_VERSION);
Expand All @@ -19,31 +20,20 @@ export function openDb() {
})
}

export function getObjectStore(db: IDBDatabase, store_name: string, mode: IDBTransactionMode) {
async function getObjectStore(store_name: string, mode: IDBTransactionMode) {
if (db === null) db = await openDb()
var tx = db.transaction(store_name, mode);
return tx.objectStore(store_name);
}

async function clearObjectStore(store_name: string) {
var store = getObjectStore(await openDb(), store_name, 'readwrite');
var req = store.clear()
req.onerror = function (evt) {
console.error("clearObjectStore:", evt);
}
}

export function getBlob(key: any, store: IDBObjectStore, success_callback: (blob: Blob) => void) {
var req: IDBRequest = store.get(key)
req.onsuccess = async function (evt) {
var value = (evt.target as IDBRequest)?.result
if (value)
success_callback(value)
}
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 = getObjectStore(await openDb(), 'ep_id_season_id', 'readonly');
var store = await getObjectStore('ep_id_season_id', 'readonly');
var req: IDBRequest = store.get(ep_id)
req.onsuccess = () => {
if (!req.result)
Expand Down

0 comments on commit f61149f

Please sign in to comment.