@kuindji/reactive
Advanced tools
@@ -5,4 +5,4 @@ import type { BaseActionBus } from "../actionBus"; | ||
| export type { BaseActionBus, ErrorListenerSignature, ListenerOptions }; | ||
| 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 | { | ||
| listener: TListener; | ||
| 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 | { | ||
| listener?: TListener; | ||
| options?: ListenerOptions; | ||
@@ -9,0 +9,0 @@ errorListener?: ErrorListenerSignature<any[]> | null; |
@@ -13,6 +13,8 @@ "use strict"; | ||
| const actionBusRef = (0, react_1.useRef)(actionBus); | ||
| const listenerRef = (0, react_1.useRef)(listener); | ||
| const listenerRef = (0, react_1.useRef)(listener || null); | ||
| const errorListenerRef = (0, react_1.useRef)(null); | ||
| const beforeActionListenerRef = (0, react_1.useRef)(null); | ||
| listenerRef.current = listener; | ||
| listenerRef.current = listener || null; | ||
| errorListenerRef.current = errorListener || null; | ||
| beforeActionListenerRef.current = beforeActionListener || null; | ||
| const genericHandler = (0, react_1.useCallback)((arg) => { | ||
@@ -22,12 +24,16 @@ var _a; | ||
| }, []); | ||
| const genericBeforeActionHandler = (0, react_1.useCallback)((...args) => { | ||
| var _a; | ||
| return ((_a = beforeActionListenerRef.current) === null || _a === void 0 ? void 0 : _a.call(beforeActionListenerRef, ...args)) || undefined; | ||
| }, []); | ||
| const genericErrorListener = (0, react_1.useCallback)((arg) => { | ||
| var _a; | ||
| return (_a = errorListenerRef.current) === null || _a === void 0 ? void 0 : _a.call(errorListenerRef, arg); | ||
| }, []); | ||
| (0, react_1.useEffect)(() => { | ||
| return () => { | ||
| actionBusRef.current.removeListener(actionName, genericHandler); | ||
| if (errorListenerRef.current) { | ||
| actionBusRef.current.removeErrorListener(errorListenerRef.current); | ||
| } | ||
| if (beforeActionListenerRef.current) { | ||
| actionBusRef.current.get(actionName) | ||
| .removeBeforeActionListener(beforeActionListenerRef.current); | ||
| } | ||
| actionBusRef.current.get(actionName) | ||
| .removeBeforeActionListener(genericBeforeActionHandler); | ||
| actionBusRef.current.removeErrorListener(genericErrorListener); | ||
| }; | ||
@@ -37,29 +43,11 @@ }, []); | ||
| actionBusRef.current.removeListener(actionName, genericHandler); | ||
| actionBusRef.current.get(actionName) | ||
| .removeBeforeActionListener(genericBeforeActionHandler); | ||
| actionBusRef.current.removeErrorListener(genericErrorListener); | ||
| actionBusRef.current = actionBus; | ||
| actionBusRef.current.addListener(actionName, genericHandler, options || undefined); | ||
| actionBusRef.current.get(actionName) | ||
| .addBeforeActionListener(genericBeforeActionHandler); | ||
| actionBusRef.current.addErrorListener(genericErrorListener); | ||
| }, [actionBus]); | ||
| (0, react_1.useEffect)(() => { | ||
| if (errorListenerRef.current !== errorListener) { | ||
| if (errorListenerRef.current) { | ||
| actionBusRef.current.removeErrorListener(errorListenerRef.current); | ||
| } | ||
| errorListenerRef.current = errorListener || null; | ||
| if (errorListener) { | ||
| actionBusRef.current.addErrorListener(errorListener); | ||
| } | ||
| } | ||
| }, [errorListener]); | ||
| (0, react_1.useEffect)(() => { | ||
| if (beforeActionListenerRef.current !== beforeActionListener) { | ||
| if (beforeActionListenerRef.current) { | ||
| actionBusRef.current.get(actionName) | ||
| .removeBeforeActionListener(beforeActionListenerRef.current); | ||
| } | ||
| beforeActionListenerRef.current = beforeActionListener || null; | ||
| if (beforeActionListener) { | ||
| actionBusRef.current.get(actionName) | ||
| .addBeforeActionListener(beforeActionListener); | ||
| } | ||
| } | ||
| }, [beforeActionListener]); | ||
| } |
+1
-1
| { | ||
| "name": "@kuindji/reactive", | ||
| "version": "1.0.18", | ||
| "version": "1.0.19", | ||
| "author": "Ivan Kuindzhi", | ||
@@ -5,0 +5,0 @@ "repository": { |
160591
-0.13%2865
-0.42%