Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make immediate webpackable by not referencing global as global variable #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 40 additions & 29 deletions dist/immediate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.immediate = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(_dereq_,module,exports){
(function (global){
'use strict';
var types = [
_dereq_('./nextTick'),
Expand All @@ -13,6 +14,19 @@ var currentQueue;
var queueIndex = -1;
var queue = [];
var scheduled = false;

var globalObject;
if(typeof window!=="undefined") {
globalObject=window;
} else if(typeof global!=="undefined") {
globalObject=global;
} else if(typeof self!=="undefined") {
globalObject=self;
} else {
globalObject=this;
}


function cleanUpNextTick() {
if (!draining || !currentQueue) {
return;
Expand Down Expand Up @@ -55,8 +69,8 @@ var scheduleDrain;
var i = -1;
var len = types.length;
while (++i < len) {
if (types[i] && types[i].test && types[i].test()) {
scheduleDrain = types[i].install(nextTick);
if (types[i] && types[i].test && types[i].test(globalObject)) {
scheduleDrain = types[i].install(nextTick, globalObject);
break;
}
}
Expand Down Expand Up @@ -97,93 +111,90 @@ function immediate(task) {
}
}

}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"./messageChannel":2,"./mutation.js":3,"./nextTick":7,"./queueMicrotask":4,"./stateChange":5,"./timeout":6}],2:[function(_dereq_,module,exports){
(function (global){
'use strict';

exports.test = function () {
if (global.setImmediate) {
exports.test = function (globalObject) {
if (globalObject.setImmediate) {
// we can only get here in IE10
// which doesn't handel postMessage well
return false;
}
return typeof global.MessageChannel !== 'undefined';
return typeof globalObject.MessageChannel !== 'undefined';
};

exports.install = function (func) {
var channel = new global.MessageChannel();
exports.install = function (func, globalObject) {
var channel = new globalObject.MessageChannel();
channel.port1.onmessage = func;
return function () {
channel.port2.postMessage(0);
};
};
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],3:[function(_dereq_,module,exports){
(function (global){
'use strict';
//based off rsvp https://github.com/tildeio/rsvp.js
//license https://github.com/tildeio/rsvp.js/blob/master/LICENSE
//https://github.com/tildeio/rsvp.js/blob/master/lib/rsvp/asap.js

var Mutation = global.MutationObserver || global.WebKitMutationObserver;
function getMutation(globalObject) {
return globalObject.MutationObserver || globalObject.WebKitMutationObserver;
}

exports.test = function () {
return Mutation;

exports.test = function (globalObject) {
return getMutation(globalObject);
};

exports.install = function (handle) {
exports.install = function (handle, globalObject) {
var Mutation = getMutation(globalObject);
var called = 0;
var observer = new Mutation(handle);
var element = global.document.createTextNode('');
var element = globalObject.document.createTextNode('');
observer.observe(element, {
characterData: true
});
return function () {
element.data = (called = ++called % 2);
};
};
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],4:[function(_dereq_,module,exports){
(function (global){
'use strict';
exports.test = function () {
return typeof global.queueMicrotask === 'function';
exports.test = function (globalObject) {
return typeof globalObject.queueMicrotask === 'function';
};

exports.install = function (func) {
exports.install = function (func, globalObject) {
return function () {
global.queueMicrotask(func);
globalObject.queueMicrotask(func);
};
};

}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],5:[function(_dereq_,module,exports){
(function (global){
'use strict';

exports.test = function () {
return 'document' in global && 'onreadystatechange' in global.document.createElement('script');
exports.test = function (globalObject) {
return 'document' in globalObject && 'onreadystatechange' in globalObject.document.createElement('script');
};

exports.install = function (handle) {
exports.install = function (handle, globalObject) {
return function () {

// Create a <script> element; its readystatechange event will be fired asynchronously once it is inserted
// into the document. Do so, thus queuing up the task. Remember to clean up once it's been called.
var scriptEl = global.document.createElement('script');
var scriptEl = globalObject.document.createElement('script');
scriptEl.onreadystatechange = function () {
handle();

scriptEl.onreadystatechange = null;
scriptEl.parentNode.removeChild(scriptEl);
scriptEl = null;
};
global.document.documentElement.appendChild(scriptEl);
globalObject.document.documentElement.appendChild(scriptEl);

return handle;
};
};
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],6:[function(_dereq_,module,exports){
'use strict';
exports.test = function () {
Expand Down
2 changes: 1 addition & 1 deletion dist/immediate.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ var currentQueue;
var queueIndex = -1;
var queue = [];
var scheduled = false;

var globalObject;
if(typeof window !== "undefined") {
globalObject = window;
} else if(typeof global !== "undefined") {
globalObject = global;
} else if(typeof self !== "undefined") {
globalObject = self;
} else {
globalObject = this;
}


function cleanUpNextTick() {
if (!draining || !currentQueue) {
return;
Expand Down Expand Up @@ -54,8 +67,8 @@ var scheduleDrain;
var i = -1;
var len = types.length;
while (++i < len) {
if (types[i] && types[i].test && types[i].test()) {
scheduleDrain = types[i].install(nextTick);
if (types[i] && types[i].test && types[i].test(globalObject)) {
scheduleDrain = types[i].install(nextTick, globalObject);
break;
}
}
Expand Down
10 changes: 5 additions & 5 deletions lib/messageChannel.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict';

exports.test = function () {
if (global.setImmediate) {
exports.test = function (globalObject) {
if (globalObject.setImmediate) {
// we can only get here in IE10
// which doesn't handel postMessage well
return false;
}
return typeof global.MessageChannel !== 'undefined';
return typeof globalObject.MessageChannel !== 'undefined';
};

exports.install = function (func) {
var channel = new global.MessageChannel();
exports.install = function (func, globalObject) {
var channel = new globalObject.MessageChannel();
channel.port1.onmessage = func;
return function () {
channel.port2.postMessage(0);
Expand Down
14 changes: 9 additions & 5 deletions lib/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
//license https://github.com/tildeio/rsvp.js/blob/master/LICENSE
//https://github.com/tildeio/rsvp.js/blob/master/lib/rsvp/asap.js

var Mutation = global.MutationObserver || global.WebKitMutationObserver;
function getMutation(globalObject) {
return globalObject.MutationObserver || globalObject.WebKitMutationObserver;
}

exports.test = function () {
return Mutation;

exports.test = function (globalObject) {
return getMutation(globalObject);
};

exports.install = function (handle) {
exports.install = function (handle, globalObject) {
var Mutation = getMutation(globalObject);
var called = 0;
var observer = new Mutation(handle);
var element = global.document.createTextNode('');
var element = globalObject.document.createTextNode('');
observer.observe(element, {
characterData: true
});
Expand Down
8 changes: 4 additions & 4 deletions lib/queueMicrotask.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
exports.test = function () {
return typeof global.queueMicrotask === 'function';
exports.test = function (globalObject) {
return typeof globalObject.queueMicrotask === 'function';
};

exports.install = function (func) {
exports.install = function (func, globalObject) {
return function () {
global.queueMicrotask(func);
globalObject.queueMicrotask(func);
};
};
10 changes: 5 additions & 5 deletions lib/stateChange.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
'use strict';

exports.test = function () {
return 'document' in global && 'onreadystatechange' in global.document.createElement('script');
exports.test = function (globalObject) {
return 'document' in globalObject && 'onreadystatechange' in globalObject.document.createElement('script');
};

exports.install = function (handle) {
exports.install = function (handle, globalObject) {
return function () {

// Create a <script> element; its readystatechange event will be fired asynchronously once it is inserted
// into the document. Do so, thus queuing up the task. Remember to clean up once it's been called.
var scriptEl = global.document.createElement('script');
var scriptEl = globalObject.document.createElement('script');
scriptEl.onreadystatechange = function () {
handle();

scriptEl.onreadystatechange = null;
scriptEl.parentNode.removeChild(scriptEl);
scriptEl = null;
};
global.document.documentElement.appendChild(scriptEl);
globalObject.document.documentElement.appendChild(scriptEl);

return handle;
};
Expand Down