@vnejs/semaphore 0.0.3 → 0.0.5
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/package.json +9 -10
- package/src/index.ts +58 -0
- package/src/queue-entry.ts +8 -0
- package/src/utils.ts +27 -0
- package/tsconfig.json +17 -0
package/package.json
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/semaphore",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
|
-
"dist"
|
|
8
|
+
"dist",
|
|
9
|
+
"src",
|
|
10
|
+
"tsconfig.json"
|
|
9
11
|
],
|
|
10
12
|
"scripts": {
|
|
11
13
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
12
|
-
"build": "
|
|
13
|
-
"publish:major": "
|
|
14
|
-
"publish:minor": "
|
|
15
|
-
"publish:patch": "
|
|
14
|
+
"build": "npx @vnejs/monorepo package",
|
|
15
|
+
"publish:major": "npx @vnejs/monorepo publish major --access public",
|
|
16
|
+
"publish:minor": "npx @vnejs/monorepo publish minor --access public",
|
|
17
|
+
"publish:patch": "npx @vnejs/monorepo publish patch --access public"
|
|
16
18
|
},
|
|
17
19
|
"author": "",
|
|
18
|
-
"license": "ISC"
|
|
19
|
-
"devDependencies": {
|
|
20
|
-
"typescript": "^6.0.3"
|
|
21
|
-
}
|
|
20
|
+
"license": "ISC"
|
|
22
21
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { QueueEntry } from "./queue-entry.js";
|
|
2
|
+
import { createQueueEntry, getPromiseField, isCanAddNewElement, race, recalcSortedQueue } from "./utils.js";
|
|
3
|
+
|
|
4
|
+
export class Semaphore {
|
|
5
|
+
queue: Record<string, QueueEntry> = {};
|
|
6
|
+
queueCurrent: QueueEntry[] = [];
|
|
7
|
+
queueSorted: QueueEntry[] = [];
|
|
8
|
+
length = 0;
|
|
9
|
+
weight = 0;
|
|
10
|
+
maxWeight = 999;
|
|
11
|
+
isQueueStarted = false;
|
|
12
|
+
|
|
13
|
+
constructor(maxWeight: number) {
|
|
14
|
+
this.maxWeight = maxWeight;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
push = (key: string, weight: number, func: () => Promise<unknown>) => {
|
|
18
|
+
const element = this.queue[key];
|
|
19
|
+
if (element) {
|
|
20
|
+
if (element.weight > weight && !element.inProcess) Object.assign(element, { weight, func });
|
|
21
|
+
return element.promise;
|
|
22
|
+
}
|
|
23
|
+
const obj = createQueueEntry(key, weight, func);
|
|
24
|
+
this.queueSorted = [];
|
|
25
|
+
this.queue[key] = obj;
|
|
26
|
+
this.length++;
|
|
27
|
+
!this.isQueueStarted && void this.startQueue();
|
|
28
|
+
return obj.promise;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
waitAll = () => Promise.all(this.queueCurrent.map(getPromiseField));
|
|
32
|
+
|
|
33
|
+
private async startQueue() {
|
|
34
|
+
this.isQueueStarted = true;
|
|
35
|
+
while (this.length) {
|
|
36
|
+
this.queueSorted = this.queueSorted.length ? this.queueSorted : recalcSortedQueue(this.queue);
|
|
37
|
+
while (isCanAddNewElement(this.queueSorted, this.weight, this.maxWeight)) {
|
|
38
|
+
const entry = this.queueSorted.pop();
|
|
39
|
+
if (entry) this.start(entry);
|
|
40
|
+
}
|
|
41
|
+
await race(this.queueCurrent);
|
|
42
|
+
}
|
|
43
|
+
this.isQueueStarted = false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private start(e: QueueEntry) {
|
|
47
|
+
this.weight += e.weight;
|
|
48
|
+
e.inProcess = true;
|
|
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
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { QueueEntry } from "./queue-entry.js";
|
|
2
|
+
|
|
3
|
+
export const getPromiseField = (e: QueueEntry) => e.promise;
|
|
4
|
+
|
|
5
|
+
export const onlyNotInProcessWithResolve = (
|
|
6
|
+
e: QueueEntry,
|
|
7
|
+
): e is QueueEntry & { resolve: () => void } => !e.inProcess && e.resolve !== undefined;
|
|
8
|
+
|
|
9
|
+
export const byWeight = (a: QueueEntry, b: QueueEntry) => b.weight - a.weight;
|
|
10
|
+
|
|
11
|
+
export const race = (entries: QueueEntry[]) =>
|
|
12
|
+
entries.length ? Promise.race(entries.map(getPromiseField)) : new Promise<void>((resolve) => setTimeout(resolve, 0));
|
|
13
|
+
|
|
14
|
+
export const createQueueEntry = (key: string, weight: number, func: () => Promise<unknown>): QueueEntry => {
|
|
15
|
+
const obj = { key, weight, func, inProcess: false } as QueueEntry;
|
|
16
|
+
const promise = new Promise<void>((resolve) => {
|
|
17
|
+
obj.resolve = resolve;
|
|
18
|
+
});
|
|
19
|
+
obj.promise = promise;
|
|
20
|
+
return obj;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const isCanAddNewElement = (queue: QueueEntry[], weight: number, maxWeight: number) =>
|
|
24
|
+
queue.length > 0 && maxWeight >= weight + queue[queue.length - 1].weight;
|
|
25
|
+
|
|
26
|
+
export const recalcSortedQueue = (queue: Record<string, QueueEntry>) =>
|
|
27
|
+
Object.values(queue).filter(onlyNotInProcessWithResolve).sort(byWeight);
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "Bundler",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"noEmitOnError": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"rootDir": "src",
|
|
13
|
+
"outDir": "dist"
|
|
14
|
+
},
|
|
15
|
+
"include": ["src/**/*.ts"],
|
|
16
|
+
"exclude": ["dist", "node_modules"]
|
|
17
|
+
}
|