[js][database] synctree cleanup
This commit is contained in:
parent
4e78c5373b
commit
5ccbd9f369
|
@ -1,6 +1,8 @@
|
|||
import EventEmitter from 'EventEmitter';
|
||||
import { Platform, NativeModules } from 'react-native';
|
||||
|
||||
import SyncTree from './utils/SyncTree';
|
||||
|
||||
const DEFAULT_APP_NAME = Platform.OS === 'ios' ? '__FIRAPP_DEFAULT' : '[DEFAULT]';
|
||||
|
||||
export default {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ type Registration = {
|
|||
once?: Boolean,
|
||||
appName: String,
|
||||
eventType: String,
|
||||
registration: String,
|
||||
eventRegistrationKey: String,
|
||||
ref: DatabaseReference,
|
||||
}
|
||||
|
||||
|
@ -53,7 +53,8 @@ export default class SyncTree {
|
|||
* @private
|
||||
*/
|
||||
_handleValueEvent(event) {
|
||||
const { eventRegistrationKey } = event.registration;
|
||||
// console.log('SyncTree.VALUE >>>', event);
|
||||
const { key, eventRegistrationKey } = event.registration;
|
||||
const registration = this.getRegistration(eventRegistrationKey);
|
||||
|
||||
if (!registration) {
|
||||
|
@ -61,10 +62,7 @@ export default class SyncTree {
|
|||
// notify native that the registration
|
||||
// no longer exists so it can remove
|
||||
// the native listeners
|
||||
return this._databaseNative.off({
|
||||
key: event.key,
|
||||
eventRegistrationKey,
|
||||
});
|
||||
return this._databaseNative.off(key, eventRegistrationKey);
|
||||
}
|
||||
|
||||
const { snapshot, previousChildName } = event.data;
|
||||
|
@ -85,6 +83,7 @@ export default class SyncTree {
|
|||
* @private
|
||||
*/
|
||||
_handleErrorEvent(event) {
|
||||
// console.log('SyncTree.ERROR >>>', event);
|
||||
const { code, message } = event.error;
|
||||
const { eventRegistrationKey, registrationCancellationKey } = event.registration;
|
||||
|
||||
|
@ -207,18 +206,24 @@ export default class SyncTree {
|
|||
* @return {String}
|
||||
*/
|
||||
addRegistration(parameters: Registration, listener): String {
|
||||
const { path, eventType, registration, once } = parameters;
|
||||
const { path, eventType, eventRegistrationKey, once } = parameters;
|
||||
|
||||
if (!this._tree[path]) this._tree[path] = {};
|
||||
if (!this._tree[path][eventType]) this._tree[path][eventType] = {};
|
||||
|
||||
this._tree[path][eventType][registration] = 0;
|
||||
this._reverseLookup[registration] = Object.assign({}, parameters);
|
||||
this._tree[path][eventType][eventRegistrationKey] = 0;
|
||||
this._reverseLookup[eventRegistrationKey] = Object.assign({}, parameters);
|
||||
|
||||
if (once) INTERNALS.SharedEventEmitter.once(registration, this._onOnceRemoveRegistration(registration, listener));
|
||||
else INTERNALS.SharedEventEmitter.addListener(registration, listener);
|
||||
if (once) {
|
||||
INTERNALS.SharedEventEmitter.once(
|
||||
eventRegistrationKey,
|
||||
this._onOnceRemoveRegistration(eventRegistrationKey, listener),
|
||||
);
|
||||
} else {
|
||||
INTERNALS.SharedEventEmitter.addListener(eventRegistrationKey, listener);
|
||||
}
|
||||
|
||||
return registration;
|
||||
return eventRegistrationKey;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -246,11 +251,7 @@ export default class SyncTree {
|
|||
// automatically unsubscribed on native when the first event is sent
|
||||
const registrationObj = this._reverseLookup[registration];
|
||||
if (registrationObj && !once) {
|
||||
this._databaseNative.off({
|
||||
key: registrationObj.key,
|
||||
eventType: registrationObj.eventType,
|
||||
eventRegistrationKey: registration,
|
||||
});
|
||||
this._databaseNative.off(registrationObj.key, registration);
|
||||
}
|
||||
|
||||
delete this._tree[path][eventType][registration];
|
||||
|
|
Loading…
Reference in New Issue