@kapeta/local-cluster-service 0.70.3 → 0.70.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/CHANGELOG.md +17 -0
- package/dist/cjs/src/storm/PageGenerator.d.ts +3 -20
- package/dist/cjs/src/storm/PageGenerator.js +94 -126
- package/dist/cjs/src/storm/page-utils.js +8 -6
- package/dist/cjs/src/storm/routes.js +2 -5
- package/dist/cjs/src/storm/utils.d.ts +5 -0
- package/dist/cjs/src/storm/utils.js +15 -1
- package/dist/esm/src/storm/PageGenerator.d.ts +3 -20
- package/dist/esm/src/storm/PageGenerator.js +94 -126
- package/dist/esm/src/storm/page-utils.js +8 -6
- package/dist/esm/src/storm/routes.js +2 -5
- package/dist/esm/src/storm/utils.d.ts +5 -0
- package/dist/esm/src/storm/utils.js +15 -1
- package/package.json +2 -1
- package/src/storm/PageGenerator.ts +104 -149
- package/src/storm/page-utils.ts +8 -6
- package/src/storm/routes.ts +2 -5
- package/src/storm/utils.ts +19 -1
- package/dist/cjs/src/storm/PromiseQueue.d.ts +0 -25
- package/dist/cjs/src/storm/PromiseQueue.js +0 -97
- package/dist/esm/src/storm/PromiseQueue.d.ts +0 -25
- package/dist/esm/src/storm/PromiseQueue.js +0 -97
- package/src/storm/PromiseQueue.ts +0 -129
@@ -1,129 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright 2023 Kapeta Inc.
|
3
|
-
* SPDX-License-Identifier: BUSL-1.1
|
4
|
-
*/
|
5
|
-
export type Future<T> = () => Promise<T>;
|
6
|
-
|
7
|
-
export type FuturePromise<T> = {
|
8
|
-
promise: Promise<T>;
|
9
|
-
resolve: (value: T) => void;
|
10
|
-
reject: (reason: any) => void;
|
11
|
-
isResolved: () => boolean;
|
12
|
-
};
|
13
|
-
|
14
|
-
export function createFuture<T = void>(): FuturePromise<T> {
|
15
|
-
let resolved = false;
|
16
|
-
let resolve: (value: T) => void = () => {
|
17
|
-
resolved = true;
|
18
|
-
};
|
19
|
-
let reject: (reason: any) => void = () => {
|
20
|
-
resolved = true;
|
21
|
-
};
|
22
|
-
|
23
|
-
const promise = new Promise<T>((res, rej) => {
|
24
|
-
resolve = (value: T) => {
|
25
|
-
resolved = true;
|
26
|
-
res(value);
|
27
|
-
};
|
28
|
-
reject = (reason: any) => {
|
29
|
-
resolved = true;
|
30
|
-
rej(reason);
|
31
|
-
};
|
32
|
-
});
|
33
|
-
return {
|
34
|
-
promise,
|
35
|
-
resolve,
|
36
|
-
reject,
|
37
|
-
isResolved: () => resolved,
|
38
|
-
};
|
39
|
-
}
|
40
|
-
|
41
|
-
type InternalFuture<T> = {
|
42
|
-
execute: Future<T>;
|
43
|
-
promise: Promise<T>;
|
44
|
-
resolve: (value: T) => void;
|
45
|
-
reject: (reason: any) => void;
|
46
|
-
};
|
47
|
-
|
48
|
-
export class PromiseQueue {
|
49
|
-
private readonly queue: InternalFuture<any>[] = [];
|
50
|
-
private readonly active: Promise<any>[] = [];
|
51
|
-
|
52
|
-
// Maximum number of concurrent promises
|
53
|
-
private readonly maxConcurrency: number;
|
54
|
-
|
55
|
-
constructor(maxConcurrency: number = 5) {
|
56
|
-
this.maxConcurrency = maxConcurrency;
|
57
|
-
}
|
58
|
-
|
59
|
-
private toInternal<T>(future: Future<T>): InternalFuture<T> {
|
60
|
-
let resolve: (value: T) => void = () => {};
|
61
|
-
let reject: (reason: any) => void = () => {};
|
62
|
-
|
63
|
-
const promise = new Promise<T>((res, rej) => {
|
64
|
-
resolve = res;
|
65
|
-
reject = rej;
|
66
|
-
});
|
67
|
-
return {
|
68
|
-
execute: future,
|
69
|
-
promise,
|
70
|
-
resolve,
|
71
|
-
reject,
|
72
|
-
};
|
73
|
-
}
|
74
|
-
|
75
|
-
public add<T>(future: Future<T>) {
|
76
|
-
const internal = this.toInternal<T>(future);
|
77
|
-
this.queue.push(internal);
|
78
|
-
this.next();
|
79
|
-
|
80
|
-
return internal.promise;
|
81
|
-
}
|
82
|
-
|
83
|
-
private next(): boolean {
|
84
|
-
if (this.active.length >= this.maxConcurrency) {
|
85
|
-
return false;
|
86
|
-
}
|
87
|
-
if (this.queue.length === 0) {
|
88
|
-
return false;
|
89
|
-
}
|
90
|
-
|
91
|
-
const future = this.queue.shift();
|
92
|
-
if (!future) {
|
93
|
-
return false;
|
94
|
-
}
|
95
|
-
|
96
|
-
const promise = future
|
97
|
-
.execute()
|
98
|
-
.then(future.resolve)
|
99
|
-
.catch(future.reject)
|
100
|
-
.finally(() => {
|
101
|
-
this.active.splice(this.active.indexOf(promise), 1);
|
102
|
-
this.next();
|
103
|
-
});
|
104
|
-
|
105
|
-
this.active.push(promise);
|
106
|
-
|
107
|
-
return this.next();
|
108
|
-
}
|
109
|
-
|
110
|
-
public cancel() {
|
111
|
-
this.queue.splice(0, this.queue.length);
|
112
|
-
this.active.splice(0, this.active.length);
|
113
|
-
}
|
114
|
-
|
115
|
-
public async wait() {
|
116
|
-
while (this.queue.length > 0 || this.active.length > 0) {
|
117
|
-
await Promise.allSettled(this.active);
|
118
|
-
this.next();
|
119
|
-
}
|
120
|
-
}
|
121
|
-
|
122
|
-
public get empty() {
|
123
|
-
return this.queue.length === 0 && this.active.length === 0;
|
124
|
-
}
|
125
|
-
|
126
|
-
public get length() {
|
127
|
-
return this.queue.length + this.active.length;
|
128
|
-
}
|
129
|
-
}
|