Skip to content

Commit ee3fad1

Browse files
zhaofengliposva
authored andcommitted
feat: support firebase@5
* index.js: docChanges is now a method The Firebase JS SDK introduced some breaking changes in 5.0.0: firebase/firebase-js-sdk@0997040 Fixes #183. * package.json: Update firebase peerDependency to 5.0.0 * Update package.json * Update index.js
1 parent 5d4d55e commit ee3fad1

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,14 @@ function bindCollection ({
119119
}
120120
}
121121

122-
const unbind = collection.onSnapshot(({ docChanges }) => {
122+
const unbind = collection.onSnapshot((ref) => {
123123
// console.log('pending', metadata.hasPendingWrites)
124124
// docs.forEach(d => console.log('doc', d, '\n', 'data', d.data()))
125125
// NOTE this will only be triggered once and it will be with all the documents
126126
// from the query appearing as added
127127
// (https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots)
128+
const docChanges = typeof ref.docChanges === 'function' ? ref.docChanges() : ref.docChanges
129+
128130
if (!isResolved && docChanges.length) {
129131
// isResolved is only meant to make sure we do the check only once
130132
isResolved = true

test/helpers/mock.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class CollectionReference extends callbacksAndErrors {
142142
setTimeout(() => {
143143
// Object.keys(this.data).map((k, i) => console.log(k, 'at', i, this.data[k].data))
144144
cb({
145-
docChanges: Object.keys(this.data).map((id, newIndex) => ({
145+
docChanges: () => Object.keys(this.data).map((id, newIndex) => ({
146146
type: 'added',
147147
doc: new DocumentSnapshot(null, new Key(id), this.data[id].data),
148148
newIndex,
@@ -162,7 +162,7 @@ class CollectionReference extends callbacksAndErrors {
162162
index: Object.keys(this.data).length
163163
})
164164
this._callCallbacks({
165-
docChanges: [
165+
docChanges: () => [
166166
{
167167
type: 'added',
168168
doc: new DocumentSnapshot(null, id, data),
@@ -194,7 +194,7 @@ class CollectionReference extends callbacksAndErrors {
194194
const ref = this.data[id.v]
195195
delete this.data[id.v]
196196
this._callCallbacks({
197-
docChanges: [
197+
docChanges: () => [
198198
{
199199
doc: new DocumentSnapshot(null, id, ref.data),
200200
type: 'removed'
@@ -213,7 +213,7 @@ class CollectionReference extends callbacksAndErrors {
213213
type = 'added'
214214
}
215215
this._callCallbacks({
216-
docChanges: [
216+
docChanges: () => [
217217
{
218218
type,
219219
doc: new DocumentSnapshot(null, id, data),

0 commit comments

Comments
 (0)