[js][database] synctree cleanup

This commit is contained in:
Salakar
2017-08-16 21:43:24 +01:00
parent 4e78c5373b
commit 5ccbd9f369
3 changed files with 36 additions and 35 deletions

View File

@@ -20,6 +20,7 @@ import {
import INTERNALS from './../../internals';
// track all event registrations by path
let listeners = 0;
/**
* Enum for event types
@@ -70,7 +71,6 @@ export default class Reference extends ReferenceBase {
constructor(database: Object, path: string, existingModifiers?: Array<DatabaseModifier>) {
super(path, database);
this._promise = null;
this._listeners = 0;
this._refListeners = {};
this._database = database;
this._query = new Query(this, path, existingModifiers);
@@ -517,7 +517,7 @@ export default class Reference extends ReferenceBase {
* @return {string}
*/
_getRegistrationKey(eventType) {
return `$${this._database._appName}$${this.path}$${this._query.queryIdentifier()}$${this._listeners}$${eventType}`;
return `$${this._database._appName}$/${this.path}$${this._query.queryIdentifier()}$${listeners}$${eventType}`;
}
/**
@@ -528,7 +528,7 @@ export default class Reference extends ReferenceBase {
* @private
*/
_getRefKey() {
return `$${this._database._appName}$${this.path}$${this._query.queryIdentifier()}`;
return `$${this._database._appName}$/${this.path}$${this._query.queryIdentifier()}`;
}
/**
@@ -636,18 +636,16 @@ export default class Reference extends ReferenceBase {
const eventRegistrationKey = this._getRegistrationKey(eventType);
const registrationCancellationKey = `${eventRegistrationKey}$cancelled`;
const _context = (cancelCallbackOrContext && !isFunction(cancelCallbackOrContext)) ? cancelCallbackOrContext : context;
const registrationObj = {
eventType,
ref: this,
path: this.path,
key: this._getRefKey(),
appName: this._database._appName,
eventRegistrationKey,
};
this._syncTree.addRegistration(
{
eventType,
ref: this,
path: this.path,
key: this._getRefKey(),
appName: this._database._appName,
registration: eventRegistrationKey,
},
_context ? callback.bind(_context) : callback,
);
this._syncTree.addRegistration(registrationObj, _context ? callback.bind(_context) : callback);
if (isFunction(cancelCallbackOrContext)) {
// cancellations have their own separate registration
@@ -661,7 +659,7 @@ export default class Reference extends ReferenceBase {
key: this._getRefKey(),
appName: this._database._appName,
eventType: `${eventType}$cancelled`,
registration: registrationCancellationKey,
eventRegistrationKey: registrationCancellationKey,
},
_context ? cancelCallbackOrContext.bind(_context) : cancelCallbackOrContext,
);
@@ -674,16 +672,17 @@ export default class Reference extends ReferenceBase {
key: this._getRefKey(),
appName: this._database._appName,
modifiers: this._query.getModifiers(),
hasCancellationCallback: isFunction(cancelCallbackOrContext),
registration: {
eventRegistrationKey,
key: registrationObj.key,
registrationCancellationKey,
hasCancellationCallback: isFunction(cancelCallbackOrContext),
},
});
// increment number of listeners - just s short way of making
// every registration unique per .on() call
this._listeners = this._listeners + 1;
listeners += 1;
// return original unbound successCallback for
// the purposes of calling .off(eventType, callback) at a later date
@@ -755,4 +754,3 @@ export default class Reference extends ReferenceBase {
return INTERNALS.SyncTree;
}
}