mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-05-04 20:11:33 +08:00
[types] Get flow type working again; Fix majority of firestore type issues
This commit is contained in:
@@ -3,11 +3,13 @@
|
||||
* DocumentReference representation wrapper
|
||||
*/
|
||||
import CollectionReference from './CollectionReference';
|
||||
import DocumentSnapshot from './DocumentSnapshot';
|
||||
import Path from './Path';
|
||||
import DocumentSnapshot, { type DocumentSnapshotNativeData } from './DocumentSnapshot';
|
||||
import { buildNativeMap } from './utils/serialize';
|
||||
import { firestoreAutoId, isFunction, isObject, isString } from '../../utils';
|
||||
|
||||
import type Firestore from './';
|
||||
import type Path from './Path';
|
||||
|
||||
export type WriteOptions = {
|
||||
merge?: boolean,
|
||||
}
|
||||
@@ -16,24 +18,27 @@ type DocumentListenOptions = {
|
||||
includeMetadataChanges: boolean,
|
||||
}
|
||||
|
||||
type ObserverOnError = (Object) => void;
|
||||
type ObserverOnNext = (DocumentSnapshot) => void;
|
||||
|
||||
type Observer = {
|
||||
next: (DocumentSnapshot) => void,
|
||||
error?: (Object) => void,
|
||||
error?: ObserverOnError,
|
||||
next: ObserverOnNext,
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* @class DocumentReference
|
||||
*/
|
||||
export default class DocumentReference {
|
||||
_documentPath: Path;
|
||||
_firestore: Object;
|
||||
_firestore: Firestore;
|
||||
|
||||
constructor(firestore: Object, documentPath: Path) {
|
||||
constructor(firestore: Firestore, documentPath: Path) {
|
||||
this._documentPath = documentPath;
|
||||
this._firestore = firestore;
|
||||
}
|
||||
|
||||
get firestore(): Object {
|
||||
get firestore(): Firestore {
|
||||
return this._firestore;
|
||||
}
|
||||
|
||||
@@ -43,6 +48,9 @@ export default class DocumentReference {
|
||||
|
||||
get parent(): CollectionReference {
|
||||
const parentPath = this._documentPath.parent();
|
||||
if (!parentPath) {
|
||||
throw new Error('Invalid document path');
|
||||
}
|
||||
return new CollectionReference(this._firestore, parentPath);
|
||||
}
|
||||
|
||||
@@ -71,9 +79,9 @@ export default class DocumentReference {
|
||||
}
|
||||
|
||||
onSnapshot(
|
||||
optionsOrObserverOrOnNext: DocumentListenOptions | Observer | (DocumentSnapshot) => void,
|
||||
observerOrOnNextOrOnError?: Observer | (DocumentSnapshot) => void | (Object) => void,
|
||||
onError?: (Object) => void
|
||||
optionsOrObserverOrOnNext: DocumentListenOptions | Observer | ObserverOnNext,
|
||||
observerOrOnNextOrOnError?: Observer | ObserverOnNext | ObserverOnError,
|
||||
onError?: ObserverOnError,
|
||||
) {
|
||||
let observer = {};
|
||||
let docListenOptions = {};
|
||||
@@ -125,8 +133,8 @@ export default class DocumentReference {
|
||||
}
|
||||
const listenerId = firestoreAutoId();
|
||||
|
||||
const listener = (nativeDocumentSnapshot) => {
|
||||
const documentSnapshot = new DocumentSnapshot(this, nativeDocumentSnapshot);
|
||||
const listener = (nativeDocumentSnapshot: DocumentSnapshotNativeData) => {
|
||||
const documentSnapshot = new DocumentSnapshot(this.firestore, nativeDocumentSnapshot);
|
||||
observer.next(documentSnapshot);
|
||||
};
|
||||
|
||||
@@ -158,7 +166,7 @@ export default class DocumentReference {
|
||||
.documentSet(this.path, nativeData, writeOptions);
|
||||
}
|
||||
|
||||
update(...args: Object | string[]): Promise<void> {
|
||||
update(...args: Array<any>): Promise<void> {
|
||||
let data = {};
|
||||
if (args.length === 1) {
|
||||
if (!isObject(args[0])) {
|
||||
@@ -190,7 +198,7 @@ export default class DocumentReference {
|
||||
* Remove document snapshot listener
|
||||
* @param listener
|
||||
*/
|
||||
_offDocumentSnapshot(listenerId: number, listener: Function) {
|
||||
_offDocumentSnapshot(listenerId: string, listener: Function) {
|
||||
this._firestore.log.info('Removing onDocumentSnapshot listener');
|
||||
this._firestore.removeListener(this._firestore._getAppEventName(`onDocumentSnapshot:${listenerId}`), listener);
|
||||
this._firestore.removeListener(this._firestore._getAppEventName(`onDocumentSnapshotError:${listenerId}`), listener);
|
||||
|
||||
Reference in New Issue
Block a user