2
0
mirror of synced 2025-01-14 16:36:07 +00:00

snapshot.forEach() now correctly implemented - now allows returning true to break iteration as per web sdk docs.

+ misc commenting / flow types
This commit is contained in:
Salakar 2017-03-09 17:53:35 +00:00
parent da8a2ec645
commit 834980f9dd

View File

@ -30,11 +30,22 @@ export default class Snapshot {
* DEFAULT API METHODS
*/
val() {
/**
* Extracts a JavaScript value from a DataSnapshot.
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#val
* @returns {any}
*/
val(): any {
return this.value;
}
child(path: string) {
/**
* 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 {
const value = deepGet(this.value, path);
const childRef = this.ref.child(path);
return new Snapshot(childRef, {
@ -48,12 +59,36 @@ export default class Snapshot {
});
}
exists() {
/**
* Returns true if this DataSnapshot contains any data.
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#exists
* @returns {boolean}
*/
exists(): Boolean {
return this.value !== null;
}
forEach(fn: (key: any) => any) {
return this.childKeys.forEach(key => fn(this.child(key)));
/**
* 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;
}
getPriority() {