@kuindji/reactive 1.0.0

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.
Files changed (49) hide show
  1. package/README.md +985 -0
  2. package/dist/action.d.ts +49 -0
  3. package/dist/action.js +77 -0
  4. package/dist/actionBus.d.ts +43 -0
  5. package/dist/actionBus.js +81 -0
  6. package/dist/actionMap.d.ts +23 -0
  7. package/dist/actionMap.js +26 -0
  8. package/dist/event.d.ts +143 -0
  9. package/dist/event.js +538 -0
  10. package/dist/eventBus.d.ts +115 -0
  11. package/dist/eventBus.js +508 -0
  12. package/dist/index.d.ts +5 -0
  13. package/dist/index.js +21 -0
  14. package/dist/lib/asyncCall.d.ts +1 -0
  15. package/dist/lib/asyncCall.js +17 -0
  16. package/dist/lib/listenerSorter.d.ts +5 -0
  17. package/dist/lib/listenerSorter.js +13 -0
  18. package/dist/lib/tagsIntersect.d.ts +1 -0
  19. package/dist/lib/tagsIntersect.js +11 -0
  20. package/dist/lib/types.d.ts +49 -0
  21. package/dist/lib/types.js +37 -0
  22. package/dist/react/ErrorBoundary.d.ts +10 -0
  23. package/dist/react/ErrorBoundary.js +25 -0
  24. package/dist/react/useAction.d.ts +21 -0
  25. package/dist/react/useAction.js +66 -0
  26. package/dist/react/useActionBus.d.ts +38 -0
  27. package/dist/react/useActionBus.js +44 -0
  28. package/dist/react/useActionMap.d.ts +23 -0
  29. package/dist/react/useActionMap.js +25 -0
  30. package/dist/react/useEvent.d.ts +47 -0
  31. package/dist/react/useEvent.js +66 -0
  32. package/dist/react/useEventBus.d.ts +117 -0
  33. package/dist/react/useEventBus.js +66 -0
  34. package/dist/react/useListenToAction.d.ts +3 -0
  35. package/dist/react/useListenToAction.js +38 -0
  36. package/dist/react/useListenToEvent.d.ts +4 -0
  37. package/dist/react/useListenToEvent.js +34 -0
  38. package/dist/react/useListenToEventBus.d.ts +5 -0
  39. package/dist/react/useListenToEventBus.js +37 -0
  40. package/dist/react/useStore.d.ts +51 -0
  41. package/dist/react/useStore.js +30 -0
  42. package/dist/react/useStoreState.d.ts +3 -0
  43. package/dist/react/useStoreState.js +32 -0
  44. package/dist/react.d.ts +10 -0
  45. package/dist/react.js +26 -0
  46. package/dist/store.d.ts +54 -0
  47. package/dist/store.js +193 -0
  48. package/package.json +70 -0
  49. package/src/index.ts +5 -0
