react-state-custom 1.0.21 → 1.0.23
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/DevToolState.d.ts +4 -0
- package/dist/index.es.js +244 -197
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/react-state-custom.css +1 -1
- package/dist/state-utils/ctx.d.ts +2 -1
- package/package.json +1 -1
- package/src/DevTool.css +29 -3
- package/src/DevTool.tsx +1 -1
- package/src/DevToolState.tsx +74 -32
- package/src/Test.tsx +7 -0
- package/src/state-utils/ctx.ts +33 -10
package/src/state-utils/ctx.ts
CHANGED
|
@@ -4,12 +4,22 @@ import { useArrayHash } from "./useArrayHash"
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
const CHANGE_EVENT = "@--change-event"
|
|
8
|
+
|
|
9
|
+
class DataEvent<D> extends Event {
|
|
10
|
+
constructor(
|
|
11
|
+
public event: keyof D,
|
|
12
|
+
public value: D[typeof event] | undefined
|
|
13
|
+
) {
|
|
14
|
+
super(String(event));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class ChangeEvent<D> extends Event {
|
|
8
19
|
constructor(
|
|
9
|
-
public
|
|
10
|
-
public value: any
|
|
20
|
+
public value: DataEvent<D>
|
|
11
21
|
) {
|
|
12
|
-
super(
|
|
22
|
+
super(CHANGE_EVENT, value);
|
|
13
23
|
}
|
|
14
24
|
}
|
|
15
25
|
|
|
@@ -28,8 +38,6 @@ export class Context<D> extends EventTarget {
|
|
|
28
38
|
super();
|
|
29
39
|
}
|
|
30
40
|
|
|
31
|
-
// private event = new EventEmitter()
|
|
32
|
-
|
|
33
41
|
/**
|
|
34
42
|
* The current data held by the context.
|
|
35
43
|
*/
|
|
@@ -48,9 +56,9 @@ export class Context<D> extends EventTarget {
|
|
|
48
56
|
|
|
49
57
|
if (value != this.data[key]) {
|
|
50
58
|
this.data[key] = value
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this.dispatchEvent(new
|
|
59
|
+
let event = new DataEvent(key, value);
|
|
60
|
+
this.dispatchEvent(event);
|
|
61
|
+
this.dispatchEvent(new ChangeEvent(event))
|
|
54
62
|
}
|
|
55
63
|
}
|
|
56
64
|
|
|
@@ -71,7 +79,22 @@ export class Context<D> extends EventTarget {
|
|
|
71
79
|
|
|
72
80
|
if (key in this.data) _listener(this.data[key])
|
|
73
81
|
|
|
74
|
-
return () =>
|
|
82
|
+
return () => this.removeEventListener(String(key), listener)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public subscribeAll(_listener: (changeKey: keyof D, newData: Partial<D>) => void) {
|
|
86
|
+
|
|
87
|
+
const listener = (event: any) => {
|
|
88
|
+
if (event instanceof ChangeEvent) {
|
|
89
|
+
const { value: data } = event
|
|
90
|
+
_listener(data.event as any as keyof D, this.data)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
this.addEventListener(String(CHANGE_EVENT), listener)
|
|
95
|
+
|
|
96
|
+
return () => this.removeEventListener(String(CHANGE_EVENT), listener)
|
|
97
|
+
|
|
75
98
|
}
|
|
76
99
|
|
|
77
100
|
}
|