Skip to content

Commit 69fb950

Browse files
committed
Cleaner handling chain.
1 parent dba8c11 commit 69fb950

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

lib/modules/database/index.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,25 +159,12 @@ export default class Database extends Base {
159159
FirestackDatabase.goOffline();
160160
}
161161

162-
addTransaction(path, updateCallback, applyLocally, onComplete) {
162+
addTransaction(path, updateCallback, applyLocally) {
163163
let id = this._generateTransactionID();
164164
this.transactions[id] = updateCallback;
165-
return new Promise((resolve, reject) => {
166-
FirestackDatabase.beginTransaction(path, id, applyLocally || false, (error, result) => {
167-
let snapshot;
168-
if (result.snapshot)
169-
snapshot = new Snapshot(new Reference(this, path.split('/'), null), result.snapshot);
170-
onComplete && onComplete(error, snapshot);
171-
if (error) {
172-
reject(error);
173-
}
174-
else {
175-
let {committed} = result;
176-
resolve({committed, snapshot});
177-
}
178-
delete this.transactions[id];
179-
});
180-
});
165+
return promisify('beginTransaction', FirestackDatabase)(path, id, applyLocally || false)
166+
.then((v) => {delete this.transactions[id]; return v;},
167+
(e) => {delete this.transactions[id]; throw e;});
181168
}
182169

183170
_generateTransactionID() {

lib/modules/database/reference.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,15 @@ export default class Reference extends ReferenceBase {
125125

126126
transaction(transactionUpdate, onComplete, applyLocally) {
127127
const path = this._dbPath();
128-
return this.db.addTransaction(path, transactionUpdate, applyLocally, onComplete);
128+
return this.db.addTransaction(path, transactionUpdate, applyLocally)
129+
.then(({ snapshot, committed }) => {snapshot: new Snapshot(this, snapshot), committed})
130+
.then(({ snapshot, committed }) => {
131+
if (isFunction(onComplete)) onComplete(null, snapshot);
132+
return {snapshot, committed};
133+
}).catch((e) => {
134+
if (isFunction(onComplete)) return onComplete(e, null);
135+
throw e;
136+
});
129137
}
130138

131139
/**

0 commit comments

Comments
 (0)