Fix Issue #93: Prevent Getters from Being Called During Decoration and Preserve this Context #104
+96
−25
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.
Description:
Howdy everyone,
This PR addresses issue #93, where getters were being invoked during the decoration phase, leading to unintended side effects such as ReferenceError due to premature access before class initialization. Previously, attempts by @neodon to resolve this issue were effective in some cases but failed to preserve correct
this
context.Proposed Solution:
To circumvent the loss of
this
context when reassigning getter implementation, I simply wrapped the original getter in a non-memoized function. This wrapped function is then assigned as the new getter, ensuring that the getter logic executes only when accessed without interfering with class instantiation or dependencies.Key Points:
@memoizeDecorator
now has its own memoized function stored using a unique Symbol so that memoization caches are per-instance.Testing
The current implementation passes all existing tests - it may be worth adding some to ensure
this
is preserved properly. I'm not sure what specific issues @neodon ran into preservingthis
, but the following code behaves as expected:Hope this helps!
Owen Rossi-Keen, Iliad.dev