UNPKG

bluebird

Version:

Full featured Promises/A+ implementation with exceptionally good performance

131 lines (119 loc) 4.23 kB
/** * Copyright (c) 2013 Petka Antonov * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions:</p> * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ "use strict"; var global = require("./global.js"); var ASSERT = require("./assert.js"); var schedule; if( typeof process !== "undefined" && process !== null && typeof process.cwd === "function" ) { if( typeof global.setImmediate !== "undefined" ) { schedule = function Promise$_Scheduler( fn ) { global.setImmediate( fn ); }; } else { schedule = function Promise$_Scheduler( fn ) { process.nextTick( fn ); }; } } else if( ( typeof MutationObserver === "function" || typeof WebkitMutationObserver === "function" || typeof WebKitMutationObserver === "function" ) && typeof document !== "undefined" && typeof document.createElement === "function" ) { schedule = (function(){ var MutationObserver = global.MutationObserver || global.WebkitMutationObserver || global.WebKitMutationObserver; var div = document.createElement("div"); var queuedFn = void 0; var observer = new MutationObserver( function Promise$_Scheduler() { var fn = queuedFn; queuedFn = void 0; fn(); } ); var cur = true; observer.observe( div, { attributes: true, childList: true, characterData: true }); return function Promise$_Scheduler( fn ) { queuedFn = fn; cur = !cur; div.setAttribute( "class", cur ? "foo" : "bar" ); }; })(); } else if ( typeof global.postMessage === "function" && typeof global.importScripts !== "function" && typeof global.addEventListener === "function" && typeof global.removeEventListener === "function" ) { var MESSAGE_KEY = "bluebird_message_key_" + Math.random(); schedule = (function(){ var queuedFn = void 0; function Promise$_Scheduler(e) { if(e.source === global && e.data === MESSAGE_KEY) { var fn = queuedFn; queuedFn = void 0; fn(); } } global.addEventListener( "message", Promise$_Scheduler, false ); return function Promise$_Scheduler( fn ) { queuedFn = fn; global.postMessage( MESSAGE_KEY, "*" ); }; })(); } else if( typeof MessageChannel === "function" ) { schedule = (function(){ var queuedFn = void 0; var channel = new MessageChannel(); channel.port1.onmessage = function Promise$_Scheduler() { var fn = queuedFn; queuedFn = void 0; fn(); }; return function Promise$_Scheduler( fn ) { queuedFn = fn; channel.port2.postMessage( null ); }; })(); } else if( global.setTimeout ) { schedule = function Promise$_Scheduler( fn ) { setTimeout( fn, 4 ); }; } else { schedule = function Promise$_Scheduler( fn ) { fn(); }; } module.exports = schedule;