[js][database] once now supports passing context - as per the web sdk

This commit is contained in:
Salakar 2017-08-07 09:46:05 +01:00
parent fb6251a6fd
commit 425dfbcc43
1 changed files with 25 additions and 9 deletions

View File

@ -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.<any>}
*/
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;
});
}