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

npm package (v2.0.3) cancel() kills the function #111

Closed
nikparo opened this issue Sep 10, 2020 · 5 comments
Closed

npm package (v2.0.3) cancel() kills the function #111

nikparo opened this issue Sep 10, 2020 · 5 comments

Comments

@nikparo
Copy link

nikparo commented Sep 10, 2020

In the latest version on npm (2.0.3) the cancel function does not unset the requestId, unlike the current master branch. If it is called while a raf call is queued, it effectively terminates the function permanently. Could you please publish a new version to npm?

@wuct
Copy link
Owner

wuct commented Sep 13, 2020

Thanks for let me know! It should be released long time ago. I have just published the latest version. Let me know whether the issue still exists.

@nikparo
Copy link
Author

nikparo commented Sep 13, 2020

Thank you. However, I ended up copying the code and extending it for our use. In particular, I added .start() and .stop() functions since I found .cancel() to be insufficient:

  1. In some browsers cancelAnimationFrame() is unable to cancel callbacks pending for the current animation frame. (cancelAnimationFrame should also cancel pending callbacks whatwg/html#4359)
  2. Unsubscription in other libraries (in my case redux) is not necessarily immediate and may still call the function even after their unsubscribe callback has been called.

I also added an isAnimationFrame function to check whether we are currently in an animation frame in order to call some methods immediately rather than their throttled counterparts. (Only those scheduled by raf-throttle unfortunately, I don't know whether a global check is possible.)

If you want I could open pull requests for some or all of these?

@wuct
Copy link
Owner

wuct commented Sep 14, 2020

Oh, I did not know the cancelAnimationFrame() works differently in different browsers, thanks for the whatwg link!
I would like to keep the API minimized, but I am also curious about how do you implement .start() and .stop() 😆
Do you use a flag to check whether the registered callback should be invoked or not?

@nikparo
Copy link
Author

nikparo commented Sep 14, 2020

Yeah, essentially it's just a flag:

const later = context => () => {
  requestId = null;
  if (!stopped) {
    callback.apply(context, lastArgs);
  }
};

const throttled = function(...args) {
  lastArgs = args;
  if (requestId === null && !stopped) {
    requestId = requestAnimationFrame(later(this));
  }
};

// ...

throttled.stop = () => {
  stopped = true;
  lastArgs = undefined;
  throttled.cancel();
};

throttled.start = () => {
  stopped = false;
};

@wuct
Copy link
Owner

wuct commented Sep 14, 2020

Thanks for sharing 👍
I am going to close the issue since the code has been released. Feel free to reopen if the issue still existed.

@wuct wuct closed this as completed Sep 14, 2020
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