wenay-common 1.0.152 → 1.0.154
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/lib/Common/Listen.d.ts +40 -2
- package/lib/Common/Listen.js +34 -1
- package/lib/test.d.ts +1 -0
- package/lib/test.js +65 -0
- package/package.json +1 -1
package/lib/Common/Listen.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export declare function funcListenCallback<T extends any[]>(a: (e: tr222<T>) =>
|
|
|
5
5
|
isRun: () => boolean;
|
|
6
6
|
run: () => (() => void) | null;
|
|
7
7
|
close: () => void;
|
|
8
|
-
addListen: (a: tr222<T>) => void;
|
|
8
|
+
addListen: (a: tr222<T>) => () => void;
|
|
9
9
|
removeListen: (a: (tr222<T>) | null) => void;
|
|
10
10
|
count: () => number;
|
|
11
11
|
};
|
|
@@ -41,7 +41,7 @@ export declare function UseListen<T extends any[]>(data?: {
|
|
|
41
41
|
isRun: () => boolean;
|
|
42
42
|
run: () => (() => void) | null;
|
|
43
43
|
close: () => void;
|
|
44
|
-
addListen: (a: tr222<T>) => void;
|
|
44
|
+
addListen: (a: tr222<T>) => () => void;
|
|
45
45
|
removeListen: (a: tr222<T> | null) => void;
|
|
46
46
|
count: () => number;
|
|
47
47
|
}];
|
|
@@ -59,4 +59,42 @@ export declare function PromiseArrayListen<T extends any = unknown>(array: ((()
|
|
|
59
59
|
count: number;
|
|
60
60
|
};
|
|
61
61
|
};
|
|
62
|
+
export type realSocket2<T extends any> = (data: {
|
|
63
|
+
callback: (data: T) => void;
|
|
64
|
+
[key: string]: any;
|
|
65
|
+
}, ...b: any[]) => (any | (() => any));
|
|
66
|
+
export type getTypeCallback<T extends realSocket2<any>> = T extends realSocket2<infer R> ? R : never;
|
|
67
|
+
type ParametersOther<T extends (forget: any, ...args: any) => any> = T extends (forget: any, ...args: infer P) => any ? P : never;
|
|
68
|
+
type tr22<T> = T extends undefined ? never : T;
|
|
69
|
+
export declare function socketBuffer3<T extends realSocket2<any | any[]>, T2 extends (readonly unknown[]) | undefined, T3 extends {
|
|
70
|
+
[key: string]: unknown;
|
|
71
|
+
}>(func: T, callbackMain: (data: getTypeCallback<T>, memo: T3) => T2, memo?: T3): (a: Omit<Parameters<T>[0], "callback"> & {
|
|
72
|
+
callback: (...data: tr22<T2>) => any;
|
|
73
|
+
}, ...b: ParametersOther<T>) => ReturnType<T>;
|
|
74
|
+
export declare function socketBufferBySnapshot<T extends realSocket2<any | any[]>, T2 extends (readonly unknown[]) | undefined, T3 extends {
|
|
75
|
+
[key: string]: unknown;
|
|
76
|
+
}>({ func, memo, callbackSave, snapshot }: {
|
|
77
|
+
func: () => T;
|
|
78
|
+
callbackSave: (data: getTypeCallback<T>, memo: T3) => T2;
|
|
79
|
+
memo: T3;
|
|
80
|
+
snapshot?: (memo: T3) => T3;
|
|
81
|
+
}): {
|
|
82
|
+
run: (a: tr222<[data: getTypeCallback<T>, memo: T3]>) => () => void;
|
|
83
|
+
snapshot: () => T3 | undefined;
|
|
84
|
+
memo: T3;
|
|
85
|
+
listenA: {
|
|
86
|
+
lastUpdate: () => Date | null;
|
|
87
|
+
lastUpdateMs: () => number | null;
|
|
88
|
+
isRun: () => boolean;
|
|
89
|
+
run: () => (() => void) | null;
|
|
90
|
+
close: () => void;
|
|
91
|
+
addListen: (a: tr222<[data: getTypeCallback<T>, memo: T3]>) => () => void;
|
|
92
|
+
removeListen: (a: tr222<[data: getTypeCallback<T>, memo: T3]> | null) => void;
|
|
93
|
+
count: () => number;
|
|
94
|
+
};
|
|
95
|
+
connect: () => void;
|
|
96
|
+
readonly disconnect: ((a: Omit<Parameters<T>[0], "callback"> & {
|
|
97
|
+
callback: (...data: tr22<T2>) => any;
|
|
98
|
+
}, ...b: ParametersOther<T>) => ReturnType<T>) | null;
|
|
99
|
+
};
|
|
62
100
|
export {};
|
package/lib/Common/Listen.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PromiseArrayListen = exports.UseListen = exports.funcListenBySocket1 = exports.funcListenBySocket = exports.funcListenBySocket2 = exports.funcListenBySocket3 = exports.funcListenCallback = void 0;
|
|
3
|
+
exports.socketBufferBySnapshot = exports.socketBuffer3 = exports.PromiseArrayListen = exports.UseListen = exports.funcListenBySocket1 = exports.funcListenBySocket = exports.funcListenBySocket2 = exports.funcListenBySocket3 = exports.funcListenCallback = void 0;
|
|
4
4
|
function funcListenCallback(a, event) {
|
|
5
5
|
const obj = new Map;
|
|
6
6
|
let close = null;
|
|
@@ -18,6 +18,7 @@ function funcListenCallback(a, event) {
|
|
|
18
18
|
addListen: (a) => {
|
|
19
19
|
obj.set(a, a);
|
|
20
20
|
event?.("add", obj.size, api);
|
|
21
|
+
return () => api.removeListen(a);
|
|
21
22
|
},
|
|
22
23
|
removeListen: (a) => {
|
|
23
24
|
obj.delete(a);
|
|
@@ -151,3 +152,35 @@ function PromiseArrayListen(array) {
|
|
|
151
152
|
};
|
|
152
153
|
}
|
|
153
154
|
exports.PromiseArrayListen = PromiseArrayListen;
|
|
155
|
+
function socketBuffer3(func, callbackMain, memo = {}) {
|
|
156
|
+
return (a, ...b) => func({ ...a, callback: (v) => { const z = callbackMain(v, memo); if (z)
|
|
157
|
+
a.callback(...z); }
|
|
158
|
+
}, ...b);
|
|
159
|
+
}
|
|
160
|
+
exports.socketBuffer3 = socketBuffer3;
|
|
161
|
+
function socketBufferBySnapshot({ func, memo = {}, callbackSave, snapshot }) {
|
|
162
|
+
let d = null;
|
|
163
|
+
const [callback, listenA] = UseListen({
|
|
164
|
+
event: (type, count, api) => {
|
|
165
|
+
if (type == "remove" && count == 0) {
|
|
166
|
+
api.close();
|
|
167
|
+
d?.();
|
|
168
|
+
}
|
|
169
|
+
if (type == "add" && count == 1)
|
|
170
|
+
api.run();
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
const connect = () => {
|
|
174
|
+
if (d == null)
|
|
175
|
+
d = socketBuffer3(func(), callbackSave, memo)({ callback });
|
|
176
|
+
};
|
|
177
|
+
const run = (...params) => {
|
|
178
|
+
if (!listenA.isRun()) {
|
|
179
|
+
snapshot?.(memo);
|
|
180
|
+
connect();
|
|
181
|
+
}
|
|
182
|
+
return listenA.addListen(...params);
|
|
183
|
+
};
|
|
184
|
+
return { run, snapshot: () => snapshot?.(memo), memo, listenA, connect, get disconnect() { return d; } };
|
|
185
|
+
}
|
|
186
|
+
exports.socketBufferBySnapshot = socketBufferBySnapshot;
|
package/lib/test.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/test.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const wenay_common_1 = require("wenay-common");
|
|
4
|
+
const Listen_1 = require("./Common/Listen");
|
|
5
|
+
function testSocketBufferBySnapshot() {
|
|
6
|
+
function snapshot() {
|
|
7
|
+
return {
|
|
8
|
+
a: 3,
|
|
9
|
+
b: 4,
|
|
10
|
+
c: 10
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function tt(a, key, time) {
|
|
14
|
+
const [startTime, endTime] = [Date.now(), Date.now() + time];
|
|
15
|
+
return ({ callback }) => {
|
|
16
|
+
let t = true;
|
|
17
|
+
async function re() {
|
|
18
|
+
const step = Math.round((Math.random()) * 500);
|
|
19
|
+
await (0, wenay_common_1.sleepAsync)(step);
|
|
20
|
+
a += Math.round((Math.random() - 0.5) * 100);
|
|
21
|
+
callback({ [key]: a });
|
|
22
|
+
if (Date.now() < endTime && t)
|
|
23
|
+
return re();
|
|
24
|
+
}
|
|
25
|
+
re();
|
|
26
|
+
return () => {
|
|
27
|
+
console.log("tttt");
|
|
28
|
+
t = false;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
const memo = {};
|
|
33
|
+
const c1 = tt(5, "a", 10000);
|
|
34
|
+
const test = (0, Listen_1.socketBufferBySnapshot)({
|
|
35
|
+
func: () => c1,
|
|
36
|
+
memo,
|
|
37
|
+
callbackSave: (data, memo1) => {
|
|
38
|
+
Object.entries(data).forEach(([key, value]) => memo[key] = value);
|
|
39
|
+
return [data, memo1];
|
|
40
|
+
},
|
|
41
|
+
snapshot: (memo) => {
|
|
42
|
+
Object.entries(snapshot()).forEach(([key, value]) => memo[key] = value);
|
|
43
|
+
return memo;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
test.snapshot();
|
|
47
|
+
console.log(test.memo);
|
|
48
|
+
const t = test.run((a, m) => {
|
|
49
|
+
console.log(a, m);
|
|
50
|
+
});
|
|
51
|
+
const t1 = test.run((a, m) => {
|
|
52
|
+
console.log(a, m);
|
|
53
|
+
});
|
|
54
|
+
(0, wenay_common_1.sleepAsync)(2000)
|
|
55
|
+
.then(() => {
|
|
56
|
+
t();
|
|
57
|
+
console.log("stop t");
|
|
58
|
+
});
|
|
59
|
+
(0, wenay_common_1.sleepAsync)(3000)
|
|
60
|
+
.then(() => {
|
|
61
|
+
t1();
|
|
62
|
+
console.log("stop t");
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
testSocketBufferBySnapshot();
|