2017-03-02 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
import Reference from './reference.js';
|
|
|
|
import { isObject, deepGet, deepExists } from './../../utils';
|
|
|
|
|
2017-03-09 18:18:10 +00:00
|
|
|
/**
|
2017-05-06 13:33:55 +00:00
|
|
|
* @class DataSnapshot
|
2017-03-09 18:18:10 +00:00
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot
|
|
|
|
*/
|
2017-03-02 11:40:08 +00:00
|
|
|
export default class Snapshot {
|
|
|
|
ref: Object;
|
|
|
|
key: string;
|
2017-03-16 16:48:03 +00:00
|
|
|
|
|
|
|
_value: any;
|
|
|
|
_priority: any;
|
|
|
|
_childKeys: Array<string>;
|
2017-03-02 11:40:08 +00:00
|
|
|
|
|
|
|
constructor(ref: Reference, snapshot: Object) {
|
|
|
|
this.key = snapshot.key;
|
2017-03-16 16:48:03 +00:00
|
|
|
|
2017-07-04 15:16:08 +00:00
|
|
|
if (ref.key !== snapshot.key) {
|
|
|
|
this.ref = ref.child(snapshot.key);
|
|
|
|
} else {
|
|
|
|
this.ref = ref;
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:48:03 +00:00
|
|
|
// internal use only
|
|
|
|
this._value = snapshot.value;
|
|
|
|
this._priority = snapshot.priority === undefined ? null : snapshot.priority;
|
|
|
|
this._childKeys = snapshot.childKeys || [];
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:53:35 +00:00
|
|
|
/**
|
|
|
|
* Extracts a JavaScript value from a DataSnapshot.
|
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#val
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
|
|
|
val(): any {
|
2017-03-16 16:48:03 +00:00
|
|
|
// clone via JSON stringify/parse - prevent modification of this._value
|
|
|
|
if (isObject(this._value) || Array.isArray(this._value)) return JSON.parse(JSON.stringify(this._value));
|
|
|
|
return this._value;
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:53:35 +00:00
|
|
|
/**
|
|
|
|
* Gets another DataSnapshot for the location at the specified relative path.
|
|
|
|
* @param path
|
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#forEach
|
|
|
|
* @returns {Snapshot}
|
|
|
|
*/
|
|
|
|
child(path: string): Snapshot {
|
2017-03-16 16:48:03 +00:00
|
|
|
const value = deepGet(this._value, path);
|
2017-03-02 11:40:08 +00:00
|
|
|
const childRef = this.ref.child(path);
|
|
|
|
return new Snapshot(childRef, {
|
|
|
|
value,
|
|
|
|
key: childRef.key,
|
|
|
|
exists: value !== null,
|
2017-03-09 18:18:10 +00:00
|
|
|
|
2017-03-09 17:18:48 +00:00
|
|
|
// todo this is wrong - child keys needs to be the ordered keys, from FB
|
|
|
|
// todo potential solution is build up a tree/map of a snapshot and its children
|
|
|
|
// todo natively and send that back to JS to be use in this class.
|
2017-03-02 11:40:08 +00:00
|
|
|
childKeys: isObject(value) ? Object.keys(value) : [],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-09 17:53:35 +00:00
|
|
|
/**
|
|
|
|
* Returns true if this DataSnapshot contains any data.
|
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#exists
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2017-04-04 16:58:20 +00:00
|
|
|
exists(): boolean {
|
2017-03-16 16:48:03 +00:00
|
|
|
return this._value !== null;
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:53:35 +00:00
|
|
|
/**
|
|
|
|
* Enumerates the top-level children in the DataSnapshot.
|
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#forEach
|
|
|
|
* @param action
|
|
|
|
*/
|
2017-04-04 16:58:20 +00:00
|
|
|
forEach(action: (key: any) => any): boolean {
|
2017-03-16 16:48:03 +00:00
|
|
|
if (!this._childKeys.length) return false;
|
2017-03-09 17:53:35 +00:00
|
|
|
let cancelled = false;
|
|
|
|
|
2017-03-16 16:48:03 +00:00
|
|
|
for (let i = 0, len = this._childKeys.length; i < len; i++) {
|
|
|
|
const key = this._childKeys[i];
|
2017-03-09 17:53:35 +00:00
|
|
|
const childSnapshot = this.child(key);
|
|
|
|
const returnValue = action(childSnapshot);
|
|
|
|
|
|
|
|
if (returnValue === true) {
|
|
|
|
cancelled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cancelled;
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 18:18:10 +00:00
|
|
|
/**
|
|
|
|
* Gets the priority value of the data in this DataSnapshot.
|
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#getPriority
|
|
|
|
* @returns {String|Number|null}
|
|
|
|
*/
|
2017-04-04 16:58:20 +00:00
|
|
|
getPriority(): string | number | null {
|
2017-03-16 16:48:03 +00:00
|
|
|
return this._priority;
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 18:18:10 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the specified child path has (non-null) data.
|
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#hasChild
|
|
|
|
* @param path
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
2017-04-04 16:58:20 +00:00
|
|
|
hasChild(path: string): boolean {
|
2017-03-16 16:48:03 +00:00
|
|
|
return deepExists(this._value, path);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 18:18:10 +00:00
|
|
|
/**
|
|
|
|
* Returns whether or not the DataSnapshot has any non-null child properties.
|
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#hasChildren
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2017-04-04 16:58:20 +00:00
|
|
|
hasChildren(): boolean {
|
2017-03-02 11:40:08 +00:00
|
|
|
return this.numChildren() > 0;
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:18:10 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of child properties of this DataSnapshot.
|
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#numChildren
|
|
|
|
* @returns {Number}
|
|
|
|
*/
|
2017-04-04 16:58:20 +00:00
|
|
|
numChildren(): number {
|
2017-03-16 16:48:03 +00:00
|
|
|
if (!isObject(this._value)) return 0;
|
|
|
|
return Object.keys(this._value).length;
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 18:18:10 +00:00
|
|
|
/**
|
|
|
|
* Returns a JSON-serializable representation of this object.
|
|
|
|
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#toJSON
|
|
|
|
* @returns {any}
|
2017-03-02 11:40:08 +00:00
|
|
|
*/
|
2017-03-09 18:18:10 +00:00
|
|
|
toJSON(): any {
|
|
|
|
return this.val();
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|