@@ -0,0 +1,10 @@
1
+ import type { ErrorListenerSignature, ErrorResponse } from "../lib/types";
2
+ export type ErrorBoundaryProps = {
3
+ children: React.ReactNode;
4
+ listener?: ErrorListenerSignature<any>;
5
+ };
6
+ export type { ErrorListenerSignature, ErrorResponse };
7
+ export declare const ErrorBoundaryContext: import("react").Context<ErrorListenerSignature<any> | null>;
8
+ declare function ErrorBoundary({ children, listener }: ErrorBoundaryProps): import("react/jsx-runtime").JSX.Element;
9
+ export default ErrorBoundary;
10
+ export { ErrorBoundary };
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorBoundaryContext = void 0;
4
+ exports.ErrorBoundary = ErrorBoundary;
5
+ const jsx_runtime_1 = require("react/jsx-runtime");
6
+ const react_1 = require("react");
7
+ exports.ErrorBoundaryContext = (0, react_1.createContext)(null);
8
+ function ErrorBoundary({ children, listener }) {
9
+ const boundaryErrorListener = (0, react_1.useContext)(exports.ErrorBoundaryContext);
10
+ const thisRef = (0, react_1.useRef)(listener);
11
+ const outerRef = (0, react_1.useRef)(boundaryErrorListener);
12
+ const thisErrorListener = (0, react_1.useCallback)((errorResponse) => {
13
+ if (thisRef.current) {
14
+ thisRef.current(errorResponse);
15
+ }
16
+ else if (outerRef.current) {
17
+ outerRef.current(errorResponse);
18
+ }
19
+ else {
20
+ throw errorResponse.error;
21
+ }
22
+ }, []);
23
+ return ((0, jsx_runtime_1.jsx)(exports.ErrorBoundaryContext.Provider, { value: thisErrorListener, children: children }));
24
+ }
25
+ exports.default = ErrorBoundary;
@@ -0,0 +1,21 @@
1
+ import type { ActionResponse, ListenerSignature } from "../action";
2
+ import type { BaseHandler, ErrorListenerSignature, ErrorResponse } from "../lib/types";
3
+ export type { ActionResponse, BaseHandler, ErrorListenerSignature, ErrorResponse, ListenerSignature, };
4
+ export declare function useAction<ActionSignature extends BaseHandler, Listener extends ListenerSignature<ActionSignature>, ErrorListener extends ErrorListenerSignature<Parameters<ActionSignature>>>(actionSignature: ActionSignature, listener?: Listener | null, errorListener?: ErrorListener | null): import("../lib/types").ApiType<import("../action").ActionDefinitionHelper<ActionSignature>, {
5
+ readonly invoke: (...args: Parameters<ActionSignature>) => Promise<ActionResponse<Awaited<ReturnType<ActionSignature>>, Parameters<ActionSignature>>>;
6
+ readonly addListener: (handler: ListenerSignature<ActionSignature>, listenerOptions?: import("..").ListenerOptions) => void;
7
+ readonly on: (handler: ListenerSignature<ActionSignature>, listenerOptions?: import("..").ListenerOptions) => void;
8
+ readonly subscribe: (handler: ListenerSignature<ActionSignature>, listenerOptions?: import("..").ListenerOptions) => void;
9
+ readonly listen: (handler: ListenerSignature<ActionSignature>, listenerOptions?: import("..").ListenerOptions) => void;
10
+ readonly removeAllListeners: (tag?: string) => void;
11
+ readonly removeListener: (handler: ListenerSignature<ActionSignature>, context?: object | null, tag?: string | null) => boolean;
12
+ readonly un: (handler: ListenerSignature<ActionSignature>, context?: object | null, tag?: string | null) => boolean;
13
+ readonly off: (handler: ListenerSignature<ActionSignature>, context?: object | null, tag?: string | null) => boolean;
14
+ readonly remove: (handler: ListenerSignature<ActionSignature>, context?: object | null, tag?: string | null) => boolean;
15
+ readonly unsubscribe: (handler: ListenerSignature<ActionSignature>, context?: object | null, tag?: string | null) => boolean;
16
+ readonly promise: (options?: import("..").ListenerOptions) => Promise<[arg: ActionResponse<Awaited<ReturnType<ActionSignature>>, Parameters<ActionSignature>>]>;
17
+ readonly addErrorListener: (handler: ErrorListenerSignature<Parameters<ActionSignature>>, listenerOptions?: import("..").ListenerOptions) => void;
18
+ readonly removeAllErrorListeners: (tag?: string) => void;
19
+ readonly removeErrorListener: (handler: ErrorListenerSignature<Parameters<ActionSignature>>, context?: object | null, tag?: string | null) => boolean;
20
+ readonly errorPromise: (options?: import("..").ListenerOptions) => Promise<[errorResponse: ErrorResponse<Parameters<ActionSignature>>]>;
21
+ }>;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useAction = useAction;
4
+ const react_1 = require("react");
5
+ const action_1 = require("../action");
6
+ const ErrorBoundary_1 = require("./ErrorBoundary");
7
+ function useAction(actionSignature, listener, errorListener) {
8
+ const boundaryErrorListener = (0, react_1.useContext)(ErrorBoundary_1.ErrorBoundaryContext);
9
+ const updateRef = (0, react_1.useRef)(0);
10
+ const listenerRef = (0, react_1.useRef)(listener);
11
+ const errorListenerRef = (0, react_1.useRef)(errorListener);
12
+ const boundaryErrorListenerRef = (0, react_1.useRef)(boundaryErrorListener);
13
+ const action = (0, react_1.useMemo)(() => {
14
+ const action = (0, action_1.createAction)(actionSignature);
15
+ if (listenerRef.current) {
16
+ action.addListener(listenerRef.current);
17
+ }
18
+ if (errorListenerRef.current) {
19
+ action.addErrorListener(errorListenerRef.current);
20
+ }
21
+ if (boundaryErrorListenerRef.current) {
22
+ action.addErrorListener(boundaryErrorListenerRef.current);
23
+ }
24
+ return action;
25
+ }, []);
26
+ (0, react_1.useEffect)(() => {
27
+ if (updateRef.current > 0) {
28
+ throw new Error("Action cannot be updated");
29
+ }
30
+ updateRef.current++;
31
+ }, [actionSignature]);
32
+ (0, react_1.useEffect)(() => {
33
+ if (listenerRef.current !== listener) {
34
+ if (listenerRef.current) {
35
+ action.removeListener(listenerRef.current);
36
+ }
37
+ listenerRef.current = listener !== null && listener !== void 0 ? listener : null;
38
+ if (listener) {
39
+ action.addListener(listener);
40
+ }
41
+ }
42
+ }, [listener]);
43
+ (0, react_1.useEffect)(() => {
44
+ if (errorListenerRef.current !== errorListener) {
45
+ if (errorListenerRef.current) {
46
+ action.removeErrorListener(errorListenerRef.current);
47
+ }
48
+ errorListenerRef.current = errorListener !== null && errorListener !== void 0 ? errorListener : null;
49
+ if (errorListener) {
50
+ action.addErrorListener(errorListener);
51
+ }
52
+ }
53
+ }, [errorListener]);
54
+ (0, react_1.useEffect)(() => {
55
+ if (boundaryErrorListenerRef.current !== boundaryErrorListener) {
56
+ if (boundaryErrorListenerRef.current) {
57
+ action.removeErrorListener(boundaryErrorListenerRef.current);
58
+ }
59
+ boundaryErrorListenerRef.current = boundaryErrorListener !== null && boundaryErrorListener !== void 0 ? boundaryErrorListener : null;
60
+ if (boundaryErrorListener) {
61
+ action.addErrorListener(boundaryErrorListener);
62
+ }
63
+ }
64
+ }, [boundaryErrorListener]);
65
+ return action;
66
+ }
@@ -0,0 +1,38 @@
1
+ import type { ActionResponse, ListenerSignature } from "../action";
2
+ import type { BaseActionsMap } from "../actionBus";
3
+ import type { ErrorListenerSignature, ErrorResponse } from "../lib/types";
4
+ export type { ActionResponse, BaseActionsMap, ErrorListenerSignature, ErrorResponse, ListenerSignature, };
5
+ export declare function useActionBus<ActionsMap extends BaseActionsMap = BaseActionsMap>(initialActions?: ActionsMap, errorListener?: ErrorListenerSignature<any[]>): import("../lib/types").ApiType<import("../actionBus").ActionBusDefinitionHelper<ActionsMap>, {
6
+ readonly add: (name: import("../lib/types").MapKey, action: import("../lib/types").BaseHandler) => void;
7
+ readonly get: <K extends import("../lib/types").KeyOf<{ [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }>>(name: K) => { [key_1 in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key_1]] extends [never] ? never : import("../lib/types").ApiType<import("../action").ActionDefinitionHelper<ActionsMap[key_1]>, {
8
+ readonly invoke: (...args: Parameters<ActionsMap[key_1]>) => Promise<ActionResponse<Awaited<ReturnType<ActionsMap[key_1]>>, Parameters<ActionsMap[key_1]>>>;
9
+ readonly addListener: (handler: ListenerSignature<ActionsMap[key_1]>, listenerOptions?: import("..").ListenerOptions) => void;
10
+ readonly on: (handler: ListenerSignature<ActionsMap[key_1]>, listenerOptions?: import("..").ListenerOptions) => void;
11
+ readonly subscribe: (handler: ListenerSignature<ActionsMap[key_1]>, listenerOptions?: import("..").ListenerOptions) => void;
12
+ readonly listen: (handler: ListenerSignature<ActionsMap[key_1]>, listenerOptions?: import("..").ListenerOptions) => void;
13
+ readonly removeAllListeners: (tag?: string) => void;
14
+ readonly removeListener: (handler: ListenerSignature<ActionsMap[key_1]>, context?: object | null, tag?: string | null) => boolean;
15
+ readonly un: (handler: ListenerSignature<ActionsMap[key_1]>, context?: object | null, tag?: string | null) => boolean;
16
+ readonly off: (handler: ListenerSignature<ActionsMap[key_1]>, context?: object | null, tag?: string | null) => boolean;
17
+ readonly remove: (handler: ListenerSignature<ActionsMap[key_1]>, context?: object | null, tag?: string | null) => boolean;
18
+ readonly unsubscribe: (handler: ListenerSignature<ActionsMap[key_1]>, context?: object | null, tag?: string | null) => boolean;
19
+ readonly promise: (options?: import("..").ListenerOptions) => Promise<[arg: ActionResponse<Awaited<ReturnType<ActionsMap[key_1]>>, Parameters<ActionsMap[key_1]>>]>;
20
+ readonly addErrorListener: (handler: ErrorListenerSignature<Parameters<ActionsMap[key_1]>>, listenerOptions?: import("..").ListenerOptions) => void;
21
+ readonly removeAllErrorListeners: (tag?: string) => void;
22
+ readonly removeErrorListener: (handler: ErrorListenerSignature<Parameters<ActionsMap[key_1]>>, context?: object | null, tag?: string | null) => boolean;
23
+ readonly errorPromise: (options?: import("..").ListenerOptions) => Promise<[errorResponse: ErrorResponse<Parameters<ActionsMap[key_1]>>]>;
24
+ }>; }[K];
25
+ readonly invoke: <K extends import("../lib/types").KeyOf<{ [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }>>(name: K, ...args: { [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }[K]["actionArguments"]) => Promise<ActionResponse<Awaited<ReturnType<ActionsMap[K]>>, Parameters<ActionsMap[K]>>>;
26
+ readonly addListener: <K extends import("../lib/types").KeyOf<{ [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }>>(name: K, handler: { [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }[K]["listenerSignature"], options?: import("..").ListenerOptions) => void;
27
+ readonly on: <K extends import("../lib/types").KeyOf<{ [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }>>(name: K, handler: { [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }[K]["listenerSignature"], options?: import("..").ListenerOptions) => void;
28
+ readonly subscribe: <K extends import("../lib/types").KeyOf<{ [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }>>(name: K, handler: { [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }[K]["listenerSignature"], options?: import("..").ListenerOptions) => void;
29
+ readonly listen: <K extends import("../lib/types").KeyOf<{ [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }>>(name: K, handler: { [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }[K]["listenerSignature"], options?: import("..").ListenerOptions) => void;
30
+ readonly once: <K extends import("../lib/types").KeyOf<{ [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }>>(name: K, handler: { [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }[K]["listenerSignature"], options?: import("..").ListenerOptions) => void;
31
+ readonly removeListener: <K extends import("../lib/types").KeyOf<{ [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }>>(name: K, handler: { [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }[K]["listenerSignature"], context?: object | null, tag?: string | null) => boolean;
32
+ readonly off: <K extends import("../lib/types").KeyOf<{ [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }>>(name: K, handler: { [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }[K]["listenerSignature"], context?: object | null, tag?: string | null) => boolean;
33
+ readonly remove: <K extends import("../lib/types").KeyOf<{ [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }>>(name: K, handler: { [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }[K]["listenerSignature"], context?: object | null, tag?: string | null) => boolean;
34
+ readonly un: <K extends import("../lib/types").KeyOf<{ [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }>>(name: K, handler: { [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }[K]["listenerSignature"], context?: object | null, tag?: string | null) => boolean;
35
+ readonly unsubscribe: <K extends import("../lib/types").KeyOf<{ [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }>>(name: K, handler: { [key in import("../lib/types").KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : import("../action").ActionDefinitionHelper<ActionsMap[key]>; }[K]["listenerSignature"], context?: object | null, tag?: string | null) => boolean;
36
+ readonly addErrorListener: (handler: ErrorListenerSignature<any[]>, listenerOptions?: import("..").ListenerOptions) => void;
37
+ readonly removeErrorListener: (handler: ErrorListenerSignature<any[]>, context?: object | null, tag?: string | null) => boolean;
38
+ }>;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useActionBus = useActionBus;
4
+ const react_1 = require("react");
5
+ const actionBus_1 = require("../actionBus");
6
+ const ErrorBoundary_1 = require("./ErrorBoundary");
7
+ function useActionBus(initialActions, errorListener) {
8
+ const boundaryErrorListener = (0, react_1.useContext)(ErrorBoundary_1.ErrorBoundaryContext);
9
+ const errorListenerRef = (0, react_1.useRef)(errorListener);
10
+ const boundaryErrorListenerRef = (0, react_1.useRef)(boundaryErrorListener);
11
+ const actionBus = (0, react_1.useMemo)(() => {
12
+ const actionBus = (0, actionBus_1.createActionBus)(initialActions);
13
+ if (errorListener) {
14
+ actionBus.addErrorListener(errorListener);
15
+ }
16
+ if (boundaryErrorListener) {
17
+ actionBus.addErrorListener(boundaryErrorListener);
18
+ }
19
+ return actionBus;
20
+ }, []);
21
+ (0, react_1.useEffect)(() => {
22
+ if (errorListenerRef.current !== errorListener) {
23
+ if (errorListenerRef.current) {
24
+ actionBus.removeErrorListener(errorListenerRef.current);
25
+ }
26
+ errorListenerRef.current = errorListener !== null && errorListener !== void 0 ? errorListener : null;
27
+ if (errorListener) {
28
+ actionBus.addErrorListener(errorListener);
29
+ }
30
+ }
31
+ }, [errorListener]);
32
+ (0, react_1.useEffect)(() => {
33
+ if (boundaryErrorListenerRef.current !== boundaryErrorListener) {
34
+ if (boundaryErrorListenerRef.current) {
35
+ actionBus.removeErrorListener(boundaryErrorListenerRef.current);
36
+ }
37
+ boundaryErrorListenerRef.current = boundaryErrorListener !== null && boundaryErrorListener !== void 0 ? boundaryErrorListener : null;
38
+ if (boundaryErrorListener) {
39
+ actionBus.addErrorListener(boundaryErrorListener);
40
+ }
41
+ }
42
+ }, [boundaryErrorListener]);
43
+ return actionBus;
44
+ }
@@ -0,0 +1,23 @@
1
+ import type { ActionResponse, ListenerSignature } from "../action";
2
+ import type { BaseActionsMap } from "../actionBus";
3
+ import type { ErrorListenerSignature, ErrorResponse, Simplify } from "../lib/types";
4
+ export type { ActionResponse, BaseActionsMap, ErrorListenerSignature, ErrorResponse, ListenerSignature, };
5
+ export declare function useActionMap<M extends BaseActionsMap>(actions: M, errorListener?: ErrorListenerSignature<any[]>): Simplify<{ [key in import("../lib/types").KeyOf<M>]: {
6
+ readonly invoke: (...args: Parameters<M[key]>) => Promise<ActionResponse<Awaited<ReturnType<M[key]>>, Parameters<M[key]>>>;
7
+ readonly addListener: (handler: ListenerSignature<M[key]>, listenerOptions?: import("..").ListenerOptions) => void;
8
+ readonly on: (handler: ListenerSignature<M[key]>, listenerOptions?: import("..").ListenerOptions) => void;
9
+ readonly subscribe: (handler: ListenerSignature<M[key]>, listenerOptions?: import("..").ListenerOptions) => void;
10
+ readonly listen: (handler: ListenerSignature<M[key]>, listenerOptions?: import("..").ListenerOptions) => void;
11
+ readonly removeAllListeners: (tag?: string) => void;
12
+ readonly removeListener: (handler: ListenerSignature<M[key]>, context?: object | null, tag?: string | null) => boolean;
13
+ readonly un: (handler: ListenerSignature<M[key]>, context?: object | null, tag?: string | null) => boolean;
14
+ readonly off: (handler: ListenerSignature<M[key]>, context?: object | null, tag?: string | null) => boolean;
15
+ readonly remove: (handler: ListenerSignature<M[key]>, context?: object | null, tag?: string | null) => boolean;
16
+ readonly unsubscribe: (handler: ListenerSignature<M[key]>, context?: object | null, tag?: string | null) => boolean;
17
+ readonly promise: (options?: import("..").ListenerOptions) => Promise<[arg: ActionResponse<Awaited<ReturnType<M[key]>>, Parameters<M[key]>>]>;
18
+ readonly addErrorListener: (handler: ErrorListenerSignature<Parameters<M[key]>>, listenerOptions?: import("..").ListenerOptions) => void;
19
+ readonly removeAllErrorListeners: (tag?: string) => void;
20
+ readonly removeErrorListener: (handler: ErrorListenerSignature<Parameters<M[key]>>, context?: object | null, tag?: string | null) => boolean;
21
+ readonly errorPromise: (options?: import("..").ListenerOptions) => Promise<[errorResponse: ErrorResponse<Parameters<M[key]>>]>;
22
+ __type: import("../action").ActionDefinitionHelper<M[key]>;
23
+ }; }>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useActionMap = useActionMap;
4
+ const react_1 = require("react");
5
+ const actionMap_1 = require("../actionMap");
6
+ const ErrorBoundary_1 = require("./ErrorBoundary");
7
+ function useActionMap(actions, errorListener) {
8
+ const boundaryErrorListener = (0, react_1.useContext)(ErrorBoundary_1.ErrorBoundaryContext);
9
+ const changeRef = (0, react_1.useRef)(0);
10
+ const actionMap = (0, react_1.useMemo)(() => {
11
+ const errorListeners = [
12
+ ...(errorListener ? [errorListener] : []),
13
+ ...(boundaryErrorListener ? [boundaryErrorListener] : []),
14
+ ].filter(l => l !== undefined);
15
+ const actionMap = (0, actionMap_1.createActionMap)(actions, errorListeners);
16
+ return actionMap;
17
+ }, []);
18
+ (0, react_1.useEffect)(() => {
19
+ if (changeRef.current > 0) {
20
+ throw new Error("useActionMap() does not support changing actions or errorListener");
21
+ }
22
+ changeRef.current++;
23
+ }, [actions, errorListener, boundaryErrorListener]);
24
+ return actionMap;
25
+ }
@@ -0,0 +1,47 @@
1
+ import { type EventOptions } from "../event";
2
+ import type { BaseHandler, ErrorListenerSignature, ErrorResponse } from "../lib/types";
3
+ export type { BaseHandler, ErrorListenerSignature, ErrorResponse, EventOptions, };
4
+ export declare function useEvent<Listener extends BaseHandler = BaseHandler, ErrorListener extends ErrorListenerSignature<Parameters<Listener>> = ErrorListenerSignature<Parameters<Listener>>>(eventOptions?: EventOptions<Listener>, listener?: Listener | null, errorListener?: ErrorListener | null): import("../lib/types").ApiType<import("../event").EventDefinitionHelper<Listener>, {
5
+ readonly addListener: (handler: Listener, listenerOptions?: import("../event").ListenerOptions) => void;
6
+ readonly on: (handler: Listener, listenerOptions?: import("../event").ListenerOptions) => void;
7
+ readonly listen: (handler: Listener, listenerOptions?: import("../event").ListenerOptions) => void;
8
+ readonly subscribe: (handler: Listener, listenerOptions?: import("../event").ListenerOptions) => void;
9
+ readonly removeListener: (handler: Listener, context?: object | null, tag?: string | null) => boolean;
10
+ readonly un: (handler: Listener, context?: object | null, tag?: string | null) => boolean;
11
+ readonly off: (handler: Listener, context?: object | null, tag?: string | null) => boolean;
12
+ readonly remove: (handler: Listener, context?: object | null, tag?: string | null) => boolean;
13
+ readonly unsubscribe: (handler: Listener, context?: object | null, tag?: string | null) => boolean;
14
+ readonly trigger: (...args: Parameters<Listener>) => void;
15
+ readonly emit: (...args: Parameters<Listener>) => void;
16
+ readonly dispatch: (...args: Parameters<Listener>) => void;
17
+ readonly hasListener: (handler?: Listener | null | undefined, context?: object | null, tag?: string | null) => boolean;
18
+ readonly has: (handler?: Listener | null | undefined, context?: object | null, tag?: string | null) => boolean;
19
+ readonly removeAllListeners: (tag?: string) => void;
20
+ readonly addErrorListener: (handler: ErrorListenerSignature<Parameters<Listener>>, context?: object | null) => void;
21
+ readonly removeErrorListener: (handler: ErrorListenerSignature<Parameters<Listener>>, context?: object | null) => boolean;
22
+ readonly suspend: (withQueue?: boolean) => void;
23
+ readonly resume: () => void;
24
+ readonly setOptions: (eventOptions: Pick<EventOptions<Listener>, "async" | "limit" | "autoTrigger">) => void;
25
+ readonly reset: () => void;
26
+ readonly isSuspended: () => boolean;
27
+ readonly isQueued: () => boolean;
28
+ readonly withTags: <T extends (...args: any[]) => any>(tags: string[], callback: T) => ReturnType<T>;
29
+ readonly promise: (options?: import("../event").ListenerOptions) => Promise<Parameters<Listener>>;
30
+ readonly first: (...args: Parameters<Listener>) => ReturnType<Listener> | undefined;
31
+ readonly resolveFirst: (...args: Parameters<Listener>) => Promise<Awaited<ReturnType<Listener>> | undefined>;
32
+ readonly all: (...args: Parameters<Listener>) => ReturnType<Listener>[];
33
+ readonly resolveAll: (...args: Parameters<Listener>) => Promise<Awaited<ReturnType<Listener>>[]>;
34
+ readonly last: (...args: Parameters<Listener>) => ReturnType<Listener> | undefined;
35
+ readonly resolveLast: (...args: Parameters<Listener>) => Promise<Awaited<ReturnType<Listener>> | undefined>;
36
+ readonly merge: (...args: Parameters<Listener>) => ReturnType<Listener> | undefined;
37
+ readonly resolveMerge: (...args: Parameters<Listener>) => Promise<Awaited<ReturnType<Listener>> | undefined>;
38
+ readonly concat: (...args: Parameters<Listener>) => (ReturnType<Listener> extends infer T ? T extends ReturnType<Listener> ? T extends (infer U)[] ? U : T : never : never)[];
39
+ readonly resolveConcat: (...args: Parameters<Listener>) => Promise<(Awaited<ReturnType<Listener>> extends infer T ? T extends Awaited<ReturnType<Listener>> ? T extends (infer U)[] ? U : T : never : never)[]>;
40
+ readonly firstNonEmpty: (...args: Parameters<Listener>) => ReturnType<Listener> | undefined;
41
+ readonly resolveFirstNonEmpty: (...args: Parameters<Listener>) => Promise<Awaited<ReturnType<Listener>> | undefined>;
42
+ readonly untilTrue: (...args: Parameters<Listener>) => void;
43
+ readonly untilFalse: (...args: Parameters<Listener>) => void;
44
+ readonly pipe: (...args: Parameters<Listener>) => ReturnType<Listener> | undefined;
45
+ readonly resolvePipe: (...args: Parameters<Listener>) => Promise<Awaited<ReturnType<Listener>> | undefined>;
46
+ readonly raw: (...args: Parameters<Listener>) => (ReturnType<Listener> extends infer T ? T extends ReturnType<Listener> ? T extends (infer U)[] ? U : T : never : never)[];
47
+ }>;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useEvent = useEvent;
4
+ const react_1 = require("react");
5
+ const event_1 = require("../event");
6
+ const ErrorBoundary_1 = require("./ErrorBoundary");
7
+ function useEvent(eventOptions = {}, listener, errorListener) {
8
+ const boundaryErrorListener = (0, react_1.useContext)(ErrorBoundary_1.ErrorBoundaryContext);
9
+ const updateRef = (0, react_1.useRef)(0);
10
+ const listenerRef = (0, react_1.useRef)(listener);
11
+ const errorListenerRef = (0, react_1.useRef)(errorListener);
12
+ const boundaryErrorListenerRef = (0, react_1.useRef)(boundaryErrorListener);
13
+ const event = (0, react_1.useMemo)(() => {
14
+ const event = (0, event_1.createEvent)(eventOptions);
15
+ if (listenerRef.current) {
16
+ event.addListener(listenerRef.current);
17
+ }
18
+ if (errorListenerRef.current) {
19
+ event.addErrorListener(errorListenerRef.current);
20
+ }
21
+ if (boundaryErrorListenerRef.current) {
22
+ event.addErrorListener(boundaryErrorListenerRef.current);
23
+ }
24
+ return event;
25
+ }, []);
26
+ (0, react_1.useEffect)(() => {
27
+ if (updateRef.current > 0) {
28
+ throw new Error("Event cannot be updated");
29
+ }
30
+ updateRef.current++;
31
+ }, [event]);
32
+ (0, react_1.useEffect)(() => {
33
+ if (listenerRef.current !== listener) {
34
+ if (listenerRef.current) {
35
+ event.removeListener(listenerRef.current);
36
+ }
37
+ listenerRef.current = listener !== null && listener !== void 0 ? listener : null;
38
+ if (listener) {
39
+ event.addListener(listener);
40
+ }
41
+ }
42
+ }, [listener]);
43
+ (0, react_1.useEffect)(() => {
44
+ if (errorListenerRef.current !== errorListener) {
45
+ if (errorListenerRef.current) {
46
+ event.removeErrorListener(errorListenerRef.current);
47
+ }
48
+ errorListenerRef.current = errorListener !== null && errorListener !== void 0 ? errorListener : null;
49
+ if (errorListener) {
50
+ event.addErrorListener(errorListener);
51
+ }
52
+ }
53
+ }, [errorListener]);
54
+ (0, react_1.useEffect)(() => {
55
+ if (boundaryErrorListenerRef.current !== boundaryErrorListener) {
56
+ if (boundaryErrorListenerRef.current) {
57
+ event.removeErrorListener(boundaryErrorListenerRef.current);
58
+ }
59
+ boundaryErrorListenerRef.current = boundaryErrorListener !== null && boundaryErrorListener !== void 0 ? boundaryErrorListener : null;
60
+ if (boundaryErrorListener) {
61
+ event.addErrorListener(boundaryErrorListener);
62
+ }
63
+ }
64
+ }, [boundaryErrorListener]);
65
+ return event;
66
+ }
@@ -0,0 +1,117 @@
1
+ import { BaseEventMap, DefaultEventMap, EventBusOptions } from "../eventBus";
2
+ import type { BaseHandler, ErrorListenerSignature, ErrorResponse } from "../lib/types";
3
+ export type { BaseEventMap, BaseHandler, ErrorListenerSignature, ErrorResponse, EventBusOptions, };
4
+ export declare function useEventBus<EventsMap extends BaseEventMap = DefaultEventMap>(eventBusOptions?: EventBusOptions<EventsMap>, allEventsListener?: BaseHandler, errorListener?: ErrorListenerSignature<any[]>): import("../lib/types").ApiType<import("../eventBus").EventBusDefinitionHelper<EventsMap>, {
5
+ readonly addListener: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, H extends import("../eventBus").GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, options?: import("..").ListenerOptions) => void;
6
+ readonly on: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, H extends import("../eventBus").GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, options?: import("..").ListenerOptions) => void;
7
+ readonly listen: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, H extends import("../eventBus").GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, options?: import("..").ListenerOptions) => void;
8
+ readonly subscribe: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, H extends import("../eventBus").GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, options?: import("..").ListenerOptions) => void;
9
+ readonly once: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, H extends import("../eventBus").GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, options?: import("..").ListenerOptions) => void;
10
+ readonly promise: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>>(name: K, options?: import("..").ListenerOptions) => Promise<Parameters<EventsMap[K]>>;
11
+ readonly removeListener: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, H extends import("../eventBus").GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
12
+ readonly un: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, H extends import("../eventBus").GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
13
+ readonly off: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, H extends import("../eventBus").GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
14
+ readonly remove: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, H extends import("../eventBus").GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
15
+ readonly unsubscribe: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, H extends import("../eventBus").GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
16
+ readonly trigger: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => void;
17
+ readonly emit: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => void;
18
+ readonly dispatch: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => void;
19
+ readonly get: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>>(name: K) => { [key in import("../lib/types").KeyOf<EventsMap>]: import("../lib/types").ApiType<import("..").EventDefinitionHelper<EventsMap[key]>, {
20
+ readonly addListener: (handler: EventsMap[key], listenerOptions?: import("..").ListenerOptions) => void;
21
+ readonly on: (handler: EventsMap[key], listenerOptions?: import("..").ListenerOptions) => void;
22
+ readonly listen: (handler: EventsMap[key], listenerOptions?: import("..").ListenerOptions) => void;
23
+ readonly subscribe: (handler: EventsMap[key], listenerOptions?: import("..").ListenerOptions) => void;
24
+ readonly removeListener: (handler: EventsMap[key], context?: object | null, tag?: string | null) => boolean;
25
+ readonly un: (handler: EventsMap[key], context?: object | null, tag?: string | null) => boolean;
26
+ readonly off: (handler: EventsMap[key], context?: object | null, tag?: string | null) => boolean;
27
+ readonly remove: (handler: EventsMap[key], context?: object | null, tag?: string | null) => boolean;
28
+ readonly unsubscribe: (handler: EventsMap[key], context?: object | null, tag?: string | null) => boolean;
29
+ readonly trigger: (...args: Parameters<EventsMap[key]>) => void;
30
+ readonly emit: (...args: Parameters<EventsMap[key]>) => void;
31
+ readonly dispatch: (...args: Parameters<EventsMap[key]>) => void;
32
+ readonly hasListener: (handler?: EventsMap[key] | null | undefined, context?: object | null, tag?: string | null) => boolean;
33
+ readonly has: (handler?: EventsMap[key] | null | undefined, context?: object | null, tag?: string | null) => boolean;
34
+ readonly removeAllListeners: (tag?: string) => void;
35
+ readonly addErrorListener: (handler: ErrorListenerSignature<Parameters<EventsMap[key]>>, context?: object | null) => void;
36
+ readonly removeErrorListener: (handler: ErrorListenerSignature<Parameters<EventsMap[key]>>, context?: object | null) => boolean;
37
+ readonly suspend: (withQueue?: boolean) => void;
38
+ readonly resume: () => void;
39
+ readonly setOptions: (eventOptions: Pick<import("..").EventOptions<EventsMap[key]>, "async" | "limit" | "autoTrigger">) => void;
40
+ readonly reset: () => void;
41
+ readonly isSuspended: () => boolean;
42
+ readonly isQueued: () => boolean;
43
+ readonly withTags: <T extends (...args: any[]) => any>(tags: string[], callback: T) => ReturnType<T>;
44
+ readonly promise: (options?: import("..").ListenerOptions) => Promise<Parameters<EventsMap[key]>>;
45
+ readonly first: (...args: Parameters<EventsMap[key]>) => ReturnType<EventsMap[key]> | undefined;
46
+ readonly resolveFirst: (...args: Parameters<EventsMap[key]>) => Promise<Awaited<ReturnType<EventsMap[key]>> | undefined>;
47
+ readonly all: (...args: Parameters<EventsMap[key]>) => ReturnType<EventsMap[key]>[];
48
+ readonly resolveAll: (...args: Parameters<EventsMap[key]>) => Promise<Awaited<ReturnType<EventsMap[key]>>[]>;
49
+ readonly last: (...args: Parameters<EventsMap[key]>) => ReturnType<EventsMap[key]> | undefined;
50
+ readonly resolveLast: (...args: Parameters<EventsMap[key]>) => Promise<Awaited<ReturnType<EventsMap[key]>> | undefined>;
51
+ readonly merge: (...args: Parameters<EventsMap[key]>) => ReturnType<EventsMap[key]> | undefined;
52
+ readonly resolveMerge: (...args: Parameters<EventsMap[key]>) => Promise<Awaited<ReturnType<EventsMap[key]>> | undefined>;
53
+ readonly concat: (...args: Parameters<EventsMap[key]>) => (ReturnType<EventsMap[key]> extends infer T ? T extends ReturnType<EventsMap[key]> ? T extends (infer U)[] ? U : T : never : never)[];
54
+ readonly resolveConcat: (...args: Parameters<EventsMap[key]>) => Promise<(Awaited<ReturnType<EventsMap[key]>> extends infer T ? T extends Awaited<ReturnType<EventsMap[key]>> ? T extends (infer U)[] ? U : T : never : never)[]>;
55
+ readonly firstNonEmpty: (...args: Parameters<EventsMap[key]>) => ReturnType<EventsMap[key]> | undefined;
56
+ readonly resolveFirstNonEmpty: (...args: Parameters<EventsMap[key]>) => Promise<Awaited<ReturnType<EventsMap[key]>> | undefined>;
57
+ readonly untilTrue: (...args: Parameters<EventsMap[key]>) => void;
58
+ readonly untilFalse: (...args: Parameters<EventsMap[key]>) => void;
59
+ readonly pipe: (...args: Parameters<EventsMap[key]>) => ReturnType<EventsMap[key]> | undefined;
60
+ readonly resolvePipe: (...args: Parameters<EventsMap[key]>) => Promise<Awaited<ReturnType<EventsMap[key]>> | undefined>;
61
+ readonly raw: (...args: Parameters<EventsMap[key]>) => (ReturnType<EventsMap[key]> extends infer T ? T extends ReturnType<EventsMap[key]> ? T extends (infer U)[] ? U : T : never : never)[];
62
+ }>; }[K];
63
+ readonly add: (name: import("../lib/types").MapKey, options?: import("..").EventOptions<BaseHandler>) => void;
64
+ readonly first: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => ReturnType<EventsMap[K]> | undefined;
65
+ readonly resolveFirst: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => Promise<Awaited<ReturnType<EventsMap[K]>> | undefined>;
66
+ readonly all: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => ReturnType<EventsMap[K]>[];
67
+ readonly resolveAll: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => Promise<Awaited<ReturnType<EventsMap[K]>>[]>;
68
+ readonly last: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => ReturnType<EventsMap[K]> | undefined;
69
+ readonly resolveLast: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => Promise<Awaited<ReturnType<EventsMap[K]>> | undefined>;
70
+ readonly merge: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => ReturnType<EventsMap[K]> | undefined;
71
+ readonly resolveMerge: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => Promise<Awaited<ReturnType<EventsMap[K]>> | undefined>;
72
+ readonly concat: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => (ReturnType<EventsMap[K]> extends infer T ? T extends ReturnType<EventsMap[K]> ? T extends (infer U)[] ? U : T : never : never)[];
73
+ readonly resolveConcat: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => Promise<(Awaited<ReturnType<EventsMap[K]>> extends infer T ? T extends Awaited<ReturnType<EventsMap[K]>> ? T extends (infer U)[] ? U : T : never : never)[]>;
74
+ readonly firstNonEmpty: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => ReturnType<EventsMap[K]> | undefined;
75
+ readonly resolveFirstNonEmpty: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => Promise<Awaited<ReturnType<EventsMap[K]>> | undefined>;
76
+ readonly untilTrue: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => ReturnType<(...args: Parameters<EventsMap[K]>) => void>;
77
+ readonly untilFalse: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => ReturnType<(...args: Parameters<EventsMap[K]>) => void>;
78
+ readonly pipe: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => ReturnType<EventsMap[K]> | undefined;
79
+ readonly resolvePipe: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => Promise<Awaited<ReturnType<EventsMap[K]>> | undefined>;
80
+ readonly raw: <K extends import("../lib/types").KeyOf<import("../eventBus").GetEventsMap<EventsMap>>, A extends import("../eventBus").GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => (ReturnType<EventsMap[K]> extends infer T ? T extends ReturnType<EventsMap[K]> ? T extends (infer U)[] ? U : T : never : never)[];
81
+ readonly withTags: <T extends (...args: any[]) => any>(tags: string[], callback: T) => ReturnType<T>;
82
+ readonly intercept: (fn: (name: import("../lib/types").MapKey, args: any[], tags: string[] | null, returnType: import("../lib/types").TriggerReturnType | null) => boolean) => void;
83
+ readonly stopIntercepting: () => void;
84
+ readonly reset: () => void;
85
+ readonly suspendAll: (withQueue?: boolean) => void;
86
+ readonly resumeAll: () => void;
87
+ readonly relay: ({ eventSource, remoteEventName, localEventName, proxyType, localEventNamePrefix, }: {
88
+ eventSource: {
89
+ on: (name: any, fn: (...args: any[]) => any, options?: import("..").ListenerOptions) => void;
90
+ addAllEventsListener: (fn: (name: any, args: any[]) => any, options?: import("..").ListenerOptions) => void;
91
+ un: (name: any, fn: (...args: any[]) => any, context?: object | null, tag?: string | null) => void;
92
+ removeAllEventsListener: (fn: (name: any, args: any[]) => any, context?: object | null, tag?: string | null) => void;
93
+ };
94
+ remoteEventName: import("../lib/types").MapKey;
95
+ localEventName?: any;
96
+ proxyType?: import("../lib/types").ProxyType;
97
+ localEventNamePrefix?: string | null;
98
+ }) => void;
99
+ readonly unrelay: ({ eventSource, remoteEventName, localEventName, proxyType, localEventNamePrefix, }: {
100
+ eventSource: {
101
+ on: (name: any, fn: (...args: any[]) => any, options?: import("..").ListenerOptions) => void;
102
+ addAllEventsListener: (fn: (name: any, args: any[]) => any, options?: import("..").ListenerOptions) => void;
103
+ un: (name: any, fn: (...args: any[]) => any, context?: object | null, tag?: string | null) => void;
104
+ removeAllEventsListener: (fn: (name: any, args: any[]) => any, context?: object | null, tag?: string | null) => void;
105
+ };
106
+ remoteEventName: import("../lib/types").MapKey;
107
+ localEventName?: any;
108
+ proxyType?: import("../lib/types").ProxyType;
109
+ localEventNamePrefix?: string | null;
110
+ }) => void;
111
+ readonly addEventSource: (eventSource: import("../eventBus").EventSource) => void;
112
+ readonly removeEventSource: (eventSource: import("../eventBus").EventSource | import("../lib/types").MapKey) => void;
113
+ readonly addAllEventsListener: (handler: (name: import("../lib/types").MapKey, args: any[], tags: string[] | null) => void, listenerOptions?: import("..").ListenerOptions) => void;
114
+ readonly removeAllEventsListener: (handler: (name: import("../lib/types").MapKey, args: any[], tags: string[] | null) => void, context?: object | null, tag?: string | null) => boolean;
115
+ readonly addErrorListener: (handler: ErrorListenerSignature<any[]>, listenerOptions?: import("..").ListenerOptions) => void;
116
+ readonly removeErrorListener: (handler: ErrorListenerSignature<any[]>, context?: object | null, tag?: string | null) => boolean;
117
+ }>;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useEventBus = useEventBus;
4
+ const react_1 = require("react");
5
+ const eventBus_1 = require("../eventBus");
6
+ const ErrorBoundary_1 = require("./ErrorBoundary");
7
+ function useEventBus(eventBusOptions, allEventsListener, errorListener) {
8
+ const boundaryErrorListener = (0, react_1.useContext)(ErrorBoundary_1.ErrorBoundaryContext);
9
+ const updateRef = (0, react_1.useRef)(0);
10
+ const errorListenerRef = (0, react_1.useRef)(errorListener);
11
+ const allEventsListenerRef = (0, react_1.useRef)(allEventsListener);
12
+ const boundaryErrorListenerRef = (0, react_1.useRef)(boundaryErrorListener);
13
+ const eventBus = (0, react_1.useMemo)(() => {
14
+ const eventBus = (0, eventBus_1.createEventBus)(eventBusOptions);
15
+ if (allEventsListener) {
16
+ eventBus.addAllEventsListener(allEventsListener);
17
+ }
18
+ if (errorListener) {
19
+ eventBus.addErrorListener(errorListener);
20
+ }
21
+ if (boundaryErrorListener) {
22
+ eventBus.addErrorListener(boundaryErrorListener);
23
+ }
24
+ return eventBus;
25
+ }, []);
26
+ (0, react_1.useEffect)(() => {
27
+ if (updateRef.current > 0) {
28
+ throw new Error("EventBus options can't be updated");
29
+ }
30
+ updateRef.current++;
31
+ }, [eventBusOptions]);
32
+ (0, react_1.useEffect)(() => {
33
+ if (allEventsListenerRef.current !== allEventsListener) {
34
+ if (allEventsListenerRef.current) {
35
+ eventBus.removeAllEventsListener(allEventsListenerRef.current);
36
+ }
37
+ allEventsListenerRef.current = allEventsListener;
38
+ if (allEventsListener) {
39
+ eventBus.addAllEventsListener(allEventsListener);
40
+ }
41
+ }
42
+ }, [allEventsListener]);
43
+ (0, react_1.useEffect)(() => {
44
+ if (errorListenerRef.current !== errorListener) {
45
+ if (errorListenerRef.current) {
46
+ eventBus.removeErrorListener(errorListenerRef.current);
47
+ }
48
+ errorListenerRef.current = errorListener;
49
+ if (errorListener) {
50
+ eventBus.addErrorListener(errorListener);
51
+ }
52
+ }
53
+ }, [errorListener]);
54
+ (0, react_1.useEffect)(() => {
55
+ if (boundaryErrorListenerRef.current !== boundaryErrorListener) {
56
+ if (boundaryErrorListenerRef.current) {
57
+ eventBus.removeErrorListener(boundaryErrorListenerRef.current);
58
+ }
59
+ boundaryErrorListenerRef.current = boundaryErrorListener;
60
+ if (boundaryErrorListener) {
61
+ eventBus.addErrorListener(boundaryErrorListener);
62
+ }
63
+ }
64
+ }, [boundaryErrorListener]);
65
+ return eventBus;
66
+ }
@@ -0,0 +1,3 @@
1
+ import type { BaseAction } from "../action";
2
+ export type { BaseAction };
3
+ export declare function useListenToAction<TAction extends BaseAction, TListenerSignature extends TAction["__type"]["listenerSignature"] = TAction["__type"]["listenerSignature"], TErrorListenerSignature extends TAction["__type"]["errorListenerSignature"] = TAction["__type"]["errorListenerSignature"]>(action: TAction, listener: TListenerSignature | null, errorListener?: TErrorListenerSignature): void;