@kuindji/reactive 1.0.21 → 1.0.24

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.
@@ -1,18 +1,15 @@
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, beforeActionListener) {
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 beforeActionListenerRef = (0, react_1.useRef)(beforeActionListener);
14
- const action = (0, react_1.useMemo)(() => {
15
- const action = (0, action_1.createAction)(actionSignature);
1
+ import { useContext, useEffect, useMemo, useRef } from "react";
2
+ import { createAction } from "../action";
3
+ import { ErrorBoundaryContext } from "./ErrorBoundary";
4
+ export function useAction(actionSignature, listener, errorListener, beforeActionListener) {
5
+ const boundaryErrorListener = useContext(ErrorBoundaryContext);
6
+ const updateRef = useRef(0);
7
+ const listenerRef = useRef(listener);
8
+ const errorListenerRef = useRef(errorListener);
9
+ const boundaryErrorListenerRef = useRef(boundaryErrorListener);
10
+ const beforeActionListenerRef = useRef(beforeActionListener);
11
+ const action = useMemo(() => {
12
+ const action = createAction(actionSignature);
16
13
  if (listenerRef.current) {
17
14
  action.addListener(listenerRef.current);
18
15
  }
@@ -27,13 +24,13 @@ function useAction(actionSignature, listener, errorListener, beforeActionListene
27
24
  }
28
25
  return action;
29
26
  }, []);
30
- (0, react_1.useEffect)(() => {
27
+ useEffect(() => {
31
28
  if (updateRef.current > 0) {
32
29
  throw new Error("Action cannot be updated");
33
30
  }
34
31
  updateRef.current++;
35
32
  }, [actionSignature]);
36
- (0, react_1.useEffect)(() => {
33
+ useEffect(() => {
37
34
  if (listenerRef.current !== listener) {
38
35
  if (listenerRef.current) {
39
36
  action.removeListener(listenerRef.current);
@@ -44,7 +41,7 @@ function useAction(actionSignature, listener, errorListener, beforeActionListene
44
41
  }
45
42
  }
46
43
  }, [listener]);
47
- (0, react_1.useEffect)(() => {
44
+ useEffect(() => {
48
45
  if (errorListenerRef.current !== errorListener) {
49
46
  if (errorListenerRef.current) {
50
47
  action.removeErrorListener(errorListenerRef.current);
@@ -55,7 +52,7 @@ function useAction(actionSignature, listener, errorListener, beforeActionListene
55
52
  }
56
53
  }
57
54
  }, [errorListener]);
58
- (0, react_1.useEffect)(() => {
55
+ useEffect(() => {
59
56
  if (boundaryErrorListenerRef.current !== boundaryErrorListener) {
60
57
  if (boundaryErrorListenerRef.current) {
61
58
  action.removeErrorListener(boundaryErrorListenerRef.current);
@@ -66,7 +63,7 @@ function useAction(actionSignature, listener, errorListener, beforeActionListene
66
63
  }
67
64
  }
68
65
  }, [boundaryErrorListener]);
69
- (0, react_1.useEffect)(() => {
66
+ useEffect(() => {
70
67
  if (beforeActionListenerRef.current !== beforeActionListener) {
71
68
  if (beforeActionListenerRef.current) {
72
69
  action.removeBeforeActionListener(beforeActionListenerRef.current);
@@ -77,7 +74,7 @@ function useAction(actionSignature, listener, errorListener, beforeActionListene
77
74
  }
78
75
  }
79
76
  }, [beforeActionListener]);
80
- (0, react_1.useEffect)(() => {
77
+ useEffect(() => {
81
78
  return () => {
82
79
  if (listenerRef.current) {
83
80
  listenerRef.current = null;
@@ -1,15 +1,12 @@
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);
1
+ import { useContext, useEffect, useMemo, useRef } from "react";
2
+ import { createActionBus } from "../actionBus";
3
+ import { ErrorBoundaryContext } from "./ErrorBoundary";
4
+ export function useActionBus(initialActions, errorListener) {
5
+ const boundaryErrorListener = useContext(ErrorBoundaryContext);
6
+ const errorListenerRef = useRef(errorListener);
7
+ const boundaryErrorListenerRef = useRef(boundaryErrorListener);
8
+ const actionBus = useMemo(() => {
9
+ const actionBus = createActionBus(initialActions);
13
10
  if (errorListener) {
14
11
  actionBus.addErrorListener(errorListener);
15
12
  }
@@ -18,7 +15,7 @@ function useActionBus(initialActions, errorListener) {
18
15
  }
19
16
  return actionBus;
20
17
  }, []);
21
- (0, react_1.useEffect)(() => {
18
+ useEffect(() => {
22
19
  if (errorListenerRef.current !== errorListener) {
23
20
  if (errorListenerRef.current) {
24
21
  actionBus.removeErrorListener(errorListenerRef.current);
@@ -29,7 +26,7 @@ function useActionBus(initialActions, errorListener) {
29
26
  }
30
27
  }
31
28
  }, [errorListener]);
32
- (0, react_1.useEffect)(() => {
29
+ useEffect(() => {
33
30
  if (boundaryErrorListenerRef.current !== boundaryErrorListener) {
34
31
  if (boundaryErrorListenerRef.current) {
35
32
  actionBus.removeErrorListener(boundaryErrorListenerRef.current);
@@ -1,21 +1,18 @@
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)(() => {
1
+ import { useContext, useEffect, useMemo, useRef } from "react";
2
+ import { createActionMap } from "../actionMap";
3
+ import { ErrorBoundaryContext } from "./ErrorBoundary";
4
+ export function useActionMap(actions, errorListener) {
5
+ const boundaryErrorListener = useContext(ErrorBoundaryContext);
6
+ const changeRef = useRef(0);
7
+ const actionMap = useMemo(() => {
11
8
  const errorListeners = [
12
9
  ...(errorListener ? [errorListener] : []),
13
10
  ...(boundaryErrorListener ? [boundaryErrorListener] : []),
14
11
  ].filter(l => l !== undefined);
15
- const actionMap = (0, actionMap_1.createActionMap)(actions, errorListeners);
12
+ const actionMap = createActionMap(actions, errorListeners);
16
13
  return actionMap;
17
14
  }, []);
18
- (0, react_1.useEffect)(() => {
15
+ useEffect(() => {
19
16
  if (changeRef.current > 0) {
20
17
  throw new Error("useActionMap() does not support changing actions or errorListener");
21
18
  }
@@ -1,16 +1,13 @@
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 listenerRef = (0, react_1.useRef)(listener);
10
- const errorListenerRef = (0, react_1.useRef)(errorListener);
11
- const boundaryErrorListenerRef = (0, react_1.useRef)(boundaryErrorListener);
12
- const event = (0, react_1.useMemo)(() => {
13
- const event = (0, event_1.createEvent)(eventOptions);
1
+ import { useContext, useEffect, useMemo, useRef } from "react";
2
+ import { createEvent } from "../event";
3
+ import { ErrorBoundaryContext } from "./ErrorBoundary";
4
+ export function useEvent(eventOptions = {}, listener, errorListener) {
5
+ const boundaryErrorListener = useContext(ErrorBoundaryContext);
6
+ const listenerRef = useRef(listener);
7
+ const errorListenerRef = useRef(errorListener);
8
+ const boundaryErrorListenerRef = useRef(boundaryErrorListener);
9
+ const event = useMemo(() => {
10
+ const event = createEvent(eventOptions);
14
11
  if (listenerRef.current) {
15
12
  event.addListener(listenerRef.current);
16
13
  }
@@ -22,7 +19,7 @@ function useEvent(eventOptions = {}, listener, errorListener) {
22
19
  }
23
20
  return event;
24
21
  }, []);
25
- (0, react_1.useEffect)(() => {
22
+ useEffect(() => {
26
23
  if (listenerRef.current !== listener) {
27
24
  if (listenerRef.current) {
28
25
  event.removeListener(listenerRef.current);
@@ -33,7 +30,7 @@ function useEvent(eventOptions = {}, listener, errorListener) {
33
30
  }
34
31
  }
35
32
  }, [listener]);
36
- (0, react_1.useEffect)(() => {
33
+ useEffect(() => {
37
34
  if (errorListenerRef.current !== errorListener) {
38
35
  if (errorListenerRef.current) {
39
36
  event.removeErrorListener(errorListenerRef.current);
@@ -44,7 +41,7 @@ function useEvent(eventOptions = {}, listener, errorListener) {
44
41
  }
45
42
  }
46
43
  }, [errorListener]);
47
- (0, react_1.useEffect)(() => {
44
+ useEffect(() => {
48
45
  if (boundaryErrorListenerRef.current !== boundaryErrorListener) {
49
46
  if (boundaryErrorListenerRef.current) {
50
47
  event.removeErrorListener(boundaryErrorListenerRef.current);
@@ -55,7 +52,7 @@ function useEvent(eventOptions = {}, listener, errorListener) {
55
52
  }
56
53
  }
57
54
  }, [boundaryErrorListener]);
58
- (0, react_1.useEffect)(() => {
55
+ useEffect(() => {
59
56
  return () => {
60
57
  if (listenerRef.current) {
61
58
  event.removeListener(listenerRef.current);
@@ -1,17 +1,14 @@
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 || null);
11
- const allEventsListenerRef = (0, react_1.useRef)(allEventsListener || null);
12
- const boundaryErrorListenerRef = (0, react_1.useRef)(boundaryErrorListener || null);
13
- const eventBus = (0, react_1.useMemo)(() => {
14
- const eventBus = (0, eventBus_1.createEventBus)(eventBusOptions);
1
+ import { useContext, useEffect, useMemo, useRef } from "react";
2
+ import { createEventBus, } from "../eventBus";
3
+ import { ErrorBoundaryContext } from "./ErrorBoundary";
4
+ export function useEventBus(eventBusOptions, allEventsListener, errorListener) {
5
+ const boundaryErrorListener = useContext(ErrorBoundaryContext);
6
+ const updateRef = useRef(0);
7
+ const errorListenerRef = useRef(errorListener || null);
8
+ const allEventsListenerRef = useRef(allEventsListener || null);
9
+ const boundaryErrorListenerRef = useRef(boundaryErrorListener || null);
10
+ const eventBus = useMemo(() => {
11
+ const eventBus = createEventBus(eventBusOptions);
15
12
  if (allEventsListener) {
16
13
  eventBus.addAllEventsListener(allEventsListener);
17
14
  }
@@ -23,7 +20,7 @@ function useEventBus(eventBusOptions, allEventsListener, errorListener) {
23
20
  }
24
21
  return eventBus;
25
22
  }, []);
26
- (0, react_1.useEffect)(() => {
23
+ useEffect(() => {
27
24
  if (eventBusOptions) {
28
25
  if (updateRef.current > 0) {
29
26
  throw new Error("EventBus options can't be updated");
@@ -31,7 +28,7 @@ function useEventBus(eventBusOptions, allEventsListener, errorListener) {
31
28
  updateRef.current++;
32
29
  }
33
30
  }, [eventBusOptions]);
34
- (0, react_1.useEffect)(() => {
31
+ useEffect(() => {
35
32
  if (allEventsListenerRef.current !== allEventsListener) {
36
33
  if (allEventsListenerRef.current) {
37
34
  eventBus.removeAllEventsListener(allEventsListenerRef.current);
@@ -42,7 +39,7 @@ function useEventBus(eventBusOptions, allEventsListener, errorListener) {
42
39
  }
43
40
  }
44
41
  }, [allEventsListener]);
45
- (0, react_1.useEffect)(() => {
42
+ useEffect(() => {
46
43
  if (errorListenerRef.current !== errorListener) {
47
44
  if (errorListenerRef.current) {
48
45
  eventBus.removeErrorListener(errorListenerRef.current);
@@ -53,7 +50,7 @@ function useEventBus(eventBusOptions, allEventsListener, errorListener) {
53
50
  }
54
51
  }
55
52
  }, [errorListener]);
56
- (0, react_1.useEffect)(() => {
53
+ useEffect(() => {
57
54
  if (boundaryErrorListenerRef.current !== boundaryErrorListener) {
58
55
  if (boundaryErrorListenerRef.current) {
59
56
  eventBus.removeErrorListener(boundaryErrorListenerRef.current);
@@ -65,7 +62,7 @@ function useEventBus(eventBusOptions, allEventsListener, errorListener) {
65
62
  }
66
63
  }
67
64
  }, [boundaryErrorListener]);
68
- (0, react_1.useEffect)(() => {
65
+ useEffect(() => {
69
66
  return () => {
70
67
  if (allEventsListenerRef.current) {
71
68
  eventBus.removeAllEventsListener(allEventsListenerRef.current);
@@ -1,23 +1,20 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useListenToAction = useListenToAction;
4
- const react_1 = require("react");
5
- function useListenToAction(action, listener, errorListener, beforeActionListener) {
6
- const listenerRef = (0, react_1.useRef)(listener);
7
- const actionRef = (0, react_1.useRef)(action);
8
- const errorListenerRef = (0, react_1.useRef)(null);
9
- const beforeActionListenerRef = (0, react_1.useRef)(null);
1
+ import { useCallback, useEffect, useRef } from "react";
2
+ export function useListenToAction(action, listener, errorListener, beforeActionListener) {
3
+ const listenerRef = useRef(listener);
4
+ const actionRef = useRef(action);
5
+ const errorListenerRef = useRef(null);
6
+ const beforeActionListenerRef = useRef(null);
10
7
  listenerRef.current = listener;
11
- const genericHandler = (0, react_1.useCallback)((arg) => {
8
+ const genericHandler = useCallback((arg) => {
12
9
  var _a;
13
10
  (_a = listenerRef.current) === null || _a === void 0 ? void 0 : _a.call(listenerRef, arg);
14
11
  }, []);
15
- (0, react_1.useEffect)(() => {
12
+ useEffect(() => {
16
13
  actionRef.current.removeListener(genericHandler);
17
14
  actionRef.current = action;
18
15
  actionRef.current.addListener(genericHandler);
19
16
  }, [action]);
20
- (0, react_1.useEffect)(() => {
17
+ useEffect(() => {
21
18
  if (errorListenerRef.current !== errorListener) {
22
19
  if (errorListenerRef.current) {
23
20
  actionRef.current.removeErrorListener(errorListenerRef.current);
@@ -28,7 +25,7 @@ function useListenToAction(action, listener, errorListener, beforeActionListener
28
25
  }
29
26
  }
30
27
  }, [errorListener]);
31
- (0, react_1.useEffect)(() => {
28
+ useEffect(() => {
32
29
  if (beforeActionListenerRef.current !== beforeActionListener) {
33
30
  if (beforeActionListenerRef.current) {
34
31
  actionRef.current.removeBeforeActionListener(beforeActionListenerRef.current);
@@ -39,7 +36,7 @@ function useListenToAction(action, listener, errorListener, beforeActionListener
39
36
  }
40
37
  }
41
38
  }, [beforeActionListener]);
42
- (0, react_1.useEffect)(() => {
39
+ useEffect(() => {
43
40
  return () => {
44
41
  actionRef.current.removeListener(genericHandler);
45
42
  if (errorListenerRef.current) {
@@ -1,50 +1,39 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useListenToActionBus = useListenToActionBus;
4
- const react_1 = require("react");
5
- function useListenToActionBus(actionBus, actionName, listener, options, errorListener, beforeActionListener) {
1
+ import { useCallback, useEffect, useRef } from "react";
2
+ export function useListenToActionBus(actionBus, actionName, listener, options, errorListener, beforeActionListener) {
6
3
  if (listener && typeof listener !== "function") {
7
4
  options = listener.options;
8
5
  errorListener = listener.errorListener;
9
6
  beforeActionListener = listener.beforeActionListener;
10
7
  listener = listener.listener;
11
8
  }
12
- const actionBusRef = (0, react_1.useRef)(actionBus);
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);
9
+ const listenerRef = useRef(listener || null);
10
+ const beforeActionListenerRef = useRef(null);
16
11
  listenerRef.current = listener || null;
17
- errorListenerRef.current = errorListener || null;
18
12
  beforeActionListenerRef.current = beforeActionListener || null;
19
- const genericHandler = (0, react_1.useCallback)((arg) => {
13
+ const genericHandler = useCallback((arg) => {
20
14
  var _a;
21
15
  return (_a = listenerRef.current) === null || _a === void 0 ? void 0 : _a.call(listenerRef, arg);
22
16
  }, []);
23
- const genericBeforeActionHandler = (0, react_1.useCallback)((...args) => {
17
+ const genericBeforeActionHandler = useCallback((...args) => {
24
18
  var _a;
25
19
  return ((_a = beforeActionListenerRef.current) === null || _a === void 0 ? void 0 : _a.call(beforeActionListenerRef, ...args)) || undefined;
26
20
  }, []);
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
- }, []);
31
- (0, react_1.useEffect)(() => {
21
+ // Main listener + beforeAction listener - tied to actionName
22
+ useEffect(() => {
23
+ actionBus.addListener(actionName, genericHandler, options || undefined);
24
+ actionBus.get(actionName).addBeforeActionListener(genericBeforeActionHandler);
32
25
  return () => {
33
- actionBusRef.current.removeListener(actionName, genericHandler);
34
- actionBusRef.current.get(actionName)
35
- .removeBeforeActionListener(genericBeforeActionHandler);
36
- actionBusRef.current.removeErrorListener(genericErrorListener);
26
+ actionBus.removeListener(actionName, genericHandler);
27
+ actionBus.get(actionName).removeBeforeActionListener(genericBeforeActionHandler);
37
28
  };
38
- }, []);
39
- (0, react_1.useEffect)(() => {
40
- actionBusRef.current.removeListener(actionName, genericHandler);
41
- actionBusRef.current.get(actionName)
42
- .removeBeforeActionListener(genericBeforeActionHandler);
43
- actionBusRef.current.removeErrorListener(genericErrorListener);
44
- actionBusRef.current = actionBus;
45
- actionBusRef.current.addListener(actionName, genericHandler, options || undefined);
46
- actionBusRef.current.get(actionName)
47
- .addBeforeActionListener(genericBeforeActionHandler);
48
- actionBusRef.current.addErrorListener(genericErrorListener);
49
- }, [actionBus]);
29
+ }, [actionBus, actionName, genericHandler, genericBeforeActionHandler]);
30
+ // Error listener - bus level
31
+ useEffect(() => {
32
+ if (errorListener) {
33
+ actionBus.addErrorListener(errorListener);
34
+ return () => {
35
+ actionBus.removeErrorListener(errorListener);
36
+ };
37
+ }
38
+ }, [actionBus, errorListener]);
50
39
  }
@@ -1,34 +1,22 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useListenToEvent = useListenToEvent;
4
- const react_1 = require("react");
5
- function useListenToEvent(event, listener, options, errorListener) {
6
- const listenerRef = (0, react_1.useRef)(listener);
7
- const eventRef = (0, react_1.useRef)(event);
8
- const errorListenerRef = (0, react_1.useRef)(errorListener);
1
+ import { useCallback, useEffect, useRef } from "react";
2
+ export function useListenToEvent(event, listener, options, errorListener) {
3
+ const listenerRef = useRef(listener);
9
4
  listenerRef.current = listener;
10
- const genericHandler = (0, react_1.useCallback)((...args) => {
5
+ const genericHandler = useCallback((...args) => {
11
6
  return listenerRef.current(...args);
12
7
  }, []);
13
- (0, react_1.useEffect)(() => {
8
+ useEffect(() => {
9
+ event.addListener(genericHandler, options);
14
10
  return () => {
15
- eventRef.current.removeListener(genericHandler);
11
+ event.removeListener(genericHandler);
16
12
  };
17
- }, []);
18
- (0, react_1.useEffect)(() => {
19
- eventRef.current.removeListener(genericHandler);
20
- eventRef.current = event;
21
- eventRef.current.addListener(genericHandler, options);
22
- }, [event]);
23
- (0, react_1.useEffect)(() => {
24
- if (errorListenerRef.current !== errorListener) {
25
- if (errorListenerRef.current) {
26
- eventRef.current.removeErrorListener(errorListenerRef.current);
27
- }
28
- errorListenerRef.current = errorListener;
29
- if (errorListener) {
30
- eventRef.current.addErrorListener(errorListener);
31
- }
13
+ }, [event, genericHandler]);
14
+ useEffect(() => {
15
+ if (errorListener) {
16
+ event.addErrorListener(errorListener);
17
+ return () => {
18
+ event.removeErrorListener(errorListener);
19
+ };
32
20
  }
33
- }, [errorListener]);
21
+ }, [event, errorListener]);
34
22
  }
@@ -1,38 +1,25 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useListenToEventBus = useListenToEventBus;
4
- const react_1 = require("react");
5
- function useListenToEventBus(eventBus, eventName, listener, options, errorListener) {
6
- const listenerRef = (0, react_1.useRef)(listener);
7
- const eventBusRef = (0, react_1.useRef)(eventBus);
8
- const errorListenerRef = (0, react_1.useRef)(errorListener);
1
+ import { useCallback, useEffect, useRef } from "react";
2
+ export function useListenToEventBus(eventBus, eventName, listener, options, errorListener) {
3
+ const listenerRef = useRef(listener);
9
4
  listenerRef.current = listener;
10
- const genericHandler = (0, react_1.useCallback)((...args) => {
5
+ const genericHandler = useCallback((...args) => {
11
6
  var _a;
12
7
  return (_a = listenerRef.current) === null || _a === void 0 ? void 0 : _a.call(listenerRef, ...args);
13
8
  }, []);
14
- (0, react_1.useEffect)(() => {
9
+ // Main listener - cleanup pattern handles eventBus/eventName changes
10
+ useEffect(() => {
11
+ eventBus.addListener(eventName, genericHandler, options);
15
12
  return () => {
16
- eventBusRef.current.removeListener(eventName, genericHandler);
17
- if (errorListenerRef.current) {
18
- eventBusRef.current.removeErrorListener(errorListenerRef.current);
19
- }
13
+ eventBus.removeListener(eventName, genericHandler);
20
14
  };
21
- }, []);
22
- (0, react_1.useEffect)(() => {
23
- eventBusRef.current.removeListener(eventName, genericHandler);
24
- eventBusRef.current = eventBus;
25
- eventBusRef.current.addListener(eventName, genericHandler, options);
26
- }, [eventBus]);
27
- (0, react_1.useEffect)(() => {
28
- if (errorListenerRef.current !== errorListener) {
29
- if (errorListenerRef.current) {
30
- eventBusRef.current.removeErrorListener(errorListenerRef.current);
31
- }
32
- errorListenerRef.current = errorListener;
33
- if (errorListener) {
34
- eventBusRef.current.addErrorListener(errorListener);
35
- }
15
+ }, [eventBus, eventName, genericHandler]);
16
+ // Error listener - cleanup pattern
17
+ useEffect(() => {
18
+ if (errorListener) {
19
+ eventBus.addErrorListener(errorListener);
20
+ return () => {
21
+ eventBus.removeErrorListener(errorListener);
22
+ };
36
23
  }
37
- }, [errorListener]);
24
+ }, [eventBus, errorListener]);
38
25
  }
@@ -1,22 +1,14 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useListenToStoreChanges = useListenToStoreChanges;
4
- const react_1 = require("react");
5
- function useListenToStoreChanges(store, key, listener, options) {
6
- const listenerRef = (0, react_1.useRef)(listener);
7
- const storeRef = (0, react_1.useRef)(store);
1
+ import { useCallback, useEffect, useRef } from "react";
2
+ export function useListenToStoreChanges(store, key, listener, options) {
3
+ const listenerRef = useRef(listener);
8
4
  listenerRef.current = listener;
9
- const genericHandler = (0, react_1.useCallback)((value, previousValue) => {
5
+ const genericHandler = useCallback((value, previousValue) => {
10
6
  return listenerRef.current(value, previousValue);
11
7
  }, []);
12
- (0, react_1.useEffect)(() => {
8
+ useEffect(() => {
9
+ store.onChange(key, genericHandler, options);
13
10
  return () => {
14
- storeRef.current.removeOnChange(key, genericHandler);
11
+ store.removeOnChange(key, genericHandler);
15
12
  };
16
- }, []);
17
- (0, react_1.useEffect)(() => {
18
- storeRef.current.removeOnChange(key, genericHandler);
19
- storeRef.current = store;
20
- storeRef.current.onChange(key, genericHandler, options);
21
- }, [store]);
13
+ }, [store, key, genericHandler]);
22
14
  }
@@ -1,11 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useStore = useStore;
4
- const react_1 = require("react");
5
- const store_1 = require("../store");
6
- function useStore(initialData = {}, config) {
7
- const store = (0, react_1.useMemo)(() => {
8
- const store = (0, store_1.createStore)(initialData);
1
+ import { useMemo } from "react";
2
+ import { createStore } from "../store";
3
+ export function useStore(initialData = {}, config) {
4
+ const store = useMemo(() => {
5
+ const store = createStore(initialData);
9
6
  if (config === null || config === void 0 ? void 0 : config.onChange) {
10
7
  for (const key in config.onChange) {
11
8
  store.onChange(key, config.onChange[key]);
@@ -13,15 +10,14 @@ function useStore(initialData = {}, config) {
13
10
  }
14
11
  if (config === null || config === void 0 ? void 0 : config.pipes) {
15
12
  for (const key in config.pipes) {
16
- // @ts-expect-error
13
+ // @ts-expect-error - TS widens for-in key to string; types are correct
17
14
  store.pipe(key, config.pipes[key]);
18
15
  }
19
16
  }
20
17
  if (config === null || config === void 0 ? void 0 : config.control) {
21
18
  for (const key in config.control) {
22
- store.control(
23
- // @ts-expect-error
24
- key, config.control[key]);
19
+ // @ts-expect-error - TS widens for-in key to string; types are correct
20
+ store.control(key, config.control[key]);
25
21
  }
26
22
  }
27
23
  return store;
@@ -1,3 +1,3 @@
1
1
  import { KeyOf } from "../lib/types";
2
2
  import { BaseStore } from "../store";
3
- export declare function useStoreState<TStore extends BaseStore, TKey extends KeyOf<TStore["__type"]["propTypes"]>>(store: TStore, key: TKey): readonly [TStore["__type"]["propTypes"][TKey], (value: TStore["__type"]["propTypes"][TKey] | ((previousValue?: TStore["__type"]["propTypes"][TKey] | undefined) => TStore["__type"]["propTypes"][TKey])) => void];
3
+ export declare function useStoreState<TStore extends BaseStore, TKey extends KeyOf<TStore["__type"]["propTypes"]>>(store: TStore, key: TKey): readonly [TStore["__type"]["propTypes"][TKey], (value: TStore["__type"]["propTypes"][TKey] | ((previousValue?: TStore["__type"]["propTypes"][TKey]) => TStore["__type"]["propTypes"][TKey])) => void];