33// ancestor components re-render before descendants
44
55const CLEARED = null
6+ const nullListeners = { notify ( ) { } }
67
78function createListenerCollection ( ) {
89 // the current/next pattern is copied from redux's createStore code.
@@ -41,12 +42,10 @@ function createListenerCollection() {
4142
4243export default class Subscription {
4344 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
4847 this . unsubscribe = null
49- this . listeners = createListenerCollection ( )
48+ this . listeners = nullListeners
5049 }
5150
5251 addNestedSub ( listener ) {
@@ -64,17 +63,21 @@ export default class Subscription {
6463
6564 trySubscribe ( ) {
6665 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 ( )
6872 }
6973 }
7074
7175 tryUnsubscribe ( ) {
7276 if ( this . unsubscribe ) {
7377 this . unsubscribe ( )
78+ this . unsubscribe = null
7479 this . listeners . clear ( )
80+ this . listeners = nullListeners
7581 }
76- this . unsubscribe = null
77- this . subscribe = null
78- this . listeners = { notify ( ) { } }
7982 }
8083}
0 commit comments