@kuindji/reactive 1.0.23 → 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/dist/action.js +5 -8
- package/dist/actionBus.js +5 -8
- package/dist/actionMap.js +3 -6
- package/dist/event.js +52 -55
- package/dist/eventBus.js +74 -77
- package/dist/index.js +7 -23
- package/dist/lib/asyncCall.js +1 -4
- 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 +10 -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 +8 -11
- package/dist/react/useListenToEvent.js +6 -9
- package/dist/react/useListenToEventBus.js +6 -9
- package/dist/react/useListenToStoreChanges.js +5 -8
- package/dist/react/useStore.js +5 -8
- package/dist/react/useStoreState.js +9 -12
- package/dist/react.js +13 -29
- package/dist/store.js +30 -34
- package/package.json +1 -1
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,28 +1,25 @@
|
|
|
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 listenerRef =
|
|
13
|
-
const beforeActionListenerRef =
|
|
9
|
+
const listenerRef = useRef(listener || null);
|
|
10
|
+
const beforeActionListenerRef = useRef(null);
|
|
14
11
|
listenerRef.current = listener || null;
|
|
15
12
|
beforeActionListenerRef.current = beforeActionListener || null;
|
|
16
|
-
const genericHandler =
|
|
13
|
+
const genericHandler = useCallback((arg) => {
|
|
17
14
|
var _a;
|
|
18
15
|
return (_a = listenerRef.current) === null || _a === void 0 ? void 0 : _a.call(listenerRef, arg);
|
|
19
16
|
}, []);
|
|
20
|
-
const genericBeforeActionHandler =
|
|
17
|
+
const genericBeforeActionHandler = useCallback((...args) => {
|
|
21
18
|
var _a;
|
|
22
19
|
return ((_a = beforeActionListenerRef.current) === null || _a === void 0 ? void 0 : _a.call(beforeActionListenerRef, ...args)) || undefined;
|
|
23
20
|
}, []);
|
|
24
21
|
// Main listener + beforeAction listener - tied to actionName
|
|
25
|
-
|
|
22
|
+
useEffect(() => {
|
|
26
23
|
actionBus.addListener(actionName, genericHandler, options || undefined);
|
|
27
24
|
actionBus.get(actionName).addBeforeActionListener(genericBeforeActionHandler);
|
|
28
25
|
return () => {
|
|
@@ -31,7 +28,7 @@ function useListenToActionBus(actionBus, actionName, listener, options, errorLis
|
|
|
31
28
|
};
|
|
32
29
|
}, [actionBus, actionName, genericHandler, genericBeforeActionHandler]);
|
|
33
30
|
// Error listener - bus level
|
|
34
|
-
|
|
31
|
+
useEffect(() => {
|
|
35
32
|
if (errorListener) {
|
|
36
33
|
actionBus.addErrorListener(errorListener);
|
|
37
34
|
return () => {
|
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const react_1 = require("react");
|
|
5
|
-
function useListenToEvent(event, listener, options, errorListener) {
|
|
6
|
-
const listenerRef = (0, react_1.useRef)(listener);
|
|
1
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
2
|
+
export function useListenToEvent(event, listener, options, errorListener) {
|
|
3
|
+
const listenerRef = useRef(listener);
|
|
7
4
|
listenerRef.current = listener;
|
|
8
|
-
const genericHandler =
|
|
5
|
+
const genericHandler = useCallback((...args) => {
|
|
9
6
|
return listenerRef.current(...args);
|
|
10
7
|
}, []);
|
|
11
|
-
|
|
8
|
+
useEffect(() => {
|
|
12
9
|
event.addListener(genericHandler, options);
|
|
13
10
|
return () => {
|
|
14
11
|
event.removeListener(genericHandler);
|
|
15
12
|
};
|
|
16
13
|
}, [event, genericHandler]);
|
|
17
|
-
|
|
14
|
+
useEffect(() => {
|
|
18
15
|
if (errorListener) {
|
|
19
16
|
event.addErrorListener(errorListener);
|
|
20
17
|
return () => {
|
|
@@ -1,23 +1,20 @@
|
|
|
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);
|
|
1
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
2
|
+
export function useListenToEventBus(eventBus, eventName, listener, options, errorListener) {
|
|
3
|
+
const listenerRef = useRef(listener);
|
|
7
4
|
listenerRef.current = listener;
|
|
8
|
-
const genericHandler =
|
|
5
|
+
const genericHandler = useCallback((...args) => {
|
|
9
6
|
var _a;
|
|
10
7
|
return (_a = listenerRef.current) === null || _a === void 0 ? void 0 : _a.call(listenerRef, ...args);
|
|
11
8
|
}, []);
|
|
12
9
|
// Main listener - cleanup pattern handles eventBus/eventName changes
|
|
13
|
-
|
|
10
|
+
useEffect(() => {
|
|
14
11
|
eventBus.addListener(eventName, genericHandler, options);
|
|
15
12
|
return () => {
|
|
16
13
|
eventBus.removeListener(eventName, genericHandler);
|
|
17
14
|
};
|
|
18
15
|
}, [eventBus, eventName, genericHandler]);
|
|
19
16
|
// Error listener - cleanup pattern
|
|
20
|
-
|
|
17
|
+
useEffect(() => {
|
|
21
18
|
if (errorListener) {
|
|
22
19
|
eventBus.addErrorListener(errorListener);
|
|
23
20
|
return () => {
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const react_1 = require("react");
|
|
5
|
-
function useListenToStoreChanges(store, key, listener, options) {
|
|
6
|
-
const listenerRef = (0, react_1.useRef)(listener);
|
|
1
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
2
|
+
export function useListenToStoreChanges(store, key, listener, options) {
|
|
3
|
+
const listenerRef = useRef(listener);
|
|
7
4
|
listenerRef.current = listener;
|
|
8
|
-
const genericHandler =
|
|
5
|
+
const genericHandler = useCallback((value, previousValue) => {
|
|
9
6
|
return listenerRef.current(value, previousValue);
|
|
10
7
|
}, []);
|
|
11
|
-
|
|
8
|
+
useEffect(() => {
|
|
12
9
|
store.onChange(key, genericHandler, options);
|
|
13
10
|
return () => {
|
|
14
11
|
store.removeOnChange(key, genericHandler);
|
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]);
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const storeRef = (0, react_1.useRef)(store);
|
|
8
|
-
const keyRef = (0, react_1.useRef)(key);
|
|
9
|
-
const onChange = (0, react_1.useCallback)((value) => {
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
export function useStoreState(store, key) {
|
|
3
|
+
const [value, setValue] = useState(store.get(key));
|
|
4
|
+
const storeRef = useRef(store);
|
|
5
|
+
const keyRef = useRef(key);
|
|
6
|
+
const onChange = useCallback((value) => {
|
|
10
7
|
setValue(value);
|
|
11
8
|
}, []);
|
|
12
|
-
const setter =
|
|
9
|
+
const setter = useCallback((value) => {
|
|
13
10
|
if (typeof value === "function") {
|
|
14
11
|
storeRef.current.set(keyRef.current, value(storeRef.current.get(keyRef.current)));
|
|
15
12
|
}
|
|
@@ -17,12 +14,12 @@ function useStoreState(store, key) {
|
|
|
17
14
|
storeRef.current.set(keyRef.current, value);
|
|
18
15
|
}
|
|
19
16
|
}, []);
|
|
20
|
-
|
|
17
|
+
useEffect(() => {
|
|
21
18
|
return () => {
|
|
22
19
|
storeRef.current.removeOnChange(keyRef.current, onChange);
|
|
23
20
|
};
|
|
24
21
|
}, []);
|
|
25
|
-
|
|
22
|
+
useEffect(() => {
|
|
26
23
|
storeRef.current.removeOnChange(keyRef.current, onChange);
|
|
27
24
|
storeRef.current = store;
|
|
28
25
|
keyRef.current = key;
|
package/dist/react.js
CHANGED
|
@@ -1,29 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./react/ErrorBoundary"), exports);
|
|
18
|
-
__exportStar(require("./react/useAction"), exports);
|
|
19
|
-
__exportStar(require("./react/useActionBus"), exports);
|
|
20
|
-
__exportStar(require("./react/useActionMap"), exports);
|
|
21
|
-
__exportStar(require("./react/useEvent"), exports);
|
|
22
|
-
__exportStar(require("./react/useEventBus"), exports);
|
|
23
|
-
__exportStar(require("./react/useListenToAction"), exports);
|
|
24
|
-
__exportStar(require("./react/useListenToActionBus"), exports);
|
|
25
|
-
__exportStar(require("./react/useListenToEvent"), exports);
|
|
26
|
-
__exportStar(require("./react/useListenToEventBus"), exports);
|
|
27
|
-
__exportStar(require("./react/useListenToStoreChanges"), exports);
|
|
28
|
-
__exportStar(require("./react/useStore"), exports);
|
|
29
|
-
__exportStar(require("./react/useStoreState"), exports);
|
|
1
|
+
export * from "./react/ErrorBoundary";
|
|
2
|
+
export * from "./react/useAction";
|
|
3
|
+
export * from "./react/useActionBus";
|
|
4
|
+
export * from "./react/useActionMap";
|
|
5
|
+
export * from "./react/useEvent";
|
|
6
|
+
export * from "./react/useEventBus";
|
|
7
|
+
export * from "./react/useListenToAction";
|
|
8
|
+
export * from "./react/useListenToActionBus";
|
|
9
|
+
export * from "./react/useListenToEvent";
|
|
10
|
+
export * from "./react/useListenToEventBus";
|
|
11
|
+
export * from "./react/useListenToStoreChanges";
|
|
12
|
+
export * from "./react/useStore";
|
|
13
|
+
export * from "./react/useStoreState";
|
package/dist/store.js
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
exports.ResetEventName = "reset";
|
|
9
|
-
exports.ErrorEventName = "error";
|
|
10
|
-
exports.EffectEventName = "effect";
|
|
11
|
-
function createStore(initialData = {}) {
|
|
1
|
+
import { createEventBus } from "./eventBus";
|
|
2
|
+
export const BeforeChangeEventName = "before";
|
|
3
|
+
export const ChangeEventName = "change";
|
|
4
|
+
export const ResetEventName = "reset";
|
|
5
|
+
export const ErrorEventName = "error";
|
|
6
|
+
export const EffectEventName = "effect";
|
|
7
|
+
export function createStore(initialData = {}) {
|
|
12
8
|
const data = new Map(Object.entries(initialData));
|
|
13
|
-
const changes =
|
|
14
|
-
const pipe =
|
|
15
|
-
const control =
|
|
9
|
+
const changes = createEventBus();
|
|
10
|
+
const pipe = createEventBus();
|
|
11
|
+
const control = createEventBus();
|
|
16
12
|
let effectKeys = [];
|
|
17
13
|
const effectInterceptor = (name, args) => {
|
|
18
|
-
if (name ===
|
|
14
|
+
if (name === ChangeEventName) {
|
|
19
15
|
effectKeys.push(...args[0]);
|
|
20
16
|
return false;
|
|
21
17
|
}
|
|
@@ -25,7 +21,7 @@ function createStore(initialData = {}) {
|
|
|
25
21
|
var _a, _b, _c, _d, _e;
|
|
26
22
|
const prev = data.get(name);
|
|
27
23
|
if (prev !== value) {
|
|
28
|
-
if (control.firstNonEmpty(
|
|
24
|
+
if (control.firstNonEmpty(BeforeChangeEventName, name, value)
|
|
29
25
|
=== false) {
|
|
30
26
|
return;
|
|
31
27
|
}
|
|
@@ -35,7 +31,7 @@ function createStore(initialData = {}) {
|
|
|
35
31
|
newValue = pipe.pipe(name, ...pipeArgs);
|
|
36
32
|
}
|
|
37
33
|
catch (error) {
|
|
38
|
-
control.trigger(
|
|
34
|
+
control.trigger(ErrorEventName, {
|
|
39
35
|
error: error instanceof Error
|
|
40
36
|
? error
|
|
41
37
|
: new Error(String(error)),
|
|
@@ -43,7 +39,7 @@ function createStore(initialData = {}) {
|
|
|
43
39
|
type: "store-pipe",
|
|
44
40
|
name,
|
|
45
41
|
});
|
|
46
|
-
if ((_a = control.get(
|
|
42
|
+
if ((_a = control.get(ErrorEventName)) === null || _a === void 0 ? void 0 : _a.hasListener()) {
|
|
47
43
|
return false;
|
|
48
44
|
}
|
|
49
45
|
throw error;
|
|
@@ -60,7 +56,7 @@ function createStore(initialData = {}) {
|
|
|
60
56
|
changes.trigger(name, ...changeArgs);
|
|
61
57
|
}
|
|
62
58
|
catch (error) {
|
|
63
|
-
control.trigger(
|
|
59
|
+
control.trigger(ErrorEventName, {
|
|
64
60
|
error: error instanceof Error
|
|
65
61
|
? error
|
|
66
62
|
: new Error(String(error)),
|
|
@@ -68,25 +64,25 @@ function createStore(initialData = {}) {
|
|
|
68
64
|
type: "store-change",
|
|
69
65
|
name,
|
|
70
66
|
});
|
|
71
|
-
if ((_b = control.get(
|
|
67
|
+
if ((_b = control.get(ErrorEventName)) === null || _b === void 0 ? void 0 : _b.hasListener()) {
|
|
72
68
|
effectKeys = [];
|
|
73
69
|
return true;
|
|
74
70
|
}
|
|
75
71
|
throw error;
|
|
76
72
|
}
|
|
77
|
-
if ((_c = control.get(
|
|
73
|
+
if ((_c = control.get(EffectEventName)) === null || _c === void 0 ? void 0 : _c.hasListener()) {
|
|
78
74
|
try {
|
|
79
75
|
const isIntercepting = control.isIntercepting();
|
|
80
76
|
if (!isIntercepting) {
|
|
81
77
|
control.intercept(effectInterceptor);
|
|
82
78
|
}
|
|
83
|
-
control.trigger(
|
|
79
|
+
control.trigger(EffectEventName, name, value);
|
|
84
80
|
if (!isIntercepting) {
|
|
85
81
|
control.stopIntercepting();
|
|
86
82
|
}
|
|
87
83
|
}
|
|
88
84
|
catch (error) {
|
|
89
|
-
control.trigger(
|
|
85
|
+
control.trigger(ErrorEventName, {
|
|
90
86
|
error: error instanceof Error
|
|
91
87
|
? error
|
|
92
88
|
: new Error(String(error)),
|
|
@@ -94,7 +90,7 @@ function createStore(initialData = {}) {
|
|
|
94
90
|
type: "store-control",
|
|
95
91
|
name,
|
|
96
92
|
});
|
|
97
|
-
if ((_d = control.get(
|
|
93
|
+
if ((_d = control.get(ErrorEventName)) === null || _d === void 0 ? void 0 : _d.hasListener()) {
|
|
98
94
|
effectKeys = [];
|
|
99
95
|
return true;
|
|
100
96
|
}
|
|
@@ -103,13 +99,13 @@ function createStore(initialData = {}) {
|
|
|
103
99
|
}
|
|
104
100
|
if (triggerChange) {
|
|
105
101
|
try {
|
|
106
|
-
control.trigger(
|
|
102
|
+
control.trigger(ChangeEventName, [name, ...effectKeys]);
|
|
107
103
|
if (!control.isIntercepting()) {
|
|
108
104
|
effectKeys = [];
|
|
109
105
|
}
|
|
110
106
|
}
|
|
111
107
|
catch (error) {
|
|
112
|
-
control.trigger(
|
|
108
|
+
control.trigger(ErrorEventName, {
|
|
113
109
|
error: error instanceof Error
|
|
114
110
|
? error
|
|
115
111
|
: new Error(String(error)),
|
|
@@ -117,7 +113,7 @@ function createStore(initialData = {}) {
|
|
|
117
113
|
type: "store-control",
|
|
118
114
|
name,
|
|
119
115
|
});
|
|
120
|
-
if ((_e = control.get(
|
|
116
|
+
if ((_e = control.get(ErrorEventName)) === null || _e === void 0 ? void 0 : _e.hasListener()) {
|
|
121
117
|
effectKeys = [];
|
|
122
118
|
return true;
|
|
123
119
|
}
|
|
@@ -146,7 +142,7 @@ function createStore(initialData = {}) {
|
|
|
146
142
|
else if (typeof name === "object") {
|
|
147
143
|
const changedKeys = [];
|
|
148
144
|
const isIntercepting = control.isIntercepting();
|
|
149
|
-
const hasEffectListener = (_a = control.get(
|
|
145
|
+
const hasEffectListener = (_a = control.get(EffectEventName)) === null || _a === void 0 ? void 0 : _a.hasListener();
|
|
150
146
|
if (hasEffectListener && !isIntercepting) {
|
|
151
147
|
control.intercept(effectInterceptor);
|
|
152
148
|
}
|
|
@@ -156,7 +152,7 @@ function createStore(initialData = {}) {
|
|
|
156
152
|
}
|
|
157
153
|
});
|
|
158
154
|
try {
|
|
159
|
-
control.trigger(
|
|
155
|
+
control.trigger(ChangeEventName, [
|
|
160
156
|
...changedKeys,
|
|
161
157
|
...effectKeys,
|
|
162
158
|
]);
|
|
@@ -166,14 +162,14 @@ function createStore(initialData = {}) {
|
|
|
166
162
|
}
|
|
167
163
|
}
|
|
168
164
|
catch (error) {
|
|
169
|
-
control.trigger(
|
|
165
|
+
control.trigger(ErrorEventName, {
|
|
170
166
|
error: error instanceof Error
|
|
171
167
|
? error
|
|
172
168
|
: new Error(String(error)),
|
|
173
169
|
args: [name],
|
|
174
170
|
type: "store-control",
|
|
175
171
|
});
|
|
176
|
-
if ((_b = control.get(
|
|
172
|
+
if ((_b = control.get(ErrorEventName)) === null || _b === void 0 ? void 0 : _b.hasListener()) {
|
|
177
173
|
return;
|
|
178
174
|
}
|
|
179
175
|
throw error;
|
|
@@ -218,7 +214,7 @@ function createStore(initialData = {}) {
|
|
|
218
214
|
const allChangedKeys = [];
|
|
219
215
|
const log = [];
|
|
220
216
|
const controlInterceptor = function (name, [changedKeys]) {
|
|
221
|
-
if (name ===
|
|
217
|
+
if (name === ChangeEventName) {
|
|
222
218
|
allChangedKeys.push(...changedKeys);
|
|
223
219
|
return false;
|
|
224
220
|
}
|
|
@@ -246,12 +242,12 @@ function createStore(initialData = {}) {
|
|
|
246
242
|
changes.trigger(propName, ...changeArgs);
|
|
247
243
|
}
|
|
248
244
|
if (allChangedKeys.length > 0) {
|
|
249
|
-
control.trigger(
|
|
245
|
+
control.trigger(ChangeEventName, allChangedKeys);
|
|
250
246
|
}
|
|
251
247
|
};
|
|
252
248
|
const reset = () => {
|
|
253
249
|
data.clear();
|
|
254
|
-
control.trigger(
|
|
250
|
+
control.trigger(ResetEventName);
|
|
255
251
|
};
|
|
256
252
|
const api = {
|
|
257
253
|
set,
|