@kuindji/reactive 1.0.14 → 1.0.16

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/dist/action.d.ts CHANGED
@@ -9,6 +9,7 @@ export type ActionResponse<Response extends any = any, Args extends any[] = any[
9
9
  args: Args;
10
10
  };
11
11
  export type ListenerSignature<ActionSignature extends BaseHandler> = (arg: ActionResponse<Awaited<ReturnType<ActionSignature>>, Parameters<ActionSignature>>) => void;
12
+ export type BeforeActionSignature<ActionSignature extends BaseHandler> = (...args: Parameters<ActionSignature>) => false | void | Promise<false | void>;
12
13
  export type ActionDefinitionHelper<A extends BaseHandler> = {
13
14
  actionSignature: A;
14
15
  actionArguments: Parameters<A>;
@@ -17,6 +18,7 @@ export type ActionDefinitionHelper<A extends BaseHandler> = {
17
18
  errorResponseType: ErrorResponse<Parameters<A>>;
18
19
  listenerArgument: ActionResponse<Awaited<ReturnType<A>>, Parameters<A>>;
19
20
  listenerSignature: ListenerSignature<A>;
21
+ beforeActionSignature: BeforeActionSignature<A>;
20
22
  errorListenerArgument: ErrorResponse<Parameters<A>>;
21
23
  errorListenerSignature: ErrorListenerSignature<Parameters<A>>;
22
24
  };
@@ -44,6 +46,10 @@ export declare function createAction<A extends BaseHandler>(action: A): ApiType<
44
46
  readonly removeAllErrorListeners: (tag?: string) => void;
45
47
  readonly removeErrorListener: (handler: ErrorListenerSignature<Parameters<A>>, context?: object | null, tag?: string | null) => boolean;
46
48
  readonly errorPromise: (options?: import("./event").ListenerOptions) => Promise<[errorResponse: ErrorResponse<Parameters<A>>]>;
49
+ readonly addBeforeActionListener: (handler: BeforeActionSignature<A>, listenerOptions?: import("./event").ListenerOptions) => void;
50
+ readonly removeAllBeforeActionListeners: (tag?: string) => void;
51
+ readonly removeBeforeActionListener: (handler: BeforeActionSignature<A>, context?: object | null, tag?: string | null) => boolean;
52
+ readonly beforeActionPromise: (options?: import("./event").ListenerOptions) => Promise<Parameters<A>>;
47
53
  }>;
48
54
  export type BaseActionDefinition = ActionDefinitionHelper<(...args: [any]) => any>;
49
55
  export type BaseAction = ReturnType<typeof createAction<(...args: [any]) => any>>;
package/dist/action.js CHANGED
@@ -13,9 +13,25 @@ exports.createAction = createAction;
13
13
  const event_1 = require("./event");
