From bece0aa78492ff89878e4867309e5c3538ff2e8b Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 25 Jul 2024 00:12:24 +0300 Subject: [PATCH] client-ts: Port services/mutex --- src/public/app/utils/{mutex.js => mutex.ts} | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) rename src/public/app/utils/{mutex.js => mutex.ts} (73%) diff --git a/src/public/app/utils/mutex.js b/src/public/app/utils/mutex.ts similarity index 73% rename from src/public/app/utils/mutex.js rename to src/public/app/utils/mutex.ts index 483e0f345..c37e78003 100644 --- a/src/public/app/utils/mutex.js +++ b/src/public/app/utils/mutex.ts @@ -1,12 +1,13 @@ export default class Mutex { + private current: Promise; + constructor() { this.current = Promise.resolve(); } - /** @returns {Promise} */ lock() { - let resolveFun; - const subPromise = new Promise(resolve => resolveFun = () => resolve()); + let resolveFun: () => void; + const subPromise = new Promise(resolve => resolveFun = () => resolve()); // Caller gets a promise that resolves when the current outstanding lock resolves const newPromise = this.current.then(() => resolveFun); // Don't allow the next request until the new promise is done @@ -15,7 +16,7 @@ export default class Mutex { return newPromise; }; - async runExclusively(cb) { + async runExclusively(cb: () => Promise) { const unlock = await this.lock(); try {