Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
CMCDragonkai committed Jun 30, 2022
1 parent d50ac51 commit 18c9c49
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
28 changes: 21 additions & 7 deletions src/LockBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import type {
Lockable,
MultiLockRequest,
MultiLockAcquire,
MultiLockAcquired
MultiLockAcquired,
} from './types';
import { withF, withG } from '@matrixai/resources';
import { ErrorAsyncLocksLockBoxConflict } from './errors';

class LockBox<L extends Lockable = Lockable> implements Lockable {
protected _locks: Map<string, L> = new Map();

public lock(...requests: Array<MultiLockRequest<L>>): ResourceAcquire<LockBox<L>> {
public lock(
...requests: Array<MultiLockRequest<L>>
): ResourceAcquire<LockBox<L>> {
return async () => {
// Convert to strings
// This creates a copy of the requests
Expand Down Expand Up @@ -215,16 +217,23 @@ class LockBox<L extends Lockable = Lockable> implements Lockable {
f: (multiLocks: Array<MultiLockAcquired<L>>) => Promise<T>,
]
): Promise<T> {
const f = params.pop() as (multiLocks: Array<MultiLockAcquired<L>>) => Promise<T>;
const lockAcquires = this.lockMulti(...(params as Array<MultiLockRequest<L>>));
const f = params.pop() as (
multiLocks: Array<MultiLockAcquired<L>>,
) => Promise<T>;
const lockAcquires = this.lockMulti(
...(params as Array<MultiLockRequest<L>>),
);

const lockAcquires_: Array<ResourceAcquire<MultiLockAcquired<L>>> =
lockAcquires.map(
([key, lockAcquire, ...lockingParams]) =>
(...r) =>
lockAcquire(...r).then(
([lockRelease, lock]) =>
[lockRelease, [key, lock, ...lockingParams]] as [ResourceRelease, MultiLockAcquired<L>],
[lockRelease, [key, lock, ...lockingParams]] as [
ResourceRelease,
MultiLockAcquired<L>,
],
),
);
return withF(lockAcquires_, f);
Expand Down Expand Up @@ -256,14 +265,19 @@ class LockBox<L extends Lockable = Lockable> implements Lockable {
const g = params.pop() as (
multiLocks: Array<MultiLockAcquired<L>>,
) => AsyncGenerator<T, TReturn, TNext>;
const lockAcquires = this.lockMulti(...(params as Array<MultiLockRequest<L>>));
const lockAcquires = this.lockMulti(
...(params as Array<MultiLockRequest<L>>),
);
const lockAcquires_: Array<ResourceAcquire<MultiLockAcquired<L>>> =
lockAcquires.map(
([key, lockAcquire, ...lockingParams]) =>
(...r) =>
lockAcquire(...r).then(
([lockRelease, lock]) =>
[lockRelease, [key, lock, ...lockingParams]] as [ResourceRelease, MultiLockAcquired<L>],
[lockRelease, [key, lock, ...lockingParams]] as [
ResourceRelease,
MultiLockAcquired<L>,
],
),
);
return withG(lockAcquires_, g);
Expand Down
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ type MultiLockAcquired<L extends Lockable = Lockable> = [
...lockingParams: Parameters<L['lock']>,
];

export type { POJO, ToString, Lockable, MultiLockRequest, MultiLockAcquire, MultiLockAcquired };
export type {
POJO,
ToString,
Lockable,
MultiLockRequest,
MultiLockAcquire,
MultiLockAcquired,
};

0 comments on commit 18c9c49

Please sign in to comment.