-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: remove resolvers, remove promise in withBeginTransaction, simplify code calling withBeginTransaction #1212
Merged
danieljbruce
merged 28 commits into
nodejs-transaction-redesign-feature-branch-5-adding-the-tests
from
nodejs-transaction-redesign-feature-branch-remove-resolver
Jan 2, 2024
Merged
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
941810e
Change resolvers
danieljbruce 4c8a4c5
Add a wrapWithBeginTransaction function for get
danieljbruce 1f533d4
Use wrapWithBeginTransaction in more places
danieljbruce 9ff6166
Add callbackWithError function
danieljbruce 7f9147f
Add return type to callbackWithError
danieljbruce 423b530
Delete code that is not used
danieljbruce dfc5998
There is no need to define a separate type
danieljbruce 267205c
TODO no longer applies
danieljbruce ddbd200
Remove TODO
danieljbruce 712d3e8
Inline the error type
danieljbruce 015c3d4
Add a comment for callbackWithError
danieljbruce d598ec6
Added comments to document sendUserCallbackData
danieljbruce 8312310
Move function
danieljbruce 665192e
Do not define a commit type only used once
danieljbruce 19b7d5e
Replace the type in the comment with Function
danieljbruce 3c69ffa
Just use function type for the callback
danieljbruce 08bf1f3
Remove withBeginTransaction
danieljbruce 2714f7c
Simplify code usage of withBeginTransaction
danieljbruce 908995b
function should not have any arguments
danieljbruce 039394e
Remove generic Args parameter
danieljbruce 34c92b9
Remove TODOs that are not relevant anymore.
danieljbruce fde0db4
Rename to withBeginTransaction
danieljbruce 7047a32
Update comments for withBeginTransaction
danieljbruce d48633c
Simplify diff
danieljbruce b32ec8d
Remove error as null
danieljbruce edc9df2
withBeginTransaction
danieljbruce d45838e
Add a comment to indicate error
danieljbruce 111a236
Rename method to beginTxAsync
danieljbruce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,12 +42,6 @@ import { | |
} from './request'; | ||
import {AggregateQuery} from './aggregate'; | ||
import {Mutex} from 'async-mutex'; | ||
import MutexInterface from 'async-mutex/lib/MutexInterface'; | ||
|
||
type RunQueryResponseOptional = [ | ||
Entity[] | undefined, | ||
RunQueryInfo | undefined, | ||
]; | ||
|
||
/** | ||
* This type matches data typically passed into a callback the user supplies. | ||
|
@@ -200,52 +194,15 @@ class Transaction extends DatastoreRequest { | |
: () => {}; | ||
const gaxOptions = | ||
typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {}; | ||
const resolver: Resolver<google.datastore.v1.ICommitResponse> = resolve => { | ||
this.#runCommit( | ||
gaxOptions, | ||
(err?: Error | null, resp?: google.datastore.v1.ICommitResponse) => { | ||
resolve({err, resp}); | ||
} | ||
); | ||
}; | ||
this.#withBeginTransaction(gaxOptions, resolver).then( | ||
(response: UserCallbackData<google.datastore.v1.ICommitResponse>) => { | ||
callback(response.err, response.resp); | ||
} | ||
this.#withBeginTransaction( | ||
gaxOptions, | ||
() => { | ||
this.#runCommit(gaxOptions, callback); | ||
}, | ||
callback | ||
); | ||
} | ||
|
||
/** | ||
* If the transaction has not begun yet then this function ensures the transaction | ||
* has started before running the resolver provided. The resolver is a function with one | ||
* argument. This argument is a function that is used to pass errors and | ||
* response data back to the caller of the withBeginTransaction function. | ||
* | ||
* @param {CallOptions | undefined} [gaxOptions] | ||
* @param {Resolver<T>} [resolver] | ||
* @returns {Promise<UserCallbackData<T>>} Returns a promise that will run | ||
* this code and resolve to an error or resolve with the data from the resolver. | ||
* @private | ||
*/ | ||
async #withBeginTransaction<T>( | ||
gaxOptions: CallOptions | undefined, | ||
resolver: Resolver<T> | ||
): Promise<UserCallbackData<T>> { | ||
if (this.#state === TransactionState.NOT_STARTED) { | ||
try { | ||
await this.#mutex.runExclusive(async () => { | ||
if (this.#state === TransactionState.NOT_STARTED) { | ||
const runResults = await this.#runAsync({gaxOptions}); | ||
this.#parseRunSuccess(runResults); | ||
} | ||
}); | ||
} catch (err: any) { | ||
return {err}; | ||
} | ||
} | ||
return await new Promise(resolver); | ||
} | ||
|
||
/** | ||
* Create a query for the specified kind. See {module:datastore/query} for all | ||
* of the available methods. | ||
|
@@ -413,15 +370,12 @@ class Transaction extends DatastoreRequest { | |
: {}; | ||
const callback = | ||
typeof optionsOrCallback === 'function' ? optionsOrCallback : cb!; | ||
const resolver: Resolver<GetResponse> = resolve => { | ||
super.get(keys, options, (err?: Error | null, resp?: GetResponse) => { | ||
resolve({err, resp}); | ||
}); | ||
}; | ||
this.#withBeginTransaction(options.gaxOptions, resolver).then( | ||
(response: UserCallbackData<GetResponse>) => { | ||
callback(response.err, response.resp); | ||
} | ||
this.#withBeginTransaction( | ||
options.gaxOptions, | ||
() => { | ||
super.get(keys, options, callback); | ||
}, | ||
callback | ||
); | ||
} | ||
|
||
|
@@ -829,20 +783,12 @@ class Transaction extends DatastoreRequest { | |
: {}; | ||
const callback = | ||
typeof optionsOrCallback === 'function' ? optionsOrCallback : cb!; | ||
const resolver: Resolver<any> = resolve => { | ||
super.runAggregationQuery( | ||
query, | ||
options, | ||
(err?: Error | null, resp?: any) => { | ||
resolve({err, resp}); | ||
} | ||
); | ||
}; | ||
this.#withBeginTransaction(options.gaxOptions, resolver).then( | ||
(response: UserCallbackData<UserCallbackData<any>>) => { | ||
const error = response.err ? response.err : null; | ||
callback(error, response.resp); | ||
} | ||
this.#withBeginTransaction( | ||
options.gaxOptions, | ||
() => { | ||
super.runAggregationQuery(query, options, callback); | ||
}, | ||
callback | ||
); | ||
} | ||
|
||
|
@@ -875,22 +821,12 @@ class Transaction extends DatastoreRequest { | |
: {}; | ||
const callback = | ||
typeof optionsOrCallback === 'function' ? optionsOrCallback : cb!; | ||
const resolver: Resolver<RunQueryResponseOptional> = resolve => { | ||
super.runQuery( | ||
query, | ||
options, | ||
(err: Error | null, entities?: Entity[], info?: RunQueryInfo) => { | ||
resolve({err, resp: [entities, info]}); | ||
} | ||
); | ||
}; | ||
this.#withBeginTransaction(options.gaxOptions, resolver).then( | ||
(response: UserCallbackData<RunQueryResponseOptional>) => { | ||
const error = response.err ? response.err : null; | ||
const entities = response.resp ? response.resp[0] : undefined; | ||
const info = response.resp ? response.resp[1] : undefined; | ||
callback(error, entities, info); | ||
} | ||
this.#withBeginTransaction( | ||
options.gaxOptions, | ||
() => { | ||
super.runQuery(query, options, callback); | ||
}, | ||
callback | ||
); | ||
} | ||
|
||
|
@@ -1084,6 +1020,41 @@ class Transaction extends DatastoreRequest { | |
|
||
this.save(entities); | ||
} | ||
|
||
/** | ||
* If the transaction has not begun yet then this function ensures the transaction | ||
* has started before running the function provided as a parameter. | ||
* | ||
* @param {CallOptions | undefined} [gaxOptions] Gax options provided by the | ||
* user that are used for the beginTransaction grpc call. | ||
* @param {function} [fn] A function which is run after ensuring a | ||
* beginTransaction call is made. | ||
* @param {function} [callback] A callback provided by the user that expects | ||
* an error in the first argument and a custom data type for the rest of the | ||
* arguments. | ||
* @private | ||
*/ | ||
#withBeginTransaction<T extends any[]>( | ||
gaxOptions: CallOptions | undefined, | ||
fn: () => void, | ||
callback: (...args: [Error | null, ...T] | [Error | null]) => void | ||
): void { | ||
(async () => { | ||
if (this.#state === TransactionState.NOT_STARTED) { | ||
try { | ||
await this.#mutex.runExclusive(async () => { | ||
if (this.#state === TransactionState.NOT_STARTED) { | ||
const runResults = await this.#runAsync({gaxOptions}); | ||
this.#parseRunSuccess(runResults); | ||
} | ||
}); | ||
} catch (err: any) { | ||
return callback(err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What kind of errors might you encounter here? A comment would be useful There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
} | ||
} | ||
return fn(); | ||
})(); | ||
} | ||
} | ||
|
||
export type ModifiedEntities = Array<{ | ||
|
@@ -1127,7 +1098,6 @@ promisifyAll(Transaction, { | |
'save', | ||
'update', | ||
'upsert', | ||
'#withBeginTransaction', | ||
], | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: it looks like runAsync's primary purpose is to call
beginTransaction
. Maybe name it something likebeginTransactionAsync
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, changed to beginTransactionAsync. Since this is in the transaction class run typically means run the transaction, but maybe beginTransactionAsync is more descriptive. Either way, this method is private so the name can be modified later if need be.