Skip to content

Commit

Permalink
feat: withLock support this scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Feb 22, 2024
1 parent 554afe3 commit eee00e4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ Calling `withLock` with the same `scope` and `key` will ensure that the callback
```typescript
import {withLock} from "lifecycle-utils";

const scope = {}; // can be a reference to any object you like
const scope = {name: 'Tommy'}; // can be a reference to any object you like
const startTime = Date.now();

async function doSomething(index: number): number {
return await withLock(scope, "myKey", async () => {
return await withLock(scope, "myKey", async function () {
await new Promise(resolve => setTimeout(resolve, 1000));
console.log("index:", index, "time:", Date.now() - startTime);
console.log("index:", index, "time:", Date.now() - startTime, "name:", this.name);
return 42;
});
}
Expand Down
14 changes: 7 additions & 7 deletions src/withLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const locks = new Map<any, Map<string, Promise<any>>>();
/**
* Only allow one instance of the callback to run at a time for a given `scope` and `key`.
*/
export async function withLock<ReturnType>(scope: any, key: string, callback: () => Promise<ReturnType>): Promise<ReturnType>;
export async function withLock<ReturnType>(
scope: any, key: string, acquireLockSignal: AbortSignal | undefined, callback: () => Promise<ReturnType>
export async function withLock<ReturnType, ScopeType>(scope: ScopeType, key: string, callback: (this: ScopeType) => Promise<ReturnType>): Promise<ReturnType>;
export async function withLock<ReturnType, ScopeType>(
scope: ScopeType, key: string, acquireLockSignal: AbortSignal | undefined, callback: (this: ScopeType) => Promise<ReturnType>
): Promise<ReturnType>;
export async function withLock<ReturnType>(
scope: any,
export async function withLock<ReturnType, ScopeType>(
scope: ScopeType,
key: string,
acquireLockSignalOrCallback: AbortSignal | undefined | (() => Promise<ReturnType>),
acquireLockSignalOrCallback: AbortSignal | undefined | ((this: ScopeType) => Promise<ReturnType>),
callback?: () => Promise<ReturnType>
): Promise<ReturnType> {
let acquireLockSignal: AbortSignal | undefined = undefined;
Expand Down Expand Up @@ -47,7 +47,7 @@ export async function withLock<ReturnType>(
throw acquireLockSignal.reason;
}

const promise = callback();
const promise = callback.call(scope);

if (!locks.has(scope))
locks.set(scope, new Map());
Expand Down
12 changes: 12 additions & 0 deletions test/withLock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,18 @@ describe("withLock", () => {
// do nothing
}
});

test("this scope works", async () => {
const scope = {data: 1};
const key = "key";

let calls = 0;
await withLock(scope, key, async function (){
expect(this).toBe(scope);
calls++;
});
expect(calls).toBe(1);
});
});


Expand Down

0 comments on commit eee00e4

Please sign in to comment.