From 425dfbcc43679965a81f4cd712332f4dc8933088 Mon Sep 17 00:00:00 2001 From: Salakar Date: Mon, 7 Aug 2017 09:46:05 +0100 Subject: [PATCH] [js][database] once now supports passing context - as per the web sdk --- lib/modules/database/reference.js | 34 +++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/modules/database/reference.js b/lib/modules/database/reference.js index cadf987f..7a95aa46 100644 --- a/lib/modules/database/reference.js +++ b/lib/modules/database/reference.js @@ -172,7 +172,11 @@ export default class Reference extends ReferenceBase { * @param onComplete * @param applyLocally */ - transaction(transactionUpdate: Function, onComplete: (error: ?Error, committed: boolean, snapshot: ?Snapshot) => *, applyLocally: boolean = false) { + transaction( + transactionUpdate: Function, + onComplete: (error: ?Error, committed: boolean, snapshot: ?Snapshot) => *, + applyLocally: boolean = false, + ) { if (!isFunction(transactionUpdate)) { return Promise.reject( new Error('Missing transactionUpdate function argument.'), @@ -190,6 +194,7 @@ export default class Reference extends ReferenceBase { return resolve({ committed, snapshot: new Snapshot(this, snapshotData) }); }; + // start the transaction natively this._database._transactionHandler.add(this, transactionUpdate, onCompleteWrapper, applyLocally); }); } @@ -199,19 +204,30 @@ export default class Reference extends ReferenceBase { * * @param eventName * @param successCallback - * @param failureCallback - * TODO @param context + * @param cancelOrContext + * @param context * @returns {Promise.} */ - once(eventName: string = 'value', successCallback: (snapshot: Object) => void, failureCallback: (error: FirebaseError) => void) { + once( + eventName: string = 'value', + successCallback: (snapshot: Object) => void, + cancelOrContext: (error: FirebaseError) => void, + context?: Object, + ) { return this._database._native.once(this._refId, this.path, this._query.getModifiers(), eventName) - .then(({ snapshot }) => new Snapshot(this, snapshot)) - .then((snapshot) => { - if (isFunction(successCallback)) successCallback(snapshot); - return snapshot; + .then(({ snapshot }) => { + const _snapshot = new Snapshot(this, snapshot); + + if (isFunction(successCallback)) { + if (isObject(cancelOrContext)) successCallback.bind(cancelOrContext)(_snapshot); + if (context && isObject(context)) successCallback.bind(context)(_snapshot); + successCallback(_snapshot); + } + + return _snapshot; }) .catch((error) => { - if (isFunction(failureCallback)) return failureCallback(error); + if (isFunction(cancelOrContext)) return cancelOrContext(error); return error; }); }