This repository was archived by the owner on Oct 16, 2025. It is now read-only.
Suggestion to simplify prevention of dangling Promise on destroy#286
Merged
mikesposito merged 2 commits intofix/internal-listenersfrom Nov 26, 2024
Merged
Conversation
331159a to
f3fe246
Compare
The `fix/internal-listeners` branch has a number of changes intended to ensure we don't have a dangling unresolved Promise when the block tracker is destroyed. This solution involved adding an additional listener to capture errors, and it involved not removing internal listeners when `destroy` is called. This required changes to some logging in `_updateAndQueue` as well. This commit is an alternative solution that avoids the use of internal listeners, thus avoiding much of the complexity in the previous solution. Instead an internal deferred Promise is used. This also might be slightly more efficient when `getLatestBlock` is called repeatedly, as we can reuse the same listener rather than creating a new one each time.
f3fe246 to
3435c0a
Compare
mcmire
reviewed
Nov 22, 2024
| // return if available | ||
| if (this._currentBlock) { | ||
| return this._currentBlock; | ||
| } else if (this.#pendingLatestBlock) { |
Contributor
There was a problem hiding this comment.
Ah, good catch here. I was thinking of also using a deferred promise and keeping it in a private field, but it seemed that calling getLatestBlock multiple times without waiting would overwrite that field. This is a good solution.
mikesposito
reviewed
Nov 22, 2024
| }); | ||
| // return newly set current block | ||
| return latestBlock; | ||
| this.#pendingLatestBlock = { promise, reject }; |
Member
There was a problem hiding this comment.
I'm wondering, if we are making this promise available to the rest of the class, would it make sense to also resolve it through this class variable and get rid of the internal listener altogether?
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
Member
Author
There was a problem hiding this comment.
True, yeah maybe so, good idea
mcmire
reviewed
Nov 22, 2024
Gudahtt
commented
Nov 22, 2024
mikesposito
added a commit
that referenced
this pull request
Dec 3, 2024
* fix: internal listeners infinite retry loop * edit `CHANGELOG` * refactor: rename to `#internalEventListeners` * refactor: simplify listener equality check Co-authored-by: Mark Stacey <markjstacey@gmail.com> * Revert "refactor: simplify listener equality check" This reverts commit eb5514d. * apply fix to `SubscribeBlockTracker` * fix: reject `getLatestBlock` promise when block tracker stops * fix: remove on error internal listener * Suggestion to simplify prevention of dangling Promise on destroy (#286) * Suggestion to simplify prevention of dangling Promise on destroy The `fix/internal-listeners` branch has a number of changes intended to ensure we don't have a dangling unresolved Promise when the block tracker is destroyed. This solution involved adding an additional listener to capture errors, and it involved not removing internal listeners when `destroy` is called. This required changes to some logging in `_updateAndQueue` as well. This commit is an alternative solution that avoids the use of internal listeners, thus avoiding much of the complexity in the previous solution. Instead an internal deferred Promise is used. This also might be slightly more efficient when `getLatestBlock` is called repeatedly, as we can reuse the same listener rather than creating a new one each time. * Unset pending latest block after it has resolved * fix: operand of a delete operator cannot be a private identifier * test: change error message * refactor: add helper methods * refactor: simplify `SubscribeBlockTracker` * Update CHANGELOG.md Co-authored-by: Mark Stacey <markjstacey@gmail.com> * fix: `SubscribeBlockTracker` throws when subscription fails * test: remove broken case * test: add case for returning the same promise * test: add coverage for `PollingBlockTracker` * test: add coverage for `SubscribeBlockTracker` * test: remove redundant tests * test: check promises return for `SubscribeBlockTracker` * rename test case Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com> * test: spy on `provider.request` * edit changelog entry * test: set automatic timer calls to 2 --------- Co-authored-by: Mark Stacey <markjstacey@gmail.com> Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
This file contains hidden or 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
The
fix/internal-listenersbranch has a number of changes intended to ensure we don't have a dangling unresolved Promise when the block tracker is destroyed. This solution involved adding an additional listener to capture errors, and it involved not removing internal listeners whendestroyis called. This required changes to some logging in_updateAndQueueas well.This commit is an alternative solution that avoids the use of internal listeners, thus avoiding much of the complexity in the previous solution. Instead an internal deferred Promise is used. This also might be slightly more efficient when
getLatestBlockis called repeatedly, as we can reuse the same listener rather than creating a new one each time.