2017-09-26 13:57:25 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Firestore representation wrapper
|
|
|
|
*/
|
2017-11-28 07:41:55 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
import { getAppEventName, SharedEventEmitter } from '../../utils/events';
|
|
|
|
import ModuleBase from '../../utils/ModuleBase';
|
2017-09-26 13:57:25 +00:00
|
|
|
import CollectionReference from './CollectionReference';
|
|
|
|
import DocumentReference from './DocumentReference';
|
2018-01-11 18:28:14 +00:00
|
|
|
import FieldPath from './FieldPath';
|
2017-10-12 08:00:46 +00:00
|
|
|
import FieldValue from './FieldValue';
|
2017-09-26 13:57:25 +00:00
|
|
|
import GeoPoint from './GeoPoint';
|
|
|
|
import Path from './Path';
|
|
|
|
import WriteBatch from './WriteBatch';
|
2017-11-17 16:17:27 +00:00
|
|
|
import INTERNALS from '../../utils/internals';
|
2017-09-26 13:57:25 +00:00
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
import type DocumentSnapshot from './DocumentSnapshot';
|
2018-02-14 13:00:19 +00:00
|
|
|
import type App from '../core/app';
|
2017-11-17 11:07:52 +00:00
|
|
|
import type QuerySnapshot from './QuerySnapshot';
|
|
|
|
|
2017-10-03 09:12:25 +00:00
|
|
|
type CollectionSyncEvent = {
|
|
|
|
appName: string,
|
|
|
|
querySnapshot?: QuerySnapshot,
|
|
|
|
error?: Object,
|
|
|
|
listenerId: string,
|
|
|
|
path: string,
|
2018-01-25 18:25:39 +00:00
|
|
|
};
|
2017-10-03 09:12:25 +00:00
|
|
|
|
2017-10-02 12:11:38 +00:00
|
|
|
type DocumentSyncEvent = {
|
|
|
|
appName: string,
|
2017-10-03 09:12:25 +00:00
|
|
|
documentSnapshot?: DocumentSnapshot,
|
2017-10-02 12:11:38 +00:00
|
|
|
error?: Object,
|
2017-10-03 09:12:25 +00:00
|
|
|
listenerId: string,
|
2017-10-02 12:11:38 +00:00
|
|
|
path: string,
|
2018-01-25 18:25:39 +00:00
|
|
|
};
|
2017-10-02 12:11:38 +00:00
|
|
|
|
2017-12-22 15:57:33 +00:00
|
|
|
const NATIVE_EVENTS = [
|
|
|
|
'firestore_collection_sync_event',
|
|
|
|
'firestore_document_sync_event',
|
|
|
|
];
|
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
export const MODULE_NAME = 'RNFirebaseFirestore';
|
|
|
|
export const NAMESPACE = 'firestore';
|
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
/**
|
|
|
|
* @class Firestore
|
|
|
|
*/
|
|
|
|
export default class Firestore extends ModuleBase {
|
2017-09-26 13:57:25 +00:00
|
|
|
_referencePath: Path;
|
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
constructor(app: App) {
|
|
|
|
super(app, {
|
2018-01-03 20:00:38 +00:00
|
|
|
events: NATIVE_EVENTS,
|
|
|
|
moduleName: MODULE_NAME,
|
2018-01-09 17:31:00 +00:00
|
|
|
multiApp: true,
|
2018-01-03 20:00:38 +00:00
|
|
|
namespace: NAMESPACE,
|
|
|
|
});
|
2017-09-26 13:57:25 +00:00
|
|
|
this._referencePath = new Path([]);
|
2017-10-02 12:11:38 +00:00
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
SharedEventEmitter.addListener(
|
2017-10-02 12:11:38 +00:00
|
|
|
// sub to internal native event - this fans out to
|
|
|
|
// public event name: onCollectionSnapshot
|
2017-12-22 15:24:31 +00:00
|
|
|
getAppEventName(this, 'firestore_collection_sync_event'),
|
2018-01-25 18:25:39 +00:00
|
|
|
this._onCollectionSyncEvent.bind(this)
|
2017-10-02 12:11:38 +00:00
|
|
|
);
|
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
SharedEventEmitter.addListener(
|
2017-10-02 12:11:38 +00:00
|
|
|
// sub to internal native event - this fans out to
|
|
|
|
// public event name: onDocumentSnapshot
|
2017-12-22 15:24:31 +00:00
|
|
|
getAppEventName(this, 'firestore_document_sync_event'),
|
2018-01-25 18:25:39 +00:00
|
|
|
this._onDocumentSyncEvent.bind(this)
|
2017-10-02 12:11:38 +00:00
|
|
|
);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
batch(): WriteBatch {
|
|
|
|
return new WriteBatch(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param collectionPath
|
|
|
|
* @returns {CollectionReference}
|
|
|
|
*/
|
|
|
|
collection(collectionPath: string): CollectionReference {
|
2017-11-28 09:21:41 +00:00
|
|
|
const path = this._referencePath.child(collectionPath);
|
2017-09-26 13:57:25 +00:00
|
|
|
if (!path.isCollection) {
|
|
|
|
throw new Error('Argument "collectionPath" must point to a collection.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new CollectionReference(this, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param documentPath
|
|
|
|
* @returns {DocumentReference}
|
|
|
|
*/
|
|
|
|
doc(documentPath: string): DocumentReference {
|
2017-11-28 09:21:41 +00:00
|
|
|
const path = this._referencePath.child(documentPath);
|
2017-09-26 13:57:25 +00:00
|
|
|
if (!path.isDocument) {
|
|
|
|
throw new Error('Argument "documentPath" must point to a document.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DocumentReference(this, path);
|
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
enablePersistence(): Promise<void> {
|
|
|
|
throw new Error('Persistence is enabled by default on the Firestore SDKs');
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
runTransaction(): Promise<any> {
|
2017-10-05 09:18:24 +00:00
|
|
|
throw new Error('firebase.firestore().runTransaction() coming soon');
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
setLogLevel(): void {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD(
|
|
|
|
'firestore',
|
|
|
|
'setLogLevel'
|
|
|
|
)
|
|
|
|
);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
settings(): void {
|
2017-10-05 09:18:24 +00:00
|
|
|
throw new Error('firebase.firestore().settings() coming soon');
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
2017-12-22 15:24:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal collection sync listener
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onCollectionSyncEvent(event: CollectionSyncEvent) {
|
|
|
|
if (event.error) {
|
2018-01-25 18:25:39 +00:00
|
|
|
SharedEventEmitter.emit(
|
|
|
|
getAppEventName(this, `onQuerySnapshotError:${event.listenerId}`),
|
|
|
|
event.error
|
|
|
|
);
|
2017-12-22 15:24:31 +00:00
|
|
|
} else {
|
2018-01-25 18:25:39 +00:00
|
|
|
SharedEventEmitter.emit(
|
|
|
|
getAppEventName(this, `onQuerySnapshot:${event.listenerId}`),
|
|
|
|
event.querySnapshot
|
|
|
|
);
|
2017-12-22 15:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal document sync listener
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onDocumentSyncEvent(event: DocumentSyncEvent) {
|
|
|
|
if (event.error) {
|
2018-01-25 18:25:39 +00:00
|
|
|
SharedEventEmitter.emit(
|
|
|
|
getAppEventName(this, `onDocumentSnapshotError:${event.listenerId}`),
|
|
|
|
event.error
|
|
|
|
);
|
2017-12-22 15:24:31 +00:00
|
|
|
} else {
|
2018-01-25 18:25:39 +00:00
|
|
|
SharedEventEmitter.emit(
|
|
|
|
getAppEventName(this, `onDocumentSnapshot:${event.listenerId}`),
|
|
|
|
event.documentSnapshot
|
|
|
|
);
|
2017-12-22 15:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const statics = {
|
2018-01-11 18:28:14 +00:00
|
|
|
FieldPath,
|
2017-10-12 08:00:46 +00:00
|
|
|
FieldValue,
|
2017-10-05 09:18:24 +00:00
|
|
|
GeoPoint,
|
2018-01-03 20:00:38 +00:00
|
|
|
enableLogging(enabled: boolean) {
|
|
|
|
if (NativeModules[MODULE_NAME]) {
|
|
|
|
NativeModules[MODULE_NAME].enableLogging(enabled);
|
2017-11-28 07:41:55 +00:00
|
|
|
}
|
|
|
|
},
|
2017-09-26 13:57:25 +00:00
|
|
|
};
|