ysmood / yaku

A lightweight promise library
https://tonicdev.com/ysmood/yaku
MIT License
291 stars 28 forks source link

Use native Promise instead of setTimeout as it is not vulnerable to throttling #51

Closed gtanczyk closed 7 years ago

gtanczyk commented 7 years ago

FF and Chrome are throttling setTimeout to 1000ms in inactive tabs: https://bugzilla.mozilla.org/show_bug.cgi?id=633421 https://bugs.chromium.org/p/chromium/issues/detail?id=66078

I've encountered a problem with flaky test and after a while of debugging it turned out to be a problem with Yaku using setTimeout. Debug log statements happened in exact 1 second time distance, while they were called one after each other. The reason for this was special behavior of Chrome on OSX, where setTimeout is clamped for a window that is not on the top.

The solution is to use native Promise if possible as this is closer to node nextTick.

Quick jsfiddle to play with this: https://jsfiddle.net/0npx5v5r/

coveralls commented 7 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling e9dd61744bcf097d48e39549e5b50ec281673eba on gtanczyk:master into cc7bc0dcf817c7f5390eabd540dff1ebf689ce18 on ysmood:master.

ysmood commented 7 years ago

That is a great idea, thanks!

ysmood commented 7 years ago

By the way in the doc section https://github.com/ysmood/yaku#yakunexttick, I mentioned that you could use mature third party lib such as https://github.com/medikoo/next-tick, it might be better than your workaround.

ysmood commented 7 years ago

I published the v0.18.3, please check it out.

gtanczyk commented 7 years ago

It works as expected :+1:

My only concern are any sort of unit tests - anything that relied on sinon and fake timers in particular.

I think I will also contribute this idea to medikoo/next-tick, because using MutationObserver to resolve a promise does not sound good. :-1:

ysmood commented 7 years ago

I added a test for it, but it's a very simple one, you can also check the code in 4fc271d.

using MutationObserver to resolve a promise does not sound good

If you dig into it, you will find out it's a good workaround and performs well on most of the browsers. For old browsers, I use https://github.com/YuzuJS/setImmediate.