[firestore][js] misc transactions internals changes
This commit is contained in:
parent
dd1bbc87f5
commit
3f4c59f81d
@ -8,7 +8,7 @@ import { buildNativeMap } from './utils/serialize';
|
|||||||
import type Firestore from './';
|
import type Firestore from './';
|
||||||
import type { TransactionMeta } from './TransactionHandler';
|
import type { TransactionMeta } from './TransactionHandler';
|
||||||
import type DocumentReference from './DocumentReference';
|
import type DocumentReference from './DocumentReference';
|
||||||
import type DocumentSnapshot from './DocumentSnapshot';
|
import DocumentSnapshot from './DocumentSnapshot';
|
||||||
import { isObject, isString } from '../../utils';
|
import { isObject, isString } from '../../utils';
|
||||||
import FieldPath from './FieldPath';
|
import FieldPath from './FieldPath';
|
||||||
import { getNativeModule } from '../../utils/native';
|
import { getNativeModule } from '../../utils/native';
|
||||||
@ -24,6 +24,9 @@ type SetOptions = {
|
|||||||
merge: boolean,
|
merge: boolean,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO docs state all get requests must be made FIRST before any modifications
|
||||||
|
// TODO so need to validate that
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class Transaction
|
* @class Transaction
|
||||||
*/
|
*/
|
||||||
@ -72,10 +75,9 @@ export default class Transaction {
|
|||||||
*/
|
*/
|
||||||
get(documentRef: DocumentReference): Promise<DocumentSnapshot> {
|
get(documentRef: DocumentReference): Promise<DocumentSnapshot> {
|
||||||
// todo validate doc ref
|
// todo validate doc ref
|
||||||
return getNativeModule(this._firestore).transactionGetDocument(
|
return getNativeModule(this._firestore)
|
||||||
this._meta.id,
|
.transactionGetDocument(this._meta.id, documentRef.path)
|
||||||
documentRef.path
|
.then(result => new DocumentSnapshot(this._firestore, result));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,7 +104,7 @@ export default class Transaction {
|
|||||||
type: 'set',
|
type: 'set',
|
||||||
path: documentRef.path,
|
path: documentRef.path,
|
||||||
data: buildNativeMap(data),
|
data: buildNativeMap(data),
|
||||||
options,
|
options: options || {},
|
||||||
});
|
});
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
@ -67,7 +67,10 @@ export default class TransactionHandler {
|
|||||||
reject: null,
|
reject: null,
|
||||||
resolve: null,
|
resolve: null,
|
||||||
updateFunction,
|
updateFunction,
|
||||||
stack: new Error().stack.slice(1),
|
stack: new Error().stack
|
||||||
|
.split('\n')
|
||||||
|
.slice(4)
|
||||||
|
.join('\n'),
|
||||||
};
|
};
|
||||||
|
|
||||||
meta.transaction = new Transaction(this._firestore, meta);
|
meta.transaction = new Transaction(this._firestore, meta);
|
||||||
@ -91,14 +94,11 @@ export default class TransactionHandler {
|
|||||||
* Destroys a local instance of a transaction meta
|
* Destroys a local instance of a transaction meta
|
||||||
*
|
*
|
||||||
* @param id
|
* @param id
|
||||||
* @param pendingAbort Notify native that there's still an transaction in
|
|
||||||
* progress that needs aborting - this is to handle a JS side
|
|
||||||
* exception
|
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_remove(id, pendingAbort = false) {
|
_remove(id) {
|
||||||
// todo confirm pending arg no longer needed
|
// todo confirm pending arg no longer needed
|
||||||
getNativeModule(this._firestore).transactionDispose(id, pendingAbort);
|
getNativeModule(this._firestore).transactionDispose(id);
|
||||||
// TODO may need delaying to next event loop
|
// TODO may need delaying to next event loop
|
||||||
delete this._pending[id];
|
delete this._pending[id];
|
||||||
}
|
}
|
||||||
@ -168,11 +168,15 @@ export default class TransactionHandler {
|
|||||||
pendingResult = await possiblePromise;
|
pendingResult = await possiblePromise;
|
||||||
}
|
}
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
updateFailed = true; // in case the user rejects with nothing
|
// 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;
|
||||||
finalError = exception;
|
finalError = exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
// reject the final promise and remove from native
|
// reject the final promise and remove from native
|
||||||
|
// update is failed when either the users updateFunction
|
||||||
|
// throws an error or rejects a promise
|
||||||
if (updateFailed) {
|
if (updateFailed) {
|
||||||
return reject(finalError);
|
return reject(finalError);
|
||||||
}
|
}
|
||||||
@ -183,7 +187,7 @@ export default class TransactionHandler {
|
|||||||
transaction._pendingResult = pendingResult;
|
transaction._pendingResult = pendingResult;
|
||||||
|
|
||||||
// send the buffered update/set/delete commands for native to process
|
// send the buffered update/set/delete commands for native to process
|
||||||
return getNativeModule(this._firestore).transactionProcessUpdateResponse(
|
return getNativeModule(this._firestore).transactionApplyBuffer(
|
||||||
id,
|
id,
|
||||||
transaction._commandBuffer
|
transaction._commandBuffer
|
||||||
);
|
);
|
||||||
|
@ -154,6 +154,7 @@ export default class Firestore extends ModuleBase {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
enableNetwork(): void {
|
enableNetwork(): void {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD(
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD(
|
||||||
@ -162,6 +163,7 @@ export default class Firestore extends ModuleBase {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
disableNetwork(): void {
|
disableNetwork(): void {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD(
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD(
|
||||||
@ -180,6 +182,7 @@ export default class Firestore extends ModuleBase {
|
|||||||
enablePersistence(): Promise<void> {
|
enablePersistence(): Promise<void> {
|
||||||
throw new Error('Persistence is enabled by default on the Firestore SDKs');
|
throw new Error('Persistence is enabled by default on the Firestore SDKs');
|
||||||
}
|
}
|
||||||
|
|
||||||
settings(): void {
|
settings(): void {
|
||||||
throw new Error('firebase.firestore().settings() coming soon');
|
throw new Error('firebase.firestore().settings() coming soon');
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user