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.
@@ -4,12 +4,22 @@ import { useArrayHash } from "./useArrayHash"
4
4
 
5
5
 
6
6
 
7
- class DataEvent extends Event {
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 event: string,
10
- public value: any
20
+ public value: DataEvent<D>
11
21
  ) {
12
- super(event);
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
- // console.count("[COUNT] " + String(key))
52
- // this.event.emit(String(key), { value })
53
- this.dispatchEvent(new DataEvent(String(key), value))
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 () => (this.removeEventListener(String(key), listener), undefined)
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
  }