From c8eb7ea7ac911a53242c239dfe461d5450b91ee5 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Tue, 16 Nov 2021 15:06:19 +0530 Subject: [PATCH] Convert Lock.js to ts --- src/matrix/e2ee/olm/Decryption.js | 2 +- src/utils/{Lock.js => Lock.ts} | 26 ++++++++++++-------------- src/utils/LockMap.js | 2 +- 3 files changed, 14 insertions(+), 16 deletions(-) rename src/utils/{Lock.js => Lock.ts} (83%) diff --git a/src/matrix/e2ee/olm/Decryption.js b/src/matrix/e2ee/olm/Decryption.js index 0af3bd23..16e617a5 100644 --- a/src/matrix/e2ee/olm/Decryption.js +++ b/src/matrix/e2ee/olm/Decryption.js @@ -16,7 +16,7 @@ limitations under the License. import {DecryptionError} from "../common.js"; import {groupBy} from "../../../utils/groupBy"; -import {MultiLock} from "../../../utils/Lock.js"; +import {MultiLock} from "../../../utils/Lock"; import {Session} from "./Session.js"; import {DecryptionResult} from "../DecryptionResult.js"; diff --git a/src/utils/Lock.js b/src/utils/Lock.ts similarity index 83% rename from src/utils/Lock.js rename to src/utils/Lock.ts index 8cfc733f..ce7101de 100644 --- a/src/utils/Lock.js +++ b/src/utils/Lock.ts @@ -15,12 +15,10 @@ limitations under the License. */ export class Lock { - constructor() { - this._promise = null; - this._resolve = null; - } + private _promise: Promise | null = null; + private _resolve: (() => void) | null = null; - tryTake() { + tryTake(): boolean { if (!this._promise) { this._promise = new Promise(resolve => { this._resolve = resolve; @@ -30,17 +28,17 @@ export class Lock { return false; } - async take() { + async take(): Promise { while(!this.tryTake()) { await this.released(); } } - get isTaken() { + get isTaken(): boolean { return !!this._promise; } - release() { + release(): void { if (this._resolve) { this._promise = null; const resolve = this._resolve; @@ -49,17 +47,17 @@ export class Lock { } } - released() { + released(): Promise | null { return this._promise; } } export class MultiLock { - constructor(locks) { - this.locks = locks; + + constructor(public readonly locks: Lock[]) { } - release() { + release(): void { for (const lock of this.locks) { lock.release(); } @@ -86,9 +84,9 @@ export function tests() { lock.tryTake(); let first; - lock.released().then(() => first = lock.tryTake()); + lock.released()!.then(() => first = lock.tryTake()); let second; - lock.released().then(() => second = lock.tryTake()); + lock.released()!.then(() => second = lock.tryTake()); const promise = lock.released(); lock.release(); await promise; diff --git a/src/utils/LockMap.js b/src/utils/LockMap.js index a73dee4a..567acf4c 100644 --- a/src/utils/LockMap.js +++ b/src/utils/LockMap.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {Lock} from "./Lock.js"; +import {Lock} from "./Lock"; export class LockMap { constructor() {