[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 Command = {
type: 'set' | 'update' | 'delete', type: 'set' | 'update' | 'delete',
path: string, path: string,
data: ?{ [string]: any }, data?: { [string]: any },
options: ?{ merge: boolean }, options?: SetOptions | {},
}; };
type SetOptions = { type SetOptions = {

View File

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

View File

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

View File

@ -1006,7 +1006,7 @@
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh; 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; showEnvVarsInLog = 0;
}; };
6AE1012F46FF8A4D1D818A12 /* [CP] Copy Pods Resources */ = { 6AE1012F46FF8A4D1D818A12 /* [CP] Copy Pods Resources */ = {