GithubHelp home page GithubHelp logo

setimmediate's People

Contributors

benjamn avatar domenic avatar donavon avatar duzun avatar guss77 avatar jphpsf avatar lcfvs avatar oresoftware avatar rubenverborgh avatar vzwick avatar yaffle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

setimmediate's Issues

Use of MutationObserver

Reading the Chrome issue related to setImmediate implementations it was suggested to use MutationObserver inside the polyfill. Thoughts for here?

How about requestIdleCallback?

Currently in chrome 47 (beta atm)

  • Could it be used?
  • Is it faster?
  • Is it better?
  • Is it slower?
  • Advantages / Disadvantage?

Not relying on global scope

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


setTimeout(() => {
  console.log('time out')
}, 0)

setimmediate(() => {
  console.log('setimmediate')
})

//  console.log : setimmediate time out
// this should be time out setimmediate ?

global.postMessage issue

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.

Referencing `setImmediate` prevents a Deno process from finishing

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:

  • Have Deno installed, or run it from a Docker container (eg. docker run -it --init denoland/deno:ubuntu sh)
  • Create a 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);
  • Run using deno run test.ts

Expected:

  • "End of program Module {}" is logged to console
  • The Deno process exits.

Observed:

  • "End of program Module {}" is logged to console
  • The Deno process never exits. It continues to idle.

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

Fatal "Operation aborted" errors when used during page load in IE6/IE7

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>

postMessage is 'undefined' and throws 'setImmediate is not defined' error

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.

image

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).

bower

Would be great to have this little util in the bower registry

simple two question

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.

  1. 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)

  2. 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

Deno support

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.

dont patch globals

var setImmediate = require("setImmediate")

It should be a clean function without global side effects.

Have side effects be opt in.

Add Sauce Labs test support

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.

Thank you and the future

This is still the best lib I was able to find. It works as-is. Thank you!

But for the future,

  1. Outdated issues should be closed.
  2. Obsolete browsers and implementations should be removed.
  3. It should not polyfill global space, it should export instead.

As for 3), with the whole CommonJS and ES6 and TypeScript mess, it's quite a lot of work.

Anyway, thank you.

setImmediate via MessageChannel performance

958748d

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?

MessageChannel

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

window vs self

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 :/

Execution order for setImmediate via postMessage is wrong:

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();
      }
    });

});

setImmediate blocks setInterval in webworker (Chrome)

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

Problem

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.

Only Chrome

It works as expected in Firefox. I can only test Chrome additionally (no access to Windows right now).

setImmediate should "Wait until any invocations of this algorithm started before this one have completed."

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

Why not just use "this"

  (function() {
     ...
  })(new Function("return this")())

What's the difference between simply using this and new Function("return this")()?

new Function('return this')() is eval == bad code

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.

Deno support

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.

module.exports instead of global polyfill?

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.

Cannot read property 'bind' of undefined

I have a project wich is in production for over a year and a half, and suddenly now, without changing anything from my package.json or any version at all I'm getting this:

image

If I go deeper into the error I manage to find this:

image

I have no idea of what could go wrong, any solutions? Thanks

How about Promise?

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;
			}
		});
	};
})();

postMessage triggering webworker communication

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:

image

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?

new Function() is unnecessary and interferes with CSP

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

Long wait in IE9 - IE11

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>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.