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

[@mantine/hooks] Interval Hook start() creates unbounded intervals #782

Closed
melloware opened this issue Jan 31, 2022 · 5 comments
Closed

Comments

@melloware
Copy link
Contributor

What package has an issue

@mantine/hooks

Describe the bug

If you call interval.start() X times it starts X timers but calling stop can only stop the latest one.

In which browser did the problem occur

ALL

If possible, please include a link to a codesandbox with the reproduced problem

https://codesandbox.io/s/sharp-matan-zx040?file=/src/App.tsx

Do you know how to fix the issue

Yes

Are you willing to participate in fixing this issue and create a pull request with the fix

Yes

Possible fix

PR being submitted with...

  const start = () => {
    if (!active) {
       setActive(true);
       intervalRef.current = window.setInterval(fn, interval);
    }
  };
@melloware melloware changed the title Interval Hook start() creates unbounded interval [@mantine/hooks] Interval Hook start() creates unbounded interval Jan 31, 2022
melloware added a commit to melloware/mantine that referenced this issue Jan 31, 2022
@melloware melloware changed the title [@mantine/hooks] Interval Hook start() creates unbounded interval [@mantine/hooks] Interval Hook start() creates unbounded intervals Jan 31, 2022
melloware added a commit to melloware/mantine that referenced this issue Feb 1, 2022
@melloware
Copy link
Contributor Author

PR was merged

@prograpp
Copy link
Contributor

I think this does not completly solve the issue. When calling the start() function multiple times in a short time, the active state won't be changed in-between due to reacts set state caching.

If I am correct, the following would solve that issue:

const start = () => {
    setActive(old => {
        if (!old) {
            intervalRef.current = window.setInterval(fn, interval);
        }
        return true;
    });
};

@melloware
Copy link
Contributor Author

I agree you want to submit a PR?

@prograpp
Copy link
Contributor

Yes, did so

rtivital pushed a commit that referenced this issue May 29, 2022
@melloware
Copy link
Contributor Author

@prograpp I still found a bug with this where I am seeing it hold two instances of my interval timer that was not happening before your fix.

I am going to submit another fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants