Skip to content

Commit 86a7f84

Browse files
committed
refactor: function to unsubscribe
1 parent 9d0ebfb commit 86a7f84

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/index.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { createSnapshot, extractRefs, callOnceWithArg, walkGet, walkSet } from './utils'
22

3+
function callUnbinds (subs) {
4+
for (const sub in subs) {
5+
subs[sub].unbind()
6+
}
7+
}
8+
39
// NOTE not convinced by the naming of subscribeToRefs and subscribeToDocument
410
// first one is calling the other on every ref and subscribeToDocument may call
511
// updateDataFromDocumentSnapshot which may call subscribeToRefs as well
@@ -109,10 +115,7 @@ function bindCollection ({
109115
},
110116
removed: ({ oldIndex }) => {
111117
array.splice(oldIndex, 1)
112-
const subs = arraySubs.splice(oldIndex, 1)[0]
113-
for (const subKey in subs) {
114-
subs[subKey].unbind()
115-
}
118+
callUnbinds(arraySubs.splice(oldIndex, 1)[0])
116119
}
117120
}
118121

@@ -153,11 +156,7 @@ function bindCollection ({
153156

154157
return () => {
155158
unbind()
156-
arraySubs.forEach(subs => {
157-
for (const subKey in subs) {
158-
subs[subKey].unbind()
159-
}
160-
})
159+
arraySubs.forEach(callUnbinds)
161160
}
162161
}
163162

@@ -202,10 +201,7 @@ function subscribeToDocument ({ ref, target, path, depth, resolve }) {
202201

203202
return () => {
204203
unbind()
205-
for (const subKey in subs) {
206-
const sub = subs[subKey]
207-
sub.unbind()
208-
}
204+
callUnbinds(subs)
209205
}
210206
}
211207

0 commit comments

Comments
 (0)