react-native-firebase/lib/modules/firestore/DocumentReference.js

261 lines
7.7 KiB
JavaScript
Raw Normal View History

/**
* @flow
* DocumentReference representation wrapper
*/
import CollectionReference from './CollectionReference';
2017-11-17 14:22:46 +00:00
import DocumentSnapshot from './DocumentSnapshot';
2018-03-20 16:07:37 +00:00
import { parseUpdateArgs } from './utils';
import { buildNativeMap } from './utils/serialize';
import { getAppEventName, SharedEventEmitter } from '../../utils/events';
import { getLogger } from '../../utils/log';
2018-03-20 16:07:37 +00:00
import { firestoreAutoId, isFunction, isObject } from '../../utils';
import { getNativeModule } from '../../utils/native';
import type Firestore from './';
2018-01-25 18:25:39 +00:00
import type {
DocumentListenOptions,
NativeDocumentSnapshot,
SetOptions,
} from './types';
import type Path from './Path';
2018-01-25 18:25:39 +00:00
type ObserverOnError = Object => void;
type ObserverOnNext = DocumentSnapshot => void;
type Observer = {
error?: ObserverOnError,
next: ObserverOnNext,
2018-01-25 18:25:39 +00:00
};
/**
* @class DocumentReference
*/
export default class DocumentReference {
_documentPath: Path;
_firestore: Firestore;
constructor(firestore: Firestore, documentPath: Path) {
this._documentPath = documentPath;
this._firestore = firestore;
}
get firestore(): Firestore {
return this._firestore;
}
get id(): string | null {
return this._documentPath.id;
}
get parent(): CollectionReference {
const parentPath = this._documentPath.parent();
2018-03-20 16:07:37 +00:00
// $FlowExpectedError: parentPath can never be null
return new CollectionReference(this._firestore, parentPath);
}
get path(): string {
return this._documentPath.relativeName;
}
collection(collectionPath: string): CollectionReference {
const path = this._documentPath.child(collectionPath);
if (!path.isCollection) {
throw new Error('Argument "collectionPath" must point to a collection.');
}
return new CollectionReference(this._firestore, path);
}
delete(): Promise<void> {
2018-01-25 18:25:39 +00:00
return getNativeModule(this._firestore).documentDelete(this.path);
}
get(): Promise<DocumentSnapshot> {
return getNativeModule(this._firestore)
.documentGet(this.path)
.then(result => new DocumentSnapshot(this._firestore, result));
}
onSnapshot(
2018-01-25 18:25:39 +00:00
optionsOrObserverOrOnNext:
| DocumentListenOptions
| Observer
| ObserverOnNext,
observerOrOnNextOrOnError?: Observer | ObserverOnNext | ObserverOnError,
2018-01-25 18:25:39 +00:00
onError?: ObserverOnError
) {
let observer: Observer;
let docListenOptions = {};
// Called with: onNext, ?onError
if (isFunction(optionsOrObserverOrOnNext)) {
if (observerOrOnNextOrOnError && !isFunction(observerOrOnNextOrOnError)) {
2018-01-25 18:25:39 +00:00
throw new Error(
'DocumentReference.onSnapshot failed: Second argument must be a valid function.'
);
}
2018-02-17 12:55:19 +00:00
// $FlowExpectedError: Not coping with the overloaded method signature
observer = {
next: optionsOrObserverOrOnNext,
error: observerOrOnNextOrOnError,
};
2018-01-25 18:25:39 +00:00
} else if (
optionsOrObserverOrOnNext &&
isObject(optionsOrObserverOrOnNext)
) {
// Called with: Observer
if (optionsOrObserverOrOnNext.next) {
if (isFunction(optionsOrObserverOrOnNext.next)) {
2018-01-25 18:25:39 +00:00
if (
optionsOrObserverOrOnNext.error &&
!isFunction(optionsOrObserverOrOnNext.error)
) {
throw new Error(
'DocumentReference.onSnapshot failed: Observer.error must be a valid function.'
);
}
2018-02-17 12:55:19 +00:00
// $FlowExpectedError: Not coping with the overloaded method signature
observer = {
next: optionsOrObserverOrOnNext.next,
error: optionsOrObserverOrOnNext.error,
};
} else {
2018-01-25 18:25:39 +00:00
throw new Error(
'DocumentReference.onSnapshot failed: Observer.next must be a valid function.'
);
}
2018-01-25 18:25:39 +00:00
} else if (
Object.prototype.hasOwnProperty.call(
optionsOrObserverOrOnNext,
'includeMetadataChanges'
)
) {
docListenOptions = optionsOrObserverOrOnNext;
// Called with: Options, onNext, ?onError
if (isFunction(observerOrOnNextOrOnError)) {
if (onError && !isFunction(onError)) {
2018-01-25 18:25:39 +00:00
throw new Error(
'DocumentReference.onSnapshot failed: Third argument must be a valid function.'
);
}
2018-02-17 12:55:19 +00:00
// $FlowExpectedError: Not coping with the overloaded method signature
observer = {
next: observerOrOnNextOrOnError,
error: onError,
};
2018-01-25 18:25:39 +00:00
// Called with Options, Observer
} else if (
observerOrOnNextOrOnError &&
isObject(observerOrOnNextOrOnError) &&
observerOrOnNextOrOnError.next
) {
if (isFunction(observerOrOnNextOrOnError.next)) {
2018-01-25 18:25:39 +00:00
if (
observerOrOnNextOrOnError.error &&
!isFunction(observerOrOnNextOrOnError.error)
) {
throw new Error(
'DocumentReference.onSnapshot failed: Observer.error must be a valid function.'
);
}
observer = {
next: observerOrOnNextOrOnError.next,
error: observerOrOnNextOrOnError.error,
};
} else {
2018-01-25 18:25:39 +00:00
throw new Error(
'DocumentReference.onSnapshot failed: Observer.next must be a valid function.'
);
}
} else {
2018-01-25 18:25:39 +00:00
throw new Error(
'DocumentReference.onSnapshot failed: Second argument must be a function or observer.'
);
}
} else {
2018-01-25 18:25:39 +00:00
throw new Error(
'DocumentReference.onSnapshot failed: First argument must be a function, observer or options.'
);
}
} else {
2018-01-25 18:25:39 +00:00
throw new Error(
'DocumentReference.onSnapshot failed: Called with invalid arguments.'
);
}
const listenerId = firestoreAutoId();
const listener = (nativeDocumentSnapshot: NativeDocumentSnapshot) => {
2018-01-25 18:25:39 +00:00
const documentSnapshot = new DocumentSnapshot(
this.firestore,
nativeDocumentSnapshot
);
observer.next(documentSnapshot);
};
// Listen to snapshot events
SharedEventEmitter.addListener(
getAppEventName(this._firestore, `onDocumentSnapshot:${listenerId}`),
2018-01-25 18:25:39 +00:00
listener
);
// Listen for snapshot error events
if (observer.error) {
SharedEventEmitter.addListener(
2018-01-25 18:25:39 +00:00
getAppEventName(
this._firestore,
`onDocumentSnapshotError:${listenerId}`
),
observer.error
);
}
// Add the native listener
2018-01-25 18:25:39 +00:00
getNativeModule(this._firestore).documentOnSnapshot(
this.path,
listenerId,
docListenOptions
);
// Return an unsubscribe method
return this._offDocumentSnapshot.bind(this, listenerId, listener);
}
set(data: Object, options?: SetOptions): Promise<void> {
const nativeData = buildNativeMap(data);
2018-01-25 18:25:39 +00:00
return getNativeModule(this._firestore).documentSet(
this.path,
nativeData,
options
2018-01-25 18:25:39 +00:00
);
}
update(...args: any[]): Promise<void> {
2018-03-20 16:07:37 +00:00
const data = parseUpdateArgs(args, 'DocumentReference.update');
const nativeData = buildNativeMap(data);
2018-01-25 18:25:39 +00:00
return getNativeModule(this._firestore).documentUpdate(
this.path,
nativeData
);
}
/**
* INTERNALS
*/
/**
* Remove document snapshot listener
* @param listener
*/
_offDocumentSnapshot(listenerId: string, listener: Function) {
getLogger(this._firestore).info('Removing onDocumentSnapshot listener');
2018-01-25 18:25:39 +00:00
SharedEventEmitter.removeListener(
getAppEventName(this._firestore, `onDocumentSnapshot:${listenerId}`),
listener
);
SharedEventEmitter.removeListener(
getAppEventName(this._firestore, `onDocumentSnapshotError:${listenerId}`),
listener
);
getNativeModule(this._firestore).documentOffSnapshot(this.path, listenerId);
}
}