Skip to content
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

fix: making auth and firestore observable compatible #4078

Merged
merged 10 commits into from
Aug 15, 2020
8 changes: 5 additions & 3 deletions packages/auth/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ export namespace FirebaseAuthTypes {
*
* @param listener A listener function which triggers when auth state changed (for example signing out).
*/
onAuthStateChanged(listener: AuthListenerCallback): () => void;
onAuthStateChanged(listener: CallbackOrObserver<AuthListenerCallback>): () => void;

/**
* Listen for changes in ID token.
Expand All @@ -1276,7 +1276,7 @@ export namespace FirebaseAuthTypes {
*
* @param listener A listener function which triggers when the users ID token changes.
*/
onIdTokenChanged(listener: AuthListenerCallback): () => void;
onIdTokenChanged(listener: CallbackOrObserver<AuthListenerCallback>): () => void;

/**
* Adds a listener to observe changes to the User object. This is a superset of everything from
Expand All @@ -1302,7 +1302,7 @@ export namespace FirebaseAuthTypes {
* @react-native-firebase
* @param listener A listener function which triggers when the users data changes.
*/
onUserChanged(listener: AuthListenerCallback): () => void;
onUserChanged(listener: CallbackOrObserver<AuthListenerCallback>): () => void;

/**
* Signs the user out.
Expand Down Expand Up @@ -1634,6 +1634,8 @@ export namespace FirebaseAuthTypes {
}
}

type CallbackOrObserver<T extends (...args: any[]) => any> = T | { next: T };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No very sure on where to put this type


declare module '@react-native-firebase/auth' {
// tslint:disable-next-line:no-duplicate-imports required otherwise doesn't work
import { ReactNativeFirebase } from '@react-native-firebase/app';
Expand Down
15 changes: 12 additions & 3 deletions packages/auth/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ class FirebaseAuthModule extends FirebaseModule {
};
}

onAuthStateChanged(listener) {
_parseListener(listenerOrObserver) {
return typeof listenerOrObserver === 'object'
? listenerOrObserver.next.bind(listenerOrObserver)
: listenerOrObserver;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also not sure where to put this utils function.


onAuthStateChanged(listenerOrObserver) {
const listener = this._parseListener(listenerOrObserver);
const subscription = this.emitter.addListener(
this.eventNameForApp('onAuthStateChanged'),
listener,
Expand All @@ -143,7 +150,8 @@ class FirebaseAuthModule extends FirebaseModule {
return () => subscription.remove();
}

onIdTokenChanged(listener) {
onIdTokenChanged(listenerOrObserver) {
const listener = this._parseListener(listenerOrObserver);
const subscription = this.emitter.addListener(
this.eventNameForApp('onIdTokenChanged'),
listener,
Expand All @@ -157,7 +165,8 @@ class FirebaseAuthModule extends FirebaseModule {
return () => subscription.remove();
}

onUserChanged(listener) {
onUserChanged(listenerOrObserver) {
const listener = this._parseListener(listenerOrObserver);
const subscription = this.emitter.addListener(this.eventNameForApp('onUserChanged'), listener);
if (this._authResult) {
Promise.resolve().then(() => {
Expand Down
28 changes: 17 additions & 11 deletions packages/firestore/lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ export function parseSetOptions(options) {
// };
// }

function isPartialObserver(input) {
return isFunction(input.next) || isFunction(input.error) || isFunction(input.complete);
}

export function parseSnapshotArgs(args) {
if (args.length === 0) {
throw new Error('expected at least one argument.');
Expand Down Expand Up @@ -174,20 +178,21 @@ export function parseSnapshotArgs(args) {
/**
* .onSnapshot({ complete: () => {}, error: (e) => {}, next: (snapshot) => {} })
*/
if (isObject(args[0]) && args[0].includeMetadataChanges === undefined) {
if (args[0].error) {
onError = args[0].error;
if (isObject(args[0]) && isPartialObserver(args[0])) {
const observer = args[0];
if (observer.error) {
onError = observer.error.bind(observer);
}
if (args[0].next) {
onNext = args[0].next;
if (observer.next) {
onNext = observer.next.bind(observer);
}
}

/**
* .onSnapshot(SnapshotListenOptions, ...
*/
if (isObject(args[0]) && args[0].includeMetadataChanges !== undefined) {
snapshotListenOptions.includeMetadataChanges = args[0].includeMetadataChanges;
if (isObject(args[0]) && !isPartialObserver(args[0])) {
snapshotListenOptions.includeMetadataChanges = Boolean(args[0].includeMetadataChanges);
if (isFunction(args[1])) {
/**
* .onSnapshot(SnapshotListenOptions, Function);
Expand All @@ -208,11 +213,12 @@ export function parseSnapshotArgs(args) {
/**
* .onSnapshot(SnapshotListenOptions, { complete: () => {}, error: (e) => {}, next: (snapshot) => {} });
*/
if (isFunction(args[1].error)) {
onError = args[1].error;
const observer = args[1];
if (isFunction(observer.error)) {
onError = observer.error.bind(observer);
}
if (isFunction(args[1].next)) {
onNext = args[1].next;
if (isFunction(observer.next)) {
onNext = observer.next.bind(observer);
}
}
}
Expand Down