@@ -4,8 +4,6 @@ import { getBatch } from './batch'
44// well as nesting subscriptions of descendant components, so that we can ensure the
55// ancestor components re-render before descendants
66
7- const nullListeners = { notify ( ) { } }
8-
97function createListenerCollection ( ) {
108 const batch = getBatch ( )
119 let first = null
@@ -71,51 +69,62 @@ function createListenerCollection() {
7169 }
7270}
7371
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+ }
8076
81- this . handleChangeWrapper = this . handleChangeWrapper . bind ( this )
82- }
77+ export function createSubscription ( store , parentSub ) {
78+ let unsubscribe
79+ let listeners = nullListeners
8380
84- addNestedSub ( listener ) {
85- this . trySubscribe ( )
86- return this . listeners . subscribe ( listener )
81+ function addNestedSub ( listener ) {
82+ trySubscribe ( )
83+ return listeners . subscribe ( listener )
8784 }
8885
89- notifyNestedSubs ( ) {
90- this . listeners . notify ( )
86+ function notifyNestedSubs ( ) {
87+ listeners . notify ( )
9188 }
9289
93- handleChangeWrapper ( ) {
94- if ( this . onStateChange ) {
95- this . onStateChange ( )
90+ function handleChangeWrapper ( ) {
91+ if ( subscription . onStateChange ) {
92+ subscription . onStateChange ( )
9693 }
9794 }
9895
99- isSubscribed ( ) {
100- return Boolean ( this . unsubscribe )
96+ function isSubscribed ( ) {
97+ return Boolean ( unsubscribe )
10198 }
10299
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 )
108105
109- this . listeners = createListenerCollection ( )
106+ listeners = createListenerCollection ( )
110107 }
111108 }
112109
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
119116 }
120117 }
118+
119+ const subscription = {
120+ addNestedSub,
121+ notifyNestedSubs,
122+ handleChangeWrapper,
123+ isSubscribed,
124+ trySubscribe,
125+ tryUnsubscribe,
126+ getListeners : ( ) => listeners ,
127+ }
128+
129+ return subscription
121130}
0 commit comments