@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.
- package/README.md +9 -10
- package/dist/action.d.ts +1 -1
- package/dist/action.js +5 -8
- package/dist/actionBus.js +9 -11
- package/dist/actionMap.js +3 -6
- package/dist/event.d.ts +1 -1
- package/dist/event.js +95 -71
- package/dist/eventBus.d.ts +21 -21
- package/dist/eventBus.js +125 -131
- package/dist/index.js +7 -23
- package/dist/lib/asyncCall.js +2 -6
- package/dist/lib/listenerSorter.js +1 -4
- package/dist/lib/tagsIntersect.js +1 -4
- package/dist/lib/types.js +4 -7
- package/dist/react/ErrorBoundary.js +13 -13
- package/dist/react/useAction.js +18 -21
- package/dist/react/useActionBus.js +11 -14
- package/dist/react/useActionMap.js +9 -12
- package/dist/react/useEvent.js +14 -17
- package/dist/react/useEventBus.js +16 -19
- package/dist/react/useListenToAction.js +11 -14
- package/dist/react/useListenToActionBus.js +22 -33
- package/dist/react/useListenToEvent.js +15 -27
- package/dist/react/useListenToEventBus.js +17 -30
- package/dist/react/useListenToStoreChanges.js +8 -16
- package/dist/react/useStore.js +8 -12
- package/dist/react/useStoreState.d.ts +1 -1
- package/dist/react/useStoreState.js +10 -12
- package/dist/react.js +13 -29
- package/dist/store.js +54 -43
- package/package.json +19 -17
package/dist/react/useAction.js
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
77
|
+
useEffect(() => {
|
|
81
78
|
return () => {
|
|
82
79
|
if (listenerRef.current) {
|
|
83
80
|
listenerRef.current = null;
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
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
|
-
|
|
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
|
-
|
|
29
|
+
useEffect(() => {
|
|
33
30
|
if (boundaryErrorListenerRef.current !== boundaryErrorListener) {
|
|
34
31
|
if (boundaryErrorListenerRef.current) {
|
|
35
32
|
actionBus.removeErrorListener(boundaryErrorListenerRef.current);
|
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
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 =
|
|
12
|
+
const actionMap = createActionMap(actions, errorListeners);
|
|
16
13
|
return actionMap;
|
|
17
14
|
}, []);
|
|
18
|
-
|
|
15
|
+
useEffect(() => {
|
|
19
16
|
if (changeRef.current > 0) {
|
|
20
17
|
throw new Error("useActionMap() does not support changing actions or errorListener");
|
|
21
18
|
}
|
package/dist/react/useEvent.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
55
|
+
useEffect(() => {
|
|
59
56
|
return () => {
|
|
60
57
|
if (listenerRef.current) {
|
|
61
58
|
event.removeListener(listenerRef.current);
|
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
65
|
+
useEffect(() => {
|
|
69
66
|
return () => {
|
|
70
67
|
if (allEventsListenerRef.current) {
|
|
71
68
|
eventBus.removeAllEventsListener(allEventsListenerRef.current);
|
|
@@ -1,23 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
const
|
|
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 =
|
|
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
|
-
|
|
12
|
+
useEffect(() => {
|
|
16
13
|
actionRef.current.removeListener(genericHandler);
|
|
17
14
|
actionRef.current = action;
|
|
18
15
|
actionRef.current.addListener(genericHandler);
|
|
19
16
|
}, [action]);
|
|
20
|
-
|
|
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
|
-
|
|
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
|
-
|
|
39
|
+
useEffect(() => {
|
|
43
40
|
return () => {
|
|
44
41
|
actionRef.current.removeListener(genericHandler);
|
|
45
42
|
if (errorListenerRef.current) {
|
|
@@ -1,50 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
|
13
|
-
const
|
|
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 =
|
|
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 =
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
.removeBeforeActionListener(genericBeforeActionHandler);
|
|
36
|
-
actionBusRef.current.removeErrorListener(genericErrorListener);
|
|
26
|
+
actionBus.removeListener(actionName, genericHandler);
|
|
27
|
+
actionBus.get(actionName).removeBeforeActionListener(genericBeforeActionHandler);
|
|
37
28
|
};
|
|
38
|
-
}, []);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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 =
|
|
5
|
+
const genericHandler = useCallback((...args) => {
|
|
11
6
|
return listenerRef.current(...args);
|
|
12
7
|
}, []);
|
|
13
|
-
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
event.addListener(genericHandler, options);
|
|
14
10
|
return () => {
|
|
15
|
-
|
|
11
|
+
event.removeListener(genericHandler);
|
|
16
12
|
};
|
|
17
|
-
}, []);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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 =
|
|
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
|
-
|
|
9
|
+
// Main listener - cleanup pattern handles eventBus/eventName changes
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
eventBus.addListener(eventName, genericHandler, options);
|
|
15
12
|
return () => {
|
|
16
|
-
|
|
17
|
-
if (errorListenerRef.current) {
|
|
18
|
-
eventBusRef.current.removeErrorListener(errorListenerRef.current);
|
|
19
|
-
}
|
|
13
|
+
eventBus.removeListener(eventName, genericHandler);
|
|
20
14
|
};
|
|
21
|
-
}, []);
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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 =
|
|
5
|
+
const genericHandler = useCallback((value, previousValue) => {
|
|
10
6
|
return listenerRef.current(value, previousValue);
|
|
11
7
|
}, []);
|
|
12
|
-
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
store.onChange(key, genericHandler, options);
|
|
13
10
|
return () => {
|
|
14
|
-
|
|
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
|
}
|
package/dist/react/useStore.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
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
|
-
|
|
23
|
-
|
|
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]
|
|
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];
|