@antti5/ship-tool-common 1.0.2 → 1.0.4
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/README.md +2 -2
- package/lib/Semaphore.js +60 -55
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
This repository contains library code and common utils for the data analysis tool for the computer game World of Warships: https://shiptool.st
|
|
2
2
|
|
|
3
|
-
Note: Other code for the project are currently in private repositories
|
|
3
|
+
**Note: Other code for the project are currently in private repositories.**
|
package/lib/Semaphore.js
CHANGED
|
@@ -1,61 +1,71 @@
|
|
|
1
1
|
class Semaphore {
|
|
2
2
|
|
|
3
|
-
static TPS_MAX = 10_000;
|
|
4
|
-
|
|
5
3
|
#value = 0;
|
|
6
4
|
#max;
|
|
7
5
|
#queue = [];
|
|
8
6
|
|
|
9
|
-
#
|
|
10
|
-
#
|
|
7
|
+
#rateLimit;
|
|
8
|
+
#rateLimitInterval;
|
|
11
9
|
|
|
12
|
-
#
|
|
13
|
-
#timestamps = [];
|
|
10
|
+
#waiters = [];
|
|
14
11
|
|
|
15
|
-
constructor(max = 1, {
|
|
12
|
+
constructor(max = 1, { rateLimit = null } = {}) {
|
|
16
13
|
this.#max = max;
|
|
17
|
-
this.#
|
|
14
|
+
this.#rateLimit = rateLimit;
|
|
18
15
|
}
|
|
19
16
|
|
|
20
17
|
acquire({ jobClass = null } = {}) {
|
|
21
|
-
|
|
18
|
+
|
|
19
|
+
/* If the semaphore is not at maximum value and no rate limit interval
|
|
20
|
+
has been set, return immediately.
|
|
21
|
+
|
|
22
|
+
If the semaphore is rate-limited, then set interval so that the next
|
|
23
|
+
acquisition will be timed. */
|
|
24
|
+
|
|
25
|
+
if (this.#value < this.#max && !this.#rateLimitInterval) {
|
|
26
|
+
if (this.#rateLimit)
|
|
27
|
+
this.#rateLimitInterval = setInterval(
|
|
28
|
+
() => this.#resolveNext(),
|
|
29
|
+
1000 / this.#rateLimit
|
|
30
|
+
);
|
|
22
31
|
this.#value++;
|
|
23
|
-
return
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
)
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return new Promise((resolve, reject) =>
|
|
36
|
+
this.#queue.push({ jobClass, resolve, reject })
|
|
37
|
+
);
|
|
28
38
|
}
|
|
29
39
|
|
|
30
40
|
release() {
|
|
31
41
|
this.#value--;
|
|
32
42
|
|
|
33
|
-
if (this.#
|
|
34
|
-
this.#
|
|
35
|
-
|
|
36
|
-
this.#timestamps.shift();
|
|
37
|
-
}
|
|
43
|
+
if (this.#value === 0 && this.#queue.length === 0)
|
|
44
|
+
while (this.#waiters.length > 0)
|
|
45
|
+
this.#waiters.pop().resolve();
|
|
38
46
|
|
|
47
|
+
if (!this.#rateLimit)
|
|
48
|
+
this.#resolveNext();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#resolveNext() {
|
|
39
52
|
if (this.#queue.length > 0 && this.#value < this.#max) {
|
|
40
53
|
this.#value++;
|
|
41
54
|
this.#queue.shift().resolve();
|
|
42
|
-
|
|
43
|
-
if (this.#waiterQueue[this.#queue.length]?.length > 0)
|
|
44
|
-
for (const { resolve } of this.#waiterQueue[this.#queue.length])
|
|
45
|
-
resolve();
|
|
46
55
|
}
|
|
47
56
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
57
|
+
if (this.#queue.length === 0 && this.#rateLimitInterval) {
|
|
58
|
+
clearInterval(this.#rateLimitInterval);
|
|
59
|
+
this.#rateLimitInterval = null;
|
|
60
|
+
}
|
|
51
61
|
}
|
|
52
62
|
|
|
53
63
|
purge() {
|
|
54
|
-
|
|
55
|
-
reject('Semaphore was purged');
|
|
64
|
+
while (this.#queue.length > 0)
|
|
65
|
+
this.#queue.pop().reject('Semaphore was purged');
|
|
56
66
|
|
|
57
|
-
|
|
58
|
-
reject('Semaphore was purged');
|
|
67
|
+
while (this.#waiters.length > 0)
|
|
68
|
+
this.#waiters.pop().reject('Semaphore was purged');
|
|
59
69
|
|
|
60
70
|
this.#queue = [];
|
|
61
71
|
this.#value = 0;
|
|
@@ -64,19 +74,19 @@ class Semaphore {
|
|
|
64
74
|
raiseJobClass(jobClass) {
|
|
65
75
|
this.#queue = [
|
|
66
76
|
...this.#queue.filter(job => job.jobClass === jobClass),
|
|
67
|
-
...this.#queue.filter(job =>
|
|
77
|
+
...this.#queue.filter(job => job?.jobClass !== jobClass)
|
|
68
78
|
];
|
|
69
79
|
}
|
|
70
80
|
|
|
71
|
-
|
|
72
|
-
|
|
81
|
+
lowerJobClass(jobClass) {
|
|
82
|
+
this.#queue = [
|
|
83
|
+
...this.#queue.filter(job => job?.jobClass !== jobClass),
|
|
84
|
+
...this.#queue.filter(job => job.jobClass === jobClass)
|
|
85
|
+
];
|
|
73
86
|
}
|
|
74
87
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return slice.length > 1 ?
|
|
78
|
-
(slice.length - 1) / (slice.at(-1) - slice.at(0)) * 1000 :
|
|
79
|
-
0;
|
|
88
|
+
getQueueLength() {
|
|
89
|
+
return this.#queue.length;
|
|
80
90
|
}
|
|
81
91
|
|
|
82
92
|
getMax() {
|
|
@@ -87,34 +97,29 @@ class Semaphore {
|
|
|
87
97
|
this.#max = max;
|
|
88
98
|
}
|
|
89
99
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
return new Promise((resolve, reject) => {
|
|
93
|
-
this.#waiter[value] ??= [];
|
|
94
|
-
this.#waiter[value].push({ resolve, reject });
|
|
95
|
-
});
|
|
96
|
-
}
|
|
100
|
+
getRateLimit() {
|
|
101
|
+
return this.#rateLimit;
|
|
97
102
|
}
|
|
98
103
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return new Promise((resolve, reject) => {
|
|
102
|
-
this.#waiterQueue[length] ??= [];
|
|
103
|
-
this.#waiterQueue[length].push({ resolve, reject });
|
|
104
|
-
});
|
|
104
|
+
setRateLimit(rateLimit) {
|
|
105
|
+
this.#rateLimit = rateLimit;
|
|
105
106
|
}
|
|
106
107
|
|
|
107
108
|
async run(func, { jobClass = null } = {}) {
|
|
108
109
|
try {
|
|
109
110
|
await this.acquire({ jobClass });
|
|
110
111
|
return await func();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
finally {
|
|
112
|
+
} finally {
|
|
114
113
|
this.release();
|
|
115
114
|
}
|
|
116
115
|
}
|
|
117
|
-
}
|
|
118
116
|
|
|
117
|
+
async wait() {
|
|
118
|
+
if (this.#value > 0 || this.#queue.length > 0)
|
|
119
|
+
return new Promise((resolve, reject) => {
|
|
120
|
+
this.#waiters.push({ resolve, reject });
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
119
124
|
|
|
120
125
|
export default Semaphore;
|