[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-wallet-core] 02/04: wallet-core: simplify/optimize async conditio
From: |
gnunet |
Subject: |
[taler-wallet-core] 02/04: wallet-core: simplify/optimize async condition |
Date: |
Tue, 22 Aug 2023 08:55:38 +0200 |
This is an automated email from the git hooks/post-receive script.
dold pushed a commit to branch master
in repository wallet-core.
commit 8c670bd06d62bb82f714f4ca8a8730b2abacc013
Author: Florian Dold <florian@dold.me>
AuthorDate: Tue Aug 22 08:54:28 2023 +0200
wallet-core: simplify/optimize async condition
---
.../taler-wallet-core/src/util/promiseUtils.ts | 24 +++++++++++-----------
1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/packages/taler-wallet-core/src/util/promiseUtils.ts
b/packages/taler-wallet-core/src/util/promiseUtils.ts
index d409686d9..23f1c06a5 100644
--- a/packages/taler-wallet-core/src/util/promiseUtils.ts
+++ b/packages/taler-wallet-core/src/util/promiseUtils.ts
@@ -23,6 +23,8 @@ export interface OpenedPromise<T> {
/**
* Get an unresolved promise together with its extracted resolve / reject
* function.
+ *
+ * Recent ECMAScript proposals also call this a promise capability.
*/
export function openPromise<T>(): OpenedPromise<T> {
let resolve: ((x?: any) => void) | null = null;
@@ -39,22 +41,20 @@ export function openPromise<T>(): OpenedPromise<T> {
}
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;
- }
+ private promCap?: OpenedPromise<void> = undefined;
+ constructor() {}
wait(): Promise<void> {
- return this._waitPromise;
+ if (!this.promCap) {
+ this.promCap = openPromise<void>();
+ }
+ return this.promCap.promise;
}
trigger(): void {
- this._resolveWaitPromise();
- const op = openPromise<void>();
- this._waitPromise = op.promise;
- this._resolveWaitPromise = op.resolve;
+ if (this.promCap) {
+ this.promCap.resolve();
+ }
+ this.promCap = undefined;
}
}
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.