UNPKG

1.74 kBJavaScriptView Raw
1/** @license MIT License (c) copyright 2010-2014 original author or authors */
2/** @author Brian Cavalier */
3/** @author John Hann */
4
5(function(define) { 'use strict';
6define(function(require) {
7
8 // Sniff "best" async scheduling option
9 // Prefer process.nextTick or MutationObserver, then check for
10 // vertx and finally fall back to setTimeout
11
12 /*jshint maxcomplexity:6*/
13 /*global process,document,setTimeout,MutationObserver,WebKitMutationObserver*/
14 var nextTick, MutationObs;
15
16 if (typeof process !== 'undefined' && process !== null &&
17 typeof process.nextTick === 'function') {
18 nextTick = function(f) {
19 process.nextTick(f);
20 };
21
22 } else if (MutationObs =
23 (typeof MutationObserver === 'function' && MutationObserver) ||
24 (typeof WebKitMutationObserver === 'function' && WebKitMutationObserver)) {
25 nextTick = (function (document, MutationObserver) {
26 var scheduled;
27 var el = document.createElement('div');
28 var o = new MutationObserver(run);
29 o.observe(el, { attributes: true });
30
31 function run() {
32 var f = scheduled;
33 scheduled = void 0;
34 f();
35 }
36
37 return function (f) {
38 scheduled = f;
39 el.setAttribute('class', 'x');
40 };
41 }(document, MutationObs));
42
43 } else {
44 nextTick = (function(cjsRequire) {
45 try {
46 // vert.x 1.x || 2.x
47 return cjsRequire('vertx').runOnLoop || cjsRequire('vertx').runOnContext;
48 } catch (ignore) {}
49
50 // capture setTimeout to avoid being caught by fake timers
51 // used in time based tests
52 var capturedSetTimeout = setTimeout;
53 return function (t) {
54 capturedSetTimeout(t, 0);
55 };
56 }(require));
57 }
58
59 return nextTick;
60});
61}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));