-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kiosk: store built js in indexeddb, not local storage
- Loading branch information
1 parent
2e524af
commit fd346f1
Showing
6 changed files
with
120 additions
and
39 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,90 @@ | ||
import { openDB, IDBPDatabase } from "idb"; | ||
|
||
class KioskDb { | ||
db: IDBPDatabase | undefined; | ||
|
||
public async initializeAsync() { | ||
if (this.db) return; | ||
this.db = await openDB("kiosk", 1, { | ||
upgrade(db) { | ||
db.createObjectStore("builtjs"); | ||
}, | ||
}); | ||
} | ||
|
||
private async getAsync<T>(storeName: string, key: string): Promise<T | undefined> { | ||
if (!this.db) { | ||
throw new Error("IndexedDb not initialized."); | ||
} | ||
try { | ||
return await this.db.get(storeName, key); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
private async setAsync<T>(storeName: string, key: string, value: T): Promise<void> { | ||
if (!this.db) { | ||
throw new Error("IndexedDb not initialized."); | ||
} | ||
try { | ||
await this.db.put(storeName, value, key); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
private async deleteAsync(storeName: string, key: string): Promise<void> { | ||
if (!this.db) { | ||
throw new Error("IndexedDb not initialized."); | ||
} | ||
try { | ||
await this.db.delete(storeName, key); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
public async getBuiltJsInfoAsync( | ||
gameId: string | ||
): Promise<pxtc.BuiltSimJsInfo | undefined> { | ||
const ver = pxt.appTarget?.versions?.target; | ||
if (!ver) return undefined; | ||
const key = `${ver}:${gameId}`; | ||
const rec = await this.getAsync<pxtc.BuiltSimJsInfo>("builtjs", key); | ||
return rec; | ||
} | ||
|
||
public async setBuiltJsInfoAsync( | ||
gameId: string, | ||
info: pxtc.BuiltSimJsInfo | ||
): Promise<void> { | ||
const ver = pxt.appTarget?.versions?.target; | ||
if (!ver) return; | ||
if (/^S/.test(gameId)) return; // skip persistent-share games for now (need to get their actual gameId and use that as the key) | ||
const key = `${ver}:${gameId}`; | ||
await this.setAsync("builtjs", key, info); | ||
} | ||
} | ||
|
||
const db = new KioskDb(); | ||
|
||
let initializeAsync = async () => { | ||
initializeAsync = async () => {}; | ||
await db.initializeAsync(); | ||
}; | ||
|
||
export async function getBuiltJsInfoAsync( | ||
gameId: string | ||
): Promise<pxtc.BuiltSimJsInfo | undefined> { | ||
await initializeAsync(); | ||
return await db.getBuiltJsInfoAsync(gameId); | ||
} | ||
|
||
export async function setBuiltJsInfoAsync( | ||
gameId: string, | ||
info: pxtc.BuiltSimJsInfo | ||
): Promise<void> { | ||
await initializeAsync(); | ||
await db.setBuiltJsInfoAsync(gameId, info); | ||
} |
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