Skip to content

chore(firestore): add support for onSnapshotsInSync #8379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,17 @@ private void sendOnSnapshotError(
databaseId,
listenerId));
}

@ReactMethod
public void onSnapshotsInSync(Promise promise) {
ListenerRegistration registration = FirebaseFirestore.getInstance()
.addSnapshotsInSyncListener(() -> {
WritableMap result = Arguments.createMap();
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("onSnapshotsInSync", result);
});

promise.resolve(null);
}
}
98 changes: 98 additions & 0 deletions packages/firestore/e2e/DocumentReference/onSnapshotsInSync.e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const COLLECTION = 'firestore';
const NO_RULE_COLLECTION = 'no_rules';

Check failure on line 18 in packages/firestore/e2e/DocumentReference/onSnapshotsInSync.e2e.js

View workflow job for this annotation

GitHub Actions / Lint

'NO_RULE_COLLECTION' is assigned a value but never used. Allowed unused vars must match /^_/u
const { wipe } = require('../helpers');
import { onSnapshotsInSync } from '../../lib/modular/snapshot';

describe('firestore().doc().onSnapshot()', function () {
before(function () {
return wipe();
});

it('onSnapshotsInSync() invokes callback and returns unsubscribe', async function () {
const firestore = firebase.firestore();
const ref = firestore.collection(COLLECTION).doc('syncTest');

Check failure on line 30 in packages/firestore/e2e/DocumentReference/onSnapshotsInSync.e2e.js

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`
await ref.set({ foo: 'bar' });

Check failure on line 32 in packages/firestore/e2e/DocumentReference/onSnapshotsInSync.e2e.js

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`
const onSync = sinon.spy();

Check failure on line 34 in packages/firestore/e2e/DocumentReference/onSnapshotsInSync.e2e.js

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`
const unsubscribeSnapshot = ref.onSnapshot(() => {});
const unsubscribeSync = onSnapshotsInSync(firestore, onSync);

Check failure on line 37 in packages/firestore/e2e/DocumentReference/onSnapshotsInSync.e2e.js

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`
// Allow sync to process
await Utils.sleep(500);

Check failure on line 40 in packages/firestore/e2e/DocumentReference/onSnapshotsInSync.e2e.js

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`
onSync.should.be.calledOnce();
unsubscribeSync.should.be.a.Function();

Check failure on line 43 in packages/firestore/e2e/DocumentReference/onSnapshotsInSync.e2e.js

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`
unsubscribeSnapshot();
unsubscribeSync();
});

// it('onSnapshotsInSync fires after listeners are in sync', () => {
// const testDocs = {
// a: { foo: 1 }
// };
// return withTestCollection(persistence, testDocs, async (coll, db) => {
// let events = [];
// const gotInitialSnapshot = (() => {
// let resolve, reject;
// const promise = new Promise((res, rej) => {
// resolve = res;
// reject = rej;
// });
// return { promise, resolve, reject };
// })();
// const docA = doc(coll, 'a');

// onSnapshot(docA, snap => {
// events.push('doc');
// gotInitialSnapshot.resolve();
// });
// await gotInitialSnapshot.promise;
// events = [];

// const done = (() => {
// let resolve, reject;
// const promise = new Promise((res, rej) => {
// resolve = res;
// reject = rej;
// });
// return { promise, resolve, reject };
// })();
// onSnapshotsInSync(db, () => {
// events.push('snapshots-in-sync');
// if (events.length === 3) {
// // We should have an initial snapshots-in-sync event, then a snapshot
// // event for set(), then another event to indicate we're in sync
// // again.
// expect(events).to.deep.equal([
// 'snapshots-in-sync',
// 'doc',
// 'snapshots-in-sync'
// ]);
// done.resolve();
// }
// });

// await setDoc(docA, { foo: 3 });
// await done.promise;
// });
// });
});
8 changes: 8 additions & 0 deletions packages/firestore/ios/RNFBFirestore/RNFBFirestoreModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@
resolve(nil);
}

