From dedfa48aa0f0186e53be33764caf6f7e065d7b0d Mon Sep 17 00:00:00 2001 From: Salakar Date: Mon, 14 Aug 2017 18:41:50 +0100 Subject: [PATCH] [js][database] _handleCancelEvent completed - now correctly handles cancellation events from .on() --- lib/modules/database/index.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/modules/database/index.js b/lib/modules/database/index.js index 1e74fe3d..fbf9bce6 100644 --- a/lib/modules/database/index.js +++ b/lib/modules/database/index.js @@ -8,6 +8,7 @@ import Reference from './reference'; import Snapshot from './snapshot'; import TransactionHandler from './transaction'; import ModuleBase from './../../utils/ModuleBase'; +import { nativeToJSError } from './../../utils'; /** * @class Database @@ -36,8 +37,15 @@ export default class Database extends ModuleBase { ); } + /** + * Routes native database 'on' events to their js equivalent counterpart. + * If there is no longer any listeners remaining for this event we internally + * call the native unsub method to prevent further events coming through. + * + * @param event + * @private + */ _handleOnEvent(event) { - console.log('>>>ON-event>>>', event); const { queryKey, body, refId } = event; const { snapshot, previousChildName } = body; @@ -64,8 +72,21 @@ export default class Database extends ModuleBase { } } + + /** + * Routes native database query listener cancellation events to their js counterparts. + * + * @param event + * @private + */ _handleCancelEvent(event) { - console.log('>>>CANCEL-event>>>', event); + const { queryKey, code, message, path, refId, appName } = event; + const remainingListeners = this.listeners(`${queryKey}:cancelled`); + + if (remainingListeners && remainingListeners.length) { + const error = nativeToJSError(code, message, { path, queryKey, refId, appName }); + this.emit(`${queryKey}:cancelled`, error); + } } /** @@ -98,14 +119,6 @@ export default class Database extends ModuleBase { ref(path: string) { return new Reference(this, path); } - - - /** - * INTERNALS - */ - - // todo handleDbEvent - // todo handleDbError } export const statics = {