Skip to content
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

Call clearInterval after setInterval when needed #1583

Merged
merged 2 commits into from
Apr 26, 2023
Merged

Conversation

mvandenburgh
Copy link
Member

This fixes a bug where the setInterval would continue firing after the user navigates from one dandiset to a new dandiset. This would result in unexpected behavior, such as the overwriting of the new dandiset with the metadata from the old one retrieved by the setInterval call.

One note about the changes - I changed the use of setInterval to window.setInterval. I couldn't get TypeScript to play nicely with the return value of setInterval for some reason; it was trying to get me to import a type from NodeJS, but since we're in a browser environment it was erroring out. They are functionally identical though.

This fixes a bug where the `setInterval` would continue firing
after the user navigates from one dandiset to a new dandiset.
This would result in unexpected behavior, such as the overwriting
of the new dandiset with the metadata from the old one retrieved
by the `setInterval` call.
@mvandenburgh
Copy link
Member Author

Vue docs explicitly mention onUnmounted as the correct lifecycle hook for this btw https://vuejs.org/api/composition-api-lifecycle.html#onunmounted

Use this hook to clean up manually created side effects such as timers, DOM event listeners or server connections.

Copy link
Member

@waxlamp waxlamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you changed the function from setInterval() to window.setInterval(), did the typechecking errors go away? This Stack Overflow question (which you may already have seen) talks about the difference between the web and Node versions of setInterval() and suggests a TypeScript idiom that would work for both environments (and allow you not to include the window. prefix).

web/src/components/DLP/OverviewTab.vue Outdated Show resolved Hide resolved
web/src/views/DandisetLandingView/DandisetPublish.vue Outdated Show resolved Hide resolved
@mvandenburgh
Copy link
Member Author

When you changed the function from setInterval() to window.setInterval(), did the typechecking errors go away? This Stack Overflow question (which you may already have seen) talks about the difference between the web and Node versions of setInterval() and suggests a TypeScript idiom that would work for both environments (and allow you not to include the window. prefix).

Yeah, I saw that SO answer. I couldn't get it to work, it causes this error for me -
Property '__promisify__' is missing in type 'Timer' but required in type 'typeof setInterval'.ts(2741)

Copy link
Member

@waxlamp waxlamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If others are happy with this, then it LGTM.

Comment on lines +336 to +338
if (timer === undefined) {
throw Error('Invalid timer value');
}
Copy link
Member

@waxlamp waxlamp Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To the extent that we think it's actually an error condition if the timer is not set at this point, this is fine. If it ever happens that we want to cancel the interval timer for some other reason, then I think we would invert this conditional, remove the throw statement, and not worry about a non-existing timer.

UPDATED TO ADD: I understand this code appears this way largely for typechecking reasons. My suggestions about the importance of recognizing this as an error condition would preserve the typechecking functions while omitting the raising of an error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll also add, the throw statement has the advantage of causing a sentry error if it somehow ever did get called.

@mvandenburgh mvandenburgh added patch Increment the patch version when merged release Create a release when this pr is merged and removed patch Increment the patch version when merged release Create a release when this pr is merged labels Apr 26, 2023
@mvandenburgh mvandenburgh merged commit 7422a5e into master Apr 26, 2023
@mvandenburgh mvandenburgh deleted the clear-intervals branch April 26, 2023 17:52
@dandibot
Copy link
Member

🚀 PR was released in v0.3.33 🚀

@dandibot dandibot added the released This issue/pull request has been released. label Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants