react-native-firebase-compiled
Version:
A well tested, feature rich Firebase implementation for React Native, supporting iOS & Android. Individual module support for Admob, Analytics, Auth, Crash Reporting, Cloud Firestore, Database, Dynamic Links, Functions, Messaging (FCM), Remote Config, Sto
166 lines (131 loc) • 3.57 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _events = require("../../utils/events");
var _log = require("../../utils/log");
var _native = require("../../utils/native");
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
let transactionId = 0;
/**
* Uses the push id generator to create a transaction id
* @returns {number}
* @private
*/
const generateTransactionId = () => transactionId++;
/**
* @class TransactionHandler
*/
class TransactionHandler {
constructor(database) {
_defineProperty(this, "_database", void 0);
_defineProperty(this, "_transactions", void 0);
this._transactions = {};
this._database = database;
_events.SharedEventEmitter.addListener((0, _events.getAppEventName)(this._database, 'database_transaction_event'), this._handleTransactionEvent.bind(this));
}
/**
* Add a new transaction and start it natively.
* @param reference
* @param transactionUpdater
* @param onComplete
* @param applyLocally
*/
add(reference, transactionUpdater, onComplete, applyLocally = false) {
const id = generateTransactionId();
this._transactions[id] = {
id,
reference,
transactionUpdater,
onComplete,
applyLocally,
completed: false,
started: true
};
(0, _native.getNativeModule)(this._database).transactionStart(reference.path, id, applyLocally);
}
/**
* INTERNALS
*/
/**
*
* @param event
* @returns {*}
* @private
*/
_handleTransactionEvent(event = {}) {
switch (event.type) {
case 'update':
return this._handleUpdate(event);
case 'error':
return this._handleError(event);
case 'complete':
return this._handleComplete(event);
default:
(0, _log.getLogger)(this._database).warn(`Unknown transaction event type: '${event.type}'`, event);
return undefined;
}
}
/**
*
* @param event
* @private
*/
_handleUpdate(event = {}) {
let newValue;
const id = event.id,
value = event.value;
try {
const transaction = this._transactions[id];
if (!transaction) return;
newValue = transaction.transactionUpdater(value);
} finally {
let abort = false;
if (newValue === undefined) {
abort = true;
}
(0, _native.getNativeModule)(this._database).transactionTryCommit(id, {
value: newValue,
abort
});
}
}
/**
*
* @param event
* @private
*/
_handleError(event = {}) {
const transaction = this._transactions[event.id];
if (transaction && !transaction.completed) {
transaction.completed = true;
try {
transaction.onComplete(event.error, false, null);
} finally {
setImmediate(() => {
delete this._transactions[event.id];
});
}
}
}
/**
*
* @param event
* @private
*/
_handleComplete(event = {}) {
const transaction = this._transactions[event.id];
if (transaction && !transaction.completed) {
transaction.completed = true;
try {
transaction.onComplete(null, event.committed, Object.assign({}, event.snapshot));
} finally {
setImmediate(() => {
delete this._transactions[event.id];
});
}
}
}
}
exports.default = TransactionHandler;
;