14
14
  function createAction(action) {
15
15
  const { trigger, addListener, removeAllListeners, removeListener, promise, } = (0, event_1.createEvent)();
16
+ const { all: triggerBeforeAction, addListener: addBeforeActionListener, removeAllListeners: removeAllBeforeActionListeners, removeListener: removeBeforeActionListener, promise: beforeActionPromise, } = (0, event_1.createEvent)();
16
17
  const { trigger: triggerError, addListener: addErrorListener, removeAllListeners: removeAllErrorListeners, removeListener: removeErrorListener, promise: errorPromise, hasListener: hasErrorListeners, } = (0, event_1.createEvent)();
17
18
  const invoke = (...args) => __awaiter(this, void 0, void 0, function* () {
18
19
  try {
20
+ const beforeResults = triggerBeforeAction(...args);
21
+ for (let before of beforeResults) {
22
+ if (before instanceof Promise) {
23
+ before = yield before;
24
+ }
25
+ if (before === false) {
26
+ const response = {
27
+ response: null,
28
+ error: "Action cancelled",
29
+ args: args,
30
+ };
31
+ trigger(response);
32
+ return response;
33
+ }
34
+ }
19
35
  let result = action(...args);
20
36
  if (result instanceof Promise) {
21
37
  result = yield result;
@@ -72,6 +88,10 @@ function createAction(action) {
72
88
  removeAllErrorListeners,
73
89
  removeErrorListener,
74
90
  errorPromise,
91
+ addBeforeActionListener,
92
+ removeAllBeforeActionListeners,
93
+ removeBeforeActionListener,
94
+ beforeActionPromise,
75
95
  };
76
96
  return api;
77
97
  }
@@ -19,5 +19,9 @@ export declare function createActionMap<M extends BaseActionsMap>(actions: M, on
19
19
  readonly removeAllErrorListeners: (tag?: string) => void;
20
20
  readonly removeErrorListener: (handler: ErrorListenerSignature<Parameters<M[key]>>, context?: object | null, tag?: string | null) => boolean;
21
21
  readonly errorPromise: (options?: import("./event").ListenerOptions) => Promise<[errorResponse: ErrorResponse<Parameters<M[key]>>]>;
22
+ readonly addBeforeActionListener: (handler: import("./action").BeforeActionSignature<M[key]>, listenerOptions?: import("./event").ListenerOptions) => void;
23
+ readonly removeAllBeforeActionListeners: (tag?: string) => void;
24
+ readonly removeBeforeActionListener: (handler: import("./action").BeforeActionSignature<M[key]>, context?: object | null, tag?: string | null) => boolean;
25
+ readonly beforeActionPromise: (options?: import("./event").ListenerOptions) => Promise<Parameters<M[key]>>;
22
26
  __type: import("./action").ActionDefinitionHelper<M[key]>;
23
27
  }; };
@@ -1,5 +1,5 @@
1
1
  import { createAction } from "../action";
2
- import type { ActionResponse, ListenerSignature } from "../action";
2
+ import type { ActionResponse, BeforeActionSignature, ListenerSignature } from "../action";
3
3
  import type { BaseHandler, ErrorListenerSignature, ErrorResponse } from "../lib/types";
4
4
  export type { ActionResponse, BaseHandler, ErrorListenerSignature, ErrorResponse, ListenerSignature, };
5
- export declare function useAction<ActionSignature extends BaseHandler, Listener extends ListenerSignature<ActionSignature>, ErrorListener extends ErrorListenerSignature<Parameters<ActionSignature>>>(actionSignature: ActionSignature, listener?: Listener | null, errorListener?: ErrorListener | null): ReturnType<typeof createAction<ActionSignature>>;
5
+ export declare function useAction<ActionSignature extends BaseHandler, Listener extends ListenerSignature<ActionSignature>, ErrorListener extends ErrorListenerSignature<Parameters<ActionSignature>>, BeforeActionListener extends BeforeActionSignature<ActionSignature>>(actionSignature: ActionSignature, listener?: Listener | null, errorListener?: ErrorListener | null, beforeActionListener?: BeforeActionListener | null): ReturnType<typeof createAction<ActionSignature>>;
@@ -4,12 +4,13 @@ exports.useAction = useAction;
4
4
  const react_1 = require("react");
5
5
  const action_1 = require("../action");
6
6
  const ErrorBoundary_1 = require("./ErrorBoundary");
7
- function useAction(actionSignature, listener, errorListener) {
7
+ function useAction(actionSignature, listener, errorListener, beforeActionListener) {
8
8
  const boundaryErrorListener = (0, react_1.useContext)(ErrorBoundary_1.ErrorBoundaryContext);
9
9
  const updateRef = (0, react_1.useRef)(0);
10
10
  const listenerRef = (0, react_1.useRef)(listener);
11
11
  const errorListenerRef = (0, react_1.useRef)(errorListener);
12
12
  const boundaryErrorListenerRef = (0, react_1.useRef)(boundaryErrorListener);
13
+ const beforeActionListenerRef = (0, react_1.useRef)(beforeActionListener);
13
14
  const action = (0, react_1.useMemo)(() => {
14
15
  const action = (0, action_1.createAction)(actionSignature);
15
16
  if (listenerRef.current) {
@@ -21,6 +22,9 @@ function useAction(actionSignature, listener, errorListener) {
21
22
  if (boundaryErrorListenerRef.current) {
22
23
  action.addErrorListener(boundaryErrorListenerRef.current);
23
24
  }
25
+ if (beforeActionListenerRef.current) {
26
+ action.addBeforeActionListener(beforeActionListenerRef.current);
27
+ }
24
28
  return action;
25
29
  }, []);
26
30
  (0, react_1.useEffect)(() => {
@@ -62,5 +66,36 @@ function useAction(actionSignature, listener, errorListener) {
62
66
  }
63
67
  }
64
68
  }, [boundaryErrorListener]);
69
+ (0, react_1.useEffect)(() => {
70
+ if (beforeActionListenerRef.current !== beforeActionListener) {
71
+ if (beforeActionListenerRef.current) {
72
+ action.removeBeforeActionListener(beforeActionListenerRef.current);
73
+ }
74
+ beforeActionListenerRef.current = beforeActionListener !== null && beforeActionListener !== void 0 ? beforeActionListener : null;
75
+ if (beforeActionListener) {
76
+ action.addBeforeActionListener(beforeActionListener);
77
+ }
78
+ }
79
+ }, [beforeActionListener]);
80
+ (0, react_1.useEffect)(() => {
81
+ return () => {
82
+ if (listenerRef.current) {
83
+ listenerRef.current = null;
84
+ }
85
+ if (errorListenerRef.current) {
86
+ errorListenerRef.current = null;
87
+ }
88
+ if (boundaryErrorListenerRef.current) {
89
+ boundaryErrorListenerRef.current = null;
90
+ }
91
+ if (beforeActionListenerRef.current) {
92
+ beforeActionListenerRef.current = null;
93
+ }
94
+ action.removeAllListeners();
95
+ action.removeAllBeforeActionListeners();
96
+ action.removeAllErrorListeners();
97
+ updateRef.current = 0;
98
+ };
99
+ }, []);
65
100
  return action;
66
101
  }
@@ -6,7 +6,6 @@ const event_1 = require("../event");
6
6
  const ErrorBoundary_1 = require("./ErrorBoundary");
7
7
  function useEvent(eventOptions = {}, listener, errorListener) {
8
8
  const boundaryErrorListener = (0, react_1.useContext)(ErrorBoundary_1.ErrorBoundaryContext);
9
- const updateRef = (0, react_1.useRef)(0);
10
9
  const listenerRef = (0, react_1.useRef)(listener);
11
10
  const errorListenerRef = (0, react_1.useRef)(errorListener);
12
11
  const boundaryErrorListenerRef = (0, react_1.useRef)(boundaryErrorListener);
@@ -23,12 +22,6 @@ function useEvent(eventOptions = {}, listener, errorListener) {
23
22
  }
24
23
  return event;
25
24
  }, []);
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
25
  (0, react_1.useEffect)(() => {
33
26
  if (listenerRef.current !== listener) {
34
27
  if (listenerRef.current) {
@@ -62,5 +55,21 @@ function useEvent(eventOptions = {}, listener, errorListener) {
62
55
  }
63
56
  }
64
57
  }, [boundaryErrorListener]);
58
+ (0, react_1.useEffect)(() => {
59
+ return () => {
60
+ if (listenerRef.current) {
61
+ event.removeListener(listenerRef.current);
62
+ listenerRef.current = null;
63
+ }
64
+ if (errorListenerRef.current) {
65
+ event.removeErrorListener(errorListenerRef.current);
66
+ errorListenerRef.current = null;
67
+ }
68
+ if (boundaryErrorListenerRef.current) {
69
+ event.removeErrorListener(boundaryErrorListenerRef.current);
70
+ boundaryErrorListenerRef.current = null;
71
+ }
72
+ };
73
+ }, []);
65
74
  return event;
66
75
  }
@@ -7,9 +7,9 @@ const ErrorBoundary_1 = require("./ErrorBoundary");
7
7
  function useEventBus(eventBusOptions, allEventsListener, errorListener) {
8
8
  const boundaryErrorListener = (0, react_1.useContext)(ErrorBoundary_1.ErrorBoundaryContext);
9
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);
10
+ const errorListenerRef = (0, react_1.useRef)(errorListener || null);
11
+ const allEventsListenerRef = (0, react_1.useRef)(allEventsListener || null);
12
+ const boundaryErrorListenerRef = (0, react_1.useRef)(boundaryErrorListener || null);
13
13
  const eventBus = (0, react_1.useMemo)(() => {
14
14
  const eventBus = (0, eventBus_1.createEventBus)(eventBusOptions);
15
15
  if (allEventsListener) {
@@ -36,7 +36,7 @@ function useEventBus(eventBusOptions, allEventsListener, errorListener) {
36
36
  if (allEventsListenerRef.current) {
37
37
  eventBus.removeAllEventsListener(allEventsListenerRef.current);
38
38
  }
39
- allEventsListenerRef.current = allEventsListener;
39
+ allEventsListenerRef.current = allEventsListener || null;
40
40
  if (allEventsListener) {
41
41
  eventBus.addAllEventsListener(allEventsListener);
42
42
  }
@@ -47,7 +47,7 @@ function useEventBus(eventBusOptions, allEventsListener, errorListener) {
47
47
  if (errorListenerRef.current) {
48
48
  eventBus.removeErrorListener(errorListenerRef.current);
49
49
  }
50
- errorListenerRef.current = errorListener;
50
+ errorListenerRef.current = errorListener || null;
51
51
  if (errorListener) {
52
52
  eventBus.addErrorListener(errorListener);
53
53
  }
@@ -58,11 +58,29 @@ function useEventBus(eventBusOptions, allEventsListener, errorListener) {
58
58
  if (boundaryErrorListenerRef.current) {
59
59
  eventBus.removeErrorListener(boundaryErrorListenerRef.current);
60
60
  }
61
- boundaryErrorListenerRef.current = boundaryErrorListener;
61
+ boundaryErrorListenerRef.current = boundaryErrorListener
62
+ || null;
62
63
  if (boundaryErrorListener) {
63
64
  eventBus.addErrorListener(boundaryErrorListener);
64
65
  }
65
66
  }
66
67
  }, [boundaryErrorListener]);
68
+ (0, react_1.useEffect)(() => {
69
+ return () => {
70
+ if (allEventsListenerRef.current) {
71
+ eventBus.removeAllEventsListener(allEventsListenerRef.current);
72
+ allEventsListenerRef.current = null;
73
+ }
74
+ if (errorListenerRef.current) {
75
+ eventBus.removeErrorListener(errorListenerRef.current);
76
+ errorListenerRef.current = null;
77
+ }
78
+ if (boundaryErrorListenerRef.current) {
79
+ eventBus.removeErrorListener(boundaryErrorListenerRef.current);
80
+ boundaryErrorListenerRef.current = null;
81
+ }
82
+ updateRef.current = 0;
83
+ };
84
+ }, []);
67
85
  return eventBus;
68
86
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kuindji/reactive",
3
- "version": "1.0.14",
3
+ "version": "1.0.16",
4
4
  "author": "Ivan Kuindzhi",
5
5
  "repository": {
6
6
  "type": "git",