wenay-common2 1.0.21 → 1.0.23
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/events/Listen.d.ts +2 -2
- package/lib/Common/events/Listen.js +14 -2
- package/lib/Common/events/UseListenTransform.d.ts +15 -0
- package/lib/Common/events/UseListenTransform.js +28 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/server/createSignatureFunction.d.ts +2 -2
- package/package.json +1 -1
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export type Listener<T extends any[]> = (...r: T) => void;
|
|
2
|
-
type NormalizeTuple<T> = T extends any[] ? T : [T];
|
|
2
|
+
export type NormalizeTuple<T> = T extends any[] ? T : [T];
|
|
3
3
|
export declare function funcListenCallbackBase<T>(b: (e: Listener<NormalizeTuple<T>>) => (void | (() => void)), data?: {
|
|
4
4
|
event?: (type: "add" | "remove", count: number, api: ReturnType<typeof funcListenCallbackBase<T>>) => void;
|
|
5
5
|
fast?: boolean;
|
|
6
|
+
addListenClose?: ReturnType<typeof funcListenCallbackBase<any>>;
|
|
6
7
|
}): {
|
|
7
8
|
func: Listener<NormalizeTuple<T>>;
|
|
8
9
|
isRun: () => boolean;
|
|
@@ -52,4 +53,3 @@ export declare function UseListen<T>(data?: Parameters<typeof funcListenCallback
|
|
|
52
53
|
readonly getAllKeys: Listener<NormalizeTuple<T>>[];
|
|
53
54
|
}];
|
|
54
55
|
export declare function isListenCallback(obj: any): obj is ReturnType<typeof funcListenCallbackBase>;
|
|
55
|
-
export {};
|
|
@@ -6,13 +6,14 @@ exports.funcListenCallback = funcListenCallback;
|
|
|
6
6
|
exports.UseListen = UseListen;
|
|
7
7
|
exports.isListenCallback = isListenCallback;
|
|
8
8
|
function funcListenCallbackBase(b, data) {
|
|
9
|
-
const { fast = true, event } = data ?? {};
|
|
9
|
+
const { fast = true, event, addListenClose } = data ?? {};
|
|
10
10
|
const obj = new Map();
|
|
11
11
|
const evClose = new Map();
|
|
12
12
|
const sinh = new Map();
|
|
13
13
|
let a = (...e) => { obj.forEach(z => z(...e)); };
|
|
14
14
|
let close = null;
|
|
15
15
|
let cached = null;
|
|
16
|
+
let closeUnsubscribe = null;
|
|
16
17
|
const getArr = () => cached ?? (cached = Array.from(obj.values()));
|
|
17
18
|
const rebuild = () => {
|
|
18
19
|
cached = null;
|
|
@@ -37,7 +38,14 @@ function funcListenCallbackBase(b, data) {
|
|
|
37
38
|
});
|
|
38
39
|
};
|
|
39
40
|
const func = (...e) => { a?.(...e); };
|
|
40
|
-
const run = () => {
|
|
41
|
+
const run = () => {
|
|
42
|
+
close = (b(func) ?? (() => { }));
|
|
43
|
+
if (addListenClose && !closeUnsubscribe) {
|
|
44
|
+
closeUnsubscribe = addListenClose.addListen(() => {
|
|
45
|
+
api.close();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
};
|
|
41
49
|
const api = {
|
|
42
50
|
func,
|
|
43
51
|
isRun: () => close !== null,
|
|
@@ -51,6 +59,10 @@ function funcListenCallbackBase(b, data) {
|
|
|
51
59
|
sinh.clear();
|
|
52
60
|
evClose.forEach(cb => cb());
|
|
53
61
|
evClose.clear();
|
|
62
|
+
if (closeUnsubscribe) {
|
|
63
|
+
closeUnsubscribe();
|
|
64
|
+
closeUnsubscribe = null;
|
|
65
|
+
}
|
|
54
66
|
},
|
|
55
67
|
eventClose: (cb) => {
|
|
56
68
|
evClose.set(cb, cb);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { funcListenCallbackBase, Listener, NormalizeTuple } from "./Listen";
|
|
2
|
+
export declare function UseListenTransform<TSource extends any[], TTarget extends any[]>(sourceListen: ReturnType<typeof funcListenCallbackBase<TSource>>, transform: (...args: NormalizeTuple<TSource>) => TTarget | null, options?: {
|
|
3
|
+
addListenClose?: ReturnType<typeof funcListenCallbackBase<any>>;
|
|
4
|
+
}): readonly [(...a: NormalizeTuple<TTarget>) => void, {
|
|
5
|
+
func: Listener<NormalizeTuple<TTarget>>;
|
|
6
|
+
isRun: () => boolean;
|
|
7
|
+
run: () => void;
|
|
8
|
+
close: () => void;
|
|
9
|
+
eventClose: (cb: () => void) => () => void;
|
|
10
|
+
removeEventClose: (cb: () => void) => void;
|
|
11
|
+
addListen: (cb: Listener<NormalizeTuple<TTarget>>, cbClose?: (() => void) | undefined) => () => void;
|
|
12
|
+
removeListen: (cb: Listener<NormalizeTuple<TTarget>> | null) => void;
|
|
13
|
+
count: () => number;
|
|
14
|
+
readonly getAllKeys: Listener<NormalizeTuple<TTarget>>[];
|
|
15
|
+
}];
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UseListenTransform = UseListenTransform;
|
|
4
|
+
const Listen_1 = require("./Listen");
|
|
5
|
+
function UseListenTransform(sourceListen, transform, options) {
|
|
6
|
+
let unsubscribeFromSource = null;
|
|
7
|
+
const [emit, targetListen] = (0, Listen_1.UseListen)({
|
|
8
|
+
event: (type, count, api) => {
|
|
9
|
+
if (type === "add" && count === 1) {
|
|
10
|
+
const sourceCallback = (...args) => {
|
|
11
|
+
const result = transform(...args);
|
|
12
|
+
if (result !== null) {
|
|
13
|
+
emit(...result);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
unsubscribeFromSource = sourceListen.addListen(sourceCallback);
|
|
17
|
+
}
|
|
18
|
+
if (type === "remove" && count === 0) {
|
|
19
|
+
if (unsubscribeFromSource) {
|
|
20
|
+
unsubscribeFromSource();
|
|
21
|
+
unsubscribeFromSource = null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
addListenClose: options?.addListenClose
|
|
26
|
+
});
|
|
27
|
+
return [emit, targetListen];
|
|
28
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export * from "./Common/events/event";
|
|
|
16
16
|
export * from "./Common/events/SocketBuffer";
|
|
17
17
|
export * from "./Common/events/SocketServerHook";
|
|
18
18
|
export * from "./Common/events/joinListens";
|
|
19
|
+
export * from "./Common/events/UseListenTransform";
|
|
19
20
|
export * from "./Common/rcp/rpc-index";
|
|
20
21
|
export * from "./server/fsKeyVolume";
|
|
21
22
|
export * from "./Common/Color";
|
package/lib/index.js
CHANGED
|
@@ -55,6 +55,7 @@ __exportStar(require("./Common/events/event"), exports);
|
|
|
55
55
|
__exportStar(require("./Common/events/SocketBuffer"), exports);
|
|
56
56
|
__exportStar(require("./Common/events/SocketServerHook"), exports);
|
|
57
57
|
__exportStar(require("./Common/events/joinListens"), exports);
|
|
58
|
+
__exportStar(require("./Common/events/UseListenTransform"), exports);
|
|
58
59
|
__exportStar(require("./Common/rcp/rpc-index"), exports);
|
|
59
60
|
__exportStar(require("./server/fsKeyVolume"), exports);
|
|
60
61
|
__exportStar(require("./Common/Color"), exports);
|
|
@@ -9,9 +9,9 @@ export type tInputBase = {
|
|
|
9
9
|
type tSignatureData = tInputBase;
|
|
10
10
|
type HmacCreator = (algorithm: string, key: string) => {
|
|
11
11
|
update: (data: string) => {
|
|
12
|
-
digest: (encoding: string) =>
|
|
12
|
+
digest: (encoding: string) => unknown;
|
|
13
13
|
};
|
|
14
14
|
};
|
|
15
|
-
export declare function createSignatureFunction(hmacCreator:
|
|
15
|
+
export declare function createSignatureFunction<T extends HmacCreator>(hmacCreator: T): (params: tSignatureData, apiSecret: string) => unknown;
|
|
16
16
|
export type SignatureFunction = ReturnType<typeof createSignatureFunction>;
|
|
17
17
|
export {};
|