wenay-common2 1.0.48 → 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/ObserveAll2/index.d.ts +2 -0
- package/lib/Common/ObserveAll2/index.js +18 -0
- package/lib/Common/ObserveAll2/reactive2.d.ts +8 -7
- package/lib/Common/ObserveAll2/reactive2.js +5 -0
- package/lib/Common/ObserveAll2/store.d.ts +98 -0
- package/lib/Common/ObserveAll2/store.js +381 -0
- package/lib/Common/events/Listen.d.ts +1 -88
- package/lib/Common/events/Listen.js +15 -189
- 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 +9 -9
- package/lib/Common/events/SocketServerHook.d.ts +14 -14
- package/lib/Common/events/UseListenTransform.d.ts +4 -4
- package/lib/Common/rcp/listen-socket.js +29 -11
- package/lib/index.d.ts +2 -1
- package/lib/index.js +3 -2
- package/package.json +3 -2
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
export type Listener<T extends any[]> = (...r: T) => void;
|
|
2
|
+
export type NormalizeTuple<T> = T extends any[] ? T : [T];
|
|
3
|
+
type key = string | symbol;
|
|
4
|
+
type cbClose = () => void;
|
|
5
|
+
declare const LISTEN_ON_BRAND: unique symbol;
|
|
6
|
+
export type ListenOn<Z extends any[] = any[]> = ((cb: Listener<Z>, opts?: {
|
|
7
|
+
cbClose?: cbClose;
|
|
8
|
+
key?: key;
|
|
9
|
+
}) => (() => void)) & {
|
|
10
|
+
readonly [LISTEN_ON_BRAND]: Z;
|
|
11
|
+
};
|
|
12
|
+
export type ListenOnCurrent<Z extends any[] = any[]> = ((cb: Listener<Z>, opts?: {
|
|
13
|
+
cbClose?: cbClose;
|
|
14
|
+
key?: key;
|
|
15
|
+
current?: ListenCurrent<Z>;
|
|
16
|
+
}) => (() => void)) & {
|
|
17
|
+
readonly [LISTEN_ON_BRAND]: Z;
|
|
18
|
+
};
|
|
19
|
+
export type ListenCurrentProvider<Z extends any[]> = () => Z | undefined;
|
|
20
|
+
export type ListenCurrent<Z extends any[]> = boolean | ListenCurrentProvider<Z>;
|
|
21
|
+
export type ListenCoreOptions<T = any> = {
|
|
22
|
+
fast?: boolean;
|
|
23
|
+
onRemove?: (k: key) => void;
|
|
24
|
+
event?: (type: 'add' | 'remove', count: number, api: ListenCoreApi<T>) => void;
|
|
25
|
+
};
|
|
26
|
+
export type ListenOptions<T> = {
|
|
27
|
+
event?: (type: 'add' | 'remove', count: number, api: ListenApi<T>) => void;
|
|
28
|
+
fast?: boolean;
|
|
29
|
+
addListenClose?: ListenApi<any>;
|
|
30
|
+
};
|
|
31
|
+
export type ListenStoreOptions<T> = ListenOptions<T> & {
|
|
32
|
+
current: ListenCurrentProvider<NormalizeTuple<T>>;
|
|
33
|
+
};
|
|
34
|
+
export declare function getListenByOn(fn: any): any;
|
|
35
|
+
export declare function isListenOn(fn: any): boolean;
|
|
36
|
+
export declare function funcListenCore<T>(options?: ListenCoreOptions<T>): {
|
|
37
|
+
func: Listener<NormalizeTuple<T>>;
|
|
38
|
+
has: (k: key) => boolean;
|
|
39
|
+
on: ListenOn<NormalizeTuple<T>>;
|
|
40
|
+
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
41
|
+
key?: key;
|
|
42
|
+
}) => () => void;
|
|
43
|
+
removeListen: (k: Listener<NormalizeTuple<T>> | null | key) => void;
|
|
44
|
+
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
45
|
+
key?: key;
|
|
46
|
+
}) => () => void;
|
|
47
|
+
close: () => void;
|
|
48
|
+
count: () => number;
|
|
49
|
+
readonly getAllKeys: key[];
|
|
50
|
+
};
|
|
51
|
+
export type ListenCoreApi<T = any> = ReturnType<typeof funcListenCore<T>>;
|
|
52
|
+
export declare function funcListenCallbackBase<T>(b: (e: Listener<NormalizeTuple<T>>) => (void | (() => void)), options?: ListenOptions<T>): {
|
|
53
|
+
func: Listener<NormalizeTuple<T>>;
|
|
54
|
+
isRun: () => boolean;
|
|
55
|
+
run: () => void;
|
|
56
|
+
close: () => void;
|
|
57
|
+
eventClose: (cb: cbClose) => () => void;
|
|
58
|
+
onClose: (cb: cbClose) => () => void;
|
|
59
|
+
removeEventClose: (cb: cbClose) => void;
|
|
60
|
+
on: ListenOn<NormalizeTuple<T>>;
|
|
61
|
+
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
62
|
+
cbClose?: cbClose;
|
|
63
|
+
key?: key;
|
|
64
|
+
}) => () => void;
|
|
65
|
+
removeListen: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
66
|
+
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
67
|
+
key?: key;
|
|
68
|
+
}) => () => void;
|
|
69
|
+
count: () => number;
|
|
70
|
+
readonly getAllKeys: key[];
|
|
71
|
+
};
|
|
72
|
+
export type ListenApi<T = any> = ReturnType<typeof funcListenCallbackBase<T>>;
|
|
73
|
+
export declare function funcListenCallbackFast<T>(a: (e: (Listener<NormalizeTuple<T>> | null)) => (void | (() => void))): {
|
|
74
|
+
func: Listener<NormalizeTuple<T>>;
|
|
75
|
+
isRun: () => boolean;
|
|
76
|
+
run: () => void;
|
|
77
|
+
close: () => void;
|
|
78
|
+
eventClose: (cb: cbClose) => () => void;
|
|
79
|
+
onClose: (cb: cbClose) => () => void;
|
|
80
|
+
removeEventClose: (cb: cbClose) => void;
|
|
81
|
+
on: ListenOn<NormalizeTuple<T>>;
|
|
82
|
+
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
83
|
+
cbClose?: cbClose;
|
|
84
|
+
key?: key;
|
|
85
|
+
}) => () => void;
|
|
86
|
+
removeListen: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
87
|
+
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
88
|
+
key?: key;
|
|
89
|
+
}) => () => void;
|
|
90
|
+
count: () => number;
|
|
91
|
+
readonly getAllKeys: key[];
|
|
92
|
+
};
|
|
93
|
+
export declare const funcListenCallback: typeof funcListenCallbackBase;
|
|
94
|
+
export declare function UseListen<T>(data?: ListenOptions<T>): readonly [(...a: NormalizeTuple<T>) => void, {
|
|
95
|
+
func: Listener<NormalizeTuple<T>>;
|
|
96
|
+
isRun: () => boolean;
|
|
97
|
+
run: () => void;
|
|
98
|
+
close: () => void;
|
|
99
|
+
eventClose: (cb: cbClose) => () => void;
|
|
100
|
+
onClose: (cb: cbClose) => () => void;
|
|
101
|
+
removeEventClose: (cb: cbClose) => void;
|
|
102
|
+
on: ListenOn<NormalizeTuple<T>>;
|
|
103
|
+
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
104
|
+
cbClose?: cbClose;
|
|
105
|
+
key?: key;
|
|
106
|
+
}) => () => void;
|
|
107
|
+
removeListen: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
108
|
+
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
109
|
+
key?: key;
|
|
110
|
+
}) => () => void;
|
|
111
|
+
count: () => number;
|
|
112
|
+
readonly getAllKeys: key[];
|
|
113
|
+
}];
|
|
114
|
+
export declare function withStoreListen<T>(base: ListenApi<T>, currentProvider: ListenCurrentProvider<NormalizeTuple<T>>): {
|
|
115
|
+
on: ListenOnCurrent<NormalizeTuple<T>>;
|
|
116
|
+
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
117
|
+
cbClose?: cbClose;
|
|
118
|
+
key?: key;
|
|
119
|
+
current?: ListenCurrent<NormalizeTuple<T>>;
|
|
120
|
+
}) => () => void;
|
|
121
|
+
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
122
|
+
key?: key;
|
|
123
|
+
current?: ListenCurrent<NormalizeTuple<T>>;
|
|
124
|
+
}) => () => void;
|
|
125
|
+
getAllKeys: key[];
|
|
126
|
+
func: Listener<NormalizeTuple<T>>;
|
|
127
|
+
isRun: () => boolean;
|
|
128
|
+
run: () => void;
|
|
129
|
+
close: () => void;
|
|
130
|
+
eventClose: (cb: cbClose) => () => void;
|
|
131
|
+
onClose: (cb: cbClose) => () => void;
|
|
132
|
+
removeEventClose: (cb: cbClose) => void;
|
|
133
|
+
removeListen: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
134
|
+
count: () => number;
|
|
135
|
+
};
|
|
136
|
+
export type ListenStoreApi<T> = ReturnType<typeof withStoreListen<T>>;
|
|
137
|
+
export declare function funcListenCallbackStore<T>(b: (e: Listener<NormalizeTuple<T>>) => (void | (() => void)), options: ListenStoreOptions<T>): {
|
|
138
|
+
on: ListenOnCurrent<NormalizeTuple<T>>;
|
|
139
|
+
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
140
|
+
cbClose?: cbClose;
|
|
141
|
+
key?: key;
|
|
142
|
+
current?: ListenCurrent<NormalizeTuple<T>> | undefined;
|
|
143
|
+
}) => () => void;
|
|
144
|
+
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
145
|
+
key?: key;
|
|
146
|
+
current?: ListenCurrent<NormalizeTuple<T>> | undefined;
|
|
147
|
+
}) => () => void;
|
|
148
|
+
getAllKeys: key[];
|
|
149
|
+
func: Listener<NormalizeTuple<T>>;
|
|
150
|
+
isRun: () => boolean;
|
|
151
|
+
run: () => void;
|
|
152
|
+
close: () => void;
|
|
153
|
+
eventClose: (cb: cbClose) => () => void;
|
|
154
|
+
onClose: (cb: cbClose) => () => void;
|
|
155
|
+
removeEventClose: (cb: cbClose) => void;
|
|
156
|
+
removeListen: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
157
|
+
count: () => number;
|
|
158
|
+
};
|
|
159
|
+
export declare function UseListenStore<T>(data: ListenStoreOptions<T>): readonly [(...a: NormalizeTuple<T>) => void, {
|
|
160
|
+
on: ListenOnCurrent<NormalizeTuple<T>>;
|
|
161
|
+
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
162
|
+
cbClose?: cbClose;
|
|
163
|
+
key?: key;
|
|
164
|
+
current?: ListenCurrent<NormalizeTuple<T>> | undefined;
|
|
165
|
+
}) => () => void;
|
|
166
|
+
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
167
|
+
key?: key;
|
|
168
|
+
current?: ListenCurrent<NormalizeTuple<T>> | undefined;
|
|
169
|
+
}) => () => void;
|
|
170
|
+
getAllKeys: key[];
|
|
171
|
+
func: Listener<NormalizeTuple<T>>;
|
|
172
|
+
isRun: () => boolean;
|
|
173
|
+
run: () => void;
|
|
174
|
+
close: () => void;
|
|
175
|
+
eventClose: (cb: cbClose) => () => void;
|
|
176
|
+
onClose: (cb: cbClose) => () => void;
|
|
177
|
+
removeEventClose: (cb: cbClose) => void;
|
|
178
|
+
removeListen: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
179
|
+
count: () => number;
|
|
180
|
+
}];
|
|
181
|
+
export declare function toListen2<T>(full: ListenApi<T>): {
|
|
182
|
+
on: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
183
|
+
key?: key;
|
|
184
|
+
}) => () => void;
|
|
185
|
+
close: () => void;
|
|
186
|
+
count: () => number;
|
|
187
|
+
};
|
|
188
|
+
export type Listen2<T> = ReturnType<typeof toListen2<T>>;
|
|
189
|
+
export declare function UseListen2<T>(data?: ListenOptions<T>): readonly [(...a: NormalizeTuple<T>) => void, {
|
|
190
|
+
on: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
191
|
+
key?: key;
|
|
192
|
+
} | undefined) => () => void;
|
|
193
|
+
close: () => void;
|
|
194
|
+
count: () => number;
|
|
195
|
+
}];
|
|
196
|
+
export declare function isListenCallback(obj: any): obj is ListenApi;
|
|
197
|
+
export {};
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.funcListenCallback = void 0;
|
|
4
|
+
exports.getListenByOn = getListenByOn;
|
|
5
|
+
exports.isListenOn = isListenOn;
|
|
6
|
+
exports.funcListenCore = funcListenCore;
|
|
7
|
+
exports.funcListenCallbackBase = funcListenCallbackBase;
|
|
8
|
+
exports.funcListenCallbackFast = funcListenCallbackFast;
|
|
9
|
+
exports.UseListen = UseListen;
|
|
10
|
+
exports.withStoreListen = withStoreListen;
|
|
11
|
+
exports.funcListenCallbackStore = funcListenCallbackStore;
|
|
12
|
+
exports.UseListenStore = UseListenStore;
|
|
13
|
+
exports.toListen2 = toListen2;
|
|
14
|
+
exports.UseListen2 = UseListen2;
|
|
15
|
+
exports.isListenCallback = isListenCallback;
|
|
16
|
+
const listenByOn = new WeakMap();
|
|
17
|
+
function getListenByOn(fn) { return typeof fn == 'function' ? listenByOn.get(fn) : undefined; }
|
|
18
|
+
function isListenOn(fn) { return typeof fn == 'function' && listenByOn.has(fn); }
|
|
19
|
+
function funcListenCore(options = {}) {
|
|
20
|
+
const { fast = true, onRemove, event } = options;
|
|
21
|
+
const subs = new Map();
|
|
22
|
+
let a = (...e) => { subs.forEach(z => z(...e)); };
|
|
23
|
+
let cached = null;
|
|
24
|
+
const getArr = () => cached ?? (cached = Array.from(subs.values()));
|
|
25
|
+
function rebuild() {
|
|
26
|
+
cached = null;
|
|
27
|
+
const size = subs.size;
|
|
28
|
+
if (size == 0) {
|
|
29
|
+
a = null;
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (size == 1) {
|
|
33
|
+
a = subs.values().next().value;
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (size == 2) {
|
|
37
|
+
const [a0, a1] = getArr();
|
|
38
|
+
a = ((...e) => { a0(...e); a1(...e); });
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
a = ((...e) => {
|
|
42
|
+
const ar = getArr();
|
|
43
|
+
for (let i = 0, len = ar.length; i < len; i++)
|
|
44
|
+
ar[i](...e);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function removeOne(k) {
|
|
48
|
+
if (!subs.has(k))
|
|
49
|
+
return;
|
|
50
|
+
subs.delete(k);
|
|
51
|
+
onRemove?.(k);
|
|
52
|
+
if (fast)
|
|
53
|
+
rebuild();
|
|
54
|
+
event?.('remove', subs.size, api);
|
|
55
|
+
}
|
|
56
|
+
const api = {
|
|
57
|
+
func: ((...e) => { a?.(...e); }),
|
|
58
|
+
has: (k) => subs.has(k),
|
|
59
|
+
on: ((cb, { key } = {}) => {
|
|
60
|
+
const k = key ?? Symbol();
|
|
61
|
+
if (subs.has(k)) {
|
|
62
|
+
subs.delete(k);
|
|
63
|
+
onRemove?.(k);
|
|
64
|
+
}
|
|
65
|
+
subs.set(k, cb);
|
|
66
|
+
if (fast)
|
|
67
|
+
rebuild();
|
|
68
|
+
event?.('add', subs.size, api);
|
|
69
|
+
return function off() { removeOne(k); };
|
|
70
|
+
}),
|
|
71
|
+
addListen: (cb, opts = {}) => api.on(cb, opts),
|
|
72
|
+
removeListen: (k) => {
|
|
73
|
+
if (typeof k == 'function') {
|
|
74
|
+
for (const [kk, cb] of [...subs])
|
|
75
|
+
if (cb === k)
|
|
76
|
+
removeOne(kk);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (k != null)
|
|
80
|
+
removeOne(k);
|
|
81
|
+
},
|
|
82
|
+
once: (cb, opts = {}) => {
|
|
83
|
+
let off = () => { };
|
|
84
|
+
off = api.on(((...e) => { off(); cb(...e); }), opts);
|
|
85
|
+
return off;
|
|
86
|
+
},
|
|
87
|
+
close: () => {
|
|
88
|
+
subs.clear();
|
|
89
|
+
if (fast)
|
|
90
|
+
rebuild();
|
|
91
|
+
},
|
|
92
|
+
count: () => subs.size,
|
|
93
|
+
get getAllKeys() { return [...subs.keys()]; },
|
|
94
|
+
};
|
|
95
|
+
listenByOn.set(api.on, api);
|
|
96
|
+
return api;
|
|
97
|
+
}
|
|
98
|
+
function funcListenCallbackBase(b, options = {}) {
|
|
99
|
+
const { fast = true, event, addListenClose } = options;
|
|
100
|
+
let closeHooks = null;
|
|
101
|
+
let close = null;
|
|
102
|
+
let closeUnsubscribe = null;
|
|
103
|
+
function forgetKey(k) {
|
|
104
|
+
closeHooks?.delete(k);
|
|
105
|
+
}
|
|
106
|
+
const core = funcListenCore({
|
|
107
|
+
fast,
|
|
108
|
+
onRemove: forgetKey,
|
|
109
|
+
event: event && function forwardRemove(type, count) {
|
|
110
|
+
if (type == 'remove')
|
|
111
|
+
event(type, count, api);
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
const api = {
|
|
115
|
+
func: core.func,
|
|
116
|
+
isRun: () => close !== null,
|
|
117
|
+
run: () => {
|
|
118
|
+
close = (b(core.func) ?? (() => { }));
|
|
119
|
+
if (addListenClose && !closeUnsubscribe) {
|
|
120
|
+
closeUnsubscribe = addListenClose.on(function onOwnerClose() { api.close(); });
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
close: () => {
|
|
124
|
+
close?.();
|
|
125
|
+
close = null;
|
|
126
|
+
core.close();
|
|
127
|
+
if (closeHooks) {
|
|
128
|
+
const hooks = closeHooks;
|
|
129
|
+
closeHooks = null;
|
|
130
|
+
hooks.forEach(function fireClose(cb) { cb(); });
|
|
131
|
+
}
|
|
132
|
+
if (closeUnsubscribe) {
|
|
133
|
+
closeUnsubscribe();
|
|
134
|
+
closeUnsubscribe = null;
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
eventClose: (cb) => {
|
|
138
|
+
closeHooks = closeHooks ?? new Map();
|
|
139
|
+
closeHooks.set(cb, cb);
|
|
140
|
+
return function offClose() { closeHooks?.delete(cb); };
|
|
141
|
+
},
|
|
142
|
+
onClose: (cb) => api.eventClose(cb),
|
|
143
|
+
removeEventClose: (cb) => { closeHooks?.delete(cb); },
|
|
144
|
+
on: ((cb, { cbClose, key } = {}) => {
|
|
145
|
+
const k = key ?? Symbol();
|
|
146
|
+
const off = core.on(cb, { key: k });
|
|
147
|
+
if (cbClose) {
|
|
148
|
+
closeHooks = closeHooks ?? new Map();
|
|
149
|
+
closeHooks.set(k, cbClose);
|
|
150
|
+
}
|
|
151
|
+
event?.('add', core.count(), api);
|
|
152
|
+
return off;
|
|
153
|
+
}),
|
|
154
|
+
addListen: (cb, opts = {}) => api.on(cb, opts),
|
|
155
|
+
removeListen: core.removeListen,
|
|
156
|
+
once: (cb, opts = {}) => {
|
|
157
|
+
let off = () => { };
|
|
158
|
+
off = api.on(((...e) => { off(); cb(...e); }), opts);
|
|
159
|
+
return off;
|
|
160
|
+
},
|
|
161
|
+
count: () => core.count(),
|
|
162
|
+
get getAllKeys() { return core.getAllKeys; },
|
|
163
|
+
};
|
|
164
|
+
listenByOn.set(api.on, api);
|
|
165
|
+
return api;
|
|
166
|
+
}
|
|
167
|
+
function funcListenCallbackFast(a) {
|
|
168
|
+
return funcListenCallbackBase(a, { fast: true });
|
|
169
|
+
}
|
|
170
|
+
exports.funcListenCallback = funcListenCallbackBase;
|
|
171
|
+
function UseListen(data = { fast: true }) {
|
|
172
|
+
let t;
|
|
173
|
+
const a = funcListenCallbackBase((e) => { t = e; }, { fast: true, ...data });
|
|
174
|
+
a.run();
|
|
175
|
+
t = a.func;
|
|
176
|
+
return [t, a];
|
|
177
|
+
}
|
|
178
|
+
function withStoreListen(base, currentProvider) {
|
|
179
|
+
function currentValue(current) {
|
|
180
|
+
if (typeof current == 'function')
|
|
181
|
+
return current();
|
|
182
|
+
return current ? currentProvider() : undefined;
|
|
183
|
+
}
|
|
184
|
+
const api = {
|
|
185
|
+
...base,
|
|
186
|
+
on: ((cb, { cbClose, key, current } = {}) => {
|
|
187
|
+
const off = base.on(cb, { cbClose, key });
|
|
188
|
+
if (current) {
|
|
189
|
+
const m = currentValue(current);
|
|
190
|
+
if (m)
|
|
191
|
+
cb(...m);
|
|
192
|
+
}
|
|
193
|
+
return off;
|
|
194
|
+
}),
|
|
195
|
+
addListen: (cb, opts = {}) => api.on(cb, opts),
|
|
196
|
+
once: (cb, opts = {}) => {
|
|
197
|
+
if (opts.current) {
|
|
198
|
+
const m = currentValue(opts.current);
|
|
199
|
+
if (m) {
|
|
200
|
+
cb(...m);
|
|
201
|
+
return () => { };
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
let off = () => { };
|
|
205
|
+
off = base.on(((...e) => { off(); cb(...e); }), { key: opts.key });
|
|
206
|
+
return off;
|
|
207
|
+
},
|
|
208
|
+
get getAllKeys() { return base.getAllKeys; },
|
|
209
|
+
};
|
|
210
|
+
listenByOn.set(api.on, api);
|
|
211
|
+
return api;
|
|
212
|
+
}
|
|
213
|
+
function funcListenCallbackStore(b, options) {
|
|
214
|
+
const { current, ...listenOptions } = options;
|
|
215
|
+
return withStoreListen(funcListenCallbackBase(b, listenOptions), current);
|
|
216
|
+
}
|
|
217
|
+
function UseListenStore(data) {
|
|
218
|
+
const { current, ...listenOptions } = data;
|
|
219
|
+
let t;
|
|
220
|
+
const base = funcListenCallbackBase((e) => { t = e; }, { fast: true, ...listenOptions });
|
|
221
|
+
const listen = withStoreListen(base, current);
|
|
222
|
+
base.run();
|
|
223
|
+
t = base.func;
|
|
224
|
+
return [t, listen];
|
|
225
|
+
}
|
|
226
|
+
function toListen2(full) {
|
|
227
|
+
return {
|
|
228
|
+
on: (cb, opts) => full.on(cb, opts),
|
|
229
|
+
close: () => full.close(),
|
|
230
|
+
count: () => full.count(),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
function UseListen2(data = { fast: true }) {
|
|
234
|
+
const [emit, full] = UseListen(data);
|
|
235
|
+
return [emit, toListen2(full)];
|
|
236
|
+
}
|
|
237
|
+
const LISTEN_CORE = ['func', 'addListen', 'removeListen', 'eventClose', 'removeEventClose'];
|
|
238
|
+
function isListenCallback(obj) {
|
|
239
|
+
if (obj == null || typeof obj != 'object')
|
|
240
|
+
return false;
|
|
241
|
+
const ks = new Set(Object.keys(obj));
|
|
242
|
+
for (const k of LISTEN_CORE)
|
|
243
|
+
if (!ks.has(k))
|
|
244
|
+
return false;
|
|
245
|
+
for (const k of LISTEN_CORE)
|
|
246
|
+
if (typeof obj[k] != 'function')
|
|
247
|
+
return false;
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
@@ -18,31 +18,31 @@ export declare function funcListenCallbackSnapshot<T extends realSocket2<any | a
|
|
|
18
18
|
memo: T4;
|
|
19
19
|
snapshot?: (memo: T4) => T3;
|
|
20
20
|
}): {
|
|
21
|
-
run: (cb: import("./
|
|
22
|
-
cbClose?: (
|
|
21
|
+
run: (cb: import("./Listen3").Listener<[data: getTypeCallback<T>, memo: T3]>, opts?: {
|
|
22
|
+
cbClose?: () => void;
|
|
23
23
|
key?: string | symbol;
|
|
24
24
|
} | undefined) => () => void;
|
|
25
25
|
snapshot: () => T3 | undefined;
|
|
26
26
|
memo: T4;
|
|
27
27
|
listenA: {
|
|
28
|
-
func: import("./
|
|
28
|
+
func: import("./Listen3").Listener<[data: getTypeCallback<T>, memo: T3]>;
|
|
29
29
|
isRun: () => boolean;
|
|
30
30
|
run: () => void;
|
|
31
31
|
close: () => void;
|
|
32
32
|
eventClose: (cb: () => void) => () => void;
|
|
33
33
|
onClose: (cb: () => void) => () => void;
|
|
34
34
|
removeEventClose: (cb: () => void) => void;
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
on: import("./Listen3").ListenOn<[data: getTypeCallback<T>, memo: T3]>;
|
|
36
|
+
addListen: (cb: import("./Listen3").Listener<[data: getTypeCallback<T>, memo: T3]>, opts?: {
|
|
37
|
+
cbClose?: () => void;
|
|
37
38
|
key?: string | symbol;
|
|
38
39
|
}) => () => void;
|
|
39
|
-
removeListen: (k: (string | symbol) | import("./
|
|
40
|
-
|
|
41
|
-
once: (cb: import("./Listen").Listener<[data: getTypeCallback<T>, memo: T3]>, opts?: {
|
|
40
|
+
removeListen: (k: (string | symbol) | import("./Listen3").Listener<[data: getTypeCallback<T>, memo: T3]> | null) => void;
|
|
41
|
+
once: (cb: import("./Listen3").Listener<[data: getTypeCallback<T>, memo: T3]>, opts?: {
|
|
42
42
|
key?: string | symbol;
|
|
43
43
|
}) => () => void;
|
|
44
44
|
count: () => number;
|
|
45
|
-
readonly getAllKeys: (
|
|
45
|
+
readonly getAllKeys: (string | symbol)[];
|
|
46
46
|
};
|
|
47
47
|
connect: () => void;
|
|
48
48
|
readonly disconnect: ((a: Omit<Parameters<T>[0], "callback"> & {
|
|
@@ -5,45 +5,45 @@ export declare function SocketServerHook(opt?: {
|
|
|
5
5
|
}): {
|
|
6
6
|
obj: {
|
|
7
7
|
[k: string]: readonly [(a_0: unknown) => void, {
|
|
8
|
-
func: import("./
|
|
8
|
+
func: import("./Listen3").Listener<[unknown]>;
|
|
9
9
|
isRun: () => boolean;
|
|
10
10
|
run: () => void;
|
|
11
11
|
close: () => void;
|
|
12
12
|
eventClose: (cb: () => void) => () => void;
|
|
13
13
|
onClose: (cb: () => void) => () => void;
|
|
14
14
|
removeEventClose: (cb: () => void) => void;
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
on: import("./Listen3").ListenOn<[unknown]>;
|
|
16
|
+
addListen: (cb: import("./Listen3").Listener<[unknown]>, opts?: {
|
|
17
|
+
cbClose?: () => void;
|
|
17
18
|
key?: string | symbol;
|
|
18
19
|
}) => () => void;
|
|
19
|
-
removeListen: (k: (string | symbol) | import("./
|
|
20
|
-
|
|
21
|
-
once: (cb: import("./Listen").Listener<[unknown]>, opts?: {
|
|
20
|
+
removeListen: (k: (string | symbol) | import("./Listen3").Listener<[unknown]> | null) => void;
|
|
21
|
+
once: (cb: import("./Listen3").Listener<[unknown]>, opts?: {
|
|
22
22
|
key?: string | symbol;
|
|
23
23
|
}) => () => void;
|
|
24
24
|
count: () => number;
|
|
25
|
-
readonly getAllKeys: (
|
|
25
|
+
readonly getAllKeys: (string | symbol)[];
|
|
26
26
|
}];
|
|
27
27
|
};
|
|
28
28
|
get(tag: string): readonly [(a_0: unknown) => void, {
|
|
29
|
-
func: import("./
|
|
29
|
+
func: import("./Listen3").Listener<[unknown]>;
|
|
30
30
|
isRun: () => boolean;
|
|
31
31
|
run: () => void;
|
|
32
32
|
close: () => void;
|
|
33
33
|
eventClose: (cb: () => void) => () => void;
|
|
34
34
|
onClose: (cb: () => void) => () => void;
|
|
35
35
|
removeEventClose: (cb: () => void) => void;
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
on: import("./Listen3").ListenOn<[unknown]>;
|
|
37
|
+
addListen: (cb: import("./Listen3").Listener<[unknown]>, opts?: {
|
|
38
|
+
cbClose?: () => void;
|
|
38
39
|
key?: string | symbol;
|
|
39
40
|
}) => () => void;
|
|
40
|
-
removeListen: (k: (string | symbol) | import("./
|
|
41
|
-
|
|
42
|
-
once: (cb: import("./Listen").Listener<[unknown]>, opts?: {
|
|
41
|
+
removeListen: (k: (string | symbol) | import("./Listen3").Listener<[unknown]> | null) => void;
|
|
42
|
+
once: (cb: import("./Listen3").Listener<[unknown]>, opts?: {
|
|
43
43
|
key?: string | symbol;
|
|
44
44
|
}) => () => void;
|
|
45
45
|
count: () => number;
|
|
46
|
-
readonly getAllKeys: (
|
|
46
|
+
readonly getAllKeys: (string | symbol)[];
|
|
47
47
|
}];
|
|
48
48
|
provider: (tag: string, data: any) => void;
|
|
49
49
|
};
|
|
@@ -9,15 +9,15 @@ export declare function UseListenTransform<TSource extends any[], TTarget extend
|
|
|
9
9
|
eventClose: (cb: () => void) => () => void;
|
|
10
10
|
onClose: (cb: () => void) => () => void;
|
|
11
11
|
removeEventClose: (cb: () => void) => void;
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
on: import("./Listen3").ListenOn<NormalizeTuple<TTarget>>;
|
|
13
|
+
addListen: (cb: Listener<NormalizeTuple<TTarget>>, opts?: {
|
|
14
|
+
cbClose?: () => void;
|
|
14
15
|
key?: string | symbol;
|
|
15
16
|
}) => () => void;
|
|
16
17
|
removeListen: (k: (string | symbol) | Listener<NormalizeTuple<TTarget>> | null) => void;
|
|
17
|
-
on: import("./Listen").ListenOn<NormalizeTuple<TTarget>>;
|
|
18
18
|
once: (cb: Listener<NormalizeTuple<TTarget>>, opts?: {
|
|
19
19
|
key?: string | symbol;
|
|
20
20
|
}) => () => void;
|
|
21
21
|
count: () => number;
|
|
22
|
-
readonly getAllKeys: (
|
|
22
|
+
readonly getAllKeys: (string | symbol)[];
|
|
23
23
|
}];
|
|
@@ -44,9 +44,12 @@ function createThrottleLatest(ms, sink) {
|
|
|
44
44
|
}
|
|
45
45
|
function listenSocket(e, d) {
|
|
46
46
|
const { stop, addListenClose, status, paramsModify, throttle } = d ?? {};
|
|
47
|
-
const
|
|
47
|
+
const subscribe = (cb, opts) => typeof e.on == "function" ? e.on(cb, opts) : e.addListen(cb, opts);
|
|
48
|
+
const subscribeClose = addListenClose && ((cb) => typeof addListenClose.on == "function" ? addListenClose.on(cb) : addListenClose.addListen(cb));
|
|
48
49
|
let last = null;
|
|
49
50
|
let active = null;
|
|
51
|
+
let activeOff = null;
|
|
52
|
+
let closeSignalOff = null;
|
|
50
53
|
let resolveWait = null;
|
|
51
54
|
let throttleCh = null;
|
|
52
55
|
function finish() {
|
|
@@ -64,11 +67,15 @@ function listenSocket(e, d) {
|
|
|
64
67
|
stop?.(last);
|
|
65
68
|
last = null;
|
|
66
69
|
}
|
|
67
|
-
if (
|
|
68
|
-
|
|
70
|
+
if (activeOff) {
|
|
71
|
+
activeOff();
|
|
72
|
+
activeOff = null;
|
|
69
73
|
active = null;
|
|
70
74
|
}
|
|
71
|
-
|
|
75
|
+
if (closeSignalOff) {
|
|
76
|
+
closeSignalOff();
|
|
77
|
+
closeSignalOff = null;
|
|
78
|
+
}
|
|
72
79
|
finish();
|
|
73
80
|
return true;
|
|
74
81
|
}
|
|
@@ -78,8 +85,15 @@ function listenSocket(e, d) {
|
|
|
78
85
|
}
|
|
79
86
|
if (last)
|
|
80
87
|
stop?.(last);
|
|
81
|
-
if (
|
|
82
|
-
|
|
88
|
+
if (activeOff) {
|
|
89
|
+
activeOff();
|
|
90
|
+
activeOff = null;
|
|
91
|
+
active = null;
|
|
92
|
+
}
|
|
93
|
+
if (closeSignalOff) {
|
|
94
|
+
closeSignalOff();
|
|
95
|
+
closeSignalOff = null;
|
|
96
|
+
}
|
|
83
97
|
if (resolveWait) {
|
|
84
98
|
resolveWait();
|
|
85
99
|
resolveWait = null;
|
|
@@ -118,18 +132,22 @@ function listenSocket(e, d) {
|
|
|
118
132
|
stop?.(last);
|
|
119
133
|
}
|
|
120
134
|
last = null;
|
|
121
|
-
if (
|
|
122
|
-
|
|
135
|
+
if (activeOff) {
|
|
136
|
+
activeOff();
|
|
137
|
+
activeOff = null;
|
|
123
138
|
active = null;
|
|
124
139
|
}
|
|
125
|
-
|
|
140
|
+
if (closeSignalOff) {
|
|
141
|
+
closeSignalOff();
|
|
142
|
+
closeSignalOff = null;
|
|
143
|
+
}
|
|
126
144
|
finish();
|
|
127
145
|
return;
|
|
128
146
|
}
|
|
129
147
|
inner(...a);
|
|
130
148
|
};
|
|
131
|
-
|
|
132
|
-
|
|
149
|
+
activeOff = subscribe(active, { cbClose: removeCallback });
|
|
150
|
+
closeSignalOff = subscribeClose?.(removeCallback) ?? null;
|
|
133
151
|
const wait = new Promise((resolve) => {
|
|
134
152
|
resolveWait = () => { resolve(); };
|
|
135
153
|
});
|
package/lib/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export * from "./Common/data/List";
|
|
|
12
12
|
export * from "./Common/data/ListNodeAnd";
|
|
13
13
|
export * from "./Common/data/objectPath";
|
|
14
14
|
export * from "./Common/events/Listen";
|
|
15
|
+
export * as ListenNext from "./Common/events/Listen2";
|
|
15
16
|
export * from "./Common/events/event";
|
|
16
17
|
export * from "./Common/events/SocketBuffer";
|
|
17
18
|
export * from "./Common/events/SocketServerHook";
|
|
@@ -31,4 +32,4 @@ export * as Bars from "./Exchange/Bars";
|
|
|
31
32
|
export * as Params from "./Exchange/CParams";
|
|
32
33
|
export * as Time from "./Common/Time";
|
|
33
34
|
export * as Color from "./Common/Color";
|
|
34
|
-
export * as ObserveAll2 from "./Common/ObserveAll2
|
|
35
|
+
export * as ObserveAll2 from "./Common/ObserveAll2";
|