RCT_EXPORT_METHOD(addSnapshotsInSyncListener:(RCTResponseSenderBlock)callback) {
FIRListenerRegistration *registration = [[FIRFirestore firestore]

Check failure on line 244 in packages/firestore/ios/RNFBFirestore/RNFBFirestoreModule.m

View workflow job for this annotation

GitHub Actions / iOS (debug)

use of undeclared identifier 'FIRListenerRegistration'

Check failure on line 244 in packages/firestore/ios/RNFBFirestore/RNFBFirestoreModule.m

View workflow job for this annotation

GitHub Actions / iOS (debug)

use of undeclared identifier 'registration'

Check failure on line 244 in packages/firestore/ios/RNFBFirestore/RNFBFirestoreModule.m

View workflow job for this annotation

GitHub Actions / iOS (release)

use of undeclared identifier 'FIRListenerRegistration'

Check failure on line 244 in packages/firestore/ios/RNFBFirestore/RNFBFirestoreModule.m

View workflow job for this annotation

GitHub Actions / iOS (release)

use of undeclared identifier 'registration'

Check failure on line 244 in packages/firestore/ios/RNFBFirestore/RNFBFirestoreModule.m

View workflow job for this annotation

GitHub Actions / iOS (release)

use of undeclared identifier 'FIRListenerRegistration'

Check failure on line 244 in packages/firestore/ios/RNFBFirestore/RNFBFirestoreModule.m

View workflow job for this annotation

GitHub Actions / iOS (release)

use of undeclared identifier 'registration'
addSnapshotsInSyncListener:^{
callback(@[]);
}];
}


- (NSMutableDictionary *)taskProgressToDictionary:(FIRLoadBundleTaskProgress *)progress {
NSMutableDictionary *progressMap = [[NSMutableDictionary alloc] init];
progressMap[@"bytesLoaded"] = @(progress.bytesLoaded);
Expand Down
25 changes: 25 additions & 0 deletions packages/firestore/lib/modular/snapshot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,28 @@
left: Query<AppModelType, DbModelType>,
right: Query<AppModelType, DbModelType>,
): boolean;

/**
* Attaches a listener for a snapshots-in-sync event.
* The snapshots-in-sync event indicates that all listeners affected by a given change have fired, even if
* a single server-generated change affects multiple listeners.
*
* @param firestore
* @param onSync
*/
export declare function onSnapshotsInSync(firestore: Firestore, onSync: () => void): Unsubscribe;

/**
* /**
* Attaches a listener for a snapshots-in-sync event.
* The snapshots-in-sync event indicates that all listeners affected by a given change have fired, even if
* a single server-generated change affects multiple listeners.
*
* @param firestore
* @param onSync
*/
export declare function onSnapshotsInSync(firestore: Firestore, observer: {

Check failure on line 250 in packages/firestore/lib/modular/snapshot.d.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `firestore:·Firestore,` with `⏎··firestore:·Firestore,⏎·`
next?: (value: void) => void;

Check failure on line 251 in packages/firestore/lib/modular/snapshot.d.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `··`
error?: (error: FirestoreError) => void;

Check failure on line 252 in packages/firestore/lib/modular/snapshot.d.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `··` with `····`
complete?: () => void;
}): Unsubscribe;
17 changes: 17 additions & 0 deletions packages/firestore/lib/modular/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/

import { MODULAR_DEPRECATION_ARG } from '../../../app/lib/common';
import { NativeEventEmitter, NativeModules } from 'react-native';

const emitter = new NativeEventEmitter(NativeModules.RNFBFirestoreModule);

/**
* @param {Query | DocumentReference} reference
Expand All @@ -18,3 +21,17 @@
export function snapshotEqual(left, right) {
return left.isEqual.call(left, right, MODULAR_DEPRECATION_ARG);
}

// export function onSnapshotsInSync(firestore, ...args) {
// return firestore.addSnapshotsInSyncListener.call(firestore, ...args, MODULAR_DEPRECATION_ARG);
// }

export function onSnapshotsInSync(_firestore, callback) {
console.log('Registering onSnapshotsInSync');

Check warning on line 30 in packages/firestore/lib/modular/snapshot.js

View check run for this annotation

Codecov / codecov/patch

packages/firestore/lib/modular/snapshot.js#L29-L30

Added lines #L29 - L30 were not covered by tests

Check warning on line 30 in packages/firestore/lib/modular/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement

Check warning on line 30 in packages/firestore/lib/modular/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement

Check warning on line 30 in packages/firestore/lib/modular/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
if (typeof firestoreInstance.onSnapshotsInSync === 'function') {
return firestoreInstance.onSnapshotsInSync(callback);

Check warning on line 32 in packages/firestore/lib/modular/snapshot.js

View check run for this annotation

Codecov / codecov/patch

packages/firestore/lib/modular/snapshot.js#L32

Added line #L32 was not covered by tests
}

console.warn('onSnapshotsInSync is not implemented');

Check warning on line 35 in packages/firestore/lib/modular/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement

Check warning on line 35 in packages/firestore/lib/modular/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement

Check warning on line 35 in packages/firestore/lib/modular/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
return () => {};

Check warning on line 36 in packages/firestore/lib/modular/snapshot.js

View check run for this annotation

Codecov / codecov/patch

packages/firestore/lib/modular/snapshot.js#L35-L36

Added lines #L35 - L36 were not covered by tests
}
Loading