[types] Fix transaction flow type errors

This commit is contained in:
Chris Bianca 2018-03-08 11:13:21 +00:00
parent a6734fa439
commit 7f8a486b40
4 changed files with 36 additions and 24 deletions

View File

@ -16,8 +16,8 @@ import { getNativeModule } from '../../utils/native';
type Command = {
type: 'set' | 'update' | 'delete',
path: string,
data: ?{ [string]: any },
options: ?{ merge: boolean },
data?: { [string]: any },
options?: SetOptions | {},
};
type SetOptions = {

View File

@ -19,9 +19,9 @@ const generateTransactionId = (): number => transactionId++;
export type TransactionMeta = {
id: number,
stack: Array<string>,
reject: null | Function,
resolve: null | Function,
stack: string[],
reject?: Function,
resolve?: Function,
transaction: Transaction,
updateFunction: (transaction: Transaction) => Promise<any>,
};
@ -37,7 +37,12 @@ type TransactionEvent = {
*/
export default class TransactionHandler {
_firestore: Firestore;
_pending: { [number]: TransactionMeta };
_pending: {
[number]: {
meta: TransactionMeta,
transaction: Transaction,
},
};
constructor(firestore: Firestore) {
this._pending = {};
@ -62,10 +67,9 @@ export default class TransactionHandler {
updateFunction: (transaction: Transaction) => Promise<any>
): Promise<any> {
const id = generateTransactionId();
const meta = {
// $FlowExpectedError: Transaction has to be populated
const meta: TransactionMeta = {
id,
reject: null,
resolve: null,
updateFunction,
stack: new Error().stack
.split('\n')
@ -73,8 +77,10 @@ export default class TransactionHandler {
.join('\n'),
};
meta.transaction = new Transaction(this._firestore, meta);
this._pending[id] = meta;
this._pending[id] = {
meta,
transaction: new Transaction(this._firestore, meta),
};
// deferred promise
return new Promise((resolve, reject) => {
@ -145,7 +151,8 @@ export default class TransactionHandler {
// abort if no longer exists js side
if (!this._pending[id]) return this._remove(id);
const { updateFunction, transaction, reject } = this._pending[id];
const { meta, transaction } = this._pending[id];
const { updateFunction, reject } = meta;
// clear any saved state from previous transaction runs
transaction._prepare();
@ -178,6 +185,7 @@ export default class TransactionHandler {
// update is failed when either the users updateFunction
// throws an error or rejects a promise
if (updateFailed) {
// $FlowExpectedError: Reject will always be present
return reject(finalError);
}
@ -201,17 +209,20 @@ export default class TransactionHandler {
*/
_handleError(event: TransactionEvent) {
const { id, error } = event;
const meta = this._pending[id];
const { meta } = this._pending[id];
if (meta) {
if (meta && error) {
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);
// $FlowExpectedError: code is needed for Firebase errors
errorWithStack.code = code;
// $FlowExpectedError: stack should be a stack trace
errorWithStack.stack = meta.stack;
// $FlowExpectedError: Reject will always be present
meta.reject(errorWithStack);
}
}
@ -224,10 +235,11 @@ export default class TransactionHandler {
*/
_handleComplete(event: TransactionEvent) {
const { id } = event;
const meta = this._pending[id];
const { meta, transaction } = this._pending[id];
if (meta) {
const pendingResult = meta.transaction._pendingResult;
const pendingResult = transaction._pendingResult;
// $FlowExpectedError: Resolve will always be present
meta.resolve(pendingResult);
}
}

View File

@ -164,7 +164,7 @@ PODS:
- React/Core
- React/fishhook
- React/RCTBlob
- RNFirebase (3.2.7):
- RNFirebase (3.3.0):
- React
- yoga (0.52.3.React)
@ -192,11 +192,11 @@ DEPENDENCIES:
EXTERNAL SOURCES:
React:
:path: ../node_modules/react-native
:path: "../node_modules/react-native"
RNFirebase:
:path: ../../ios/RNFirebase.podspec
:path: "../../ios/RNFirebase.podspec"
yoga:
:path: ../node_modules/react-native/ReactCommon/yoga
:path: "../node_modules/react-native/ReactCommon/yoga"
SPEC CHECKSUMS:
BoringSSL: f3d6b8ce199b9c450a8cfc14895d07a2627fc232
@ -224,13 +224,13 @@ SPEC CHECKSUMS:
gRPC-ProtoRPC: f29e8b7445e0d3c0311678ab121e6c164da4ca5e
gRPC-RxLibrary: 8e0067bfe8a054022c7a81470baace4f2f633b48
GTMSessionFetcher: 5bb1eae636127de695590f50e7d248483eb891e6
leveldb-library: 08cba283675b7ed2d99629a4bc5fd052cd2bb6a5
leveldb-library: '08cba283675b7ed2d99629a4bc5fd052cd2bb6a5'
nanopb: 5601e6bca2dbf1ed831b519092ec110f66982ca3
Protobuf: 8a9838fba8dae3389230e1b7f8c104aa32389c03
React: c0dfd2dfc970019d1ae7d48bf24cef530992e079
RNFirebase: 3a141a97041ea0757e2036c2bb18acbe9f0e105d
RNFirebase: 056b672391f3e7b572d8ef221c84603970e0c114
yoga: f45a46b966e1eb0c7a532cfd4beec5b97332ba48
PODFILE CHECKSUM: 67c98bcb203cb992da590bcab6f690f727653ca5
COCOAPODS: 1.3.1
COCOAPODS: 1.2.1

View File

@ -1006,7 +1006,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
6AE1012F46FF8A4D1D818A12 /* [CP] Copy Pods Resources */ = {