'use strict'; var updatesScheduled = false; var batchStack = []; // A stack of batched updates var unbatchedQueue = []; // Used for scheduled batched updates var win = window; var setImmediate = win.setImmediate; if (!setImmediate) { if (win.postMessage) { var queue = []; var messageName = 'si'; win.addEventListener('message', function (event) { var source = event.source; if (source == win || !source && event.data === messageName) { event.stopPropagation(); if (queue.length > 0) { var fn = queue.shift(); fn(); } } }, true); setImmediate = function(fn) { queue.push(fn); win.postMessage(messageName, '*'); }; } else { setImmediate = setTimeout; } } /** * This function is called when we schedule the update of "unbatched" * updates to components. */ function updateUnbatchedComponents() { if (unbatchedQueue.length) { try { updateComponents(unbatchedQueue); } finally { // Reset the flag now that this scheduled batch update // is complete so that we can later schedule another // batched update if needed updatesScheduled = false; } } } function scheduleUpdates() { if (updatesScheduled) { // We have already scheduled a batched update for the // process.nextTick so nothing to do return; } updatesScheduled = true; setImmediate(updateUnbatchedComponents); } function updateComponents(queue) { // Loop over the components in the queue and update them. // NOTE: It is okay if the queue grows during the iteration // since we will still get to them at the end for (var i=0; i