yuzujs / setimmediate Goto Github PK
View Code? Open in Web Editor NEWA cross-browser implementation of the new setImmediate API.
License: MIT License
A cross-browser implementation of the new setImmediate API.
License: MIT License
Would you add support for component and bower?
Reading the Chrome issue related to setImmediate implementations it was suggested to use MutationObserver inside the polyfill. Thoughts for here?
Currently in chrome 47 (beta atm)
Hey all,
Is there a way to import this package as a function (in my case with webpack) and not have it polyfill the function on the global variable? I'd like to be able to use this function in lib code and not alter other people's global variables.
Thanks!
setTimeout(() => {
console.log('time out')
}, 0)
setimmediate(() => {
console.log('setimmediate')
})
// console.log : setimmediate time out
// this should be time out setimmediate ?
Hello,
Using installPostMessageImplementation()
will use global.postMessage to send messages. If, let's say you maintain a browser extension that has access to window
and executes this code, you will send a message that a web page could be listening to. If the web page has code that is listening for messages, but does not clean the message before processing (JSON.parse(message)
), you could break a site unintentionally.
I know as professionals we should clean input before processing or try/catch, etc, but big sites such as icloud.com
fall victim to this issue.
After navigating to the notes section after logging in to icloud.com
the page will error if you execute a setImmediate()
call sometime during loading.
Firefox logs show:
Message: TypeError: can't define property "onmessage": Object is not extensible
Stack:
canUsePostMessage
Removing the setImmediate
lib resolves the issue.
I found this issue working with Deno (v1.23.3) and another library that has a transitive dependency on setimmediate
. That bug report is here.
The bug is that if setimmediate
is referenced in a Deno script, the deno script will never finish the process normally. Instead, Deno.exit()
(similar to NodeJS process.exit()
) must be called.
To reproduce:
docker run -it --init denoland/deno:ubuntu sh
)test.ts
file with the following contents:
import * as setImmediate from 'https://raw.githubusercontent.com/YuzuJS/setImmediate/master/setImmediate.js';
console.log('End of program', setImmediate);
deno run test.ts
Expected:
Observed:
I'm pretty sure this is caused by the opening of a MessageChannel
and setting a message listener for one of the ports; https://github.com/YuzuJS/setImmediate/blob/master/setImmediate.js#L126
In Internet Explorer 6 and 7, modifying the DOM from Javascript during page load is problematic and can lead to an "Operation aborted" error.
The readystatechange implementation of setImmediate, which is used in IE6 and 7,
does modify the DOM and causes this error if setImmediate
is called before
the page is completely loaded.
For example the following HTML causes such a "Operation aborted" popup, followed
by an error page:
<html>
<head>
<script src="setImmediate.js" type="text/javascript"> </script>
<script type="text/javascript">
setImmediate(function(){});
</script>
</head>
</html>
Instead of relying in ugly sleep function(new Date() eating CPU until request finishes)
We are using setImmediate through a Promise polyfill (https://github.com/taylorhakes/promise-polyfill) and for the most part it is working perfectly. However, on a few sites, it is throwing a "setImmediate is not defined" error. I have traced the problem down to where it looks for postMessage
(https://github.com/YuzuJS/setImmediate/blob/master/setImmediate.js#L87)
It is not getting through that if statement because postMessage
is returning as undefined.
However, when I type postMessage
in the console, it shows up as a function.
Is there a way that we can check to see if postMessage
is available in the window object as well as in the global?
Am I missing something here?
We have two sites this is happening on and hundreds of sites where there is no issue (including all our dev sites).
Would be great to have this little util in the bower registry
Please fix @benjamn...
I added an easy way to test with npm run test-browser
. IE emulation mode works well.
This is not really an issue (in the sense of that somethings wrong with the code). Rather I have two questions and would appreciate some insight if somebody can give it. thank you.
I am concerned with the runIfPresent thing. I experience that on Firefox 23 it happens that really there sometimes is the case currentlyRunningATask is actually still set to true. How can this ever actually happen. I thought that that (discarding the case of WebWorkers) Javascript guarantees that there is only one thing at a time executing which will be able to run until the end. As it is I cannot see the case or reason why/when it would occur that runIfPresent is executed while still having not finished execution the task from last time. This I assume could at best only happen when I use the function called via setImmediate to forcefully call runIfPresent. Anyway in Firefox (my version is 23) when I do some
window.setImmediate(function(){alert("hello_1");}
window.setImmediate(function(){alert("hello_2");}
window.setImmediate(function(){alert("hello_3");}
it really is happening that the currentlyRunningATask. I fear this might still be one of the oddities related to crazy firefox way to do alert stuff. (http://stackoverflow.com/q/16987893/1711186)
It seems using "canUsePostMessage" generates a message window.postMessage which will trigger an "message" even for the only eventuallly attached callback onGlobalMessage. This does not make a problem there (as the posted message is empty and hence does nothing there).
For any help I am happy, thanks
I like the visual demo of quicksort - setImmediate is much faster than using timers. Does this run fast in Deno? There is no setImmediate in Deno from what I can see.
i have found that using mutation observer work much faster that postMessage, can you add it to your library. here are some examples
http://jsfiddle.net/6TZ9J/1/
http://jsfiddle.net/uLgcxhj7/
tested in 36 chrome
var setImmediate = require("setImmediate")
It should be a clean function without global side effects.
Have side effects be opt in.
it will be good, if clearTimeout will clear setImmediate timers
of course, replacing native clearTimeout required to implement that.
I just ripped out Testling because it never works. But working cross-browser cloud testing would be nice.
Zuul has this baked in to some extent; need to look into it more.
@kriskowal also had a pretty extensive doc on how to run the Q v2 tests in Sauce Labs, but I'm having trouble finding it now.
This is still the best lib I was able to find. It works as-is. Thank you!
But for the future,
As for 3), with the whole CommonJS and ES6 and TypeScript mess, it's quite a lot of work.
Anyway, thank you.
On latest Safari 7.1.2 setImmediate sort @ http://jphpsf.github.io/setImmediate-shim-demo/
performs no better (and sometimes worse) than the 4ms setTimeout. Chrome & Firefox seem
unaffected.
Does anyone know if Apple throttled their postMessage implementation down to 4ms?
your code looks like this:
function installMessageChannelImplementation(attachTo) {
attachTo.setImmediate = function () {
var handle = tasks.addFromSetImmediateArguments(arguments);
// Create a channel and immediately post a message to it with the handle.
var channel = new global.MessageChannel();
channel.port1.onmessage = function () {
tasks.runIfPresent(handle);
};
// IE10 requires a message, so send `null`.
channel.port2.postMessage(null);
return handle;
};
}
if you rewrite your code to this:
function installMessageChannelImplementation(attachTo) {
var channel = new global.MessageChannel();
channel.port1.onmessage = function (event) {
var handle = event.data;
tasks.runIfPresent(handle);
};
attachTo.setImmediate = function () {
var handle = tasks.addFromSetImmediateArguments(arguments);
channel.port2.postMessage(handle);
return handle;
};
}
it will work better?
Chrome and Opera supports MessageChanel,
seems setImmediate can be implemented with it:
var m = new MessageChannel();
var setZTimeout = function (f) {
m.port1.onmessage = function (x) {
f();
};
m.port2.postMessage('');
}
MessageChannel works form Workers and SharedWorkers too, althout window.postMessage can't work form Workers
Hi,
is there an exact reason of choosing 'self' over a 'window' object?
I found couple of sites that were using some weird CMS libraries that actually rewrote the 'self' object to their own hence breaking the polyfill and throwing an error about setImmediate not defined.
Is there a way how to bypass this issue?
Thanks in advance.
Kinda desperate :/
my test, Firefox browser:
asyncTest("Execution order for setImmediate via postMessage", 1, function () {
var orderOfExecution = '';
var tmp = setImmediate(function () {
});
clearImmediate(tmp);
window.onmessage = function (event) {
event = event || window.event;
if (event.data === 'some other message') {
orderOfExecution += '1';
if (orderOfExecution.length === 2) {
strictEqual(orderOfExecution === '12', true, "Execution order is wrong");
start();
}
}
};
window.postMessage('some other message', '*');
setImmediate(function () {
orderOfExecution += '2';
if (orderOfExecution.length === 2) {
strictEqual(orderOfExecution === '12', true, "Execution order is wrong");
start();
}
});
});
Hello!
I found a problem with setImmediate in a webworker in Chrome, that does not happen in a regular browser script.
The minimal problem demonstration consists of 3 files: index.html, webworker.js and setImmediate.js:
https://gist.github.com/sisou/36349497b4d65e7db0ce98812c53d88f
In the index.html, I start a setInterval
to output stuff every second. Then, I run a function work()
20000x that calls itself when it is done with setImmediate(work)
.
In the index.html inline script, this works fine and I get the work and the output every second.
In parallel I also start a webworker with the exact same code, but there the setInterval is not processed every second.
It works as expected in Firefox. I can only test Chrome additionally (no access to Windows right now).
another test:
Chrome this test fails for current implementation:
asyncTest("setImmediate should wait other immediates even with alert,promt,showModalDialog", 1, function () {
var completed = false;
setImmediate(function () {
if (window.showModalDialog) {
window.showModalDialog('selfclose.html');
}
completed = true;
});
setImmediate(function () {
strictEqual(completed, true, "Execution order is wrong");
start();
});
});
where selfclose.html contains:
<script> setTimeout(function () { window.close(); }, 1000); </script>see my fork for workaround
(function() {
...
})(new Function("return this")())
What's the difference between simply using this
and new Function("return this")()
?
I'm considering using this library in a Firefox Addon and Chrome Extension, but because of the last line my addon will be rejected by Mozilla Addon reviewers, as it is using eval.
Chrome also complains:
Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:".
, and I don't like the idea of relaxing Content Security Policy.
Is there a special reason for getting the global
that way?
Why not typeof global != 'undefined' ? global : this
?
This should work in all browsers/extensions + node.js.
I like the visual demo of quicksort - setImmediate is much faster than using timers. Does this run fast in Deno? There is no setImmediate in Deno from what I can see.
Hi!
I've tried to use this module by the CommonJS convention, like that:
var setImmediate = require('setimmediate');
setImmediate(function () {});
And found out that this code doesn't work.
Is there a specific reason you keep to exposing setImmediate
to the global scope instead of exporting a module?
It's quite uncomfortable to use that as a polyfill as a module, because there's a special service for polyfills, polyfill.io, which ensures the environment and in that a user can avoid importing polyfills as dependencies.
Besides, user can always compile module standalone with browserify -s
, so it is not necessary to force him into using global-scope setImmediate.
I don't use the lib but why don't you use promises?
Example:
window.setImmediate = function(f) {
var args = Array.prototype.slice.call(arguments, 1);
Promise.resolve().then(function() {
f.apply(undefined, args);
});
};
Or optimized version:
window.setImmediate = (function() {
var resolvedPromise = Promise.resolve(0);
return function(f) {
for (var i = 1, args = []; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
resolvedPromise.then(function(v) {
switch (args.length) {
case 0: f(); break;
case 1: f(args[0]); break;
case 2: f(args[0], args[1]); break;
case 3: f(args[0], args[1], args[2]); break;
default: f.apply(undefined, args); break;
}
});
};
})();
I'd suggest adding some mention of queueMicrotask
in the section of the README on macrotasks/microtasks: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/queueMicrotask
Not offering a specific PR, but seems it would be germane and of interest.
I'm not sure if this is an issue for here.
if i put YuzuJS/setImmediate before jdarling/Object.observe. then my cpu usage will rush a high level and keeping there. (safari 7.0.6 / osx 10.9.4)
there is a simple jsfiddle without any other code. just reference two js library.
http://jsfiddle.net/colder/ey6mrdfg/1/
It looks like there is a bug inside IE10+'s implementation of setImmedate:
http://codeforhire.com/2013/09/21/setimmediate-and-messagechannel-broken-on-internet-explorer-10/
It might be desirable to ignore IE's setImmedate implementation and use the onreadystatechange trick for it.
When I install this package from npm, I don't get the documentation because the files
property in package.json
only lists index.js
:
Lines 14 to 16 in f1ccbfd
This makes working offline much more difficult.
I am using JSZip which depends on this library. Recently I have been getting errors in my onmessage handler, getting data that I don't recognize. Console logs revealed the following:
The onmessage handler that is being triggered is for a specific worker on the main thread, which further confuses me -- I'm not sure why, but it seems this library is somehow hooking into a specific webworker postMessage call?
trying to use JSDOM, which uses setImmediate, in an Atom package and run into content security policy restrictions which prevent the use of new Function
and eval
. it seems using function(){return this}
would work just as well and avoid this completely
Works in all browsers, but in IE9 - IE11 I am getting consistent 1.5s delay on every call.
I even tried to comment out verification for existing implementation, but nothing changed.
What's even more confusing - your demo works fast in those browsers. And I tried the version that's in the demo, also no luck. How is it possible - I don't know.
I'm lost here.
Do you see anything wrong with this test? -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="../src/set-immediate.js"></script>
<title>Title</title>
</head>
<body>
</body>
<script type="application/javascript">
var start = Date.now();
setImmediate(function(){
alert(Date.now() - start);
});
</script>
</html>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.