From 521e26a91dbb3e6f3b856b5b3ee3abf27679997a Mon Sep 17 00:00:00 2001 From: Josep Sayol Date: Fri, 16 Jun 2017 13:48:13 +0200 Subject: [PATCH] WIP: fix error when checking EventRegistration callbacks --- src/database/core/view/EventRegistration.ts | 6 ++---- src/utils/obj.ts | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/database/core/view/EventRegistration.ts b/src/database/core/view/EventRegistration.ts index 896cc3f93a4..5b6c70771e4 100644 --- a/src/database/core/view/EventRegistration.ts +++ b/src/database/core/view/EventRegistration.ts @@ -1,6 +1,6 @@ import { DataSnapshot } from '../../api/DataSnapshot'; import { DataEvent, CancelEvent, Event } from './Event'; -import { contains, getCount, getAnyKey } from '../../../utils/obj'; +import { contains, getCount, getAnyKey, every } from '../../../utils/obj'; import { assert } from '../../../utils/assert'; import { Path } from '../util/Path'; import { Change } from './Change'; @@ -241,9 +241,7 @@ export class ChildEventRegistration implements EventRegistration { ); } else { // Exact match on each key. - return this.callbacks_.every(function (cb, eventType) { - return other.callbacks_[eventType] === cb; - }); + return every(this.callbacks_, (eventType, cb) => other.callbacks_[eventType] === cb); } } } diff --git a/src/utils/obj.ts b/src/utils/obj.ts index 78a48add08d..3019cf945f5 100644 --- a/src/utils/obj.ts +++ b/src/utils/obj.ts @@ -110,4 +110,23 @@ export const getValues = function(obj) { res[i++] = obj[key]; } return res; -}; \ No newline at end of file +}; + +/** + * Tests whether every key/value pair in an object pass the test implemented + * by the provided function + * + * @param {?Object.} obj Object to test. + * @param {!function(K, V)} fn Function to call for each key and value. + * @template K,V + */ +export const every = function(obj: Object, fn: (k: string, v?: V) => boolean): boolean { + for (let key in obj) { + if (Object.prototype.hasOwnProperty.call(obj, key)) { + if (!fn(key, obj[key])) { + return false; + } + } + } + return true; +};