summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2019-12-02 17:35:47 +0100
committerFlorian Dold <florian.dold@gmail.com>2019-12-02 17:35:47 +0100
commitb5ee6b7b4ee506712f51e1b90e9256c4b0c0c603 (patch)
tree7d8bb4398ab52b58a5223d37e058eaea6c72f963 /src/util
parente1369ff7e8fc02116b9c4261036f0e42e3423cf4 (diff)
downloadwallet-core-b5ee6b7b4ee506712f51e1b90e9256c4b0c0c603.tar.gz
wallet-core-b5ee6b7b4ee506712f51e1b90e9256c4b0c0c603.tar.bz2
wallet-core-b5ee6b7b4ee506712f51e1b90e9256c4b0c0c603.zip
pending operations WIP
Diffstat (limited to 'src/util')
-rw-r--r--src/util/asyncMemo.ts4
-rw-r--r--src/util/promiseUtils.ts21
-rw-r--r--src/util/query.ts19
-rw-r--r--src/util/timer.ts8
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");