@vnejs/semaphore 0.2.0 → 0.2.1

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/dist/index.js CHANGED
@@ -42,7 +42,10 @@ export class Semaphore {
42
42
  this.weight += e.weight;
43
43
  e.inProcess = true;
44
44
  this.queueCurrent.push(e);
45
- e.func().then(() => {
45
+ Promise.resolve()
46
+ .then(() => e.func())
47
+ .catch(() => undefined)
48
+ .finally(() => {
46
49
  this.weight -= e.weight;
47
50
  this.queueCurrent = this.queueCurrent.filter((a) => a.key !== e.key);
48
51
  this.length--;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/semaphore",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -10,7 +10,7 @@
10
10
  "tsconfig.json"
11
11
  ],
12
12
  "scripts": {
13
- "test": "echo \"Error: no test specified\" && exit 1",
13
+ "test": "npx @vnejs/monorepo test",
14
14
  "build": "npx @vnejs/monorepo package",
15
15
  "publish:major": "npx @vnejs/monorepo publish major --access public",
16
16
  "publish:minor": "npx @vnejs/monorepo publish minor --access public",
@@ -19,6 +19,7 @@
19
19
  "author": "",
20
20
  "license": "ISC",
21
21
  "devDependencies": {
22
- "@vnejs/configs.ts-common": "~0.2.0"
22
+ "@vnejs/configs.ts-common": "~0.2.0",
23
+ "@vnejs/configs.vitest": "~0.2.0"
23
24
  }
24
25
  }
package/src/index.ts CHANGED
@@ -47,12 +47,15 @@ export class Semaphore {
47
47
  this.weight += e.weight;
48
48
  e.inProcess = true;
49
49
  this.queueCurrent.push(e);
50
- e.func().then(() => {
51
- this.weight -= e.weight;
52
- this.queueCurrent = this.queueCurrent.filter((a) => a.key !== e.key);
53
- this.length--;
54
- delete this.queue[e.key];
55
- e.resolve?.();
56
- });
50
+ Promise.resolve()
51
+ .then(() => e.func())
52
+ .catch(() => undefined)
53
+ .finally(() => {
54
+ this.weight -= e.weight;
55
+ this.queueCurrent = this.queueCurrent.filter((a) => a.key !== e.key);
56
+ this.length--;
57
+ delete this.queue[e.key];
58
+ e.resolve?.();
59
+ });
57
60
  }
58
61
  }
@@ -0,0 +1,34 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { Semaphore } from "../index.js";
4
+
5
+ describe("Semaphore", () => {
6
+ it("releases the slot when func rejects so the next push can start", async () => {
7
+ const semaphore = new Semaphore(1);
8
+ let secondStarted = false;
9
+
10
+ const first = semaphore.push("fail", 1, async () => {
11
+ throw new Error("load failed");
12
+ });
13
+ const second = semaphore.push("ok", 1, async () => {
14
+ secondStarted = true;
15
+ });
16
+
17
+ await first;
18
+ await second;
19
+
20
+ expect(secondStarted).toBe(true);
21
+ expect(semaphore.weight).toBe(0);
22
+ expect(semaphore.length).toBe(0);
23
+ });
24
+
25
+ it("resolves push when func rejects", async () => {
26
+ const semaphore = new Semaphore(1);
27
+
28
+ await expect(
29
+ semaphore.push("fail", 1, async () => {
30
+ throw new Error("load failed");
31
+ }),
32
+ ).resolves.toBeUndefined();
33
+ });
34
+ });
package/tsconfig.json CHANGED
@@ -9,6 +9,7 @@
9
9
  ],
10
10
  "exclude": [
11
11
  "dist",
12
- "node_modules"
12
+ "node_modules",
13
+ "src/tests"
13
14
  ]
14
15
  }