2017-03-24 22:53:56 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Database representation wrapper
|
|
|
|
*/
|
|
|
|
|
2017-07-30 06:34:41 +00:00
|
|
|
let transactionId = 0;
|
2017-03-24 22:53:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Database
|
|
|
|
*/
|
2017-06-30 16:23:32 +00:00
|
|
|
export default class TransactionHandler {
|
|
|
|
constructor(database: Object) {
|
|
|
|
this._transactions = {};
|
|
|
|
this._database = database;
|
2017-07-30 06:34:41 +00:00
|
|
|
|
|
|
|
this._transactionListener = this._database.addListener(
|
|
|
|
this._database._getAppEventName('database_transaction_event'),
|
|
|
|
this._handleTransactionEvent.bind(this),
|
2017-03-24 22:53:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-02 09:38:30 +00:00
|
|
|
* Add a new transaction and start it natively.
|
2017-03-24 22:53:56 +00:00
|
|
|
* @param reference
|
|
|
|
* @param transactionUpdater
|
|
|
|
* @param onComplete
|
|
|
|
* @param applyLocally
|
|
|
|
*/
|
2017-07-30 06:34:41 +00:00
|
|
|
add(reference: Object, transactionUpdater: Function, onComplete?: Function, applyLocally?: boolean = false) {
|
2017-03-24 22:53:56 +00:00
|
|
|
const id = this._generateTransactionId();
|
|
|
|
|
2017-06-30 16:23:32 +00:00
|
|
|
this._transactions[id] = {
|
2017-03-24 22:53:56 +00:00
|
|
|
id,
|
|
|
|
reference,
|
|
|
|
transactionUpdater,
|
|
|
|
onComplete,
|
|
|
|
applyLocally,
|
|
|
|
completed: false,
|
|
|
|
started: true,
|
|
|
|
};
|
|
|
|
|
2017-07-30 06:34:41 +00:00
|
|
|
this._database._native.transactionStart(reference.path, id, applyLocally);
|
2017-03-24 22:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* INTERNALS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the push id generator to create a transaction id
|
|
|
|
* @returns {string}
|
|
|
|
* @private
|
|
|
|
*/
|
2017-04-04 16:58:20 +00:00
|
|
|
_generateTransactionId(): string {
|
2017-07-30 06:34:41 +00:00
|
|
|
return transactionId++;
|
2017-03-24 22:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* @returns {*}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleTransactionEvent(event: Object = {}) {
|
|
|
|
switch (event.type) {
|
|
|
|
case 'update':
|
|
|
|
return this._handleUpdate(event);
|
|
|
|
case 'error':
|
2017-04-04 16:58:47 +00:00
|
|
|
return this._handleError(event);
|
2017-03-24 22:53:56 +00:00
|
|
|
case 'complete':
|
|
|
|
return this._handleComplete(event);
|
|
|
|
default:
|
|
|
|
this.log.warn(`Unknown transaction event type: '${event.type}'`, event);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleUpdate(event: Object = {}) {
|
|
|
|
let newValue;
|
|
|
|
const { id, value } = event;
|
|
|
|
|
|
|
|
try {
|
2017-06-30 16:23:32 +00:00
|
|
|
const transaction = this._transactions[id];
|
2017-07-30 06:34:41 +00:00
|
|
|
if (!transaction) return;
|
|
|
|
|
2017-03-24 22:53:56 +00:00
|
|
|
newValue = transaction.transactionUpdater(value);
|
|
|
|
} finally {
|
|
|
|
let abort = false;
|
|
|
|
|
|
|
|
if (newValue === undefined) {
|
|
|
|
abort = true;
|
|
|
|
}
|
|
|
|
|
2017-07-30 06:34:41 +00:00
|
|
|
this._database._native.transactionTryCommit(id, { value: newValue, abort });
|
2017-03-24 22:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleError(event: Object = {}) {
|
2017-06-30 16:23:32 +00:00
|
|
|
const transaction = this._transactions[event.id];
|
2017-03-24 22:53:56 +00:00
|
|
|
if (transaction && !transaction.completed) {
|
|
|
|
transaction.completed = true;
|
|
|
|
try {
|
2017-07-30 06:34:41 +00:00
|
|
|
transaction.onComplete(new Error(event.error.message, event.error.code), null);
|
2017-03-24 22:53:56 +00:00
|
|
|
} finally {
|
|
|
|
setImmediate(() => {
|
2017-06-30 16:23:32 +00:00
|
|
|
delete this._transactions[event.id];
|
2017-03-24 22:53:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleComplete(event: Object = {}) {
|
2017-06-30 16:23:32 +00:00
|
|
|
const transaction = this._transactions[event.id];
|
2017-03-24 22:53:56 +00:00
|
|
|
if (transaction && !transaction.completed) {
|
|
|
|
transaction.completed = true;
|
|
|
|
try {
|
|
|
|
transaction.onComplete(null, event.committed, Object.assign({}, event.snapshot));
|
|
|
|
} finally {
|
|
|
|
setImmediate(() => {
|
2017-06-30 16:23:32 +00:00
|
|
|
delete this._transactions[event.id];
|
2017-03-24 22:53:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|