This commit is contained in:
Salakar 2017-10-29 01:10:34 +01:00
parent 9a17379e0e
commit 07204789f0
2 changed files with 29 additions and 5 deletions

View File

@ -736,14 +736,15 @@ export default class Reference extends ReferenceBase {
// remove the callback. // remove the callback.
// Remove only a single registration // Remove only a single registration
if (eventType && originalCallback) { if (eventType && originalCallback) {
const registrations = this._syncTree.getRegistrationsByPathEvent(this.path, eventType); const registration = this._syncTree.getOneByPathEventListener(this.path, eventType, originalCallback);
if (!registration) return [];
// remove the paired cancellation registration if any exist // remove the paired cancellation registration if any exist
this._syncTree.removeListenersForRegistrations([`${registrations[0]}$cancelled`]); this._syncTree.removeListenersForRegistrations([`${registration}$cancelled`]);
// remove only the first registration to match firebase web sdk // remove only the first registration to match firebase web sdk
// call multiple times to remove multiple registrations // call multiple times to remove multiple registrations
return this._syncTree.removeListenerRegistrations(originalCallback, [registrations[0]]); return this._syncTree.removeListenerRegistrations(originalCallback, [registration]);
} }
// Firebase Docs: // Firebase Docs:

View File

@ -11,6 +11,7 @@ type Registration = {
once?: Boolean, once?: Boolean,
appName: String, appName: String,
eventType: String, eventType: String,
listener: Function,
eventRegistrationKey: String, eventRegistrationKey: String,
ref: DatabaseReference, ref: DatabaseReference,
} }
@ -197,6 +198,28 @@ export default class SyncTree {
return Object.keys(this._tree[path][eventType]); return Object.keys(this._tree[path][eventType]);
} }
/**
* Returns a single registration key for the specified path, eventType, and listener
*
* @param path
* @param eventType
* @param listener
* @return {Array}
*/
getOneByPathEventListener(path: string, eventType: string, listener: Function): Array {
if (!this._tree[path]) return [];
if (!this._tree[path][eventType]) return [];
const registrationsForPathEvent = Object.entries(this._tree[path][eventType]);
for (let i = 0; i < registrationsForPathEvent.length; i++) {
const registration = registrationsForPathEvent[i];
if (registration[1] === listener) return registration[0];
}
return null;
}
/** /**
* Register a new listener. * Register a new listener.
@ -211,8 +234,8 @@ export default class SyncTree {
if (!this._tree[path]) this._tree[path] = {}; if (!this._tree[path]) this._tree[path] = {};
if (!this._tree[path][eventType]) this._tree[path][eventType] = {}; if (!this._tree[path][eventType]) this._tree[path][eventType] = {};
this._tree[path][eventType][eventRegistrationKey] = 0; this._tree[path][eventType][eventRegistrationKey] = listener;
this._reverseLookup[eventRegistrationKey] = Object.assign({}, parameters); this._reverseLookup[eventRegistrationKey] = Object.assign({ listener }, parameters);
if (once) { if (once) {
INTERNALS.SharedEventEmitter.once( INTERNALS.SharedEventEmitter.once(