From 18c9c493a8e7050dc2d6b4ba57f09834391c366c Mon Sep 17 00:00:00 2001 From: Roger Qiu Date: Thu, 30 Jun 2022 16:42:04 +1000 Subject: [PATCH] WIP --- src/LockBox.ts | 28 +++++++++++++++++++++------- src/types.ts | 9 ++++++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/LockBox.ts b/src/LockBox.ts index e001d19..ace9726 100644 --- a/src/LockBox.ts +++ b/src/LockBox.ts @@ -4,7 +4,7 @@ import type { Lockable, MultiLockRequest, MultiLockAcquire, - MultiLockAcquired + MultiLockAcquired, } from './types'; import { withF, withG } from '@matrixai/resources'; import { ErrorAsyncLocksLockBoxConflict } from './errors'; @@ -12,7 +12,9 @@ import { ErrorAsyncLocksLockBoxConflict } from './errors'; class LockBox implements Lockable { protected _locks: Map = new Map(); - public lock(...requests: Array>): ResourceAcquire> { + public lock( + ...requests: Array> + ): ResourceAcquire> { return async () => { // Convert to strings // This creates a copy of the requests @@ -215,8 +217,12 @@ class LockBox implements Lockable { f: (multiLocks: Array>) => Promise, ] ): Promise { - const f = params.pop() as (multiLocks: Array>) => Promise; - const lockAcquires = this.lockMulti(...(params as Array>)); + const f = params.pop() as ( + multiLocks: Array>, + ) => Promise; + const lockAcquires = this.lockMulti( + ...(params as Array>), + ); const lockAcquires_: Array>> = lockAcquires.map( @@ -224,7 +230,10 @@ class LockBox implements Lockable { (...r) => lockAcquire(...r).then( ([lockRelease, lock]) => - [lockRelease, [key, lock, ...lockingParams]] as [ResourceRelease, MultiLockAcquired], + [lockRelease, [key, lock, ...lockingParams]] as [ + ResourceRelease, + MultiLockAcquired, + ], ), ); return withF(lockAcquires_, f); @@ -256,14 +265,19 @@ class LockBox implements Lockable { const g = params.pop() as ( multiLocks: Array>, ) => AsyncGenerator; - const lockAcquires = this.lockMulti(...(params as Array>)); + const lockAcquires = this.lockMulti( + ...(params as Array>), + ); const lockAcquires_: Array>> = lockAcquires.map( ([key, lockAcquire, ...lockingParams]) => (...r) => lockAcquire(...r).then( ([lockRelease, lock]) => - [lockRelease, [key, lock, ...lockingParams]] as [ResourceRelease, MultiLockAcquired], + [lockRelease, [key, lock, ...lockingParams]] as [ + ResourceRelease, + MultiLockAcquired, + ], ), ); return withG(lockAcquires_, g); diff --git a/src/types.ts b/src/types.ts index 8bab0f7..15a4f09 100644 --- a/src/types.ts +++ b/src/types.ts @@ -41,4 +41,11 @@ type MultiLockAcquired = [ ...lockingParams: Parameters, ]; -export type { POJO, ToString, Lockable, MultiLockRequest, MultiLockAcquire, MultiLockAcquired }; +export type { + POJO, + ToString, + Lockable, + MultiLockRequest, + MultiLockAcquire, + MultiLockAcquired, +};