const callbacks = []; let pending = false; function flushCallbacks() { pending = false; const copiedCallbacks = callbacks.slice(0); callbacks.length = 0; copiedCallbacks.forEach(callback => callback()); } let microTimerFunction = null; let macroTimerFunction = null; if (window && typeof (window as any).setImmediate !== 'undefined') { macroTimerFunction = () => { (window as any).setImmediate(flushCallbacks); }; } else if (typeof MessageChannel !== 'undefined') { const channel = new MessageChannel(); const port = channel.port2; channel.port1.onmessage = flushCallbacks; macroTimerFunction = () => { port.postMessage(1); }; } else { macroTimerFunction = () => { window.setTimeout(flushCallbacks, 0); }; } if (typeof Promise !== 'undefined') { const promise = Promise.resolve(); microTimerFunction = () => { promise.then(flushCallbacks); }; } else { // fallback to macro microTimerFunction = macroTimerFunction; } const nextTick = (callback: Function) => { callbacks.push(callback); if (!pending) { pending = true; microTimerFunction(); } }; export { nextTick, };