wenay-common2 1.0.46 → 1.0.49
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 +51 -37
- package/lib/Common/Color.d.ts +3 -0
- package/lib/Common/Color.js +4 -0
- package/lib/Common/ObserveAll2/index.d.ts +2 -0
- package/lib/Common/ObserveAll2/index.js +18 -0
- package/lib/Common/ObserveAll2/reactive2.d.ts +33 -0
- package/lib/Common/ObserveAll2/reactive2.js +254 -0
- package/lib/Common/ObserveAll2/store.d.ts +98 -0
- package/lib/Common/ObserveAll2/store.js +381 -0
- package/lib/Common/Time.d.ts +8 -0
- package/lib/Common/Time.js +41 -14
- package/lib/Common/async/PromiseArrayListen.d.ts +3 -3
- package/lib/Common/async/PromiseArrayListen.js +10 -13
- package/lib/Common/async/createIterableObject.js +1 -1
- package/lib/Common/async/waitRun.d.ts +11 -4
- package/lib/Common/async/waitRun.js +74 -44
- package/lib/Common/core/Decorator.d.ts +3 -1
- package/lib/Common/core/Decorator.js +5 -2
- package/lib/Common/core/common.d.ts +25 -7
- package/lib/Common/core/common.js +114 -34
- package/lib/Common/data/ByteStream.d.ts +1 -1
- package/lib/Common/data/ByteStream.js +21 -8
- package/lib/Common/data/List.d.ts +5 -0
- package/lib/Common/data/List.js +13 -4
- package/lib/Common/data/ListNodeAnd.js +2 -2
- package/lib/Common/data/objectPath.d.ts +4 -0
- package/lib/Common/data/objectPath.js +8 -3
- package/lib/Common/events/Listen.d.ts +1 -55
- package/lib/Common/events/Listen.js +15 -154
- package/lib/Common/events/Listen2.d.ts +1 -0
- package/lib/Common/events/Listen2.js +17 -0
- package/lib/Common/events/Listen3.d.ts +197 -0
- package/lib/Common/events/Listen3.js +249 -0
- package/lib/Common/events/SocketBuffer.d.ts +14 -9
- package/lib/Common/events/SocketServerHook.d.ts +26 -13
- package/lib/Common/events/SocketServerHook.js +2 -1
- package/lib/Common/events/UseListenTransform.d.ts +10 -5
- package/lib/Common/events/event.d.ts +12 -0
- package/lib/Common/events/event.js +10 -6
- package/lib/Common/events/joinListens.d.ts +2 -0
- package/lib/Common/events/joinListens.js +13 -12
- package/lib/Common/funcTimeWait.d.ts +28 -0
- package/lib/Common/funcTimeWait.js +18 -1
- package/lib/Common/isProxy.d.ts +1 -0
- package/lib/Common/isProxy.js +2 -0
- package/lib/Common/node_console.d.ts +2 -0
- package/lib/Common/node_console.js +3 -76
- package/lib/Common/rcp/createRpcServerAutoWithProtocolDetection.d.ts +2 -0
- package/lib/Common/rcp/createRpcServerAutoWithProtocolDetection.js +63 -30
- package/lib/Common/rcp/listen-deep.d.ts +13 -5
- package/lib/Common/rcp/listen-socket.d.ts +21 -3
- package/lib/Common/rcp/listen-socket.js +126 -17
- package/lib/Common/rcp/oldCommonsServer.d.ts +1 -1
- package/lib/Common/rcp/oldCommonsServer.js +15 -1
- package/lib/Common/rcp/old/320/241ommonsServerMini.js +1 -0
- package/lib/Common/rcp/rpc-caps.d.ts +10 -0
- package/lib/Common/rcp/rpc-caps.js +16 -0
- package/lib/Common/rcp/rpc-client.d.ts +23 -1
- package/lib/Common/rcp/rpc-client.js +306 -25
- package/lib/Common/rcp/rpc-clientHub.d.ts +7 -1
- package/lib/Common/rcp/rpc-clientHub.js +25 -2
- package/lib/Common/rcp/rpc-index.d.ts +3 -0
- package/lib/Common/rcp/rpc-index.js +3 -0
- package/lib/Common/rcp/rpc-inproc.d.ts +17 -0
- package/lib/Common/rcp/rpc-inproc.js +30 -0
- package/lib/Common/rcp/rpc-off.d.ts +10 -0
- package/lib/Common/rcp/rpc-off.js +22 -0
- package/lib/Common/rcp/rpc-protocol.d.ts +6 -0
- package/lib/Common/rcp/rpc-protocol.js +4 -2
- package/lib/Common/rcp/rpc-server-auto.d.ts +14 -3
- package/lib/Common/rcp/rpc-server-auto.js +81 -6
- package/lib/Common/rcp/rpc-server.d.ts +17 -2
- package/lib/Common/rcp/rpc-server.js +127 -31
- package/lib/Common/rcp/rpc-shape.d.ts +17 -0
- package/lib/Common/rcp/rpc-shape.js +42 -0
- package/lib/Common/rcp/rpc-walk.js +29 -11
- package/lib/Exchange/Bars.d.ts +3 -0
- package/lib/Exchange/Bars.js +4 -1
- package/lib/Exchange/CParams.d.ts +2 -0
- package/lib/Exchange/CParams.js +21 -14
- package/lib/Exchange/MarketData.d.ts +6 -0
- package/lib/Exchange/MarketData.js +4 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +3 -1
- package/lib/server/WebHook3.js +22 -9
- package/package.json +3 -1
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createStore = createStore;
|
|
4
|
+
exports.exposeStore = exposeStore;
|
|
5
|
+
exports.createStoreMirror = createStoreMirror;
|
|
6
|
+
const reactive2_1 = require("./reactive2");
|
|
7
|
+
const hasSetImmediate = typeof setImmediate == "function";
|
|
8
|
+
function pathKey(path) {
|
|
9
|
+
return path.map(String).join(".");
|
|
10
|
+
}
|
|
11
|
+
function schedule(drain, flush) {
|
|
12
|
+
if (drain == null) {
|
|
13
|
+
flush();
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (drain == "micro") {
|
|
17
|
+
queueMicrotask(flush);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (drain == "immediate") {
|
|
21
|
+
(hasSetImmediate ? setImmediate : setTimeout)(flush, 0);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (typeof drain == "number") {
|
|
25
|
+
setTimeout(flush, drain);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
drain(flush);
|
|
29
|
+
}
|
|
30
|
+
function createDrained(fn, drain) {
|
|
31
|
+
let scheduled = false;
|
|
32
|
+
let latest = null;
|
|
33
|
+
let closed = false;
|
|
34
|
+
return {
|
|
35
|
+
push(...a) {
|
|
36
|
+
if (closed)
|
|
37
|
+
return;
|
|
38
|
+
if (drain == null) {
|
|
39
|
+
fn(...a);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
latest = a;
|
|
43
|
+
if (scheduled)
|
|
44
|
+
return;
|
|
45
|
+
scheduled = true;
|
|
46
|
+
schedule(drain, () => {
|
|
47
|
+
scheduled = false;
|
|
48
|
+
const x = latest;
|
|
49
|
+
latest = null;
|
|
50
|
+
if (!closed && x)
|
|
51
|
+
fn(...x);
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
close() { closed = true; latest = null; },
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function isObj(v) {
|
|
58
|
+
return v != null && typeof v == "object";
|
|
59
|
+
}
|
|
60
|
+
function getAt(root, path) {
|
|
61
|
+
let cur = root;
|
|
62
|
+
for (const k of path) {
|
|
63
|
+
if (!isObj(cur))
|
|
64
|
+
return undefined;
|
|
65
|
+
cur = cur[k];
|
|
66
|
+
}
|
|
67
|
+
return cur;
|
|
68
|
+
}
|
|
69
|
+
function hasAt(root, path) {
|
|
70
|
+
let cur = root;
|
|
71
|
+
for (const k of path) {
|
|
72
|
+
if (!isObj(cur) || !(k in cur))
|
|
73
|
+
return false;
|
|
74
|
+
cur = cur[k];
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
function ensureParent(root, path) {
|
|
79
|
+
let cur = root;
|
|
80
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
81
|
+
const k = path[i];
|
|
82
|
+
if (!isObj(cur[k]))
|
|
83
|
+
cur[k] = {};
|
|
84
|
+
cur = cur[k];
|
|
85
|
+
}
|
|
86
|
+
return cur;
|
|
87
|
+
}
|
|
88
|
+
function replaceRoot(root, value) {
|
|
89
|
+
for (const k of Reflect.ownKeys(root))
|
|
90
|
+
if (!isObj(value) || !(k in value))
|
|
91
|
+
delete root[k];
|
|
92
|
+
if (isObj(value))
|
|
93
|
+
for (const k of Reflect.ownKeys(value))
|
|
94
|
+
root[k] = value[k];
|
|
95
|
+
}
|
|
96
|
+
function setAt(root, path, value) {
|
|
97
|
+
if (path.length == 0) {
|
|
98
|
+
replaceRoot(root, value);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const p = ensureParent(root, path);
|
|
102
|
+
p[path[path.length - 1]] = value;
|
|
103
|
+
}
|
|
104
|
+
function snapshotValue(value, seen = new WeakMap()) {
|
|
105
|
+
if (!isObj(value))
|
|
106
|
+
return value;
|
|
107
|
+
if (value instanceof Date)
|
|
108
|
+
return new Date(value.valueOf());
|
|
109
|
+
if (value instanceof RegExp)
|
|
110
|
+
return new RegExp(value.source, value.flags);
|
|
111
|
+
if (value instanceof Map) {
|
|
112
|
+
const out = new Map();
|
|
113
|
+
seen.set(value, out);
|
|
114
|
+
value.forEach((v, k) => out.set(snapshotValue(k, seen), snapshotValue(v, seen)));
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
if (value instanceof Set) {
|
|
118
|
+
const out = new Set();
|
|
119
|
+
seen.set(value, out);
|
|
120
|
+
value.forEach(v => out.add(snapshotValue(v, seen)));
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
const old = seen.get(value);
|
|
124
|
+
if (old)
|
|
125
|
+
return old;
|
|
126
|
+
const out = Array.isArray(value) ? [] : {};
|
|
127
|
+
seen.set(value, out);
|
|
128
|
+
for (const k of Reflect.ownKeys(value))
|
|
129
|
+
out[k] = snapshotValue(value[k], seen);
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
function maskPaths(mask, base = []) {
|
|
133
|
+
if (mask === true || mask == null)
|
|
134
|
+
return [base];
|
|
135
|
+
if (!isObj(mask))
|
|
136
|
+
return [base];
|
|
137
|
+
const out = [];
|
|
138
|
+
for (const k of Object.keys(mask))
|
|
139
|
+
out.push(...maskPaths(mask[k], [...base, k]));
|
|
140
|
+
return out;
|
|
141
|
+
}
|
|
142
|
+
function pickSnapshot(root, mask, base = []) {
|
|
143
|
+
if (mask === true || mask == null)
|
|
144
|
+
return snapshotValue(getAt(root, base));
|
|
145
|
+
const out = {};
|
|
146
|
+
for (const k of Object.keys(mask))
|
|
147
|
+
out[k] = pickSnapshot(root, mask[k], [...base, k]);
|
|
148
|
+
return out;
|
|
149
|
+
}
|
|
150
|
+
function applyMask(root, mask, data, base = []) {
|
|
151
|
+
if (mask === true || mask == null) {
|
|
152
|
+
setAt(root, base, snapshotValue(data));
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
for (const k of Object.keys(mask))
|
|
156
|
+
applyMask(root, mask[k], data?.[k], [...base, k]);
|
|
157
|
+
}
|
|
158
|
+
function watchTarget(root, path) {
|
|
159
|
+
let cur = root;
|
|
160
|
+
let lastReactive = root;
|
|
161
|
+
for (const k of path) {
|
|
162
|
+
if (!isObj(cur) || !(k in cur))
|
|
163
|
+
return lastReactive;
|
|
164
|
+
const next = cur[k];
|
|
165
|
+
if ((0, reactive2_1.isReactive)(next)) {
|
|
166
|
+
cur = next;
|
|
167
|
+
lastReactive = next;
|
|
168
|
+
}
|
|
169
|
+
else
|
|
170
|
+
return lastReactive;
|
|
171
|
+
}
|
|
172
|
+
return lastReactive;
|
|
173
|
+
}
|
|
174
|
+
function sameLeaf(a, b, ae, be) {
|
|
175
|
+
if (ae !== be)
|
|
176
|
+
return false;
|
|
177
|
+
if (!ae && !be)
|
|
178
|
+
return true;
|
|
179
|
+
return Object.is(a, b);
|
|
180
|
+
}
|
|
181
|
+
function makeCtx(store, path) {
|
|
182
|
+
return {
|
|
183
|
+
store,
|
|
184
|
+
node: getNode(store, path),
|
|
185
|
+
path: [...path],
|
|
186
|
+
pathString: pathKey(path),
|
|
187
|
+
exists: hasAt(store._state, path),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
function incCount(store, path) {
|
|
191
|
+
const k = pathKey(path);
|
|
192
|
+
store._counts.set(k, (store._counts.get(k) ?? 0) + 1);
|
|
193
|
+
}
|
|
194
|
+
function decCount(store, path) {
|
|
195
|
+
const k = pathKey(path);
|
|
196
|
+
const n = (store._counts.get(k) ?? 0) - 1;
|
|
197
|
+
if (n > 0)
|
|
198
|
+
store._counts.set(k, n);
|
|
199
|
+
else
|
|
200
|
+
store._counts.delete(k);
|
|
201
|
+
}
|
|
202
|
+
function subscribePath(store, path, cb, opts = {}, once = false) {
|
|
203
|
+
let done = false;
|
|
204
|
+
let offUpdate = null;
|
|
205
|
+
let lastExists = hasAt(store._state, path);
|
|
206
|
+
let lastValue = getAt(store._state, path);
|
|
207
|
+
const drained = createDrained((value, ctx) => {
|
|
208
|
+
if (done)
|
|
209
|
+
return;
|
|
210
|
+
cb(value, ctx);
|
|
211
|
+
if (once)
|
|
212
|
+
off();
|
|
213
|
+
}, opts.drain);
|
|
214
|
+
function emitNow() {
|
|
215
|
+
drained.push(getAt(store._state, path), makeCtx(store, path));
|
|
216
|
+
}
|
|
217
|
+
function attach() {
|
|
218
|
+
offUpdate?.();
|
|
219
|
+
const target = watchTarget(store._state, path);
|
|
220
|
+
offUpdate = (0, reactive2_1.onUpdate)(target, () => {
|
|
221
|
+
const exists = hasAt(store._state, path);
|
|
222
|
+
const value = getAt(store._state, path);
|
|
223
|
+
const valueIsObject = (0, reactive2_1.isReactive)(value);
|
|
224
|
+
const watchedSelf = target === value;
|
|
225
|
+
if (!valueIsObject && !watchedSelf && sameLeaf(lastValue, value, lastExists, exists))
|
|
226
|
+
return;
|
|
227
|
+
lastExists = exists;
|
|
228
|
+
lastValue = value;
|
|
229
|
+
const nextTarget = watchTarget(store._state, path);
|
|
230
|
+
if (nextTarget !== target && !done)
|
|
231
|
+
attach();
|
|
232
|
+
emitNow();
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function off() {
|
|
236
|
+
if (done)
|
|
237
|
+
return;
|
|
238
|
+
done = true;
|
|
239
|
+
drained.close();
|
|
240
|
+
offUpdate?.();
|
|
241
|
+
offUpdate = null;
|
|
242
|
+
decCount(store, path);
|
|
243
|
+
}
|
|
244
|
+
incCount(store, path);
|
|
245
|
+
if (opts.current && lastExists) {
|
|
246
|
+
cb(lastValue, makeCtx(store, path));
|
|
247
|
+
if (once) {
|
|
248
|
+
off();
|
|
249
|
+
return off;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
attach();
|
|
253
|
+
return off;
|
|
254
|
+
}
|
|
255
|
+
function getNode(store, path) {
|
|
256
|
+
const k = pathKey(path);
|
|
257
|
+
const cached = store._nodeCache.get(k);
|
|
258
|
+
if (cached)
|
|
259
|
+
return cached;
|
|
260
|
+
const api = {
|
|
261
|
+
get path() { return [...path]; },
|
|
262
|
+
get pathString() { return pathKey(path); },
|
|
263
|
+
get: () => getAt(store._state, path),
|
|
264
|
+
has: () => hasAt(store._state, path),
|
|
265
|
+
snapshot: () => snapshotValue(getAt(store._state, path)),
|
|
266
|
+
set: (value) => setAt(store._state, path, value),
|
|
267
|
+
replace: (value) => setAt(store._state, path, value),
|
|
268
|
+
on: (cb, opts) => subscribePath(store, path, cb, opts, false),
|
|
269
|
+
once: (cb, opts) => subscribePath(store, path, cb, opts, true),
|
|
270
|
+
update: (mask, opts) => createSelection(store, path, mask, opts),
|
|
271
|
+
at: (key) => getNode(store, [...path, key]),
|
|
272
|
+
count: () => store._counts.get(pathKey(path)) ?? 0,
|
|
273
|
+
};
|
|
274
|
+
const proxy = new Proxy(api, {
|
|
275
|
+
get(target, p) {
|
|
276
|
+
if (p === "then")
|
|
277
|
+
return undefined;
|
|
278
|
+
if (p in target)
|
|
279
|
+
return target[p];
|
|
280
|
+
if (typeof p == "symbol")
|
|
281
|
+
return undefined;
|
|
282
|
+
return getNode(store, [...path, p]);
|
|
283
|
+
},
|
|
284
|
+
ownKeys() {
|
|
285
|
+
const v = getAt(store._state, path);
|
|
286
|
+
return isObj(v) ? Reflect.ownKeys(v) : [];
|
|
287
|
+
},
|
|
288
|
+
getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }; },
|
|
289
|
+
});
|
|
290
|
+
store._nodeCache.set(k, proxy);
|
|
291
|
+
return proxy;
|
|
292
|
+
}
|
|
293
|
+
function createSelection(store, base, mask, defaults = {}) {
|
|
294
|
+
const fullPaths = maskPaths(mask, base);
|
|
295
|
+
const rootNode = getNode(store, base);
|
|
296
|
+
const ctx = () => ({ store, node: rootNode, mask, paths: fullPaths.map(p => [...p]) });
|
|
297
|
+
const get = () => pickSnapshot(store._state, mask, base);
|
|
298
|
+
return {
|
|
299
|
+
mask,
|
|
300
|
+
paths: fullPaths.map(p => [...p]),
|
|
301
|
+
get,
|
|
302
|
+
on(cb, opts = {}) {
|
|
303
|
+
const o = { ...defaults, ...opts, current: false };
|
|
304
|
+
const drained = createDrained(() => cb(get(), ctx()), opts.drain ?? defaults.drain ?? "micro");
|
|
305
|
+
const offs = fullPaths.map(p => subscribePath(store, p, () => drained.push(), o, false));
|
|
306
|
+
if ((opts.current ?? defaults.current))
|
|
307
|
+
cb(get(), ctx());
|
|
308
|
+
return () => { drained.close(); for (const off of offs)
|
|
309
|
+
off(); };
|
|
310
|
+
},
|
|
311
|
+
once(cb, opts = {}) {
|
|
312
|
+
let off = () => { };
|
|
313
|
+
off = this.on((v, c) => { off(); cb(v, c); }, { ...opts, current: opts.current ?? defaults.current });
|
|
314
|
+
return off;
|
|
315
|
+
},
|
|
316
|
+
onEach(cb, opts = {}) {
|
|
317
|
+
const o = { ...defaults, ...opts };
|
|
318
|
+
const offs = fullPaths.map(p => subscribePath(store, p, cb, o, false));
|
|
319
|
+
return () => { for (const off of offs)
|
|
320
|
+
off(); };
|
|
321
|
+
},
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
function createStore(initial, opts = {}) {
|
|
325
|
+
const state = (0, reactive2_1.reactive)(initial, opts);
|
|
326
|
+
let store;
|
|
327
|
+
store = {
|
|
328
|
+
_state: state,
|
|
329
|
+
_nodeCache: new Map(),
|
|
330
|
+
_counts: new Map(),
|
|
331
|
+
state,
|
|
332
|
+
get node() { return getNode(store, []); },
|
|
333
|
+
get: () => state,
|
|
334
|
+
snapshot: () => snapshotValue(state),
|
|
335
|
+
replace: (value) => replaceRoot(state, value),
|
|
336
|
+
on: (cb, opts) => getNode(store, []).on(cb, opts),
|
|
337
|
+
once: (cb, opts) => getNode(store, []).once(cb, opts),
|
|
338
|
+
update: (mask, opts) => createSelection(store, [], mask, opts),
|
|
339
|
+
listen: () => (0, reactive2_1.listenUpdate)(state),
|
|
340
|
+
count: () => Array.from(store._counts.values()).reduce((a, b) => a + b, 0),
|
|
341
|
+
};
|
|
342
|
+
return store;
|
|
343
|
+
}
|
|
344
|
+
function exposeStore(store) {
|
|
345
|
+
return {
|
|
346
|
+
get: (mask) => mask ? store.update(mask).get() : store.snapshot(),
|
|
347
|
+
set: (path, value) => {
|
|
348
|
+
let node = store.node;
|
|
349
|
+
for (const k of path)
|
|
350
|
+
node = node.at(k);
|
|
351
|
+
node.replace(value);
|
|
352
|
+
},
|
|
353
|
+
replace: (path, value) => {
|
|
354
|
+
let node = store.node;
|
|
355
|
+
for (const k of path)
|
|
356
|
+
node = node.at(k);
|
|
357
|
+
node.replace(value);
|
|
358
|
+
},
|
|
359
|
+
changed: store.listen(),
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
function createStoreMirror(remote, initial = {}, opts = {}) {
|
|
363
|
+
const store = createStore(initial, opts);
|
|
364
|
+
async function sync(mask, subOpts = { current: true }) {
|
|
365
|
+
async function pull() {
|
|
366
|
+
const snap = await remote.get(mask);
|
|
367
|
+
applyMask(store.state, mask, snap);
|
|
368
|
+
}
|
|
369
|
+
if (subOpts.current !== false)
|
|
370
|
+
await pull();
|
|
371
|
+
const drained = createDrained(() => { void pull(); }, subOpts.drain);
|
|
372
|
+
const changed = remote.changed;
|
|
373
|
+
const off = typeof changed?.on == "function"
|
|
374
|
+
? changed.on(() => drained.push())
|
|
375
|
+
: typeof changed?.addListen == "function"
|
|
376
|
+
? changed.addListen(() => drained.push())
|
|
377
|
+
: (() => { });
|
|
378
|
+
return () => { drained.close(); off?.(); };
|
|
379
|
+
}
|
|
380
|
+
return Object.assign(store, { sync });
|
|
381
|
+
}
|
package/lib/Common/Time.d.ts
CHANGED
|
@@ -191,12 +191,18 @@ export declare function timeLocalToStr_yyyymmdd_hhmmss(date: const_Date, dateDel
|
|
|
191
191
|
export declare function timeLocalToStr_yyyymmdd_hhmmss_ms(date: const_Date, dateDelim?: string): string;
|
|
192
192
|
export declare function timeToString_yyyymmdd_hhmm_offset(date: const_Date): string;
|
|
193
193
|
export declare function timeToString_yyyymmdd_hhmmss_offset(date: const_Date): string;
|
|
194
|
+
export type tTimeFormatPattern = 'HH:mm:ss' | 'HH:mm:ss.SSS' | 'yyyy-MM-dd' | 'yyyy-MM-dd HH:mm' | 'yyyy-MM-dd HH:mm:ss' | 'yyyy-MM-dd HH:mm:ss.SSS' | 'yyyy-MM-dd HH:mm O' | 'yyyy-MM-dd HH:mm:ss O';
|
|
195
|
+
export declare function format(date: const_Date, pattern: tTimeFormatPattern, opts?: {
|
|
196
|
+
utc?: boolean;
|
|
197
|
+
}): string;
|
|
194
198
|
export declare function convertDatesToStrings(arg: any): any;
|
|
195
199
|
export declare function toPrintObject(arg: any): any;
|
|
196
200
|
export declare function durationToStr(duration_ms: number): string;
|
|
197
201
|
export declare function durationToStrNullable(duration_ms: number | null | undefined): string | null;
|
|
198
202
|
export declare function durationToStr_h_mm_ss(duration_ms: number): string;
|
|
199
203
|
export declare function durationToStr_h_mm_ss_ms(duration_ms: number): string;
|
|
204
|
+
export type tDurationFormatPattern = 'H:mm:ss' | 'H:mm:ss.SSS';
|
|
205
|
+
export declare function formatDuration(duration_ms: number, pattern?: tDurationFormatPattern): string;
|
|
200
206
|
export declare class CDelayer {
|
|
201
207
|
protected remainPause: number;
|
|
202
208
|
sleepAsync(pause_ms_getter: () => number | null): Promise<void>;
|
|
@@ -204,3 +210,5 @@ export declare class CDelayer {
|
|
|
204
210
|
type NullableTime = const_Date | undefined | null;
|
|
205
211
|
export declare function MinTime<T1 extends NullableTime, T2 extends NullableTime>(time1: T1, time2: T2): T1 | NonNullable<T2>;
|
|
206
212
|
export declare function MaxTime<T1 extends NullableTime, T2 extends NullableTime>(time1: T1, time2: T2): T1 | NonNullable<T2>;
|
|
213
|
+
export declare const minDate: typeof MinTime;
|
|
214
|
+
export declare const maxDate: typeof MaxTime;
|
package/lib/Common/Time.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CDelayer = exports.Period = exports.PeriodSpan = exports.TF = exports.TIME_UNIT = exports.M1_MS = exports.H1_MS = exports.D1_MS = exports.W1_MS = exports.W1_S = exports.D1_S = exports.H1_S = void 0;
|
|
3
|
+
exports.maxDate = exports.minDate = exports.CDelayer = exports.Period = exports.PeriodSpan = exports.TF = exports.TIME_UNIT = exports.M1_MS = exports.H1_MS = exports.D1_MS = exports.W1_MS = exports.W1_S = exports.D1_S = exports.H1_S = void 0;
|
|
4
4
|
exports.timeToStr_hhmmss_ms = timeToStr_hhmmss_ms;
|
|
5
5
|
exports.timeToStr_hhmmss = timeToStr_hhmmss;
|
|
6
6
|
exports.timeToStr_yyyymmdd_hhmm = timeToStr_yyyymmdd_hhmm;
|
|
@@ -14,12 +14,14 @@ exports.timeLocalToStr_yyyymmdd_hhmmss = timeLocalToStr_yyyymmdd_hhmmss;
|
|
|
14
14
|
exports.timeLocalToStr_yyyymmdd_hhmmss_ms = timeLocalToStr_yyyymmdd_hhmmss_ms;
|
|
15
15
|
exports.timeToString_yyyymmdd_hhmm_offset = timeToString_yyyymmdd_hhmm_offset;
|
|
16
16
|
exports.timeToString_yyyymmdd_hhmmss_offset = timeToString_yyyymmdd_hhmmss_offset;
|
|
17
|
+
exports.format = format;
|
|
17
18
|
exports.convertDatesToStrings = convertDatesToStrings;
|
|
18
19
|
exports.toPrintObject = toPrintObject;
|
|
19
20
|
exports.durationToStr = durationToStr;
|
|
20
21
|
exports.durationToStrNullable = durationToStrNullable;
|
|
21
22
|
exports.durationToStr_h_mm_ss = durationToStr_h_mm_ss;
|
|
22
23
|
exports.durationToStr_h_mm_ss_ms = durationToStr_h_mm_ss_ms;
|
|
24
|
+
exports.formatDuration = formatDuration;
|
|
23
25
|
exports.MinTime = MinTime;
|
|
24
26
|
exports.MaxTime = MaxTime;
|
|
25
27
|
const common_1 = require("./core/common");
|
|
@@ -293,6 +295,19 @@ function timeLocalToStr_yyyymmdd_hhmmss(date, dateDelim = "-") { return timeLoca
|
|
|
293
295
|
function timeLocalToStr_yyyymmdd_hhmmss_ms(date, dateDelim = "-") { return timeLocalToStr_yyyymmdd_hhmmss(date, dateDelim) + "." + str3(date.getMilliseconds()); }
|
|
294
296
|
function timeToString_yyyymmdd_hhmm_offset(date) { let offset = date.getTimezoneOffset(); return timeLocalToStr_yyyymmdd_hhmm(date) + " GMT" + (offset < 0 ? '+' : '') + (-offset / 60); }
|
|
295
297
|
function timeToString_yyyymmdd_hhmmss_offset(date) { let offset = date.getTimezoneOffset(); return timeLocalToStr_yyyymmdd_hhmmss(date) + " GMT" + (offset < 0 ? '+' : '') + (-offset / 60); }
|
|
298
|
+
function format(date, pattern, opts) {
|
|
299
|
+
let utc = opts?.utc ?? true;
|
|
300
|
+
switch (pattern) {
|
|
301
|
+
case 'HH:mm:ss': return utc ? timeToStr_hhmmss(date) : timeLocalToStr_hhmmss(date);
|
|
302
|
+
case 'HH:mm:ss.SSS': return utc ? timeToStr_hhmmss_ms(date) : (timeLocalToStr_hhmmss(date) + '.' + str3(date.getMilliseconds()));
|
|
303
|
+
case 'yyyy-MM-dd': return utc ? timeToStr_yyyymmdd_hhmm(date).slice(0, 10) : timeLocalToStr_yyyymmdd(date);
|
|
304
|
+
case 'yyyy-MM-dd HH:mm': return utc ? timeToStr_yyyymmdd_hhmm(date) : timeLocalToStr_yyyymmdd_hhmm(date);
|
|
305
|
+
case 'yyyy-MM-dd HH:mm:ss': return utc ? timeToStr_yyyymmdd_hhmmss(date) : timeLocalToStr_yyyymmdd_hhmmss(date);
|
|
306
|
+
case 'yyyy-MM-dd HH:mm:ss.SSS': return utc ? timeToStr_yyyymmdd_hhmmss_ms(date) : timeLocalToStr_yyyymmdd_hhmmss_ms(date);
|
|
307
|
+
case 'yyyy-MM-dd HH:mm O': return timeToString_yyyymmdd_hhmm_offset(date);
|
|
308
|
+
case 'yyyy-MM-dd HH:mm:ss O': return timeToString_yyyymmdd_hhmmss_offset(date);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
296
311
|
Date.prototype.toString = function () { return timeToString_yyyymmdd_hhmmss_offset(this); };
|
|
297
312
|
Date.prototype.toDateString = function () { return timeLocalToStr_yyyymmdd(this); };
|
|
298
313
|
Date.prototype.toTimeString = function () { let offset = this.getTimezoneOffset(); return timeLocalToStr_hhmmss(this) + " GMT" + (offset < 0 ? '+' : '') + (-offset / 60); };
|
|
@@ -331,25 +346,32 @@ function replaceConsoleCommands() {
|
|
|
331
346
|
}
|
|
332
347
|
function durationToStr(duration_ms) {
|
|
333
348
|
let units = [[exports.D1_MS, "д"], [exports.H1_MS, "ч"], [exports.M1_MS, "м"], [1000, "c"], [1, "мс"]];
|
|
334
|
-
let
|
|
349
|
+
let firstUnit = null;
|
|
350
|
+
let firstCount = 0;
|
|
335
351
|
let str = "";
|
|
336
352
|
for (let unit of units) {
|
|
337
353
|
let unitCountFloat = duration_ms / unit[0];
|
|
338
|
-
if (unitCountFloat < 1.1 &&
|
|
354
|
+
if (unitCountFloat < 1.1 && firstUnit == null)
|
|
355
|
+
continue;
|
|
356
|
+
if (firstUnit == null && unitCountFloat <= 10) {
|
|
357
|
+
firstUnit = unit;
|
|
358
|
+
firstCount = Math.floor(unitCountFloat);
|
|
359
|
+
duration_ms %= unit[0];
|
|
339
360
|
continue;
|
|
340
|
-
let unitCount;
|
|
341
|
-
if (lastUnit || unitCountFloat > 10) {
|
|
342
|
-
unitCount = Math.round(unitCountFloat);
|
|
343
|
-
lastUnit = unit;
|
|
344
361
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
362
|
+
let unitCount = Math.round(unitCountFloat);
|
|
363
|
+
if (firstUnit && unitCount * unit[0] >= firstUnit[0]) {
|
|
364
|
+
firstCount++;
|
|
365
|
+
unitCount = 0;
|
|
366
|
+
}
|
|
367
|
+
if (firstUnit)
|
|
368
|
+
str += firstCount + firstUnit[1] + " ";
|
|
369
|
+
if (unitCount > 0 || firstUnit == null)
|
|
370
|
+
str += unitCount + unit[1] + " ";
|
|
371
|
+
return str;
|
|
352
372
|
}
|
|
373
|
+
if (firstUnit)
|
|
374
|
+
str += firstCount + firstUnit[1] + " ";
|
|
353
375
|
return str;
|
|
354
376
|
}
|
|
355
377
|
function durationToStrNullable(duration_ms) { return duration_ms == null ? null : durationToStr(duration_ms); }
|
|
@@ -358,6 +380,9 @@ function durationToStr_h_mm_ss(duration_ms) {
|
|
|
358
380
|
return Math.trunc(duration_ms / exports.H1_MS) + ":" + str2(time.getUTCMinutes()) + ":" + str2(time.getUTCSeconds());
|
|
359
381
|
}
|
|
360
382
|
function durationToStr_h_mm_ss_ms(duration_ms) { return durationToStr_h_mm_ss(duration_ms) + "." + str3(Math.trunc(duration_ms % 1000)); }
|
|
383
|
+
function formatDuration(duration_ms, pattern = 'H:mm:ss') {
|
|
384
|
+
return pattern == 'H:mm:ss.SSS' ? durationToStr_h_mm_ss_ms(duration_ms) : durationToStr_h_mm_ss(duration_ms);
|
|
385
|
+
}
|
|
361
386
|
async function sleepAsync(msec) {
|
|
362
387
|
return new Promise((resolve, reject) => { setTimeout(resolve, msec); });
|
|
363
388
|
}
|
|
@@ -391,3 +416,5 @@ function MinTime(time1, time2) {
|
|
|
391
416
|
function MaxTime(time1, time2) {
|
|
392
417
|
return time1 && time2 && time1.valueOf() >= time2.valueOf() ? time1 : time2 ?? time1;
|
|
393
418
|
}
|
|
419
|
+
exports.minDate = MinTime;
|
|
420
|
+
exports.maxDate = MaxTime;
|
|
@@ -2,10 +2,10 @@ export declare function PromiseArrayListen<T extends any = unknown>(array: ((()
|
|
|
2
2
|
listenOk: (a: (...d: [data: T, i: number, countOk: number, countError: number, count: number]) => any) => () => void;
|
|
3
3
|
listenError: (a: (...d: [error: any, i: number, countOk: number, countError: number, count: number]) => any) => () => void;
|
|
4
4
|
promise: {
|
|
5
|
-
all: () => Promise<
|
|
6
|
-
allSettled: () => Promise<PromiseSettledResult<
|
|
5
|
+
all: () => Promise<any[]>;
|
|
6
|
+
allSettled: () => Promise<PromiseSettledResult<any>[]>;
|
|
7
7
|
};
|
|
8
|
-
getData():
|
|
8
|
+
getData(): Promise<any>[];
|
|
9
9
|
status(): {
|
|
10
10
|
ok: number;
|
|
11
11
|
error: number;
|
|
@@ -15,23 +15,20 @@ function PromiseArrayListen(array) {
|
|
|
15
15
|
++errorCount;
|
|
16
16
|
c[0](error, i, ok, errorCount, count);
|
|
17
17
|
};
|
|
18
|
-
const
|
|
19
|
-
|
|
18
|
+
const wrap = (promise, i) => promise.then(r => { a(r, i); return r; }).catch((er) => { b(er, i); throw er; });
|
|
19
|
+
const arr = array.map((e, i) => e instanceof Promise ? wrap(e, i)
|
|
20
|
+
: () => wrap((async () => e())(), i));
|
|
21
|
+
const started = [];
|
|
22
|
+
const startAll = () => arr.map((x, i) => started[i] ??= (typeof x === 'function' ? x() : x));
|
|
20
23
|
return {
|
|
21
|
-
listenOk: (a) =>
|
|
22
|
-
|
|
23
|
-
return () => t[1].removeListen(a);
|
|
24
|
-
},
|
|
25
|
-
listenError: (a) => {
|
|
26
|
-
c[1].addListen(a);
|
|
27
|
-
return () => c[1].removeListen(a);
|
|
28
|
-
},
|
|
24
|
+
listenOk: (a) => t[1].addListen(a),
|
|
25
|
+
listenError: (a) => c[1].addListen(a),
|
|
29
26
|
promise: {
|
|
30
|
-
all: () => Promise.all(
|
|
31
|
-
allSettled: () => Promise.allSettled(
|
|
27
|
+
all: () => Promise.all(startAll()),
|
|
28
|
+
allSettled: () => Promise.allSettled(startAll()),
|
|
32
29
|
},
|
|
33
30
|
getData() {
|
|
34
|
-
return
|
|
31
|
+
return startAll();
|
|
35
32
|
},
|
|
36
33
|
status() {
|
|
37
34
|
return { ok, error: errorCount, count };
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
export declare function
|
|
1
|
+
export declare function createThrottle(): {
|
|
2
|
+
throttle: (ms: number, func: () => any) => void;
|
|
3
|
+
debounce: (ms: number, func: () => any) => Promise<void>;
|
|
2
4
|
throttleAsync: (ms: number, func: () => any) => void;
|
|
3
5
|
debounceAsync: (ms: number, func: () => any) => Promise<void>;
|
|
4
6
|
};
|
|
7
|
+
export declare const enhancedWaitRun: typeof createThrottle;
|
|
5
8
|
export declare function createAsyncQueue(concurrency?: number): {
|
|
6
|
-
|
|
9
|
+
add: <T>(task: () => Promise<T>) => Promise<T>;
|
|
7
10
|
onIdle: () => Promise<void>;
|
|
11
|
+
readonly size: number;
|
|
12
|
+
enqueue: <T>(task: () => Promise<T>) => Promise<T>;
|
|
8
13
|
getQueueSize: () => number;
|
|
9
14
|
};
|
|
10
15
|
export declare function enhancedQueueRun(maxParallelTasks?: number): {
|
|
@@ -23,9 +28,11 @@ export declare function queueRun(n?: number): {
|
|
|
23
28
|
nextRun: () => Promise<any>;
|
|
24
29
|
run: () => Promise<void>;
|
|
25
30
|
};
|
|
26
|
-
export declare function
|
|
31
|
+
export declare function createReadyGate(): {
|
|
27
32
|
add: (fn: () => any) => number | undefined;
|
|
28
|
-
|
|
33
|
+
ready: () => Promise<void>;
|
|
29
34
|
isReady: () => boolean;
|
|
30
35
|
tasks: () => (() => any)[];
|
|
36
|
+
setReady: () => Promise<void>;
|
|
31
37
|
};
|
|
38
|
+
export declare const createTaskQueue: typeof createReadyGate;
|