mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-24 04:24:52 +08:00
more work on multiple app initialization
This commit is contained in:
@@ -2,26 +2,19 @@
|
||||
* @flow
|
||||
* Database representation wrapper
|
||||
*/
|
||||
import { NativeModules } from 'react-native';
|
||||
|
||||
import { Base } from './../base';
|
||||
import { generatePushID } from './../../utils';
|
||||
|
||||
const FirebaseDatabase = NativeModules.RNFirebaseDatabase;
|
||||
|
||||
/**
|
||||
* @class Database
|
||||
*/
|
||||
export default class TransactionHandler extends Base {
|
||||
constructor(firebase: Object, database: Object, FirebaseDatabaseEvt: Object) {
|
||||
super(firebase, {});
|
||||
this.transactions = {};
|
||||
this.database = database;
|
||||
this.namespace = 'firebase:database:transaction';
|
||||
|
||||
this.transactionListener = FirebaseDatabaseEvt.addListener(
|
||||
export default class TransactionHandler {
|
||||
constructor(database: Object) {
|
||||
this._transactions = {};
|
||||
this._database = database;
|
||||
this._transactionListener = this._database._eventEmitter.addListener(
|
||||
'database_transaction_event',
|
||||
event => this._handleTransactionEvent(event)
|
||||
event => this._handleTransactionEvent(event),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -40,7 +33,7 @@ export default class TransactionHandler extends Base {
|
||||
) {
|
||||
const id = this._generateTransactionId();
|
||||
|
||||
this.transactions[id] = {
|
||||
this._transactions[id] = {
|
||||
id,
|
||||
reference,
|
||||
transactionUpdater,
|
||||
@@ -50,7 +43,7 @@ export default class TransactionHandler extends Base {
|
||||
started: true,
|
||||
};
|
||||
|
||||
FirebaseDatabase.startTransaction(reference.path, id, applyLocally);
|
||||
this._database._native.startTransaction(reference.path, id, applyLocally);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +56,7 @@ export default class TransactionHandler extends Base {
|
||||
* @private
|
||||
*/
|
||||
_generateTransactionId(): string {
|
||||
return generatePushID(this.database.serverTimeOffset);
|
||||
return generatePushID(this._database.serverTimeOffset);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,7 +89,7 @@ export default class TransactionHandler extends Base {
|
||||
const { id, value } = event;
|
||||
|
||||
try {
|
||||
const transaction = this.transactions[id];
|
||||
const transaction = this._transactions[id];
|
||||
// todo handle when transaction no longer exists on js side?
|
||||
newValue = transaction.transactionUpdater(value);
|
||||
} finally {
|
||||
@@ -106,7 +99,7 @@ export default class TransactionHandler extends Base {
|
||||
abort = true;
|
||||
}
|
||||
|
||||
FirebaseDatabase.tryCommitTransaction(id, { value: newValue, abort });
|
||||
this._database._native.tryCommitTransaction(id, { value: newValue, abort });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,14 +109,14 @@ export default class TransactionHandler extends Base {
|
||||
* @private
|
||||
*/
|
||||
_handleError(event: Object = {}) {
|
||||
const transaction = this.transactions[event.id];
|
||||
const transaction = this._transactions[event.id];
|
||||
if (transaction && !transaction.completed) {
|
||||
transaction.completed = true;
|
||||
try {
|
||||
transaction.onComplete(new Error(event.message, event.code), null);
|
||||
} finally {
|
||||
setImmediate(() => {
|
||||
delete this.transactions[event.id];
|
||||
delete this._transactions[event.id];
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -135,14 +128,14 @@ export default class TransactionHandler extends Base {
|
||||
* @private
|
||||
*/
|
||||
_handleComplete(event: Object = {}) {
|
||||
const transaction = this.transactions[event.id];
|
||||
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];
|
||||
delete this._transactions[event.id];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user