Draft-js: Remove use of setImmediate from fbjs

Created on 11 Jul 2019  路  7Comments  路  Source: facebook/draft-js

Draft-js uses non-complaint setImmediate polyfill from fbjs. fbjs has had open tickets to fix this issue for years, it's time to assume it will never be fixed and stop using it.

fbjs assumes there is a global called global and attempts to polyfill setimmediate on it. Then fbjs tries to expose this polyfilled function incorrectly. In spec conformant environments there is no global called global and a runtime error occurs from trying to access a nonexistent variable.

Note the tc39 proposal where they got global from changed to globalThis nearly a year ago.

Why not just setTimeout(fn, 0)? It is only being used as a hack anyway for other event handlers to fire first.

All 7 comments

Maybe @zpao can shed some light on the direction of fbjs and future plans for these polyfills?

Just to add another non-legacy use-case this is happening in electron when using AntD.

Why not just setTimeout(fn, 0)? It is only being used as a hack anyway for other event handlers to fire first.

Because it may be not fast enough?
If you compare setTimeout(callback, 0) with setImmediate(callback) you should see, that setTimeout waits more then 0ms before the callback gets executed.

You can see the difference here.

var timeouts = 250;
var timeout = function(){
    --timeouts ? window.setTimeout(timeout, 0) : console.timeEnd('setTimeout');
}
console.time('setTimeout');
timeout();

var immediates = 250;
var immediate = function(){
    --immediates ? window.setImmediate(immediate) : console.timeEnd('setImmediate');
}
console.time('setImmediate');
immediate();

If you like to try out the snippet above, I suggest you to do it in the browser console of the demo page, where the setImmediate is pollyfilled for browsers which don't support it natively

The point, where I 100% agree is, that it is really sad, that the fbjs doesn't fix their setImmediate export...

or just import https://www.npmjs.com/package/setimmediate themselves.

I've created a pull request https://github.com/facebook/fbjs/pull/387, which should fix most of the issues related to the setImmediate/global things. So let's see if it helps... At least it makes draft-js usable in IE11 馃檹

2 years later since https://github.com/facebook/fbjs/issues/290, any quick solution here?

Was this page helpful?
0 / 5 - 0 ratings