Skip to content

Commit c684b15

Browse files
committed
refactors subscription to be slightly clearer
1 parent 0af93c2 commit c684b15

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/utils/Subscription.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
// well as nesting subscriptions of descendant components, so that we can ensure the
33
// ancestor components re-render before descendants
44

5-
function initListeners() {
5+
const CLEARED = null
6+
7+
function createListenerCollection() {
8+
// the current/next pattern is copied from redux's createStore code.
9+
// TODO: refactor+expose that code to be reusable here?
610
let current = []
711
let next = []
812

913
return {
1014
clear() {
11-
next = null
12-
current = null
15+
next = CLEARED
16+
current = CLEARED
1317
},
1418

1519
notify() {
@@ -25,7 +29,7 @@ function initListeners() {
2529
next.push(listener)
2630

2731
return function unsubscribe() {
28-
if (!isSubscribed || !current) return
32+
if (!isSubscribed || current === CLEARED) return
2933
isSubscribed = false
3034

3135
if (next === current) next = current.slice()
@@ -42,7 +46,7 @@ export default class Subscription {
4246
: store.subscribe.bind(store)
4347

4448
this.unsubscribe = null
45-
this.listeners = initListeners()
49+
this.listeners = createListenerCollection()
4650
}
4751

4852
addNestedSub(listener) {

0 commit comments

Comments
 (0)