fix(idempotency): preserve scope of decorated class #2693
Merged
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.
Summary
Changes
This PR addresses the bug in the linked issue that caused class methods decorated with the
@idempotent
decorator to lose the original scope of the class.In simpler terms, when working with classes it's common to reference other class methods or properties. In JavaScript this is done via the
this
keyword, which in the case of classes is bound to the class instance.For example, in the code snippet below the
handler
method is able to access thelogger
property by using thethis
keyword, which at runtime is bound to thefooInstance
instance.When working with class method decorators in TypeScript it's important to keep in mind that the decorator replaces the method implementation with one it defines. In doing so, it's important to pass down the original scope so that the decorated method maintains access to the
this
scope.This PR makes changes to the
makeIdempotent
function, theIdempotencyHandler
class, and theidempotent
decorator implementation so that this happens.Specifically, the PR:
makeIdempotent
function from an arrow function to a regular function so that it maintains a binding tothis
idempotent
decorator to explicitly.bind(this)
on themakeIdempotent
function when replacing the implementation of the decorated methodIdempotencyHandler
to now accept a newthisArg
parameter and restore it onto the original function when calling itI have also added a new unit test case and modified one of the integration test cases to confirm that the fix works.
Issue number: closes #2692
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.