@@ -18,6 +18,7 @@ class HYEventStore {
18
18
this . state = options . state
19
19
this . _observe ( options . state )
20
20
this . event = new EventBus ( )
21
+ this . eventV2 = new EventBus ( )
21
22
}
22
23
23
24
_observe ( state ) {
@@ -32,6 +33,7 @@ class HYEventStore {
32
33
if ( _value === newValue ) return
33
34
_value = newValue
34
35
_this . event . emit ( key , _value )
36
+ _this . eventV2 . emit ( key , { [ key ] : _value } )
35
37
}
36
38
} )
37
39
} )
@@ -40,9 +42,8 @@ class HYEventStore {
40
42
onState ( stateKey , stateCallback ) {
41
43
const keys = Object . keys ( this . state )
42
44
if ( keys . indexOf ( stateKey ) === - 1 ) {
43
- throw new Error ( "then state does not contain your key" )
45
+ throw new Error ( "the state does not contain your key" )
44
46
}
45
-
46
47
this . event . on ( stateKey , stateCallback )
47
48
48
49
// callback
@@ -53,7 +54,37 @@ class HYEventStore {
53
54
stateCallback . apply ( this . state , [ value ] )
54
55
}
55
56
57
+ // ["name", "age"] callback1
58
+ // ["name", "height"] callback2
59
+
60
+ onStates ( statekeys , stateCallback ) {
61
+ const keys = Object . keys ( this . state )
62
+ const value = { }
63
+ for ( const theKey of statekeys ) {
64
+ if ( keys . indexOf ( theKey ) === - 1 ) {
65
+ throw new Error ( "the state does not contain your key" )
66
+ }
67
+ this . eventV2 . on ( theKey , stateCallback )
68
+ value [ theKey ] = this . state [ theKey ]
69
+ }
70
+
71
+ stateCallback . apply ( this . state , [ value ] )
72
+ }
73
+
74
+ offStates ( stateKeys , stateCallback ) {
75
+ stateKeys . forEach ( theKey => {
76
+ if ( keys . indexOf ( stateKey ) === - 1 ) {
77
+ throw new Error ( "the state does not contain your key" )
78
+ }
79
+ this . eventV2 . off ( theKey , stateCallback )
80
+ } )
81
+ }
82
+
56
83
offState ( stateKey , stateCallback ) {
84
+ const keys = Object . keys ( this . state )
85
+ if ( keys . indexOf ( stateKey ) === - 1 ) {
86
+ throw new Error ( "the state does not contain your key" )
87
+ }
57
88
this . event . off ( stateKey , stateCallback )
58
89
}
59
90
0 commit comments