3
3
// ancestor components re-render before descendants
4
4
5
5
const CLEARED = null
6
+ const nullListeners = { notify ( ) { } }
6
7
7
8
function createListenerCollection ( ) {
8
9
// the current/next pattern is copied from redux's createStore code.
@@ -41,12 +42,10 @@ function createListenerCollection() {
41
42
42
43
export default class Subscription {
43
44
constructor ( store , parentSub ) {
44
- this . subscribe = parentSub
45
- ? parentSub . addNestedSub . bind ( parentSub )
46
- : store . subscribe . bind ( store )
47
-
45
+ this . store = store
46
+ this . parentSub = parentSub
48
47
this . unsubscribe = null
49
- this . listeners = createListenerCollection ( )
48
+ this . listeners = nullListeners
50
49
}
51
50
52
51
addNestedSub ( listener ) {
@@ -64,17 +63,21 @@ export default class Subscription {
64
63
65
64
trySubscribe ( ) {
66
65
if ( ! this . unsubscribe ) {
67
- this . unsubscribe = this . subscribe ( this . onStateChange )
66
+ // this.onStateChange is set by connectAdvanced.initSubscription()
67
+ this . unsubscribe = this . parentSub
68
+ ? this . parentSub . addNestedSub ( this . onStateChange )
69
+ : this . store . subscribe ( this . onStateChange )
70
+
71
+ this . listeners = createListenerCollection ( )
68
72
}
69
73
}
70
74
71
75
tryUnsubscribe ( ) {
72
76
if ( this . unsubscribe ) {
73
77
this . unsubscribe ( )
78
+ this . unsubscribe = null
74
79
this . listeners . clear ( )
80
+ this . listeners = nullListeners
75
81
}
76
- this . unsubscribe = null
77
- this . subscribe = null
78
- this . listeners = { notify ( ) { } }
79
82
}
80
83
}
0 commit comments