@simplysm/core-common 13.0.69 → 13.0.71
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 +66 -267
- package/dist/common.types.d.ts +14 -14
- package/dist/errors/argument-error.d.ts +10 -10
- package/dist/errors/argument-error.d.ts.map +1 -1
- package/dist/errors/argument-error.js +2 -2
- package/dist/errors/argument-error.js.map +1 -1
- package/dist/errors/not-implemented-error.d.ts +8 -8
- package/dist/errors/not-implemented-error.js +2 -2
- package/dist/errors/not-implemented-error.js.map +1 -1
- package/dist/errors/sd-error.d.ts +10 -10
- package/dist/errors/sd-error.d.ts.map +1 -1
- package/dist/errors/timeout-error.d.ts +10 -10
- package/dist/errors/timeout-error.js +3 -3
- package/dist/errors/timeout-error.js.map +1 -1
- package/dist/extensions/arr-ext.d.ts +2 -2
- package/dist/extensions/arr-ext.helpers.d.ts +8 -8
- package/dist/extensions/arr-ext.helpers.js +1 -1
- package/dist/extensions/arr-ext.helpers.js.map +1 -1
- package/dist/extensions/arr-ext.js +13 -13
- package/dist/extensions/arr-ext.js.map +1 -1
- package/dist/extensions/arr-ext.types.d.ts +57 -57
- package/dist/extensions/arr-ext.types.d.ts.map +1 -1
- package/dist/extensions/map-ext.d.ts +16 -16
- package/dist/extensions/set-ext.d.ts +11 -11
- package/dist/features/debounce-queue.d.ts +17 -15
- package/dist/features/debounce-queue.d.ts.map +1 -1
- package/dist/features/debounce-queue.js +6 -6
- package/dist/features/debounce-queue.js.map +1 -1
- package/dist/features/event-emitter.d.ts +20 -20
- package/dist/features/event-emitter.js +17 -17
- package/dist/features/serial-queue.d.ts +11 -11
- package/dist/features/serial-queue.js +5 -5
- package/dist/features/serial-queue.js.map +1 -1
- package/dist/globals.d.ts +4 -4
- package/dist/types/date-only.d.ts +64 -64
- package/dist/types/date-only.d.ts.map +1 -1
- package/dist/types/date-only.js +63 -63
- package/dist/types/date-time.d.ts +37 -37
- package/dist/types/date-time.d.ts.map +1 -1
- package/dist/types/date-time.js +54 -37
- package/dist/types/date-time.js.map +1 -1
- package/dist/types/lazy-gc-map.d.ts +26 -26
- package/dist/types/lazy-gc-map.d.ts.map +1 -1
- package/dist/types/lazy-gc-map.js +26 -26
- package/dist/types/lazy-gc-map.js.map +1 -1
- package/dist/types/time.d.ts +25 -25
- package/dist/types/time.d.ts.map +1 -1
- package/dist/types/time.js +25 -25
- package/dist/types/time.js.map +1 -1
- package/dist/types/uuid.d.ts +11 -11
- package/dist/types/uuid.d.ts.map +1 -1
- package/dist/types/uuid.js +12 -12
- package/dist/types/uuid.js.map +1 -1
- package/dist/utils/bytes.d.ts +17 -17
- package/dist/utils/bytes.js +4 -4
- package/dist/utils/bytes.js.map +1 -1
- package/dist/utils/date-format.d.ts +45 -45
- package/dist/utils/date-format.js +1 -1
- package/dist/utils/date-format.js.map +1 -1
- package/dist/utils/error.d.ts +4 -4
- package/dist/utils/json.d.ts +17 -17
- package/dist/utils/json.js +3 -3
- package/dist/utils/json.js.map +1 -1
- package/dist/utils/num.d.ts +23 -23
- package/dist/utils/obj.d.ts +111 -111
- package/dist/utils/obj.d.ts.map +1 -1
- package/dist/utils/obj.js +3 -3
- package/dist/utils/obj.js.map +1 -1
- package/dist/utils/path.d.ts +10 -10
- package/dist/utils/primitive.d.ts +5 -5
- package/dist/utils/primitive.js +1 -1
- package/dist/utils/primitive.js.map +1 -1
- package/dist/utils/str.d.ts +46 -46
- package/dist/utils/str.d.ts.map +1 -1
- package/dist/utils/str.js +5 -5
- package/dist/utils/str.js.map +1 -1
- package/dist/utils/template-strings.d.ts +26 -26
- package/dist/utils/transferable.d.ts +18 -18
- package/dist/utils/transferable.js +1 -1
- package/dist/utils/transferable.js.map +1 -1
- package/dist/utils/wait.d.ts +9 -9
- package/dist/utils/xml.d.ts +13 -13
- package/dist/utils/xml.d.ts.map +1 -1
- package/dist/utils/xml.js +1 -0
- package/dist/utils/xml.js.map +1 -1
- package/dist/zip/sd-zip.d.ts +22 -22
- package/dist/zip/sd-zip.js +16 -16
- package/package.json +4 -4
- package/src/common.types.ts +17 -17
- package/src/errors/argument-error.ts +15 -15
- package/src/errors/not-implemented-error.ts +9 -9
- package/src/errors/sd-error.ts +12 -12
- package/src/errors/timeout-error.ts +12 -12
- package/src/extensions/arr-ext.helpers.ts +10 -10
- package/src/extensions/arr-ext.ts +57 -57
- package/src/extensions/arr-ext.types.ts +59 -59
- package/src/extensions/map-ext.ts +16 -16
- package/src/extensions/set-ext.ts +11 -11
- package/src/features/debounce-queue.ts +21 -19
- package/src/features/event-emitter.ts +25 -25
- package/src/features/serial-queue.ts +13 -13
- package/src/globals.ts +4 -4
- package/src/index.ts +1 -1
- package/src/types/date-only.ts +83 -83
- package/src/types/date-time.ts +64 -44
- package/src/types/lazy-gc-map.ts +45 -45
- package/src/types/time.ts +34 -34
- package/src/types/uuid.ts +17 -17
- package/src/utils/bytes.ts +35 -35
- package/src/utils/date-format.ts +65 -65
- package/src/utils/error.ts +4 -4
- package/src/utils/json.ts +39 -39
- package/src/utils/num.ts +23 -23
- package/src/utils/obj.ts +138 -138
- package/src/utils/path.ts +10 -10
- package/src/utils/primitive.ts +6 -6
- package/src/utils/str.ts +260 -261
- package/src/utils/template-strings.ts +29 -29
- package/src/utils/transferable.ts +284 -284
- package/src/utils/wait.ts +10 -10
- package/src/utils/xml.ts +20 -19
- package/src/zip/sd-zip.ts +25 -25
- package/tests/errors/errors.spec.ts +80 -0
- package/tests/extensions/array-extension.spec.ts +796 -0
- package/tests/extensions/map-extension.spec.ts +147 -0
- package/tests/extensions/set-extension.spec.ts +74 -0
- package/tests/types/date-only.spec.ts +638 -0
- package/tests/types/date-time.spec.ts +391 -0
- package/tests/types/lazy-gc-map.spec.ts +692 -0
- package/tests/types/time.spec.ts +559 -0
- package/tests/types/uuid.spec.ts +74 -0
- package/tests/utils/bytes-utils.spec.ts +230 -0
- package/tests/utils/date-format.spec.ts +373 -0
- package/tests/utils/debounce-queue.spec.ts +272 -0
- package/tests/utils/json.spec.ts +486 -0
- package/tests/utils/number.spec.ts +157 -0
- package/tests/utils/object.spec.ts +829 -0
- package/tests/utils/path.spec.ts +78 -0
- package/tests/utils/primitive.spec.ts +43 -0
- package/tests/utils/sd-event-emitter.spec.ts +216 -0
- package/tests/utils/serial-queue.spec.ts +365 -0
- package/tests/utils/string.spec.ts +281 -0
- package/tests/utils/template-strings.spec.ts +57 -0
- package/tests/utils/transferable.spec.ts +703 -0
- package/tests/utils/wait.spec.ts +145 -0
- package/tests/utils/xml.spec.ts +146 -0
- package/tests/zip/sd-zip.spec.ts +238 -0
- package/docs/extensions.md +0 -503
- package/docs/features.md +0 -109
- package/docs/types.md +0 -486
- package/docs/utils.md +0 -780
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { DebounceQueue, waitTime as time, SdError } from "@simplysm/core-common";
|
|
3
|
+
|
|
4
|
+
describe("DebounceQueue", () => {
|
|
5
|
+
//#region Debounce behavior
|
|
6
|
+
|
|
7
|
+
describe("Debounce behavior", () => {
|
|
8
|
+
it("Executes only last request", async () => {
|
|
9
|
+
const queue = new DebounceQueue(50);
|
|
10
|
+
const calls: number[] = [];
|
|
11
|
+
|
|
12
|
+
queue.run(() => {
|
|
13
|
+
calls.push(1);
|
|
14
|
+
});
|
|
15
|
+
queue.run(() => {
|
|
16
|
+
calls.push(2);
|
|
17
|
+
});
|
|
18
|
+
queue.run(() => {
|
|
19
|
+
calls.push(3);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// Wait for debounce
|
|
23
|
+
await time(100);
|
|
24
|
+
|
|
25
|
+
// Only last request executed
|
|
26
|
+
expect(calls).toEqual([3]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("Executes after delay", async () => {
|
|
30
|
+
const queue = new DebounceQueue(100);
|
|
31
|
+
const calls: number[] = [];
|
|
32
|
+
|
|
33
|
+
queue.run(() => {
|
|
34
|
+
calls.push(1);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// After 50ms not yet executed
|
|
38
|
+
await time(50);
|
|
39
|
+
expect(calls).toEqual([]);
|
|
40
|
+
|
|
41
|
+
// After 100ms executed
|
|
42
|
+
await time(100);
|
|
43
|
+
expect(calls).toEqual([1]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("Executes immediately if no delay", async () => {
|
|
47
|
+
const queue = new DebounceQueue();
|
|
48
|
+
const calls: number[] = [];
|
|
49
|
+
|
|
50
|
+
queue.run(() => {
|
|
51
|
+
calls.push(1);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Small wait (event loop)
|
|
55
|
+
await time(10);
|
|
56
|
+
|
|
57
|
+
expect(calls).toEqual([1]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("If new request arrives during execution, executes after completion", async () => {
|
|
61
|
+
const queue = new DebounceQueue(10);
|
|
62
|
+
const calls: number[] = [];
|
|
63
|
+
|
|
64
|
+
queue.run(async () => {
|
|
65
|
+
calls.push(1);
|
|
66
|
+
await time(50); // Wait during execution
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Wait for first execution start
|
|
70
|
+
await time(20);
|
|
71
|
+
|
|
72
|
+
// Add new request during execution
|
|
73
|
+
queue.run(() => {
|
|
74
|
+
calls.push(2);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Wait for all work to complete
|
|
78
|
+
await time(100);
|
|
79
|
+
|
|
80
|
+
expect(calls).toEqual([1, 2]);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
|
|
86
|
+
//#region Error handling
|
|
87
|
+
|
|
88
|
+
describe("Error handling", () => {
|
|
89
|
+
it("Emits error event on error", async () => {
|
|
90
|
+
const queue = new DebounceQueue(10);
|
|
91
|
+
const errors: SdError[] = [];
|
|
92
|
+
|
|
93
|
+
queue.on("error", (err) => {
|
|
94
|
+
errors.push(err);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
queue.run(() => {
|
|
98
|
+
throw new Error("test error");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
await time(50);
|
|
102
|
+
|
|
103
|
+
expect(errors).toHaveLength(1);
|
|
104
|
+
expect(errors[0]).toBeInstanceOf(SdError);
|
|
105
|
+
expect(errors[0].message).toContain("Error occurred while executing task");
|
|
106
|
+
expect(errors[0].message).toContain("test error");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("Next request executes normally even if error occurred", async () => {
|
|
110
|
+
const queue = new DebounceQueue(10);
|
|
111
|
+
const calls: number[] = [];
|
|
112
|
+
const errors: SdError[] = [];
|
|
113
|
+
|
|
114
|
+
// Add error listener to prevent unhandled rejection
|
|
115
|
+
queue.on("error", (err) => {
|
|
116
|
+
errors.push(err);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
queue.run(() => {
|
|
120
|
+
throw new Error("error");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
await time(50);
|
|
124
|
+
|
|
125
|
+
queue.run(() => {
|
|
126
|
+
calls.push(1);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
await time(50);
|
|
130
|
+
|
|
131
|
+
expect(calls).toEqual([1]);
|
|
132
|
+
expect(errors).toHaveLength(1);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("pendingFn executes even if error occurred during execution", async () => {
|
|
136
|
+
const queue = new DebounceQueue(10);
|
|
137
|
+
const calls: number[] = [];
|
|
138
|
+
const errors: SdError[] = [];
|
|
139
|
+
|
|
140
|
+
queue.on("error", (err) => {
|
|
141
|
+
errors.push(err);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// First request: error occurs
|
|
145
|
+
queue.run(() => {
|
|
146
|
+
calls.push(1);
|
|
147
|
+
throw new Error("error 1");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
await time(20);
|
|
151
|
+
|
|
152
|
+
// Add new request during execution
|
|
153
|
+
queue.run(() => {
|
|
154
|
+
calls.push(2);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
await time(100);
|
|
158
|
+
|
|
159
|
+
expect(calls).toEqual([1, 2]);
|
|
160
|
+
expect(errors).toHaveLength(1);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
//#endregion
|
|
165
|
+
|
|
166
|
+
//#region dispose
|
|
167
|
+
|
|
168
|
+
describe("dispose()", () => {
|
|
169
|
+
it("Clears pending tasks and timers", async () => {
|
|
170
|
+
const queue = new DebounceQueue(100);
|
|
171
|
+
const calls: number[] = [];
|
|
172
|
+
|
|
173
|
+
queue.run(() => {
|
|
174
|
+
calls.push(1);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Dispose during debounce wait
|
|
178
|
+
await time(50);
|
|
179
|
+
queue.dispose();
|
|
180
|
+
|
|
181
|
+
// After debounce time passes, still not executed
|
|
182
|
+
await time(100);
|
|
183
|
+
|
|
184
|
+
expect(calls).toEqual([]);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("New tasks ignored after dispose", async () => {
|
|
188
|
+
const queue = new DebounceQueue(50);
|
|
189
|
+
const calls: number[] = [];
|
|
190
|
+
|
|
191
|
+
queue.run(() => {
|
|
192
|
+
calls.push(1);
|
|
193
|
+
});
|
|
194
|
+
queue.dispose();
|
|
195
|
+
|
|
196
|
+
// Add new task after dispose - ignored
|
|
197
|
+
queue.run(() => {
|
|
198
|
+
calls.push(2);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
await time(100);
|
|
202
|
+
|
|
203
|
+
// Tasks after dispose not executed
|
|
204
|
+
expect(calls).toEqual([]);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("Safe to call multiple times", () => {
|
|
208
|
+
const queue = new DebounceQueue(50);
|
|
209
|
+
|
|
210
|
+
// Multiple calls without error
|
|
211
|
+
queue.dispose();
|
|
212
|
+
queue.dispose();
|
|
213
|
+
queue.dispose();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("Auto-disposed with using statement", async () => {
|
|
217
|
+
const calls: number[] = [];
|
|
218
|
+
{
|
|
219
|
+
using queue = new DebounceQueue(100);
|
|
220
|
+
queue.run(() => {
|
|
221
|
+
calls.push(1);
|
|
222
|
+
});
|
|
223
|
+
await time(50);
|
|
224
|
+
} // dispose automatically called at using block end
|
|
225
|
+
await time(100);
|
|
226
|
+
// Disposed during debounce wait, not executed
|
|
227
|
+
expect(calls).toEqual([]);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
//#endregion
|
|
232
|
+
|
|
233
|
+
//#region Synchronous function support
|
|
234
|
+
|
|
235
|
+
describe("Synchronous function support", () => {
|
|
236
|
+
it("Can execute synchronous function", async () => {
|
|
237
|
+
const queue = new DebounceQueue(10);
|
|
238
|
+
const calls: number[] = [];
|
|
239
|
+
|
|
240
|
+
queue.run(() => {
|
|
241
|
+
calls.push(1);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
await time(50);
|
|
245
|
+
|
|
246
|
+
expect(calls).toEqual([1]);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("Can mix synchronous and asynchronous functions", async () => {
|
|
250
|
+
const queue = new DebounceQueue(10);
|
|
251
|
+
const calls: number[] = [];
|
|
252
|
+
|
|
253
|
+
queue.run(() => {
|
|
254
|
+
calls.push(1);
|
|
255
|
+
});
|
|
256
|
+
queue.run(async () => {
|
|
257
|
+
await time(10);
|
|
258
|
+
calls.push(2);
|
|
259
|
+
});
|
|
260
|
+
queue.run(() => {
|
|
261
|
+
calls.push(3);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
await time(100);
|
|
265
|
+
|
|
266
|
+
// Only last request executed
|
|
267
|
+
expect(calls).toEqual([3]);
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
//#endregion
|
|
272
|
+
});
|