-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Open the a1111 image and display the corresponding resources of Civitai
- Loading branch information
Showing
5 changed files
with
367 additions
and
20 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,69 @@ | ||
/** | ||
* 資料表名稱 | ||
*/ | ||
var DbStoreName = { | ||
civitaiResources: "civitaiResources", | ||
} | ||
|
||
class IndexedDBManager { | ||
|
||
private storeNames: string[] = [DbStoreName.civitaiResources]; // 資料表名稱 | ||
private dbPromise: Promise<IDBDatabase>; | ||
constructor(private dbName: string, private version: number) { | ||
this.dbPromise = this.init(); | ||
} | ||
|
||
private init(): Promise<IDBDatabase> { | ||
|
||
return new Promise((resolve, reject) => { | ||
const request = indexedDB.open(this.dbName, this.version); | ||
|
||
request.onerror = (event) => { | ||
reject("Error opening DB"); | ||
}; | ||
|
||
request.onupgradeneeded = (event) => { | ||
const db = (event.target as IDBOpenDBRequest).result; | ||
this.storeNames.forEach(storeName => { | ||
if (!db.objectStoreNames.contains(storeName)) { | ||
db.createObjectStore(storeName, { keyPath: "id" }); | ||
} | ||
}); | ||
resolve(db); | ||
}; | ||
|
||
request.onsuccess = (event) => { | ||
resolve((event.target as IDBOpenDBRequest).result); | ||
}; | ||
}); | ||
} | ||
|
||
public async getData(storeName: string, id: string): Promise<any> { | ||
const db = await this.dbPromise; | ||
return new Promise((resolve, reject) => { | ||
const transaction = db.transaction([storeName]); | ||
const objectStore = transaction.objectStore(storeName); | ||
const request = objectStore.get(id); | ||
|
||
request.onerror = (event) => { | ||
reject("Error getting data from DB"); | ||
}; | ||
|
||
request.onsuccess = () => { | ||
resolve(request.result); | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* | ||
* @param storeName 資料表名稱 | ||
* @param data 必須是物件,且有 id 屬性,例如 {id:123, name:"abc"} | ||
*/ | ||
public async saveData(storeName: string, data: any): Promise<void> { | ||
const db = await this.dbPromise; | ||
const transaction = db.transaction([storeName], "readwrite"); | ||
const objectStore = transaction.objectStore(storeName); | ||
objectStore.add(data); | ||
} | ||
} |
Oops, something went wrong.