2018-02-11 23:37:21 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Firestore Transaction representation wrapper
|
|
|
|
*/
|
|
|
|
import { getAppEventName, SharedEventEmitter } from '../../utils/events';
|
|
|
|
import { getNativeModule } from '../../utils/native';
|
|
|
|
import Transaction from './Transaction';
|
|
|
|
import type Firestore from './';
|
|
|
|
|
|
|
|
let transactionId = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the push id generator to create a transaction id
|
|
|
|
* @returns {number}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
const generateTransactionId = (): number => transactionId++;
|
|
|
|
|
|
|
|
export type TransactionMeta = {
|
|
|
|
id: number,
|
2018-03-08 11:13:21 +00:00
|
|
|
stack: string[],
|
|
|
|
reject?: Function,
|
|
|
|
resolve?: Function,
|
2018-02-11 23:37:21 +00:00
|
|
|
transaction: Transaction,
|
|
|
|
updateFunction: (transaction: Transaction) => Promise<any>,
|
|
|
|
};
|
|
|
|
|
|
|
|
type TransactionEvent = {
|
|
|
|
id: number,
|
|
|
|
type: 'update' | 'error' | 'complete',
|
|
|
|
error: ?{ code: string, message: string },
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class TransactionHandler
|
|
|
|
*/
|
|
|
|
export default class TransactionHandler {
|
|
|
|
_firestore: Firestore;
|
2018-03-08 11:13:21 +00:00
|
|
|
_pending: {
|
|
|
|
[number]: {
|
|
|
|
meta: TransactionMeta,
|
|
|
|
transaction: Transaction,
|
|
|
|
},
|
|
|
|
};
|
2018-02-11 23:37:21 +00:00
|
|
|
|
|
|
|
constructor(firestore: Firestore) {
|
|
|
|
this._pending = {};
|
|
|
|
this._firestore = firestore;
|
2018-02-13 13:58:23 +00:00
|
|
|
SharedEventEmitter.addListener(
|
2018-02-11 23:37:21 +00:00
|
|
|
getAppEventName(this._firestore, 'firestore_transaction_event'),
|
|
|
|
this._handleTransactionEvent.bind(this)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* -------------
|
|
|
|
* INTERNAL API
|
|
|
|
* -------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new transaction and start it natively.
|
|
|
|
* @param updateFunction
|
|
|
|
*/
|
|
|
|
_add(
|
|
|
|
updateFunction: (transaction: Transaction) => Promise<any>
|
|
|
|
): Promise<any> {
|
|
|
|
const id = generateTransactionId();
|
2018-03-08 11:13:21 +00:00
|
|
|
// $FlowExpectedError: Transaction has to be populated
|
|
|
|
const meta: TransactionMeta = {
|
2018-02-11 23:37:21 +00:00
|
|
|
id,
|
|
|
|
updateFunction,
|
2018-02-23 03:02:17 +00:00
|
|
|
stack: new Error().stack
|
|
|
|
.split('\n')
|
|
|
|
.slice(4)
|
|
|
|
.join('\n'),
|
2018-02-11 23:37:21 +00:00
|
|
|
};
|
|
|
|
|
2018-03-08 11:13:21 +00:00
|
|
|
this._pending[id] = {
|
|
|
|
meta,
|
|
|
|
transaction: new Transaction(this._firestore, meta),
|
|
|
|
};
|
2018-02-11 23:37:21 +00:00
|
|
|
|
|
|
|
// deferred promise
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
getNativeModule(this._firestore).transactionBegin(id);
|
|
|
|
meta.resolve = r => {
|
|
|
|
resolve(r);
|
|
|
|
this._remove(id);
|
|
|
|
};
|
|
|
|
meta.reject = e => {
|
|
|
|
reject(e);
|
|
|
|
this._remove(id);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys a local instance of a transaction meta
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* @private
|
|
|
|
*/
|
2018-02-23 03:02:17 +00:00
|
|
|
_remove(id) {
|
|
|
|
getNativeModule(this._firestore).transactionDispose(id);
|
2018-02-11 23:37:21 +00:00
|
|
|
delete this._pending[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* -------------
|
|
|
|
* EVENTS
|
|
|
|
* -------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles incoming native transaction events and distributes to correct
|
|
|
|
* internal handler by event.type
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* @returns {*}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleTransactionEvent(event: TransactionEvent) {
|
2018-03-20 16:07:37 +00:00
|
|
|
// eslint-disable-next-line default-case
|
2018-02-11 23:37:21 +00:00
|
|
|
switch (event.type) {
|
|
|
|
case 'update':
|
2018-03-20 16:07:37 +00:00
|
|
|
this._handleUpdate(event);
|
|
|
|
break;
|
2018-02-11 23:37:21 +00:00
|
|
|
case 'error':
|
2018-03-20 16:07:37 +00:00
|
|
|
this._handleError(event);
|
|
|
|
break;
|
2018-02-11 23:37:21 +00:00
|
|
|
case 'complete':
|
2018-03-20 16:07:37 +00:00
|
|
|
this._handleComplete(event);
|
|
|
|
break;
|
2018-02-11 23:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles incoming native transaction update events
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
async _handleUpdate(event: TransactionEvent) {
|
|
|
|
const { id } = event;
|
|
|
|
// abort if no longer exists js side
|
|
|
|
if (!this._pending[id]) return this._remove(id);
|
|
|
|
|
2018-03-08 11:13:21 +00:00
|
|
|
const { meta, transaction } = this._pending[id];
|
|
|
|
const { updateFunction, reject } = meta;
|
2018-02-11 23:37:21 +00:00
|
|
|
|
|
|
|
// clear any saved state from previous transaction runs
|
|
|
|
transaction._prepare();
|
|
|
|
|
|
|
|
let finalError;
|
|
|
|
let updateFailed;
|
|
|
|
let pendingResult;
|
|
|
|
|
|
|
|
// run the users custom update functionality
|
|
|
|
try {
|
|
|
|
const possiblePromise = updateFunction(transaction);
|
|
|
|
|
|
|
|
// validate user has returned a promise in their update function
|
|
|
|
// TODO must it actually return a promise? Can't find any usages of it without one...
|
|
|
|
if (!possiblePromise || !possiblePromise.then) {
|
|
|
|
finalError = new Error(
|
|
|
|
'Update function for `firestore.runTransaction(updateFunction)` must return a Promise.'
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
pendingResult = await possiblePromise;
|
|
|
|
}
|
|
|
|
} catch (exception) {
|
2018-02-23 03:02:17 +00:00
|
|
|
// exception can still be falsey if user `Promise.reject();` 's with no args
|
|
|
|
// so we track the exception with a updateFailed boolean to ensure no fall-through
|
|
|
|
updateFailed = true;
|
2018-02-11 23:37:21 +00:00
|
|
|
finalError = exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reject the final promise and remove from native
|
2018-02-23 03:02:17 +00:00
|
|
|
// update is failed when either the users updateFunction
|
|
|
|
// throws an error or rejects a promise
|
2018-02-11 23:37:21 +00:00
|
|
|
if (updateFailed) {
|
2018-03-08 11:13:21 +00:00
|
|
|
// $FlowExpectedError: Reject will always be present
|
2018-02-11 23:37:21 +00:00
|
|
|
return reject(finalError);
|
|
|
|
}
|
|
|
|
|
|
|
|
// capture the resolved result as we'll need this
|
|
|
|
// to resolve the runTransaction() promise when
|
|
|
|
// native emits that the transaction is final
|
|
|
|
transaction._pendingResult = pendingResult;
|
|
|
|
|
|
|
|
// send the buffered update/set/delete commands for native to process
|
2018-02-23 03:02:17 +00:00
|
|
|
return getNativeModule(this._firestore).transactionApplyBuffer(
|
2018-02-11 23:37:21 +00:00
|
|
|
id,
|
|
|
|
transaction._commandBuffer
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles incoming native transaction error events
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleError(event: TransactionEvent) {
|
|
|
|
const { id, error } = event;
|
2018-03-08 11:13:21 +00:00
|
|
|
const { meta } = this._pending[id];
|
2018-02-11 23:37:21 +00:00
|
|
|
|
2018-03-08 11:13:21 +00:00
|
|
|
if (meta && error) {
|
2018-02-11 23:37:21 +00:00
|
|
|
const { code, message } = error;
|
|
|
|
// build a JS error and replace its stack
|
|
|
|
// with the captured one at start of transaction
|
|
|
|
// so it's actually relevant to the user
|
|
|
|
const errorWithStack = new Error(message);
|
2018-03-08 11:13:21 +00:00
|
|
|
// $FlowExpectedError: code is needed for Firebase errors
|
2018-02-11 23:37:21 +00:00
|
|
|
errorWithStack.code = code;
|
2018-03-08 11:13:21 +00:00
|
|
|
// $FlowExpectedError: stack should be a stack trace
|
2018-02-11 23:37:21 +00:00
|
|
|
errorWithStack.stack = meta.stack;
|
|
|
|
|
2018-03-08 11:13:21 +00:00
|
|
|
// $FlowExpectedError: Reject will always be present
|
2018-02-11 23:37:21 +00:00
|
|
|
meta.reject(errorWithStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles incoming native transaction complete events
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleComplete(event: TransactionEvent) {
|
|
|
|
const { id } = event;
|
2018-03-08 11:13:21 +00:00
|
|
|
const { meta, transaction } = this._pending[id];
|
2018-02-11 23:37:21 +00:00
|
|
|
|
|
|
|
if (meta) {
|
2018-03-08 11:13:21 +00:00
|
|
|
const pendingResult = transaction._pendingResult;
|
|
|
|
// $FlowExpectedError: Resolve will always be present
|
2018-02-11 23:37:21 +00:00
|
|
|
meta.resolve(pendingResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|