fix(event-handler): fix decorated scope in appsync events #3974
+189
−11
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 improves the decorator logic of the
AppSyncEventsResolver
by fixing a fundamental issue that prevented it from being useful.Prior to this fix the decorators would consistently cause the decorated methods to become unbound and lose the reference to
this
, preventing customers to access other class methods or properties from within the decorated methods.This was happening because unlike other decorator implementations in the codebase, the one in this area of the codebase works in two steps:
app.resolve
method can call a decorated method depending on routing decisionsBy the time the reference is called, the initial scope is lost causing the issue. At the same time, the
this
scope is not yet available at init time - this is because decorators are evaluated before class init - so it's not possible to save a reference or bindthis
along with the the decorated method.Because of this, we need to get around this decorator design aspect.
Specifically, at runtime (step 2) - which is when
app.resolve()
is called - we introspect the class methods, find their definition, and when we encounter the.resolve
keyword (which represents the method), we save a reference tothis
for later use. This reference is then used and passed to the decorated method to apply the correctthis
value.While this is relatively unorthodox, as long as we find one instance of
this
that belongs to the class to which the decorated method belongs it should be fine.Issue number: fixes #3973
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.