Projekt

Obecné

Profil

Stáhnout (5.29 KB) Statistiky
| Větev: | Revize:
1
// shim for using process in browser
2
var process = module.exports = {};
3

    
4
// cached from whatever global is present so that test runners that stub it
5
// don't break things.  But we need to wrap it in a try catch in case it is
6
// wrapped in strict mode code which doesn't define any globals.  It's inside a
7
// function because try/catches deoptimize in certain engines.
8

    
9
var cachedSetTimeout;
10
var cachedClearTimeout;
11

    
12
function defaultSetTimout() {
13
    throw new Error('setTimeout has not been defined');
14
}
15
function defaultClearTimeout () {
16
    throw new Error('clearTimeout has not been defined');
17
}
18
(function () {
19
    try {
20
        if (typeof setTimeout === 'function') {
21
            cachedSetTimeout = setTimeout;
22
        } else {
23
            cachedSetTimeout = defaultSetTimout;
24
        }
25
    } catch (e) {
26
        cachedSetTimeout = defaultSetTimout;
27
    }
28
    try {
29
        if (typeof clearTimeout === 'function') {
30
            cachedClearTimeout = clearTimeout;
31
        } else {
32
            cachedClearTimeout = defaultClearTimeout;
33
        }
34
    } catch (e) {
35
        cachedClearTimeout = defaultClearTimeout;
36
    }
37
} ())
38
function runTimeout(fun) {
39
    if (cachedSetTimeout === setTimeout) {
40
        //normal enviroments in sane situations
41
        return setTimeout(fun, 0);
42
    }
43
    // if setTimeout wasn't available but was latter defined
44
    if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
45
        cachedSetTimeout = setTimeout;
46
        return setTimeout(fun, 0);
47
    }
48
    try {
49
        // when when somebody has screwed with setTimeout but no I.E. maddness
50
        return cachedSetTimeout(fun, 0);
51
    } catch(e){
52
        try {
53
            // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
54
            return cachedSetTimeout.call(null, fun, 0);
55
        } catch(e){
56
            // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
57
            return cachedSetTimeout.call(this, fun, 0);
58
        }
59
    }
60

    
61

    
62
}
63
function runClearTimeout(marker) {
64
    if (cachedClearTimeout === clearTimeout) {
65
        //normal enviroments in sane situations
66
        return clearTimeout(marker);
67
    }
68
    // if clearTimeout wasn't available but was latter defined
69
    if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
70
        cachedClearTimeout = clearTimeout;
71
        return clearTimeout(marker);
72
    }
73
    try {
74
        // when when somebody has screwed with setTimeout but no I.E. maddness
75
        return cachedClearTimeout(marker);
76
    } catch (e){
77
        try {
78
            // When we are in I.E. but the script has been evaled so I.E. doesn't  trust the global object when called normally
79
            return cachedClearTimeout.call(null, marker);
80
        } catch (e){
81
            // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
82
            // Some versions of I.E. have different rules for clearTimeout vs setTimeout
83
            return cachedClearTimeout.call(this, marker);
84
        }
85
    }
86

    
87

    
88

    
89
}
90
var queue = [];
91
var draining = false;
92
var currentQueue;
93
var queueIndex = -1;
94

    
95
function cleanUpNextTick() {
96
    if (!draining || !currentQueue) {
97
        return;
98
    }
99
    draining = false;
100
    if (currentQueue.length) {
101
        queue = currentQueue.concat(queue);
102
    } else {
103
        queueIndex = -1;
104
    }
105
    if (queue.length) {
106
        drainQueue();
107
    }
108
}
109

    
110
function drainQueue() {
111
    if (draining) {
112
        return;
113
    }
114
    var timeout = runTimeout(cleanUpNextTick);
115
    draining = true;
116

    
117
    var len = queue.length;
118
    while(len) {
119
        currentQueue = queue;
120
        queue = [];
121
        while (++queueIndex < len) {
122
            if (currentQueue) {
123
                currentQueue[queueIndex].run();
124
            }
125
        }
126
        queueIndex = -1;
127
        len = queue.length;
128
    }
129
    currentQueue = null;
130
    draining = false;
131
    runClearTimeout(timeout);
132
}
133

    
134
process.nextTick = function (fun) {
135
    var args = new Array(arguments.length - 1);
136
    if (arguments.length > 1) {
137
        for (var i = 1; i < arguments.length; i++) {
138
            args[i - 1] = arguments[i];
139
        }
140
    }
141
    queue.push(new Item(fun, args));
142
    if (queue.length === 1 && !draining) {
143
        runTimeout(drainQueue);
144
    }
145
};
146

    
147
// v8 likes predictible objects
148
function Item(fun, array) {
149
    this.fun = fun;
150
    this.array = array;
151
}
152
Item.prototype.run = function () {
153
    this.fun.apply(null, this.array);
154
};
155
process.title = 'browser';
156
process.browser = true;
157
process.env = {};
158
process.argv = [];
159
process.version = ''; // empty string to avoid regexp issues
160
process.versions = {};
161

    
162
function noop() {}
163

    
164
process.on = noop;
165
process.addListener = noop;
166
process.once = noop;
167
process.off = noop;
168
process.removeListener = noop;
169
process.removeAllListeners = noop;
170
process.emit = noop;
171
process.prependListener = noop;
172
process.prependOnceListener = noop;
173

    
174
process.listeners = function (name) { return [] }
175

    
176
process.binding = function (name) {
177
    throw new Error('process.binding is not supported');
178
};
179

    
180
process.cwd = function () { return '/' };
181
process.chdir = function (dir) {
182
    throw new Error('process.chdir is not supported');
183
};
184
process.umask = function() { return 0; };
(3-3/6)