@@ -4,8 +4,6 @@ import { getBatch } from './batch'
4
4
// well as nesting subscriptions of descendant components, so that we can ensure the
5
5
// ancestor components re-render before descendants
6
6
7
- const nullListeners = { notify ( ) { } }
8
-
9
7
function createListenerCollection ( ) {
10
8
const batch = getBatch ( )
11
9
let first = null
@@ -71,51 +69,62 @@ function createListenerCollection() {
71
69
}
72
70
}
73
71
74
- export default class Subscription {
75
- constructor ( store , parentSub ) {
76
- this . store = store
77
- this . parentSub = parentSub
78
- this . unsubscribe = null
79
- this . listeners = nullListeners
72
+ const nullListeners = {
73
+ notify ( ) { } ,
74
+ get : ( ) => [ ] ,
75
+ }
80
76
81
- this . handleChangeWrapper = this . handleChangeWrapper . bind ( this )
82
- }
77
+ export function createSubscription ( store , parentSub ) {
78
+ let unsubscribe
79
+ let listeners = nullListeners
83
80
84
- addNestedSub ( listener ) {
85
- this . trySubscribe ( )
86
- return this . listeners . subscribe ( listener )
81
+ function addNestedSub ( listener ) {
82
+ trySubscribe ( )
83
+ return listeners . subscribe ( listener )
87
84
}
88
85
89
- notifyNestedSubs ( ) {
90
- this . listeners . notify ( )
86
+ function notifyNestedSubs ( ) {
87
+ listeners . notify ( )
91
88
}
92
89
93
- handleChangeWrapper ( ) {
94
- if ( this . onStateChange ) {
95
- this . onStateChange ( )
90
+ function handleChangeWrapper ( ) {
91
+ if ( subscription . onStateChange ) {
92
+ subscription . onStateChange ( )
96
93
}
97
94
}
98
95
99
- isSubscribed ( ) {
100
- return Boolean ( this . unsubscribe )
96
+ function isSubscribed ( ) {
97
+ return Boolean ( unsubscribe )
101
98
}
102
99
103
- trySubscribe ( ) {
104
- if ( ! this . unsubscribe ) {
105
- this . unsubscribe = this . parentSub
106
- ? this . parentSub . addNestedSub ( this . handleChangeWrapper )
107
- : this . store . subscribe ( this . handleChangeWrapper )
100
+ function trySubscribe ( ) {
101
+ if ( ! unsubscribe ) {
102
+ unsubscribe = parentSub
103
+ ? parentSub . addNestedSub ( handleChangeWrapper )
104
+ : store . subscribe ( handleChangeWrapper )
108
105
109
- this . listeners = createListenerCollection ( )
106
+ listeners = createListenerCollection ( )
110
107
}
111
108
}
112
109
113
- tryUnsubscribe ( ) {
114
- if ( this . unsubscribe ) {
115
- this . unsubscribe ( )
116
- this . unsubscribe = null
117
- this . listeners . clear ( )
118
- this . listeners = nullListeners
110
+ function tryUnsubscribe ( ) {
111
+ if ( unsubscribe ) {
112
+ unsubscribe ( )
113
+ unsubscribe = undefined
114
+ listeners . clear ( )
115
+ listeners = nullListeners
119
116
}
120
117
}
118
+
119
+ const subscription = {
120
+ addNestedSub,
121
+ notifyNestedSubs,
122
+ handleChangeWrapper,
123
+ isSubscribed,
124
+ trySubscribe,
125
+ tryUnsubscribe,
126
+ getListeners : ( ) => listeners ,
127
+ }
128
+
129
+ return subscription
121
130
}
0 commit comments