Comparing version 0.7.3 to 0.8.0
{ | ||
"name": "setasap", | ||
"version": "0.7.3", | ||
"version": "0.8.0", | ||
"description": "setImmediate polyfill for the browser and node", | ||
@@ -5,0 +5,0 @@ "main": "setAsap.js", |
@@ -5,3 +5,3 @@ (function (main, undefined) { | ||
var callbacks = [], timeout, hiddenDiv, scriptEl, timeoutFn; | ||
if(typeof window !== 'undefined') main = window; | ||
if (typeof window !== 'undefined') main = window; | ||
// Modern browsers, fastest async | ||
@@ -11,9 +11,6 @@ if (main.MutationObserver) { | ||
(new MutationObserver(executeCallbacks)).observe(hiddenDiv, { attributes: true }); | ||
return function (callback) { | ||
if (!callbacks.length) { | ||
hiddenDiv.setAttribute('yes', 'no'); | ||
} | ||
callbacks.push(callback); | ||
}; | ||
// Browsers that support postMessage | ||
return getAsap(function () { | ||
hiddenDiv.setAttribute('yes', 'no'); | ||
}); | ||
// Browsers that support postMessage | ||
} else if (!main.setImmediate && main.postMessage && !main.importScripts && main.addEventListener) { | ||
@@ -24,3 +21,2 @@ var MESSAGE_PREFIX = "com.setImmediate" + Math.random(), hasPostMessage = false; | ||
if (event.source === main && event.data === MESSAGE_PREFIX) { | ||
hasPostMessage = false; | ||
executeCallbacks(); | ||
@@ -32,37 +28,34 @@ } | ||
return function (fn) { | ||
callbacks.push(fn); | ||
if (!hasPostMessage) { | ||
hasPostMessage = true; | ||
main.postMessage(MESSAGE_PREFIX, "*"); | ||
} | ||
}; | ||
// IE browsers without postMessage | ||
return getAsap(function () { | ||
main.postMessage(MESSAGE_PREFIX, "*"); | ||
}); | ||
// IE browsers without postMessage | ||
} else if (!main.setImmediate && main.document && 'onreadystatechange' in document.createElement('script')) { | ||
return function (callback) { | ||
callbacks.push(callback); | ||
if (!scriptEl) { | ||
scriptEl = document.createElement("script"); | ||
scriptEl.onreadystatechange = function () { | ||
scriptEl.onreadystatechange = null; | ||
scriptEl.parentNode.removeChild(scriptEl); | ||
scriptEl = null; | ||
executeCallbacks(); | ||
}; | ||
document.body.appendChild(scriptEl); | ||
} | ||
}; | ||
// All other browsers and node | ||
return getAsap(function () { | ||
scriptEl = document.createElement("script"); | ||
scriptEl.onreadystatechange = function () { | ||
scriptEl.onreadystatechange = null; | ||
scriptEl.parentNode.removeChild(scriptEl); | ||
scriptEl = null; | ||
executeCallbacks(); | ||
}; | ||
document.body.appendChild(scriptEl); | ||
}); | ||
// All other browsers and node | ||
} else { | ||
timeoutFn = main.setImmediate || (main.process && main.process.nextTick) || setTimeout; | ||
return getAsap(function () { | ||
timeoutFn(function () { | ||
executeCallbacks(); | ||
}, 0); | ||
}); | ||
} | ||
function getAsap(fn) { | ||
return function (callback) { | ||
if (!callback.length) { | ||
fn(); | ||
} | ||
callbacks.push(callback); | ||
if (!timeout) { | ||
timeout = timeoutFn(function () { | ||
timeout = undefined; | ||
executeCallbacks(); | ||
}, 0); | ||
} | ||
}; | ||
} | ||
} | ||
@@ -88,3 +81,2 @@ | ||
} | ||
})(this); |
@@ -1,2 +0,2 @@ | ||
/*! setasap 13-05-2014 */ | ||
!function(a,b){"use strict";var c=function(){function c(){var a=h;h=[];for(var b=0,c=a.length;c>b;b++)a[b]()}var d,e,f,g,h=[];if("undefined"!=typeof window&&(a=window),a.MutationObserver)return e=document.createElement("div"),new MutationObserver(c).observe(e,{attributes:!0}),function(a){h.length||e.setAttribute("yes","no"),h.push(a)};if(!a.setImmediate&&a.postMessage&&!a.importScripts&&a.addEventListener){var i="com.setImmediate"+Math.random(),j=!1,k=function(b){b.source===a&&b.data===i&&(j=!1,c())};return a.addEventListener("message",k,!1),function(b){h.push(b),j||(j=!0,a.postMessage(i,"*"))}}return!a.setImmediate&&a.document&&"onreadystatechange"in document.createElement("script")?function(a){h.push(a),f||(f=document.createElement("script"),f.onreadystatechange=function(){f.onreadystatechange=null,f.parentNode.removeChild(f),f=null,c()},document.body.appendChild(f))}:(g=a.setImmediate||a.process&&a.process.nextTick||setTimeout,function(a){h.push(a),d||(d=g(function(){d=b,c()},0))})}();"undefined"!=typeof module&&module.exports?module.exports=c:"undefined"!=typeof require&&require.amd?define(function(){return c}):a.setAsap=c}(this); | ||
/*! setasap 08-01-2015 */ | ||
!function(a){"use strict";var b=function(){function b(a){return function(b){b.length||a(),g.push(b)}}function c(){var a=g;g=[];for(var b=0,c=a.length;c>b;b++)a[b]()}var d,e,f,g=[];if("undefined"!=typeof window&&(a=window),a.MutationObserver)return d=document.createElement("div"),new MutationObserver(c).observe(d,{attributes:!0}),b(function(){d.setAttribute("yes","no")});if(!a.setImmediate&&a.postMessage&&!a.importScripts&&a.addEventListener){var h="com.setImmediate"+Math.random(),i=function(b){b.source===a&&b.data===h&&c()};return a.addEventListener("message",i,!1),b(function(){a.postMessage(h,"*")})}return!a.setImmediate&&a.document&&"onreadystatechange"in document.createElement("script")?b(function(){e=document.createElement("script"),e.onreadystatechange=function(){e.onreadystatechange=null,e.parentNode.removeChild(e),e=null,c()},document.body.appendChild(e)}):(f=a.setImmediate||a.process&&a.process.nextTick||setTimeout,b(function(){f(function(){c()},0)}))}();"undefined"!=typeof module&&module.exports?module.exports=b:"undefined"!=typeof require&&require.amd?define(function(){return b}):a.setAsap=b}(this); |
9379
204