react-native-firebase/lib/modules/database/snapshot.js

124 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-03-02 11:40:08 +00:00
/**
* @flow
*/
import Reference from './reference.js';
import { isObject, deepGet, deepExists } from './../../utils';
export default class Snapshot {
static key: String;
static value: Object;
static exists: boolean;
static hasChildren: boolean;
static childKeys: String[];
ref: Object;
key: string;
value: any;
exists: boolean;
priority: any;
childKeys: Array<string>;
constructor(ref: Reference, snapshot: Object) {
this.ref = ref;
this.key = snapshot.key;
this.value = snapshot.value;
this.priority = snapshot.priority === undefined ? null : snapshot.priority;
this.childKeys = snapshot.childKeys || [];
}
/*
* DEFAULT API METHODS
*/
/**
* 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-02 11:40:08 +00:00
return this.value;
}
/**
* 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-02 11:40:08 +00:00
const value = deepGet(this.value, path);
const childRef = this.ref.child(path);
return new Snapshot(childRef, {
value,
key: childRef.key,
exists: value !== null,
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) : [],
});
}
/**
* Returns true if this DataSnapshot contains any data.
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#exists
* @returns {boolean}
*/
exists(): Boolean {
2017-03-02 11:40:08 +00:00
return this.value !== null;
}
/**
* Enumerates the top-level children in the DataSnapshot.
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#forEach
* @param action
*/
forEach(action: (key: any) => any): Boolean {
if (!this.childKeys.length) return false;
let cancelled = false;
for (let i = 0, len = this.childKeys.length; i < len; i++) {
const key = this.childKeys[i];
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
}
getPriority() {
return this.priority;
}
hasChild(path: string) {
return deepExists(this.value, path);
}
hasChildren() {
return this.numChildren() > 0;
}
numChildren() {
if (!isObject(this.value)) return 0;
return Object.keys(this.value).length;
}
/*
* EXTRA API METHODS
*/
map(fn: (key: string) => mixed) {
const arr = [];
this.forEach((item, i) => arr.push(fn(item, i)));
return arr;
}
reverseMap(fn: (key: string) => mixed) {
return this.map(fn).reverse();
}
}