diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/asyncMemo.ts | 4 | ||||
-rw-r--r-- | src/util/promiseUtils.ts | 21 | ||||
-rw-r--r-- | src/util/query.ts | 19 | ||||
-rw-r--r-- | src/util/timer.ts | 8 |
4 files changed, 49 insertions, 3 deletions
diff --git a/src/util/asyncMemo.ts b/src/util/asyncMemo.ts index 8b7b1c9bb..34868ab4f 100644 --- a/src/util/asyncMemo.ts +++ b/src/util/asyncMemo.ts @@ -21,8 +21,8 @@ export interface MemoEntry<T> { } export class AsyncOpMemo<T> { - n = 0; - memo: { [k: string]: MemoEntry<T> } = {}; + private n = 0; + private memo: { [k: string]: MemoEntry<T> } = {}; put(key: string, p: Promise<T>): Promise<T> { const n = this.n++; this.memo[key] = { diff --git a/src/util/promiseUtils.ts b/src/util/promiseUtils.ts index eb649471b..9add2c407 100644 --- a/src/util/promiseUtils.ts +++ b/src/util/promiseUtils.ts @@ -36,4 +36,25 @@ export function openPromise<T>(): OpenedPromise<T> { throw Error(); } return { resolve, reject, promise }; +} + +export class AsyncCondition { + private _waitPromise: Promise<void>; + private _resolveWaitPromise: (val: void) => void; + constructor() { + const op = openPromise<void>(); + this._waitPromise = op.promise; + this._resolveWaitPromise = op.resolve; + } + + wait(): Promise<void> { + return this._waitPromise; + } + + trigger(): void { + this._resolveWaitPromise(); + const op = openPromise<void>(); + this._waitPromise = op.promise; + this._resolveWaitPromise = op.resolve; + } }
\ No newline at end of file diff --git a/src/util/query.ts b/src/util/query.ts index 5726bcaa6..6942d471e 100644 --- a/src/util/query.ts +++ b/src/util/query.ts @@ -351,15 +351,32 @@ class TransactionHandle { } } +export function runWithReadTransaction<T>( + db: IDBDatabase, + stores: Store<any>[], + f: (t: TransactionHandle) => Promise<T>, +): Promise<T> { + return runWithTransaction<T>(db, stores, f, "readonly"); +} + export function runWithWriteTransaction<T>( db: IDBDatabase, stores: Store<any>[], f: (t: TransactionHandle) => Promise<T>, ): Promise<T> { + return runWithTransaction<T>(db, stores, f, "readwrite"); +} + +function runWithTransaction<T>( + db: IDBDatabase, + stores: Store<any>[], + f: (t: TransactionHandle) => Promise<T>, + mode: "readonly" | "readwrite", +): Promise<T> { const stack = Error("Failed transaction was started here."); return new Promise((resolve, reject) => { const storeName = stores.map(x => x.name); - const tx = db.transaction(storeName, "readwrite"); + const tx = db.transaction(storeName, mode); let funResult: any = undefined; let gotFunResult: boolean = false; tx.oncomplete = () => { diff --git a/src/util/timer.ts b/src/util/timer.ts index d3bb5d485..865c17faf 100644 --- a/src/util/timer.ts +++ b/src/util/timer.ts @@ -105,6 +105,14 @@ export class TimerGroup { } } + resolveAfter(delayMs: number): Promise<void> { + return new Promise<void>((resolve, reject) => { + this.after(delayMs, () => { + resolve(); + }); + }); + } + after(delayMs: number, callback: () => void): TimerHandle { if (this.stopped) { console.warn("dropping timer since timer group is stopped"); |