2017-03-02 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
2017-12-04 12:07:41 +00:00
|
|
|
* Database Reference representation wrapper
|
2017-03-02 11:40:08 +00:00
|
|
|
*/
|
|
|
|
import Query from './query.js';
|
|
|
|
import Snapshot from './snapshot';
|
|
|
|
import Disconnect from './disconnect';
|
2017-12-22 15:24:31 +00:00
|
|
|
import { getLogger } from '../../utils/log';
|
2018-01-05 17:20:02 +00:00
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-11-23 17:29:40 +00:00
|
|
|
import ReferenceBase from '../../utils/ReferenceBase';
|
2017-08-15 20:29:50 +00:00
|
|
|
|
2017-08-14 10:05:49 +00:00
|
|
|
import {
|
|
|
|
promiseOrCallback,
|
|
|
|
isFunction,
|
|
|
|
isObject,
|
|
|
|
isString,
|
|
|
|
tryJSONParse,
|
|
|
|
tryJSONStringify,
|
|
|
|
generatePushID,
|
2017-11-23 17:29:40 +00:00
|
|
|
} from '../../utils';
|
2017-03-02 11:40:08 +00:00
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
import SyncTree from '../../utils/SyncTree';
|
2017-08-15 20:29:50 +00:00
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
import type Database from './';
|
2017-11-23 17:29:40 +00:00
|
|
|
import type { DatabaseModifier, FirebaseError } from '../../types';
|
|
|
|
|
2017-08-15 20:29:50 +00:00
|
|
|
// track all event registrations by path
|
2017-08-16 20:43:24 +00:00
|
|
|
let listeners = 0;
|
2017-03-02 11:40:08 +00:00
|
|
|
|
|
|
|
/**
|
2017-05-06 13:33:55 +00:00
|
|
|
* Enum for event types
|
|
|
|
* @readonly
|
|
|
|
* @enum {String}
|
|
|
|
*/
|
|
|
|
const ReferenceEventTypes = {
|
|
|
|
value: 'value',
|
|
|
|
child_added: 'child_added',
|
|
|
|
child_removed: 'child_removed',
|
|
|
|
child_changed: 'child_changed',
|
|
|
|
child_moved: 'child_moved',
|
|
|
|
};
|
|
|
|
|
2017-11-23 17:29:40 +00:00
|
|
|
type DatabaseListener = {
|
|
|
|
listenerId: number;
|
|
|
|
eventName: string;
|
|
|
|
successCallback: Function;
|
|
|
|
failureCallback?: Function;
|
|
|
|
}
|
|
|
|
|
2017-05-06 13:33:55 +00:00
|
|
|
/**
|
|
|
|
* @typedef {String} ReferenceLocation - Path to location in the database, relative
|
|
|
|
* to the root reference. Consists of a path where segments are separated by a
|
|
|
|
* forward slash (/) and ends in a ReferenceKey - except the root location, which
|
|
|
|
* has no ReferenceKey.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // root reference location: '/'
|
|
|
|
* // non-root reference: '/path/to/referenceKey'
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {String} ReferenceKey - Identifier for each location that is unique to that
|
|
|
|
* location, within the scope of its parent. The last part of a ReferenceLocation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a specific location in your Database that can be used for
|
|
|
|
* reading or writing data.
|
|
|
|
*
|
|
|
|
* You can reference the root using firebase.database().ref() or a child location
|
|
|
|
* by calling firebase.database().ref("child/path").
|
|
|
|
*
|
2017-03-10 10:46:55 +00:00
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.Reference
|
2017-03-02 11:40:08 +00:00
|
|
|
* @class Reference
|
2017-05-06 13:33:55 +00:00
|
|
|
* @extends ReferenceBase
|
2017-03-02 11:40:08 +00:00
|
|
|
*/
|
|
|
|
export default class Reference extends ReferenceBase {
|
2017-12-22 15:24:31 +00:00
|
|
|
_database: Database;
|
2017-12-04 12:07:41 +00:00
|
|
|
_promise: ?Promise<*>;
|
2017-07-31 17:25:31 +00:00
|
|
|
_query: Query;
|
2017-12-04 12:07:41 +00:00
|
|
|
_refListeners: { [listenerId: number]: DatabaseListener };
|
2017-03-02 11:40:08 +00:00
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
constructor(database: Database, path: string, existingModifiers?: Array<DatabaseModifier>) {
|
2018-01-05 17:20:02 +00:00
|
|
|
super(path);
|
2017-07-31 17:25:31 +00:00
|
|
|
this._promise = null;
|
|
|
|
this._refListeners = {};
|
|
|
|
this._database = database;
|
|
|
|
this._query = new Query(this, path, existingModifiers);
|
2017-12-22 15:24:31 +00:00
|
|
|
getLogger(database).debug('Created new Reference', this._getRefKey());
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-05 20:49:28 +00:00
|
|
|
* By calling `keepSynced(true)` on a location, the data for that location will
|
|
|
|
* automatically be downloaded and kept in sync, even when no listeners are
|
|
|
|
* attached for that location. Additionally, while a location is kept synced,
|
|
|
|
* it will not be evicted from the persistent disk cache.
|
2017-03-02 11:40:08 +00:00
|
|
|
*
|
2017-08-05 20:49:28 +00:00
|
|
|
* @link https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query.html#keepSynced(boolean)
|
2017-03-02 11:40:08 +00:00
|
|
|
* @param bool
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
keepSynced(bool: boolean): Promise<void> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this._database).keepSynced(this._getRefKey(), this.path, this._query.getModifiers(), bool);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-05 20:49:28 +00:00
|
|
|
* Writes data to this Database location.
|
2017-03-02 11:40:08 +00:00
|
|
|
*
|
2017-08-05 20:49:28 +00:00
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.Reference#set
|
2017-03-02 11:40:08 +00:00
|
|
|
* @param value
|
2017-08-05 20:49:28 +00:00
|
|
|
* @param onComplete
|
|
|
|
* @returns {Promise}
|
2017-03-02 11:40:08 +00:00
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
set(value: any, onComplete?: Function): Promise<void> {
|
2017-08-05 20:49:28 +00:00
|
|
|
return promiseOrCallback(
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this._database).set(this.path, this._serializeAnyType(value)),
|
2017-08-05 20:49:28 +00:00
|
|
|
onComplete,
|
|
|
|
);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 17:18:16 +00:00
|
|
|
/**
|
2017-08-05 20:49:28 +00:00
|
|
|
* Sets a priority for the data at this Database location.
|
2017-07-19 17:18:16 +00:00
|
|
|
*
|
2017-08-05 20:49:28 +00:00
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.Reference#setPriority
|
2017-07-19 17:18:16 +00:00
|
|
|
* @param priority
|
2017-08-05 20:49:28 +00:00
|
|
|
* @param onComplete
|
|
|
|
* @returns {Promise}
|
2017-07-19 17:18:16 +00:00
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
setPriority(priority: string | number | null, onComplete?: Function): Promise<void> {
|
2017-07-19 17:18:16 +00:00
|
|
|
const _priority = this._serializeAnyType(priority);
|
2017-08-05 20:49:28 +00:00
|
|
|
|
|
|
|
return promiseOrCallback(
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this._database).setPriority(this.path, _priority),
|
2017-08-05 20:49:28 +00:00
|
|
|
onComplete,
|
|
|
|
);
|
2017-07-19 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-05 20:49:28 +00:00
|
|
|
* Writes data the Database location. Like set() but also specifies the priority for that data.
|
2017-07-19 17:18:16 +00:00
|
|
|
*
|
2017-08-05 20:49:28 +00:00
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.Reference#setWithPriority
|
2017-07-30 06:34:41 +00:00
|
|
|
* @param value
|
2017-07-19 17:18:16 +00:00
|
|
|
* @param priority
|
2017-08-05 20:49:28 +00:00
|
|
|
* @param onComplete
|
|
|
|
* @returns {Promise}
|
2017-07-19 17:18:16 +00:00
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
setWithPriority(value: any, priority: string | number | null, onComplete?: Function): Promise<void> {
|
2017-07-19 17:18:16 +00:00
|
|
|
const _value = this._serializeAnyType(value);
|
2017-07-30 06:34:41 +00:00
|
|
|
const _priority = this._serializeAnyType(priority);
|
2017-08-05 20:49:28 +00:00
|
|
|
|
|
|
|
return promiseOrCallback(
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this._database).setWithPriority(this.path, _value, _priority),
|
2017-08-05 20:49:28 +00:00
|
|
|
onComplete,
|
|
|
|
);
|
2017-07-19 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 11:40:08 +00:00
|
|
|
/**
|
2017-08-05 20:49:28 +00:00
|
|
|
* Writes multiple values to the Database at once.
|
2017-03-02 11:40:08 +00:00
|
|
|
*
|
2017-08-05 20:49:28 +00:00
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.Reference#update
|
2017-03-02 11:40:08 +00:00
|
|
|
* @param val
|
2017-08-05 20:49:28 +00:00
|
|
|
* @param onComplete
|
|
|
|
* @returns {Promise}
|
2017-03-02 11:40:08 +00:00
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
update(val: Object, onComplete?: Function): Promise<void> {
|
2017-03-02 11:40:08 +00:00
|
|
|
const value = this._serializeObject(val);
|
2017-08-05 20:49:28 +00:00
|
|
|
|
|
|
|
return promiseOrCallback(
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this._database).update(this.path, value),
|
2017-08-05 20:49:28 +00:00
|
|
|
onComplete,
|
|
|
|
);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-05 20:49:28 +00:00
|
|
|
* Removes the data at this Database location.
|
2017-03-02 11:40:08 +00:00
|
|
|
*
|
2017-08-05 20:49:28 +00:00
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.Reference#remove
|
|
|
|
* @param onComplete
|
|
|
|
* @return {Promise}
|
2017-03-02 11:40:08 +00:00
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
remove(onComplete?: Function): Promise<void> {
|
2017-08-05 20:49:28 +00:00
|
|
|
return promiseOrCallback(
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this._database).remove(this.path),
|
2017-08-05 20:49:28 +00:00
|
|
|
onComplete,
|
|
|
|
);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-30 06:34:41 +00:00
|
|
|
* Atomically modifies the data at this location.
|
2017-08-05 20:49:28 +00:00
|
|
|
*
|
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.Reference#transaction
|
2017-07-30 06:34:41 +00:00
|
|
|
* @param transactionUpdate
|
2017-03-02 11:40:08 +00:00
|
|
|
* @param onComplete
|
2017-07-30 06:34:41 +00:00
|
|
|
* @param applyLocally
|
2017-03-07 16:54:04 +00:00
|
|
|
*/
|
2017-08-07 08:46:05 +00:00
|
|
|
transaction(
|
|
|
|
transactionUpdate: Function,
|
|
|
|
onComplete: (error: ?Error, committed: boolean, snapshot: ?Snapshot) => *,
|
|
|
|
applyLocally: boolean = false,
|
|
|
|
) {
|
2017-07-30 06:34:41 +00:00
|
|
|
if (!isFunction(transactionUpdate)) {
|
2017-12-04 12:07:41 +00:00
|
|
|
return Promise.reject(new Error('Missing transactionUpdate function argument.'));
|
2017-05-06 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 06:34:41 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const onCompleteWrapper = (error, committed, snapshotData) => {
|
2017-08-05 21:00:06 +00:00
|
|
|
if (isFunction(onComplete)) {
|
2017-12-04 12:07:41 +00:00
|
|
|
if (error) {
|
|
|
|
onComplete(error, committed, null);
|
2017-11-09 15:34:52 +00:00
|
|
|
} else {
|
|
|
|
onComplete(null, committed, new Snapshot(this, snapshotData));
|
|
|
|
}
|
2017-07-30 06:34:41 +00:00
|
|
|
}
|
2017-05-06 13:33:55 +00:00
|
|
|
|
2017-08-05 21:00:06 +00:00
|
|
|
if (error) return reject(error);
|
|
|
|
return resolve({ committed, snapshot: new Snapshot(this, snapshotData) });
|
2017-07-30 06:34:41 +00:00
|
|
|
};
|
2017-05-06 13:33:55 +00:00
|
|
|
|
2017-08-07 08:46:05 +00:00
|
|
|
// start the transaction natively
|
2017-07-31 17:25:31 +00:00
|
|
|
this._database._transactionHandler.add(this, transactionUpdate, onCompleteWrapper, applyLocally);
|
2017-07-30 06:34:41 +00:00
|
|
|
});
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 06:34:41 +00:00
|
|
|
|
2017-03-07 16:54:04 +00:00
|
|
|
/**
|
|
|
|
*
|
2017-04-26 11:21:53 +00:00
|
|
|
* @param eventName
|
2017-03-07 16:54:04 +00:00
|
|
|
* @param successCallback
|
2017-08-07 08:46:05 +00:00
|
|
|
* @param cancelOrContext
|
|
|
|
* @param context
|
2017-07-30 06:34:41 +00:00
|
|
|
* @returns {Promise.<any>}
|
2017-03-07 16:54:04 +00:00
|
|
|
*/
|
2017-08-07 08:46:05 +00:00
|
|
|
once(
|
|
|
|
eventName: string = 'value',
|
|
|
|
successCallback: (snapshot: Object) => void,
|
|
|
|
cancelOrContext: (error: FirebaseError) => void,
|
|
|
|
context?: Object,
|
|
|
|
) {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this._database).once(this._getRefKey(), this.path, this._query.getModifiers(), eventName)
|
2017-08-07 08:46:05 +00:00
|
|
|
.then(({ snapshot }) => {
|
|
|
|
const _snapshot = new Snapshot(this, snapshot);
|
|
|
|
|
|
|
|
if (isFunction(successCallback)) {
|
|
|
|
if (isObject(cancelOrContext)) successCallback.bind(cancelOrContext)(_snapshot);
|
|
|
|
if (context && isObject(context)) successCallback.bind(context)(_snapshot);
|
|
|
|
successCallback(_snapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _snapshot;
|
2017-03-07 16:54:04 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2017-08-07 08:46:05 +00:00
|
|
|
if (isFunction(cancelOrContext)) return cancelOrContext(error);
|
2017-07-30 06:34:41 +00:00
|
|
|
return error;
|
2017-03-02 11:40:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-07 17:35:48 +00:00
|
|
|
/**
|
2017-05-06 13:33:55 +00:00
|
|
|
*
|
2017-07-30 06:34:41 +00:00
|
|
|
* @param value
|
2017-03-24 22:53:56 +00:00
|
|
|
* @param onComplete
|
2017-07-30 06:34:41 +00:00
|
|
|
* @returns {*}
|
2017-03-24 22:53:56 +00:00
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
push(value: any, onComplete?: Function): Reference | Promise<void> {
|
2017-07-30 06:34:41 +00:00
|
|
|
if (value === null || value === undefined) {
|
2017-10-05 11:45:54 +00:00
|
|
|
return new Reference(this._database, `${this.path}/${generatePushID(this._database._serverTimeOffset)}`);
|
2017-05-10 16:37:03 +00:00
|
|
|
}
|
2017-03-24 22:53:56 +00:00
|
|
|
|
2017-10-05 11:45:54 +00:00
|
|
|
const newRef = new Reference(this._database, `${this.path}/${generatePushID(this._database._serverTimeOffset)}`);
|
2017-07-30 06:34:41 +00:00
|
|
|
const promise = newRef.set(value);
|
2017-03-24 22:53:56 +00:00
|
|
|
|
2017-08-14 12:30:44 +00:00
|
|
|
// if callback provided then internally call the set promise with value
|
|
|
|
if (isFunction(onComplete)) {
|
|
|
|
return promise
|
2018-01-05 17:20:02 +00:00
|
|
|
// $FlowBug: Reports that onComplete can change to null despite the null check: https://github.com/facebook/flow/issues/1655
|
2017-08-14 12:30:44 +00:00
|
|
|
.then(() => onComplete(null, newRef))
|
2018-01-05 17:20:02 +00:00
|
|
|
// $FlowBug: Reports that onComplete can change to null despite the null check: https://github.com/facebook/flow/issues/1655
|
2017-08-14 12:30:44 +00:00
|
|
|
.catch(error => onComplete(error, null));
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise attach promise to 'thenable' reference and return the
|
|
|
|
// new reference
|
|
|
|
newRef._setThenable(promise);
|
|
|
|
return newRef;
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MODIFIERS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {Reference}
|
|
|
|
*/
|
|
|
|
orderByKey(): Reference {
|
2017-08-28 12:28:16 +00:00
|
|
|
return this.orderBy('orderByKey');
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {Reference}
|
|
|
|
*/
|
|
|
|
orderByPriority(): Reference {
|
2017-08-28 12:28:16 +00:00
|
|
|
return this.orderBy('orderByPriority');
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {Reference}
|
|
|
|
*/
|
|
|
|
orderByValue(): Reference {
|
2017-08-28 12:28:16 +00:00
|
|
|
return this.orderBy('orderByValue');
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param key
|
|
|
|
* @returns {Reference}
|
|
|
|
*/
|
|
|
|
orderByChild(key: string): Reference {
|
2017-08-28 12:28:16 +00:00
|
|
|
return this.orderBy('orderByChild', key);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param key
|
|
|
|
* @returns {Reference}
|
|
|
|
*/
|
|
|
|
orderBy(name: string, key?: string): Reference {
|
2017-07-31 17:25:31 +00:00
|
|
|
const newRef = new Reference(this._database, this.path, this._query.getModifiers());
|
2017-08-05 20:49:28 +00:00
|
|
|
newRef._query.orderBy(name, key);
|
2017-03-02 11:40:08 +00:00
|
|
|
return newRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LIMITS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param limit
|
|
|
|
* @returns {Reference}
|
|
|
|
*/
|
|
|
|
limitToLast(limit: number): Reference {
|
2017-08-28 12:28:16 +00:00
|
|
|
return this.limit('limitToLast', limit);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param limit
|
|
|
|
* @returns {Reference}
|
|
|
|
*/
|
|
|
|
limitToFirst(limit: number): Reference {
|
2017-08-28 12:28:16 +00:00
|
|
|
return this.limit('limitToFirst', limit);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param limit
|
|
|
|
* @returns {Reference}
|
|
|
|
*/
|
|
|
|
limit(name: string, limit: number): Reference {
|
2017-07-31 17:25:31 +00:00
|
|
|
const newRef = new Reference(this._database, this.path, this._query.getModifiers());
|
2017-08-05 20:49:28 +00:00
|
|
|
newRef._query.limit(name, limit);
|
2017-03-02 11:40:08 +00:00
|
|
|
return newRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FILTERS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* @param key
|
|
|
|
* @returns {Reference}
|
|
|
|
*/
|
|
|
|
equalTo(value: any, key?: string): Reference {
|
2017-08-28 12:28:16 +00:00
|
|
|
return this.filter('equalTo', value, key);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* @param key
|
|
|
|
* @returns {Reference}
|
|
|
|
*/
|
|
|
|
endAt(value: any, key?: string): Reference {
|
2017-08-28 12:28:16 +00:00
|
|
|
return this.filter('endAt', value, key);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* @param key
|
|
|
|
* @returns {Reference}
|
|
|
|
*/
|
|
|
|
startAt(value: any, key?: string): Reference {
|
2017-08-28 12:28:16 +00:00
|
|
|
return this.filter('startAt', value, key);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param value
|
|
|
|
* @param key
|
|
|
|
* @returns {Reference}
|
|
|
|
*/
|
|
|
|
filter(name: string, value: any, key?: string): Reference {
|
2017-07-31 17:25:31 +00:00
|
|
|
const newRef = new Reference(this._database, this.path, this._query.getModifiers());
|
2017-08-05 20:49:28 +00:00
|
|
|
newRef._query.filter(name, value, key);
|
2017-03-02 11:40:08 +00:00
|
|
|
return newRef;
|
|
|
|
}
|
|
|
|
|
2017-03-08 13:21:21 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {Disconnect}
|
|
|
|
*/
|
2017-09-07 15:36:47 +00:00
|
|
|
onDisconnect(): Disconnect {
|
|
|
|
return new Disconnect(this);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:21:21 +00:00
|
|
|
/**
|
2017-05-06 13:33:55 +00:00
|
|
|
* Creates a Reference to a child of the current Reference, using a relative path.
|
|
|
|
* No validation is performed on the path to ensure it has a valid format.
|
|
|
|
* @param {String} path relative to current ref's location
|
|
|
|
* @returns {!Reference} A new Reference to the path provided, relative to the current
|
|
|
|
* Reference
|
|
|
|
* {@link https://firebase.google.com/docs/reference/js/firebase.database.Reference#child}
|
2017-03-08 13:21:21 +00:00
|
|
|
*/
|
2017-09-07 15:36:47 +00:00
|
|
|
child(path: string): Reference {
|
2017-07-31 17:25:31 +00:00
|
|
|
return new Reference(this._database, `${this.path}/${path}`);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:21:21 +00:00
|
|
|
/**
|
|
|
|
* Return the ref as a path string
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-03-02 11:40:08 +00:00
|
|
|
toString(): string {
|
2017-03-24 22:53:56 +00:00
|
|
|
return this.path;
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-22 16:59:04 +00:00
|
|
|
/**
|
|
|
|
* Returns whether another Reference represent the same location and are from the
|
|
|
|
* same instance of firebase.app.App - multiple firebase apps not currently supported.
|
|
|
|
* @param {Reference} otherRef - Other reference to compare to this one
|
|
|
|
* @return {Boolean} Whether otherReference is equal to this one
|
2017-05-06 13:33:55 +00:00
|
|
|
*
|
2017-04-22 16:59:04 +00:00
|
|
|
* {@link https://firebase.google.com/docs/reference/js/firebase.database.Reference#isEqual}
|
|
|
|
*/
|
|
|
|
isEqual(otherRef: Reference): boolean {
|
2017-08-14 12:51:52 +00:00
|
|
|
return !!otherRef
|
|
|
|
&& otherRef.constructor === Reference
|
|
|
|
&& otherRef.key === this.key
|
|
|
|
&& this._query.queryIdentifier() === otherRef._query.queryIdentifier();
|
2017-04-22 16:59:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* GETTERS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-05-06 13:33:55 +00:00
|
|
|
* The parent location of a Reference, or null for the root Reference.
|
|
|
|
* @type {Reference}
|
|
|
|
*
|
|
|
|
* {@link https://firebase.google.com/docs/reference/js/firebase.database.Reference#parent}
|
2017-03-02 11:40:08 +00:00
|
|
|
*/
|
2017-06-30 16:23:32 +00:00
|
|
|
get parent(): Reference | null {
|
2017-03-02 11:40:08 +00:00
|
|
|
if (this.path === '/') return null;
|
2017-07-31 17:25:31 +00:00
|
|
|
return new Reference(this._database, this.path.substring(0, this.path.lastIndexOf('/')));
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-22 08:27:37 +00:00
|
|
|
/**
|
|
|
|
* A reference to itself
|
|
|
|
* @type {!Reference}
|
2017-05-06 13:33:55 +00:00
|
|
|
*
|
2017-04-22 08:27:37 +00:00
|
|
|
* {@link https://firebase.google.com/docs/reference/js/firebase.database.Reference#ref}
|
|
|
|
*/
|
|
|
|
get ref(): Reference {
|
|
|
|
return this;
|
|
|
|
}
|
2017-03-02 11:40:08 +00:00
|
|
|
|
|
|
|
/**
|
2017-05-06 13:33:55 +00:00
|
|
|
* Reference to the root of the database: '/'
|
|
|
|
* @type {!Reference}
|
|
|
|
*
|
|
|
|
* {@link https://firebase.google.com/docs/reference/js/firebase.database.Reference#root}
|
2017-03-02 11:40:08 +00:00
|
|
|
*/
|
|
|
|
get root(): Reference {
|
2017-07-31 17:25:31 +00:00
|
|
|
return new Reference(this._database, '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access then method of promise if set
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
then(fnResolve: (any) => any, fnReject: (any) => any) {
|
2017-09-20 10:50:34 +00:00
|
|
|
if (isFunction(fnResolve) && this._promise && this._promise.then) {
|
|
|
|
return this._promise.then.bind(this._promise)((result) => {
|
2017-08-14 17:42:39 +00:00
|
|
|
this._promise = null;
|
2017-09-20 10:50:34 +00:00
|
|
|
return fnResolve(result);
|
|
|
|
}, (possibleErr) => {
|
|
|
|
this._promise = null;
|
2017-09-20 10:53:50 +00:00
|
|
|
|
2017-09-20 10:50:34 +00:00
|
|
|
if (isFunction(fnReject)) {
|
|
|
|
return fnReject(possibleErr);
|
|
|
|
}
|
2017-09-20 10:53:50 +00:00
|
|
|
|
2017-09-20 10:50:34 +00:00
|
|
|
throw possibleErr;
|
2017-08-14 17:42:39 +00:00
|
|
|
});
|
2017-07-31 17:25:31 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 10:53:50 +00:00
|
|
|
throw new Error("Cannot read property 'then' of undefined.");
|
2017-07-31 17:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access catch method of promise if set
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
catch(fnReject: (any) => any) {
|
2017-09-20 10:50:34 +00:00
|
|
|
if (isFunction(fnReject) && this._promise && this._promise.catch) {
|
|
|
|
return this._promise.catch.bind(this._promise)((possibleErr) => {
|
2017-08-14 17:42:39 +00:00
|
|
|
this._promise = null;
|
2017-09-20 10:50:34 +00:00
|
|
|
return fnReject(possibleErr);
|
2017-08-14 17:42:39 +00:00
|
|
|
});
|
2017-07-31 17:25:31 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 10:53:50 +00:00
|
|
|
throw new Error("Cannot read property 'catch' of undefined.");
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* INTERNALS
|
|
|
|
*/
|
|
|
|
|
2017-08-14 10:05:49 +00:00
|
|
|
/**
|
2017-08-15 20:29:50 +00:00
|
|
|
* Generate a unique registration key.
|
|
|
|
*
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
_getRegistrationKey(eventType: string): string {
|
2018-01-03 20:00:38 +00:00
|
|
|
return `$${this._database.app.name}$/${this.path}$${this._query.queryIdentifier()}$${listeners}$${eventType}`;
|
2017-08-15 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a string that uniquely identifies this
|
|
|
|
* combination of path and query modifiers
|
|
|
|
*
|
2017-08-14 10:05:49 +00:00
|
|
|
* @return {string}
|
2017-08-15 20:29:50 +00:00
|
|
|
* @private
|
2017-08-14 10:05:49 +00:00
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
_getRefKey(): string {
|
2018-01-03 20:00:38 +00:00
|
|
|
return `$${this._database.app.name}$/${this.path}$${this._query.queryIdentifier()}`;
|
2017-08-14 10:05:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 17:25:31 +00:00
|
|
|
/**
|
|
|
|
* Set the promise this 'thenable' reference relates to
|
|
|
|
* @param promise
|
|
|
|
* @private
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
_setThenable(promise: Promise<*>) {
|
2017-07-31 17:25:31 +00:00
|
|
|
this._promise = promise;
|
|
|
|
}
|
|
|
|
|
2017-03-02 11:40:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param obj
|
|
|
|
* @returns {Object}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_serializeObject(obj: Object) {
|
|
|
|
if (!isObject(obj)) return obj;
|
|
|
|
|
|
|
|
// json stringify then parse it calls toString on Objects / Classes
|
|
|
|
// that support it i.e new Date() becomes a ISO string.
|
|
|
|
return tryJSONParse(tryJSONStringify(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* @returns {*}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_serializeAnyType(value: any) {
|
|
|
|
if (isObject(value)) {
|
|
|
|
return {
|
|
|
|
type: 'object',
|
|
|
|
value: this._serializeObject(value),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: typeof value,
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
}
|
2017-07-30 06:34:41 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-15 20:29:50 +00:00
|
|
|
* Register a listener for data changes at the current ref's location.
|
|
|
|
* The primary method of reading data from a Database.
|
2017-07-30 06:34:41 +00:00
|
|
|
*
|
2017-08-15 20:29:50 +00:00
|
|
|
* Listeners can be unbound using {@link off}.
|
|
|
|
*
|
|
|
|
* Event Types:
|
|
|
|
*
|
|
|
|
* - value: {@link callback}.
|
|
|
|
* - child_added: {@link callback}
|
|
|
|
* - child_removed: {@link callback}
|
|
|
|
* - child_changed: {@link callback}
|
|
|
|
* - child_moved: {@link callback}
|
|
|
|
*
|
|
|
|
* @param {ReferenceEventType} eventType - Type of event to attach a callback for.
|
|
|
|
* @param {ReferenceEventCallback} callback - Function that will be called
|
|
|
|
* when the event occurs with the new data.
|
|
|
|
* @param {cancelCallbackOrContext=} cancelCallbackOrContext - Optional callback that is called
|
|
|
|
* if the event subscription fails. {@link cancelCallbackOrContext}
|
|
|
|
* @param {*=} context - Optional object to bind the callbacks to when calling them.
|
|
|
|
* @returns {ReferenceEventCallback} callback function, unmodified (unbound), for
|
|
|
|
* convenience if you want to pass an inline function to on() and store it later for
|
|
|
|
* removing using off().
|
|
|
|
*
|
|
|
|
* {@link https://firebase.google.com/docs/reference/js/firebase.database.Reference#on}
|
2017-07-30 06:34:41 +00:00
|
|
|
*/
|
2018-01-05 17:20:02 +00:00
|
|
|
on(eventType: string, callback: (Snapshot) => any, cancelCallbackOrContext?: (Object) => any | Object, context?: Object): Function {
|
2017-08-14 10:05:49 +00:00
|
|
|
if (!eventType) {
|
|
|
|
throw new Error('Query.on failed: Function called with 0 arguments. Expects at least 2.');
|
|
|
|
}
|
2017-07-30 06:34:41 +00:00
|
|
|
|
2017-08-14 10:05:49 +00:00
|
|
|
if (!isString(eventType) || !ReferenceEventTypes[eventType]) {
|
|
|
|
throw new Error(`Query.on failed: First argument must be a valid string event type: "${Object.keys(ReferenceEventTypes).join(', ')}"`);
|
|
|
|
}
|
2017-07-30 06:34:41 +00:00
|
|
|
|
2017-08-14 10:05:49 +00:00
|
|
|
if (!callback) {
|
|
|
|
throw new Error('Query.on failed: Function called with 1 argument. Expects at least 2.');
|
|
|
|
}
|
2017-07-30 06:34:41 +00:00
|
|
|
|
2017-08-14 10:05:49 +00:00
|
|
|
if (!isFunction(callback)) {
|
|
|
|
throw new Error('Query.on failed: Second argument must be a valid function.');
|
|
|
|
}
|
2017-07-30 06:34:41 +00:00
|
|
|
|
2017-08-14 17:42:39 +00:00
|
|
|
if (cancelCallbackOrContext && !isFunction(cancelCallbackOrContext) && !isObject(context) && !isObject(cancelCallbackOrContext)) {
|
|
|
|
throw new Error('Query.on failed: Function called with 3 arguments, but third optional argument `cancelCallbackOrContext` was not a function.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cancelCallbackOrContext && !isFunction(cancelCallbackOrContext) && context) {
|
|
|
|
throw new Error('Query.on failed: Function called with 4 arguments, but third optional argument `cancelCallbackOrContext` was not a function.');
|
2017-08-14 10:05:49 +00:00
|
|
|
}
|
2017-07-30 06:34:41 +00:00
|
|
|
|
2017-08-15 20:29:50 +00:00
|
|
|
const eventRegistrationKey = this._getRegistrationKey(eventType);
|
|
|
|
const registrationCancellationKey = `${eventRegistrationKey}$cancelled`;
|
2017-08-14 17:42:39 +00:00
|
|
|
const _context = (cancelCallbackOrContext && !isFunction(cancelCallbackOrContext)) ? cancelCallbackOrContext : context;
|
2017-08-16 20:43:24 +00:00
|
|
|
const registrationObj = {
|
|
|
|
eventType,
|
|
|
|
ref: this,
|
|
|
|
path: this.path,
|
|
|
|
key: this._getRefKey(),
|
2018-01-03 20:00:38 +00:00
|
|
|
appName: this._database.app.name,
|
2017-08-16 20:43:24 +00:00
|
|
|
eventRegistrationKey,
|
|
|
|
};
|
2017-07-30 06:34:41 +00:00
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
SyncTree.addRegistration({
|
|
|
|
...registrationObj,
|
|
|
|
listener: _context ? callback.bind(_context) : callback,
|
|
|
|
});
|
2017-07-30 06:34:41 +00:00
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
if (cancelCallbackOrContext && isFunction(cancelCallbackOrContext)) {
|
2017-08-15 20:29:50 +00:00
|
|
|
// cancellations have their own separate registration
|
|
|
|
// as these are one off events, and they're not guaranteed
|
|
|
|
// to occur either, only happens on failure to register on native
|
2018-01-05 17:20:02 +00:00
|
|
|
SyncTree.addRegistration({
|
|
|
|
ref: this,
|
|
|
|
once: true,
|
|
|
|
path: this.path,
|
|
|
|
key: this._getRefKey(),
|
|
|
|
appName: this._database.app.name,
|
|
|
|
eventType: `${eventType}$cancelled`,
|
|
|
|
eventRegistrationKey: registrationCancellationKey,
|
|
|
|
listener: _context ? cancelCallbackOrContext.bind(_context) : cancelCallbackOrContext,
|
|
|
|
});
|
2017-07-30 06:34:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 10:05:49 +00:00
|
|
|
// initialise the native listener if not already listening
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this._database).on({
|
2017-08-14 10:05:49 +00:00
|
|
|
eventType,
|
|
|
|
path: this.path,
|
2017-08-15 20:29:50 +00:00
|
|
|
key: this._getRefKey(),
|
2018-01-03 20:00:38 +00:00
|
|
|
appName: this._database.app.name,
|
2017-08-14 10:05:49 +00:00
|
|
|
modifiers: this._query.getModifiers(),
|
2017-08-16 20:43:24 +00:00
|
|
|
hasCancellationCallback: isFunction(cancelCallbackOrContext),
|
2017-08-15 20:29:50 +00:00
|
|
|
registration: {
|
|
|
|
eventRegistrationKey,
|
2017-08-16 20:43:24 +00:00
|
|
|
key: registrationObj.key,
|
2017-08-15 20:29:50 +00:00
|
|
|
registrationCancellationKey,
|
|
|
|
},
|
2017-08-14 10:05:49 +00:00
|
|
|
});
|
2017-07-30 06:34:41 +00:00
|
|
|
|
2017-08-15 20:29:50 +00:00
|
|
|
// increment number of listeners - just s short way of making
|
|
|
|
// every registration unique per .on() call
|
2017-08-16 20:43:24 +00:00
|
|
|
listeners += 1;
|
2017-08-15 20:29:50 +00:00
|
|
|
|
|
|
|
// return original unbound successCallback for
|
|
|
|
// the purposes of calling .off(eventType, callback) at a later date
|
2017-08-14 10:05:49 +00:00
|
|
|
return callback;
|
2017-07-30 06:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-15 20:29:50 +00:00
|
|
|
* Detaches a callback previously attached with on().
|
2017-07-30 06:34:41 +00:00
|
|
|
*
|
2017-08-15 20:29:50 +00:00
|
|
|
* Detach a callback previously attached with on(). Note that if on() was called
|
|
|
|
* multiple times with the same eventType and callback, the callback will be called
|
|
|
|
* multiple times for each event, and off() must be called multiple times to
|
|
|
|
* remove the callback. Calling off() on a parent listener will not automatically
|
|
|
|
* remove listeners registered on child nodes, off() must also be called on any
|
|
|
|
* child listeners to remove the callback.
|
|
|
|
*
|
|
|
|
* If a callback is not specified, all callbacks for the specified eventType will be removed.
|
|
|
|
* Similarly, if no eventType or callback is specified, all callbacks for the Reference will be removed.
|
2017-08-14 10:05:49 +00:00
|
|
|
* @param eventType
|
|
|
|
* @param originalCallback
|
2017-07-30 06:34:41 +00:00
|
|
|
*/
|
|
|
|
off(eventType?: string = '', originalCallback?: () => any) {
|
2017-08-15 20:29:50 +00:00
|
|
|
if (!arguments.length) {
|
|
|
|
// Firebase Docs:
|
|
|
|
// if no eventType or callback is specified, all callbacks for the Reference will be removed.
|
2018-01-03 20:00:38 +00:00
|
|
|
return SyncTree.removeListenersForRegistrations(SyncTree.getRegistrationsByPath(this.path));
|
2017-08-15 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* VALIDATE ARGS
|
|
|
|
*/
|
2017-08-14 10:05:49 +00:00
|
|
|
if (eventType && (!isString(eventType) || !ReferenceEventTypes[eventType])) {
|
|
|
|
throw new Error(`Query.off failed: First argument must be a valid string event type: "${Object.keys(ReferenceEventTypes).join(', ')}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (originalCallback && !isFunction(originalCallback)) {
|
|
|
|
throw new Error('Query.off failed: Function called with 2 arguments, but second optional argument was not a function.');
|
|
|
|
}
|
|
|
|
|
2017-08-15 20:29:50 +00:00
|
|
|
// Firebase Docs:
|
|
|
|
// Note that if on() was called
|
|
|
|
// multiple times with the same eventType and callback, the callback will be called
|
|
|
|
// multiple times for each event, and off() must be called multiple times to
|
|
|
|
// remove the callback.
|
|
|
|
// Remove only a single registration
|
|
|
|
if (eventType && originalCallback) {
|
2018-01-03 20:00:38 +00:00
|
|
|
const registration = SyncTree.getOneByPathEventListener(this.path, eventType, originalCallback);
|
2017-10-29 00:10:34 +00:00
|
|
|
if (!registration) return [];
|
2017-08-15 20:29:50 +00:00
|
|
|
|
|
|
|
// remove the paired cancellation registration if any exist
|
2018-01-03 20:00:38 +00:00
|
|
|
SyncTree.removeListenersForRegistrations([`${registration}$cancelled`]);
|
2017-08-15 20:29:50 +00:00
|
|
|
|
|
|
|
// remove only the first registration to match firebase web sdk
|
|
|
|
// call multiple times to remove multiple registrations
|
2018-01-03 20:00:38 +00:00
|
|
|
return SyncTree.removeListenerRegistrations(originalCallback, [registration]);
|
2017-07-30 06:34:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 20:29:50 +00:00
|
|
|
// Firebase Docs:
|
|
|
|
// If a callback is not specified, all callbacks for the specified eventType will be removed.
|
2018-01-03 20:00:38 +00:00
|
|
|
const registrations = SyncTree.getRegistrationsByPathEvent(this.path, eventType);
|
2017-08-15 20:29:50 +00:00
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
SyncTree.removeListenersForRegistrations(
|
|
|
|
SyncTree.getRegistrationsByPathEvent(this.path, `${eventType}$cancelled`),
|
2017-08-15 20:29:50 +00:00
|
|
|
);
|
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
return SyncTree.removeListenersForRegistrations(registrations);
|
2017-08-15 20:29:50 +00:00
|
|
|
}
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|