@kuindji/reactive 1.0.17 → 1.0.19
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.
|
@@ -2,4 +2,9 @@ import type { BaseActionBus } from "../actionBus";
|
|
|
2
2
|
import type { ListenerOptions } from "../event";
|
|
3
3
|
import type { ErrorListenerSignature, KeyOf } from "../lib/types";
|
|
4
4
|
export type { BaseActionBus, ErrorListenerSignature, ListenerOptions };
|
|
5
|
-
export declare function useListenToActionBus<TActionBus extends BaseActionBus, TKey extends KeyOf<TActionBus["__type"]["actions"]>, TListener extends TActionBus["__type"]["actions"][TKey]["listenerSignature"]>(actionBus: TActionBus, actionName: TKey, listener
|
|
5
|
+
export declare function useListenToActionBus<TActionBus extends BaseActionBus, TKey extends KeyOf<TActionBus["__type"]["actions"]>, TListener extends TActionBus["__type"]["actions"][TKey]["listenerSignature"], TBeforeActionListener extends TActionBus["__type"]["actions"][TKey]["beforeActionSignature"]>(actionBus: TActionBus, actionName: TKey, listener?: TListener | null | {
|
|
6
|
+
listener?: TListener;
|
|
7
|
+
options?: ListenerOptions;
|
|
8
|
+
errorListener?: ErrorListenerSignature<any[]> | null;
|
|
9
|
+
beforeActionListener?: TBeforeActionListener | null;
|
|
10
|
+
}, options?: ListenerOptions | null, errorListener?: ErrorListenerSignature<any[]> | null, beforeActionListener?: TBeforeActionListener | null): void;
|
|
@@ -2,37 +2,49 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useListenToActionBus = useListenToActionBus;
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
-
function useListenToActionBus(actionBus, actionName, listener, options, errorListener) {
|
|
6
|
-
|
|
5
|
+
function useListenToActionBus(actionBus, actionName, listener, options, errorListener, beforeActionListener) {
|
|
6
|
+
if (listener && typeof listener !== "function") {
|
|
7
|
+
options = listener.options;
|
|
8
|
+
errorListener = listener.errorListener;
|
|
9
|
+
beforeActionListener = listener.beforeActionListener;
|
|
10
|
+
listener = listener.listener;
|
|
11
|
+
}
|
|
7
12
|
const actionBusRef = (0, react_1.useRef)(actionBus);
|
|
8
|
-
const
|
|
9
|
-
|
|
13
|
+
const listenerRef = (0, react_1.useRef)(listener || null);
|
|
14
|
+
const errorListenerRef = (0, react_1.useRef)(null);
|
|
15
|
+
const beforeActionListenerRef = (0, react_1.useRef)(null);
|
|
16
|
+
listenerRef.current = listener || null;
|
|
17
|
+
errorListenerRef.current = errorListener || null;
|
|
18
|
+
beforeActionListenerRef.current = beforeActionListener || null;
|
|
10
19
|
const genericHandler = (0, react_1.useCallback)((arg) => {
|
|
11
20
|
var _a;
|
|
12
21
|
return (_a = listenerRef.current) === null || _a === void 0 ? void 0 : _a.call(listenerRef, arg);
|
|
13
22
|
}, []);
|
|
23
|
+
const genericBeforeActionHandler = (0, react_1.useCallback)((...args) => {
|
|
24
|
+
var _a;
|
|
25
|
+
return ((_a = beforeActionListenerRef.current) === null || _a === void 0 ? void 0 : _a.call(beforeActionListenerRef, ...args)) || undefined;
|
|
26
|
+
}, []);
|
|
27
|
+
const genericErrorListener = (0, react_1.useCallback)((arg) => {
|
|
28
|
+
var _a;
|
|
29
|
+
return (_a = errorListenerRef.current) === null || _a === void 0 ? void 0 : _a.call(errorListenerRef, arg);
|
|
30
|
+
}, []);
|
|
14
31
|
(0, react_1.useEffect)(() => {
|
|
15
32
|
return () => {
|
|
16
33
|
actionBusRef.current.removeListener(actionName, genericHandler);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
34
|
+
actionBusRef.current.get(actionName)
|
|
35
|
+
.removeBeforeActionListener(genericBeforeActionHandler);
|
|
36
|
+
actionBusRef.current.removeErrorListener(genericErrorListener);
|
|
20
37
|
};
|
|
21
38
|
}, []);
|
|
22
39
|
(0, react_1.useEffect)(() => {
|
|
23
40
|
actionBusRef.current.removeListener(actionName, genericHandler);
|
|
41
|
+
actionBusRef.current.get(actionName)
|
|
42
|
+
.removeBeforeActionListener(genericBeforeActionHandler);
|
|
43
|
+
actionBusRef.current.removeErrorListener(genericErrorListener);
|
|
24
44
|
actionBusRef.current = actionBus;
|
|
25
|
-
actionBusRef.current.addListener(actionName, genericHandler, options);
|
|
45
|
+
actionBusRef.current.addListener(actionName, genericHandler, options || undefined);
|
|
46
|
+
actionBusRef.current.get(actionName)
|
|
47
|
+
.addBeforeActionListener(genericBeforeActionHandler);
|
|
48
|
+
actionBusRef.current.addErrorListener(genericErrorListener);
|
|
26
49
|
}, [actionBus]);
|
|
27
|
-
(0, react_1.useEffect)(() => {
|
|
28
|
-
if (errorListenerRef.current !== errorListener) {
|
|
29
|
-
if (errorListenerRef.current) {
|
|
30
|
-
actionBusRef.current.removeErrorListener(errorListenerRef.current);
|
|
31
|
-
}
|
|
32
|
-
errorListenerRef.current = errorListener;
|
|
33
|
-
if (errorListener) {
|
|
34
|
-
actionBusRef.current.addErrorListener(errorListener);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}, [errorListener]);
|
|
38
50
|
}
|