@kuindji/reactive 1.0.13 → 1.0.15

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.
@@ -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,22 @@ 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
+ event.removeAllListeners();
73
+ };
74
+ }, []);
65
75
  return event;
66
76
  }
@@ -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
  }
@@ -19,7 +19,7 @@ function useStoreState(store, key) {
19
19
  }, []);
20
20
  (0, react_1.useEffect)(() => {
21
21
  return () => {
22
- storeRef.current.onChange(keyRef.current, onChange);
22
+ storeRef.current.removeOnChange(keyRef.current, onChange);
23
23
  };
24
24
  }, []);
25
25
  (0, react_1.useEffect)(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kuindji/reactive",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "author": "Ivan Kuindzhi",
5
5
  "repository": {
6
6
  "type": "git",