Skip to content
Prev Previous commit
Next Next commit
Cleaner handling chain.
  • Loading branch information
FrankSalad committed Jan 6, 2017
commit 69fb950eff0f3be066777bc5f8928c8f5a74c7b1
21 changes: 4 additions & 17 deletions lib/modules/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,12 @@ export default class Database extends Base {
FirestackDatabase.goOffline();
}

addTransaction(path, updateCallback, applyLocally, onComplete) {
addTransaction(path, updateCallback, applyLocally) {
let id = this._generateTransactionID();
this.transactions[id] = updateCallback;
return new Promise((resolve, reject) => {
FirestackDatabase.beginTransaction(path, id, applyLocally || false, (error, result) => {
let snapshot;
if (result.snapshot)
snapshot = new Snapshot(new Reference(this, path.split('/'), null), result.snapshot);
onComplete && onComplete(error, snapshot);
if (error) {
reject(error);
}
else {
let {committed} = result;
resolve({committed, snapshot});
}
delete this.transactions[id];
});
});
return promisify('beginTransaction', FirestackDatabase)(path, id, applyLocally || false)
.then((v) => {delete this.transactions[id]; return v;},
(e) => {delete this.transactions[id]; throw e;});
}

_generateTransactionID() {
Expand Down
10 changes: 9 additions & 1 deletion lib/modules/database/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,15 @@ export default class Reference extends ReferenceBase {

transaction(transactionUpdate, onComplete, applyLocally) {
const path = this._dbPath();
return this.db.addTransaction(path, transactionUpdate, applyLocally, onComplete);
return this.db.addTransaction(path, transactionUpdate, applyLocally)
.then(({ snapshot, committed }) => {snapshot: new Snapshot(this, snapshot), committed})
.then(({ snapshot, committed }) => {
if (isFunction(onComplete)) onComplete(null, snapshot);
return {snapshot, committed};
}).catch((e) => {
if (isFunction(onComplete)) return onComplete(e, null);
throw e;
});
}

/**
Expand Down