@@ -16,38 +16,41 @@ class ChangeObserver {
16
16
constructor ( initialState , evaluator ) {
17
17
this . __prevState = initialState
18
18
this . __evaluator = evaluator
19
- this . __prevValues = Immutable . Map ( { } )
19
+ this . __prevValues = Immutable . Map ( )
20
20
this . __observers = [ ]
21
21
}
22
22
23
23
/**
24
24
* @param {Immutable.Map } newState
25
25
*/
26
26
notifyObservers ( newState ) {
27
- var currentValues = Immutable . Map ( )
27
+ if ( this . __observers . length > 0 ) {
28
+ var currentValues = Immutable . Map ( )
28
29
29
- this . __observers . forEach ( entry => {
30
- var getter = entry . getter
31
- var code = hashCode ( getter )
32
- var prevState = this . __prevState
33
- var prevValue
30
+ this . __observers . forEach ( entry => {
31
+ var getter = entry . getter
32
+ var code = hashCode ( getter )
33
+ var prevState = this . __prevState
34
+ var prevValue
34
35
35
- if ( this . __prevValues . has ( code ) ) {
36
- prevValue = this . __prevValues . get ( code )
37
- } else {
38
- prevValue = this . __evaluator . evaluate ( prevState , getter )
39
- this . __prevValues = this . __prevValues . set ( code , prevValue )
40
- }
36
+ if ( this . __prevValues . has ( code ) ) {
37
+ prevValue = this . __prevValues . get ( code )
38
+ } else {
39
+ prevValue = this . __evaluator . evaluate ( prevState , getter )
40
+ this . __prevValues = this . __prevValues . set ( code , prevValue )
41
+ }
41
42
42
- var currValue = this . __evaluator . evaluate ( newState , getter )
43
+ var currValue = this . __evaluator . evaluate ( newState , getter )
43
44
44
- if ( ! isEqual ( prevValue , currValue ) ) {
45
- entry . handler . call ( null , currValue )
46
- currentValues = currentValues . set ( code , currValue )
47
- }
48
- } )
45
+ if ( ! isEqual ( prevValue , currValue ) ) {
46
+ entry . handler . call ( null , currValue )
47
+ currentValues = currentValues . set ( code , currValue )
48
+ }
49
+ } )
50
+
51
+ this . __prevValues = currentValues
52
+ }
49
53
this . __prevState = newState
50
- this . __prevValues = currentValues
51
54
}
52
55
53
56
/**
@@ -74,6 +77,16 @@ class ChangeObserver {
74
77
}
75
78
}
76
79
80
+ /**
81
+ * Updates the change appliers prevState when silent store registration happens
82
+ * this allows the proper hasChanged check for notify observers if a store registration
83
+ * happens in between
84
+ * @param {Immutable.Map } prevState
85
+ */
86
+ updatePreviousState ( prevState ) {
87
+ this . __prevState = prevState
88
+ }
89
+
77
90
/**
78
91
* Resets and clears all observers and reinitializes back to the supplied
79
92
* previous state
@@ -82,7 +95,7 @@ class ChangeObserver {
82
95
*/
83
96
reset ( prevState ) {
84
97
this . __prevState = prevState
85
- this . __prevValues = Immutable . Map ( { } )
98
+ this . __prevValues = Immutable . Map ( )
86
99
this . __observers = [ ]
87
100
}
88
101
}
0 commit comments