async-queue-runner 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/_cjs/locking.js CHANGED
@@ -12,39 +12,52 @@ function reversePromiseFactory() {
12
12
  };
13
13
  }
14
14
  class LockManager {
15
- lockedScopes = new Map();
15
+ queuedScopes = new Map();
16
+ manualLocks = new Map();
16
17
  isLocked(scope) {
17
- return this.lockedScopes.has(scope);
18
+ return this.queuedScopes.has(scope);
18
19
  }
19
20
  lock(scope) {
20
- if (this.lockedScopes.has(scope)) {
21
+ if (this.queuedScopes.has(scope)) {
21
22
  throw new Error(`scope "${scope}" is already locked`);
22
23
  }
23
- this.lockedScopes.set(scope, reversePromiseFactory());
24
+ const manual = reversePromiseFactory();
25
+ this.manualLocks.set(scope, manual);
26
+ this.queuedScopes.set(scope, manual.promise);
24
27
  }
25
28
  unlock(scope) {
26
- if (!this.lockedScopes.has(scope)) {
29
+ const manual = this.manualLocks.get(scope);
30
+ if (!manual) {
27
31
  return;
28
32
  }
29
- this.lockedScopes.get(scope).resolve();
30
- this.lockedScopes.delete(scope);
33
+ manual.resolve();
34
+ this.manualLocks.delete(scope);
35
+ if (this.queuedScopes.get(scope) === manual.promise) {
36
+ this.queuedScopes.delete(scope);
37
+ }
31
38
  }
32
39
  wait(scope) {
33
- if (!this.lockedScopes.has(scope)) {
40
+ const tail = this.queuedScopes.get(scope);
41
+ if (!tail) {
34
42
  return Promise.resolve();
35
43
  }
36
- return this.lockedScopes.get(scope).promise;
44
+ return tail;
37
45
  }
38
46
  async runWithLock(scope, fn) {
39
- if (this.isLocked(scope)) {
40
- await this.wait(scope);
47
+ const previous = this.queuedScopes.get(scope);
48
+ const current = reversePromiseFactory();
49
+ this.queuedScopes.set(scope, current.promise);
50
+ if (previous) {
51
+ await previous;
41
52
  }
42
- this.lock(scope);
43
53
  try {
44
54
  await fn();
45
55
  }
46
56
  finally {
47
- this.unlock(scope);
57
+ current.resolve();
58
+ if (this.queuedScopes.get(scope) === current.promise && !this.manualLocks.has(scope)) {
59
+ this.queuedScopes.delete(scope);
60
+ }
48
61
  }
49
62
  }
50
63
  }
package/_esm/locking.js CHANGED
@@ -9,39 +9,52 @@ function reversePromiseFactory() {
9
9
  };
10
10
  }
11
11
  export class LockManager {
12
- lockedScopes = new Map();
12
+ queuedScopes = new Map();
13
+ manualLocks = new Map();
13
14
  isLocked(scope) {
14
- return this.lockedScopes.has(scope);
15
+ return this.queuedScopes.has(scope);
15
16
  }
16
17
  lock(scope) {
17
- if (this.lockedScopes.has(scope)) {
18
+ if (this.queuedScopes.has(scope)) {
18
19
  throw new Error(`scope "${scope}" is already locked`);
19
20
  }
20
- this.lockedScopes.set(scope, reversePromiseFactory());
21
+ const manual = reversePromiseFactory();
22
+ this.manualLocks.set(scope, manual);
23
+ this.queuedScopes.set(scope, manual.promise);
21
24
  }
22
25
  unlock(scope) {
23
- if (!this.lockedScopes.has(scope)) {
26
+ const manual = this.manualLocks.get(scope);
27
+ if (!manual) {
24
28
  return;
25
29
  }
26
- this.lockedScopes.get(scope).resolve();
27
- this.lockedScopes.delete(scope);
30
+ manual.resolve();
31
+ this.manualLocks.delete(scope);
32
+ if (this.queuedScopes.get(scope) === manual.promise) {
33
+ this.queuedScopes.delete(scope);
34
+ }
28
35
  }
29
36
  wait(scope) {
30
- if (!this.lockedScopes.has(scope)) {
37
+ const tail = this.queuedScopes.get(scope);
38
+ if (!tail) {
31
39
  return Promise.resolve();
32
40
  }
33
- return this.lockedScopes.get(scope).promise;
41
+ return tail;
34
42
  }
35
43
  async runWithLock(scope, fn) {
36
- if (this.isLocked(scope)) {
37
- await this.wait(scope);
44
+ const previous = this.queuedScopes.get(scope);
45
+ const current = reversePromiseFactory();
46
+ this.queuedScopes.set(scope, current.promise);
47
+ if (previous) {
48
+ await previous;
38
49
  }
39
- this.lock(scope);
40
50
  try {
41
51
  await fn();
42
52
  }
43
53
  finally {
44
- this.unlock(scope);
54
+ current.resolve();
55
+ if (this.queuedScopes.get(scope) === current.promise && !this.manualLocks.has(scope)) {
56
+ this.queuedScopes.delete(scope);
57
+ }
45
58
  }
46
59
  }
47
60
  }
@@ -7,7 +7,8 @@ export type LockingContext = {
7
7
  runWithLock: (scope: string, fn: () => Promise<void>) => Promise<void>;
8
8
  };
9
9
  export declare class LockManager implements LockingContext {
10
- private lockedScopes;
10
+ private queuedScopes;
11
+ private manualLocks;
11
12
  isLocked(scope: string): boolean;
12
13
  lock(scope: string): void;
13
14
  unlock(scope: string): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "async-queue-runner",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Library to run in parallel extendable queue of tasks",
5
5
  "scripts": {
6
6
  "compile": "tsc --project ./tsconfig.json",