wenay-common2 1.0.61 → 1.0.63
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 +8 -8
- package/lib/Common/Observe/index.d.ts +5 -0
- package/lib/Common/{ObserveAll2 → Observe}/index.js +3 -1
- package/lib/Common/Observe/reactive.d.ts +21 -0
- package/lib/Common/{ObserveAll2/reactive2.js → Observe/reactive.js} +7 -7
- package/lib/Common/Observe/reactive2.d.ts +1 -0
- package/lib/Common/{events/Listen2.js → Observe/reactive2.js} +1 -1
- package/lib/Common/Observe/store-manager.d.ts +123 -0
- package/lib/Common/Observe/store-manager.js +227 -0
- package/lib/Common/Observe/store-offline.d.ts +88 -0
- package/lib/Common/Observe/store-offline.js +278 -0
- package/lib/Common/{ObserveAll2 → Observe}/store-replay.d.ts +9 -38
- package/lib/Common/{ObserveAll2 → Observe}/store-replay.js +1 -1
- package/lib/Common/{ObserveAll2 → Observe}/store.d.ts +3 -3
- package/lib/Common/{ObserveAll2 → Observe}/store.js +25 -31
- package/lib/Common/async/promiseProgress.d.ts +12 -0
- package/lib/Common/async/promiseProgress.js +29 -0
- package/lib/Common/events/Listen.d.ts +129 -1
- package/lib/Common/events/Listen.js +243 -15
- package/lib/Common/events/Listen3.d.ts +1 -210
- package/lib/Common/events/Listen3.js +15 -253
- package/lib/Common/events/SocketBuffer.d.ts +13 -33
- package/lib/Common/events/SocketBuffer.js +8 -8
- package/lib/Common/events/SocketServerHook.d.ts +5 -45
- package/lib/Common/events/SocketServerHook.js +1 -1
- package/lib/Common/events/joinListens.d.ts +6 -7
- package/lib/Common/events/joinListens.js +3 -4
- package/lib/Common/events/mapListen.d.ts +4 -0
- package/lib/Common/events/mapListen.js +25 -0
- package/lib/Common/events/replay-conflate.d.ts +2 -22
- package/lib/Common/events/replay-conflate.js +5 -5
- package/lib/Common/events/replay-history.d.ts +1 -1
- package/lib/Common/events/replay-listen.d.ts +20 -66
- package/lib/Common/events/replay-listen.js +10 -13
- package/lib/Common/events/replay-wire.d.ts +1 -1
- package/lib/Common/rcp/createRpcServerAutoWithProtocolDetection.d.ts +3 -3
- package/lib/Common/rcp/createRpcServerAutoWithProtocolDetection.js +10 -10
- package/lib/Common/rcp/listen-deep.d.ts +10 -10
- package/lib/Common/rcp/listen-deep.js +1 -1
- package/lib/Common/rcp/listen-socket.d.ts +13 -13
- package/lib/Common/rcp/listen-socket.js +4 -3
- package/lib/Common/rcp/rpc-client-auto.d.ts +2 -2
- package/lib/Common/rcp/rpc-client-auto.js +3 -2
- package/lib/Common/rcp/rpc-client.js +6 -4
- package/lib/Common/rcp/rpc-index.d.ts +2 -1
- package/lib/Common/rcp/rpc-index.js +3 -2
- package/lib/Common/rcp/rpc-path.d.ts +1 -0
- package/lib/Common/rcp/rpc-path.js +6 -0
- package/lib/Common/rcp/rpc-server-auto.d.ts +2 -2
- package/lib/Common/rcp/rpc-server-auto.js +7 -11
- package/lib/Common/rcp/rpc-server.js +11 -7
- package/lib/client.d.ts +1 -1
- package/lib/client.js +1 -1
- package/lib/index.d.ts +3 -4
- package/lib/index.js +4 -5
- package/package.json +5 -3
- package/lib/Common/ObserveAll2/index.d.ts +0 -3
- package/lib/Common/ObserveAll2/reactive2.d.ts +0 -61
- package/lib/Common/async/PromiseArrayListen.d.ts +0 -14
- package/lib/Common/async/PromiseArrayListen.js +0 -37
- package/lib/Common/events/Listen2.d.ts +0 -1
- package/lib/Common/events/UseListenTransform.d.ts +0 -24
- package/lib/Common/events/UseListenTransform.js +0 -28
- package/lib/Common/rcp/test.d.ts +0 -1
- package/lib/Common/rcp/test.js +0 -79
|
@@ -1 +1,129 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type Listener<T extends any[]> = (...args: T) => void;
|
|
2
|
+
export type NormalizeTuple<T> = T extends any[] ? T : [T];
|
|
3
|
+
export type ListenKey = string | symbol;
|
|
4
|
+
export type ListenOff = () => void;
|
|
5
|
+
type CloseCallback = () => void;
|
|
6
|
+
declare const LISTEN_ON_BRAND: unique symbol;
|
|
7
|
+
export type ListenOn<Z extends any[] = any[]> = ((cb: Listener<Z>, opts?: {
|
|
8
|
+
cbClose?: CloseCallback;
|
|
9
|
+
key?: ListenKey;
|
|
10
|
+
}) => ListenOff) & {
|
|
11
|
+
readonly [LISTEN_ON_BRAND]: Z;
|
|
12
|
+
};
|
|
13
|
+
export type ListenOnCurrent<Z extends any[] = any[]> = ((cb: Listener<Z>, opts?: {
|
|
14
|
+
cbClose?: CloseCallback;
|
|
15
|
+
key?: ListenKey;
|
|
16
|
+
current?: ListenCurrent<Z>;
|
|
17
|
+
}) => ListenOff) & {
|
|
18
|
+
readonly [LISTEN_ON_BRAND]: Z;
|
|
19
|
+
};
|
|
20
|
+
export type ListenCurrentProvider<Z extends any[]> = () => Z | undefined;
|
|
21
|
+
export type ListenCurrent<Z extends any[]> = boolean | ListenCurrentProvider<Z>;
|
|
22
|
+
export type ListenCoreApi<T = any> = {
|
|
23
|
+
emit: Listener<NormalizeTuple<T>>;
|
|
24
|
+
has(key: ListenKey): boolean;
|
|
25
|
+
on: ListenOn<NormalizeTuple<T>>;
|
|
26
|
+
off(keyOrCallback: Listener<NormalizeTuple<T>> | null | ListenKey): void;
|
|
27
|
+
once(cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
28
|
+
key?: ListenKey;
|
|
29
|
+
}): ListenOff;
|
|
30
|
+
close(): void;
|
|
31
|
+
count(): number;
|
|
32
|
+
keys(): ListenKey[];
|
|
33
|
+
};
|
|
34
|
+
export type ListenApi<T = any> = ListenCoreApi<T> & {
|
|
35
|
+
isRunning(): boolean;
|
|
36
|
+
run(): void;
|
|
37
|
+
onClose(cb: CloseCallback): ListenOff;
|
|
38
|
+
};
|
|
39
|
+
export type ListenCoreOptions<T = any> = {
|
|
40
|
+
fast?: boolean;
|
|
41
|
+
onRemove?: (key: ListenKey) => void;
|
|
42
|
+
event?: (type: 'add' | 'remove', count: number, api: ListenCoreApi<T>) => void;
|
|
43
|
+
};
|
|
44
|
+
export type ListenOptions<T = any> = {
|
|
45
|
+
event?: (type: 'add' | 'remove', count: number, api: ListenApi<T>) => void;
|
|
46
|
+
fast?: boolean;
|
|
47
|
+
closeOn?: ListenApi<any>;
|
|
48
|
+
};
|
|
49
|
+
export type ListenStoreOptions<T> = ListenOptions<T> & {
|
|
50
|
+
current: ListenCurrentProvider<NormalizeTuple<T>>;
|
|
51
|
+
};
|
|
52
|
+
export type ListenOnBrand<Z extends any[] = any[]> = {
|
|
53
|
+
readonly [LISTEN_ON_BRAND]: Z;
|
|
54
|
+
};
|
|
55
|
+
export declare function getListenByOn(fn: any): any;
|
|
56
|
+
export declare function isListenOn(fn: any): boolean;
|
|
57
|
+
export declare function registerListenOn(on: Function, api: any): void;
|
|
58
|
+
export declare function createListenCore<T>(options?: ListenCoreOptions<T>): ListenCoreApi<T>;
|
|
59
|
+
export declare function createListen<T>(producer: (emit: Listener<NormalizeTuple<T>>) => (void | ListenOff), options?: ListenOptions<T>): ListenApi<T>;
|
|
60
|
+
export declare function createFastListen<T>(producer: (emit: Listener<NormalizeTuple<T>>) => (void | ListenOff)): ListenApi<T>;
|
|
61
|
+
export declare function listen<T>(options?: ListenOptions<T>): readonly [Listener<NormalizeTuple<T>>, ListenApi<T>];
|
|
62
|
+
export declare function withStoreListen<T>(base: ListenApi<T>, currentProvider: ListenCurrentProvider<NormalizeTuple<T>>): {
|
|
63
|
+
on: ListenOnCurrent<NormalizeTuple<T>>;
|
|
64
|
+
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
65
|
+
key?: ListenKey;
|
|
66
|
+
current?: ListenCurrent<NormalizeTuple<T>>;
|
|
67
|
+
}) => ListenOff;
|
|
68
|
+
emit: Listener<NormalizeTuple<T>>;
|
|
69
|
+
has(key: ListenKey): boolean;
|
|
70
|
+
off(keyOrCallback: ListenKey | Listener<NormalizeTuple<T>> | null): void;
|
|
71
|
+
close(): void;
|
|
72
|
+
count(): number;
|
|
73
|
+
keys(): ListenKey[];
|
|
74
|
+
isRunning(): boolean;
|
|
75
|
+
run(): void;
|
|
76
|
+
onClose(cb: CloseCallback): ListenOff;
|
|
77
|
+
};
|
|
78
|
+
export type ListenStoreApi<T> = ReturnType<typeof withStoreListen<T>>;
|
|
79
|
+
export declare function createStoreListen<T>(producer: (emit: Listener<NormalizeTuple<T>>) => (void | ListenOff), options: ListenStoreOptions<T>): {
|
|
80
|
+
on: ListenOnCurrent<NormalizeTuple<T>>;
|
|
81
|
+
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
82
|
+
key?: ListenKey;
|
|
83
|
+
current?: ListenCurrent<NormalizeTuple<T>> | undefined;
|
|
84
|
+
}) => ListenOff;
|
|
85
|
+
emit: Listener<NormalizeTuple<T>>;
|
|
86
|
+
has(key: ListenKey): boolean;
|
|
87
|
+
off(keyOrCallback: ListenKey | Listener<NormalizeTuple<T>> | null): void;
|
|
88
|
+
close(): void;
|
|
89
|
+
count(): number;
|
|
90
|
+
keys(): ListenKey[];
|
|
91
|
+
isRunning(): boolean;
|
|
92
|
+
run(): void;
|
|
93
|
+
onClose(cb: CloseCallback): ListenOff;
|
|
94
|
+
};
|
|
95
|
+
export declare function listenStore<T>(options: ListenStoreOptions<T>): readonly [Listener<NormalizeTuple<T>>, {
|
|
96
|
+
on: ListenOnCurrent<NormalizeTuple<T>>;
|
|
97
|
+
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
98
|
+
key?: ListenKey;
|
|
99
|
+
current?: ListenCurrent<NormalizeTuple<T>> | undefined;
|
|
100
|
+
}) => ListenOff;
|
|
101
|
+
emit: Listener<NormalizeTuple<T>>;
|
|
102
|
+
has(key: ListenKey): boolean;
|
|
103
|
+
off(keyOrCallback: ListenKey | Listener<NormalizeTuple<T>> | null): void;
|
|
104
|
+
close(): void;
|
|
105
|
+
count(): number;
|
|
106
|
+
keys(): ListenKey[];
|
|
107
|
+
isRunning(): boolean;
|
|
108
|
+
run(): void;
|
|
109
|
+
onClose(cb: CloseCallback): ListenOff;
|
|
110
|
+
}];
|
|
111
|
+
export declare function toSlimListen<T>(full: ListenApi<T>): {
|
|
112
|
+
on: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
113
|
+
key?: ListenKey;
|
|
114
|
+
}) => ListenOff;
|
|
115
|
+
off: (keyOrCallback: Listener<NormalizeTuple<T>> | null | ListenKey) => void;
|
|
116
|
+
close: () => void;
|
|
117
|
+
count: () => number;
|
|
118
|
+
};
|
|
119
|
+
export type SlimListen<T> = ReturnType<typeof toSlimListen<T>>;
|
|
120
|
+
export declare function slimListen<T>(options?: ListenOptions<T>): readonly [Listener<NormalizeTuple<T>>, {
|
|
121
|
+
on: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
122
|
+
key?: ListenKey;
|
|
123
|
+
} | undefined) => ListenOff;
|
|
124
|
+
off: (keyOrCallback: ListenKey | Listener<NormalizeTuple<T>> | null) => void;
|
|
125
|
+
close: () => void;
|
|
126
|
+
count: () => number;
|
|
127
|
+
}];
|
|
128
|
+
export declare function isListenCallback(obj: any): obj is ListenApi;
|
|
129
|
+
export {};
|
|
@@ -1,17 +1,245 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
3
|
+
exports.getListenByOn = getListenByOn;
|
|
4
|
+
exports.isListenOn = isListenOn;
|
|
5
|
+
exports.registerListenOn = registerListenOn;
|
|
6
|
+
exports.createListenCore = createListenCore;
|
|
7
|
+
exports.createListen = createListen;
|
|
8
|
+
exports.createFastListen = createFastListen;
|
|
9
|
+
exports.listen = listen;
|
|
10
|
+
exports.withStoreListen = withStoreListen;
|
|
11
|
+
exports.createStoreListen = createStoreListen;
|
|
12
|
+
exports.listenStore = listenStore;
|
|
13
|
+
exports.toSlimListen = toSlimListen;
|
|
14
|
+
exports.slimListen = slimListen;
|
|
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 registerListenOn(on, api) { listenByOn.set(on, api); }
|
|
20
|
+
function createListenCore(options = {}) {
|
|
21
|
+
const { fast = true, onRemove, event } = options;
|
|
22
|
+
const subs = new Map();
|
|
23
|
+
let dispatcher = (...args) => { subs.forEach(cb => cb(...args)); };
|
|
24
|
+
let cached = null;
|
|
25
|
+
const getArr = () => cached ?? (cached = Array.from(subs.values()));
|
|
26
|
+
function rebuild() {
|
|
27
|
+
cached = null;
|
|
28
|
+
const size = subs.size;
|
|
29
|
+
if (size == 0) {
|
|
30
|
+
dispatcher = null;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (size == 1) {
|
|
34
|
+
dispatcher = subs.values().next().value;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (size == 2) {
|
|
38
|
+
const [a, b] = getArr();
|
|
39
|
+
dispatcher = ((...args) => { a(...args); b(...args); });
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
dispatcher = ((...args) => {
|
|
43
|
+
const arr = getArr();
|
|
44
|
+
for (let i = 0; i < arr.length; i++)
|
|
45
|
+
arr[i](...args);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function removeOne(key) {
|
|
49
|
+
if (!subs.has(key))
|
|
50
|
+
return;
|
|
51
|
+
subs.delete(key);
|
|
52
|
+
onRemove?.(key);
|
|
53
|
+
if (fast)
|
|
54
|
+
rebuild();
|
|
55
|
+
event?.('remove', subs.size, api);
|
|
56
|
+
}
|
|
57
|
+
const api = {
|
|
58
|
+
emit: ((...args) => { dispatcher?.(...args); }),
|
|
59
|
+
has: (key) => subs.has(key),
|
|
60
|
+
on: ((cb, { key } = {}) => {
|
|
61
|
+
const k = key ?? Symbol();
|
|
62
|
+
if (subs.has(k)) {
|
|
63
|
+
subs.delete(k);
|
|
64
|
+
onRemove?.(k);
|
|
65
|
+
}
|
|
66
|
+
subs.set(k, cb);
|
|
67
|
+
if (fast)
|
|
68
|
+
rebuild();
|
|
69
|
+
event?.('add', subs.size, api);
|
|
70
|
+
return function off() { removeOne(k); };
|
|
71
|
+
}),
|
|
72
|
+
off: (keyOrCallback) => {
|
|
73
|
+
if (typeof keyOrCallback == 'function') {
|
|
74
|
+
for (const [key, cb] of [...subs])
|
|
75
|
+
if (cb === keyOrCallback)
|
|
76
|
+
removeOne(key);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (keyOrCallback != null)
|
|
80
|
+
removeOne(keyOrCallback);
|
|
81
|
+
},
|
|
82
|
+
once: (cb, opts = {}) => {
|
|
83
|
+
let off = () => { };
|
|
84
|
+
off = api.on(((...args) => { off(); cb(...args); }), opts);
|
|
85
|
+
return off;
|
|
86
|
+
},
|
|
87
|
+
close: () => {
|
|
88
|
+
subs.clear();
|
|
89
|
+
if (fast)
|
|
90
|
+
rebuild();
|
|
91
|
+
},
|
|
92
|
+
count: () => subs.size,
|
|
93
|
+
keys: () => [...subs.keys()],
|
|
94
|
+
};
|
|
95
|
+
listenByOn.set(api.on, api);
|
|
96
|
+
return api;
|
|
97
|
+
}
|
|
98
|
+
function createListen(producer, options = {}) {
|
|
99
|
+
const { fast = true, event, closeOn } = options;
|
|
100
|
+
let teardown = null;
|
|
101
|
+
let closeSignalOff = null;
|
|
102
|
+
let closeHooks = null;
|
|
103
|
+
function forgetKey(key) {
|
|
104
|
+
closeHooks?.delete(key);
|
|
105
|
+
}
|
|
106
|
+
const core = createListenCore({
|
|
107
|
+
fast,
|
|
108
|
+
onRemove: forgetKey,
|
|
109
|
+
event: event && ((type, count) => {
|
|
110
|
+
if (type == 'remove')
|
|
111
|
+
event(type, count, api);
|
|
112
|
+
}),
|
|
113
|
+
});
|
|
114
|
+
const api = {
|
|
115
|
+
emit: core.emit,
|
|
116
|
+
has: core.has,
|
|
117
|
+
isRunning: () => teardown !== null,
|
|
118
|
+
run: () => {
|
|
119
|
+
if (teardown)
|
|
120
|
+
return;
|
|
121
|
+
teardown = (producer(core.emit) ?? (() => { }));
|
|
122
|
+
if (closeOn && !closeSignalOff)
|
|
123
|
+
closeSignalOff = closeOn.on(() => api.close());
|
|
124
|
+
},
|
|
125
|
+
close: () => {
|
|
126
|
+
const stop = teardown;
|
|
127
|
+
teardown = null;
|
|
128
|
+
stop?.();
|
|
129
|
+
core.close();
|
|
130
|
+
if (closeHooks) {
|
|
131
|
+
const hooks = closeHooks;
|
|
132
|
+
closeHooks = null;
|
|
133
|
+
hooks.forEach(cb => cb());
|
|
134
|
+
}
|
|
135
|
+
closeSignalOff?.();
|
|
136
|
+
closeSignalOff = null;
|
|
137
|
+
},
|
|
138
|
+
onClose: (cb) => {
|
|
139
|
+
closeHooks = closeHooks ?? new Map();
|
|
140
|
+
closeHooks.set(cb, cb);
|
|
141
|
+
return function offClose() { closeHooks?.delete(cb); };
|
|
142
|
+
},
|
|
143
|
+
on: ((cb, { cbClose, key } = {}) => {
|
|
144
|
+
const k = key ?? Symbol();
|
|
145
|
+
const off = core.on(cb, { key: k });
|
|
146
|
+
if (cbClose) {
|
|
147
|
+
closeHooks = closeHooks ?? new Map();
|
|
148
|
+
closeHooks.set(k, cbClose);
|
|
149
|
+
}
|
|
150
|
+
event?.('add', core.count(), api);
|
|
151
|
+
return off;
|
|
152
|
+
}),
|
|
153
|
+
off: core.off,
|
|
154
|
+
once: (cb, opts = {}) => {
|
|
155
|
+
let off = () => { };
|
|
156
|
+
off = api.on(((...args) => { off(); cb(...args); }), opts);
|
|
157
|
+
return off;
|
|
158
|
+
},
|
|
159
|
+
count: core.count,
|
|
160
|
+
keys: core.keys,
|
|
161
|
+
};
|
|
162
|
+
listenByOn.set(api.on, api);
|
|
163
|
+
return api;
|
|
164
|
+
}
|
|
165
|
+
function createFastListen(producer) {
|
|
166
|
+
return createListen(producer, { fast: true });
|
|
167
|
+
}
|
|
168
|
+
function listen(options = { fast: true }) {
|
|
169
|
+
let emit;
|
|
170
|
+
const api = createListen((next) => { emit = next; }, { fast: true, ...options });
|
|
171
|
+
api.run();
|
|
172
|
+
emit = api.emit;
|
|
173
|
+
return [emit, api];
|
|
174
|
+
}
|
|
175
|
+
function withStoreListen(base, currentProvider) {
|
|
176
|
+
function currentValue(current) {
|
|
177
|
+
if (typeof current == 'function')
|
|
178
|
+
return current();
|
|
179
|
+
return current ? currentProvider() : undefined;
|
|
180
|
+
}
|
|
181
|
+
const api = {
|
|
182
|
+
...base,
|
|
183
|
+
on: ((cb, { cbClose, key, current } = {}) => {
|
|
184
|
+
const off = base.on(cb, { cbClose, key });
|
|
185
|
+
if (current) {
|
|
186
|
+
const value = currentValue(current);
|
|
187
|
+
if (value)
|
|
188
|
+
cb(...value);
|
|
189
|
+
}
|
|
190
|
+
return off;
|
|
191
|
+
}),
|
|
192
|
+
once: (cb, opts = {}) => {
|
|
193
|
+
if (opts.current) {
|
|
194
|
+
const value = currentValue(opts.current);
|
|
195
|
+
if (value) {
|
|
196
|
+
cb(...value);
|
|
197
|
+
return () => { };
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
let off = () => { };
|
|
201
|
+
off = base.on(((...args) => { off(); cb(...args); }), { key: opts.key });
|
|
202
|
+
return off;
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
listenByOn.set(api.on, api);
|
|
206
|
+
return api;
|
|
207
|
+
}
|
|
208
|
+
function createStoreListen(producer, options) {
|
|
209
|
+
const { current, ...listenOptions } = options;
|
|
210
|
+
return withStoreListen(createListen(producer, listenOptions), current);
|
|
211
|
+
}
|
|
212
|
+
function listenStore(options) {
|
|
213
|
+
const { current, ...listenOptions } = options;
|
|
214
|
+
let emit;
|
|
215
|
+
const base = createListen((next) => { emit = next; }, { fast: true, ...listenOptions });
|
|
216
|
+
const api = withStoreListen(base, current);
|
|
217
|
+
base.run();
|
|
218
|
+
emit = base.emit;
|
|
219
|
+
return [emit, api];
|
|
220
|
+
}
|
|
221
|
+
function toSlimListen(full) {
|
|
222
|
+
return {
|
|
223
|
+
on: (cb, opts) => full.on(cb, opts),
|
|
224
|
+
off: (keyOrCallback) => full.off(keyOrCallback),
|
|
225
|
+
close: () => full.close(),
|
|
226
|
+
count: () => full.count(),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function slimListen(options = { fast: true }) {
|
|
230
|
+
const [emit, full] = listen(options);
|
|
231
|
+
return [emit, toSlimListen(full)];
|
|
232
|
+
}
|
|
233
|
+
const LISTEN_CORE = ['emit', 'on', 'off', 'onClose', 'run', 'isRunning', 'close', 'count'];
|
|
234
|
+
function isListenCallback(obj) {
|
|
235
|
+
if (obj == null || typeof obj != 'object')
|
|
236
|
+
return false;
|
|
237
|
+
const keys = new Set(Object.keys(obj));
|
|
238
|
+
for (const key of LISTEN_CORE)
|
|
239
|
+
if (!keys.has(key))
|
|
240
|
+
return false;
|
|
241
|
+
for (const key of LISTEN_CORE)
|
|
242
|
+
if (typeof obj[key] != 'function')
|
|
243
|
+
return false;
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
@@ -1,210 +1 @@
|
|
|
1
|
-
export
|
|
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 registerListenOn(on: Function, api: any): void;
|
|
37
|
-
export type ListenOnBrand<Z extends any[] = any[]> = {
|
|
38
|
-
readonly [LISTEN_ON_BRAND]: Z;
|
|
39
|
-
};
|
|
40
|
-
export declare function funcListenCore<T>(options?: ListenCoreOptions<T>): {
|
|
41
|
-
func: Listener<NormalizeTuple<T>>;
|
|
42
|
-
has: (k: key) => boolean;
|
|
43
|
-
on: ListenOn<NormalizeTuple<T>>;
|
|
44
|
-
off: (k: Listener<NormalizeTuple<T>> | null | key) => void;
|
|
45
|
-
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
46
|
-
key?: key;
|
|
47
|
-
}) => () => void;
|
|
48
|
-
removeListen: (k: Listener<NormalizeTuple<T>> | null | key) => void;
|
|
49
|
-
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
50
|
-
key?: key;
|
|
51
|
-
}) => () => void;
|
|
52
|
-
close: () => void;
|
|
53
|
-
count: () => number;
|
|
54
|
-
readonly getAllKeys: key[];
|
|
55
|
-
};
|
|
56
|
-
export type ListenCoreApi<T = any> = ReturnType<typeof funcListenCore<T>>;
|
|
57
|
-
export declare function funcListenCallbackBase<T>(b: (e: Listener<NormalizeTuple<T>>) => (void | (() => void)), options?: ListenOptions<T>): {
|
|
58
|
-
func: Listener<NormalizeTuple<T>>;
|
|
59
|
-
isRun: () => boolean;
|
|
60
|
-
run: () => void;
|
|
61
|
-
close: () => void;
|
|
62
|
-
eventClose: (cb: cbClose) => () => void;
|
|
63
|
-
onClose: (cb: cbClose) => () => void;
|
|
64
|
-
removeEventClose: (cb: cbClose) => void;
|
|
65
|
-
on: ListenOn<NormalizeTuple<T>>;
|
|
66
|
-
off: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
67
|
-
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
68
|
-
cbClose?: cbClose;
|
|
69
|
-
key?: key;
|
|
70
|
-
}) => () => void;
|
|
71
|
-
removeListen: (k: Listener<NormalizeTuple<T>> | null | key) => void;
|
|
72
|
-
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
73
|
-
key?: key;
|
|
74
|
-
}) => () => void;
|
|
75
|
-
count: () => number;
|
|
76
|
-
readonly getAllKeys: key[];
|
|
77
|
-
};
|
|
78
|
-
export type ListenApi<T = any> = ReturnType<typeof funcListenCallbackBase<T>>;
|
|
79
|
-
export declare function funcListenCallbackFast<T>(a: (e: (Listener<NormalizeTuple<T>> | null)) => (void | (() => void))): {
|
|
80
|
-
func: Listener<NormalizeTuple<T>>;
|
|
81
|
-
isRun: () => boolean;
|
|
82
|
-
run: () => void;
|
|
83
|
-
close: () => void;
|
|
84
|
-
eventClose: (cb: cbClose) => () => void;
|
|
85
|
-
onClose: (cb: cbClose) => () => void;
|
|
86
|
-
removeEventClose: (cb: cbClose) => void;
|
|
87
|
-
on: ListenOn<NormalizeTuple<T>>;
|
|
88
|
-
off: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
89
|
-
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
90
|
-
cbClose?: cbClose;
|
|
91
|
-
key?: key;
|
|
92
|
-
}) => () => void;
|
|
93
|
-
removeListen: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
94
|
-
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
95
|
-
key?: key;
|
|
96
|
-
}) => () => void;
|
|
97
|
-
count: () => number;
|
|
98
|
-
readonly getAllKeys: key[];
|
|
99
|
-
};
|
|
100
|
-
export declare const funcListenCallback: typeof funcListenCallbackBase;
|
|
101
|
-
export declare function UseListen<T>(data?: ListenOptions<T>): readonly [(...a: NormalizeTuple<T>) => void, {
|
|
102
|
-
func: Listener<NormalizeTuple<T>>;
|
|
103
|
-
isRun: () => boolean;
|
|
104
|
-
run: () => void;
|
|
105
|
-
close: () => void;
|
|
106
|
-
eventClose: (cb: cbClose) => () => void;
|
|
107
|
-
onClose: (cb: cbClose) => () => void;
|
|
108
|
-
removeEventClose: (cb: cbClose) => void;
|
|
109
|
-
on: ListenOn<NormalizeTuple<T>>;
|
|
110
|
-
off: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
111
|
-
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
112
|
-
cbClose?: cbClose;
|
|
113
|
-
key?: key;
|
|
114
|
-
}) => () => void;
|
|
115
|
-
removeListen: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
116
|
-
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
117
|
-
key?: key;
|
|
118
|
-
}) => () => void;
|
|
119
|
-
count: () => number;
|
|
120
|
-
readonly getAllKeys: key[];
|
|
121
|
-
}];
|
|
122
|
-
export declare function withStoreListen<T>(base: ListenApi<T>, currentProvider: ListenCurrentProvider<NormalizeTuple<T>>): {
|
|
123
|
-
on: ListenOnCurrent<NormalizeTuple<T>>;
|
|
124
|
-
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
125
|
-
cbClose?: cbClose;
|
|
126
|
-
key?: key;
|
|
127
|
-
current?: ListenCurrent<NormalizeTuple<T>>;
|
|
128
|
-
}) => () => void;
|
|
129
|
-
removeListen: (k: Listener<NormalizeTuple<T>> | null | key) => void;
|
|
130
|
-
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
131
|
-
key?: key;
|
|
132
|
-
current?: ListenCurrent<NormalizeTuple<T>>;
|
|
133
|
-
}) => () => void;
|
|
134
|
-
getAllKeys: key[];
|
|
135
|
-
func: Listener<NormalizeTuple<T>>;
|
|
136
|
-
isRun: () => boolean;
|
|
137
|
-
run: () => void;
|
|
138
|
-
close: () => void;
|
|
139
|
-
eventClose: (cb: cbClose) => () => void;
|
|
140
|
-
onClose: (cb: cbClose) => () => void;
|
|
141
|
-
removeEventClose: (cb: cbClose) => void;
|
|
142
|
-
off: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
143
|
-
count: () => number;
|
|
144
|
-
};
|
|
145
|
-
export type ListenStoreApi<T> = ReturnType<typeof withStoreListen<T>>;
|
|
146
|
-
export declare function funcListenCallbackStore<T>(b: (e: Listener<NormalizeTuple<T>>) => (void | (() => void)), options: ListenStoreOptions<T>): {
|
|
147
|
-
on: ListenOnCurrent<NormalizeTuple<T>>;
|
|
148
|
-
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
149
|
-
cbClose?: cbClose;
|
|
150
|
-
key?: key;
|
|
151
|
-
current?: ListenCurrent<NormalizeTuple<T>> | undefined;
|
|
152
|
-
}) => () => void;
|
|
153
|
-
removeListen: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
154
|
-
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
155
|
-
key?: key;
|
|
156
|
-
current?: ListenCurrent<NormalizeTuple<T>> | undefined;
|
|
157
|
-
}) => () => void;
|
|
158
|
-
getAllKeys: key[];
|
|
159
|
-
func: Listener<NormalizeTuple<T>>;
|
|
160
|
-
isRun: () => boolean;
|
|
161
|
-
run: () => void;
|
|
162
|
-
close: () => void;
|
|
163
|
-
eventClose: (cb: cbClose) => () => void;
|
|
164
|
-
onClose: (cb: cbClose) => () => void;
|
|
165
|
-
removeEventClose: (cb: cbClose) => void;
|
|
166
|
-
off: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
167
|
-
count: () => number;
|
|
168
|
-
};
|
|
169
|
-
export declare function UseListenStore<T>(data: ListenStoreOptions<T>): readonly [(...a: NormalizeTuple<T>) => void, {
|
|
170
|
-
on: ListenOnCurrent<NormalizeTuple<T>>;
|
|
171
|
-
addListen: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
172
|
-
cbClose?: cbClose;
|
|
173
|
-
key?: key;
|
|
174
|
-
current?: ListenCurrent<NormalizeTuple<T>> | undefined;
|
|
175
|
-
}) => () => void;
|
|
176
|
-
removeListen: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
177
|
-
once: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
178
|
-
key?: key;
|
|
179
|
-
current?: ListenCurrent<NormalizeTuple<T>> | undefined;
|
|
180
|
-
}) => () => void;
|
|
181
|
-
getAllKeys: key[];
|
|
182
|
-
func: Listener<NormalizeTuple<T>>;
|
|
183
|
-
isRun: () => boolean;
|
|
184
|
-
run: () => void;
|
|
185
|
-
close: () => void;
|
|
186
|
-
eventClose: (cb: cbClose) => () => void;
|
|
187
|
-
onClose: (cb: cbClose) => () => void;
|
|
188
|
-
removeEventClose: (cb: cbClose) => void;
|
|
189
|
-
off: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
190
|
-
count: () => number;
|
|
191
|
-
}];
|
|
192
|
-
export declare function toListen2<T>(full: ListenApi<T>): {
|
|
193
|
-
on: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
194
|
-
key?: key;
|
|
195
|
-
}) => () => void;
|
|
196
|
-
off: (k: Listener<NormalizeTuple<T>> | null | key) => void;
|
|
197
|
-
close: () => void;
|
|
198
|
-
count: () => number;
|
|
199
|
-
};
|
|
200
|
-
export type Listen2<T> = ReturnType<typeof toListen2<T>>;
|
|
201
|
-
export declare function UseListen2<T>(data?: ListenOptions<T>): readonly [(...a: NormalizeTuple<T>) => void, {
|
|
202
|
-
on: (cb: Listener<NormalizeTuple<T>>, opts?: {
|
|
203
|
-
key?: key;
|
|
204
|
-
} | undefined) => () => void;
|
|
205
|
-
off: (k: key | Listener<NormalizeTuple<T>> | null) => void;
|
|
206
|
-
close: () => void;
|
|
207
|
-
count: () => number;
|
|
208
|
-
}];
|
|
209
|
-
export declare function isListenCallback(obj: any): obj is ListenApi;
|
|
210
|
-
export {};
|
|
1
|
+
export * from './Listen';
|