@antti5/ship-tool-common 1.0.2 → 1.0.3
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 +38 -57
- 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,62 @@
|
|
|
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
|
+
#resolveInterval;
|
|
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
|
-
if (this.#value < this.#max) {
|
|
18
|
+
if (this.#value < this.#max && !this.#rateLimit) {
|
|
22
19
|
this.#value++;
|
|
23
20
|
return new Promise(resolve => resolve());
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (this.#rateLimit && !this.#resolveInterval)
|
|
24
|
+
this.#resolveInterval = setInterval(() => this.#resolveNext(), 1000 / this.#rateLimit);
|
|
25
|
+
|
|
26
|
+
return new Promise((resolve, reject) =>
|
|
27
|
+
this.#queue.push({ jobClass, resolve, reject })
|
|
28
|
+
);
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
release() {
|
|
31
32
|
this.#value--;
|
|
32
33
|
|
|
33
|
-
if (this.#
|
|
34
|
-
this.#
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
if (this.#value === 0 && this.#queue.length === 0)
|
|
35
|
+
while (this.#waiters.length > 0)
|
|
36
|
+
this.#waiters.pop().resolve();
|
|
37
|
+
|
|
38
|
+
if (!this.#rateLimit)
|
|
39
|
+
this.#resolveNext();
|
|
40
|
+
}
|
|
38
41
|
|
|
42
|
+
#resolveNext() {
|
|
39
43
|
if (this.#queue.length > 0 && this.#value < this.#max) {
|
|
40
44
|
this.#value++;
|
|
41
45
|
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
46
|
}
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
if (this.#queue.length === 0 && this.#resolveInterval) {
|
|
49
|
+
clearInterval(this.#resolveInterval);
|
|
50
|
+
this.#resolveInterval = null;
|
|
51
|
+
}
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
purge() {
|
|
54
|
-
|
|
55
|
-
reject('Semaphore was purged');
|
|
55
|
+
while (this.#queue.length > 0)
|
|
56
|
+
this.#queue.pop().reject('Semaphore was purged');
|
|
56
57
|
|
|
57
|
-
|
|
58
|
-
reject('Semaphore was purged');
|
|
58
|
+
while (this.#waiters.length > 0)
|
|
59
|
+
this.#waiters.pop().reject('Semaphore was purged');
|
|
59
60
|
|
|
60
61
|
this.#queue = [];
|
|
61
62
|
this.#value = 0;
|
|
@@ -72,13 +73,6 @@ class Semaphore {
|
|
|
72
73
|
return this.#queue.length;
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
getTps(count = Semaphore.TPS_MAX) {
|
|
76
|
-
const slice = this.#timestamps.slice(-count);
|
|
77
|
-
return slice.length > 1 ?
|
|
78
|
-
(slice.length - 1) / (slice.at(-1) - slice.at(0)) * 1000 :
|
|
79
|
-
0;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
76
|
getMax() {
|
|
83
77
|
return this.#max;
|
|
84
78
|
}
|
|
@@ -87,34 +81,21 @@ class Semaphore {
|
|
|
87
81
|
this.#max = max;
|
|
88
82
|
}
|
|
89
83
|
|
|
90
|
-
async wait(value = 0) {
|
|
91
|
-
if (this.#value > value) {
|
|
92
|
-
return new Promise((resolve, reject) => {
|
|
93
|
-
this.#waiter[value] ??= [];
|
|
94
|
-
this.#waiter[value].push({ resolve, reject });
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async waitQueue(length = 0) {
|
|
100
|
-
while (this.#queue.length > length)
|
|
101
|
-
return new Promise((resolve, reject) => {
|
|
102
|
-
this.#waiterQueue[length] ??= [];
|
|
103
|
-
this.#waiterQueue[length].push({ resolve, reject });
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
84
|
async run(func, { jobClass = null } = {}) {
|
|
108
85
|
try {
|
|
109
86
|
await this.acquire({ jobClass });
|
|
110
87
|
return await func();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
finally {
|
|
88
|
+
} finally {
|
|
114
89
|
this.release();
|
|
115
90
|
}
|
|
116
91
|
}
|
|
117
|
-
}
|
|
118
92
|
|
|
93
|
+
async wait() {
|
|
94
|
+
if (this.#value > 0 || this.#queue.length > 0)
|
|
95
|
+
return new Promise((resolve, reject) => {
|
|
96
|
+
this.#waiters.push({ resolve, reject });
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
119
100
|
|
|
120
101
|
export default Semaphore;
|