-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtasks.js
247 lines (213 loc) · 5.88 KB
/
tasks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
var env = require("./env");
var util = require("./util");
var slice = Array.prototype.slice;
if(env.isNode) {
var globalTimeoutId = 1;
}
var addTimer = function(callback, Zone){
var timeoutId = callback();
var id = timeoutId;
if(env.isNode && typeof id !== "number") {
id = timeoutId.__timeoutId = globalTimeoutId++;
}
var zone = Zone.current;
if(!zone.isResolved) {
zone.ids[id] = timeoutId;
}
return {
timeoutId: timeoutId,
id: id
};
};
var removeTimer = function(timeoutId, callback, Zone){
// If no timeoutId is passed just call the parent
// https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/clearTimeout#Notes
if(timeoutId == null) {
return callback();
}
var zone = Zone.current;
var ids = zone.ids;
var id = (env.isNode && typeof timeoutId !== "number") ?
timeoutId.__timeoutId : timeoutId;
if(!zone.isResolved && ids[id]) {
delete ids[id];
zone.removeWait();
}
return callback();
};
var getGlobalEventHandlersNames = function getGlobalEventHandlersNames() {
var names = [];
if (!env.isNode) {
names = Object.getOwnPropertyNames(HTMLElement.prototype).filter(
function isGlobalEventHandler(name) {
return name.substr(0, 2) === "on";
}
);
}
return names;
};
var defineSetTask = function defineSetTask(set, Zone) {
return function setTask(newValue) {
if (newValue) {
var outHandler = newValue[EVENT_HANDLER];
if (outHandler === undefined) {
outHandler = Zone.current.wrap(newValue);
newValue[EVENT_HANDLER] = outHandler;
}
set.call(this, outHandler);
} else {
set.call(this, newValue);
}
};
};
// Add tasks to wrap event handlers set using `.on[eventname] = handler` syntax
// e.g: `el.onclick = function doSomething() {};`
getGlobalEventHandlersNames().forEach(function(name) {
exports[name] = defineSetTask;
});
exports.setTimeout = function(setTimeout, Zone){
return function(fn, timeout){
var args = Array.prototype.slice.call(arguments);
var zone = Zone.current;
var idInfo;
args[0] = zone.waitFor(function(){
delete zone.ids[idInfo.id];
return fn.apply(this, arguments);
});
var self = this;
idInfo = addTimer(function(){
return setTimeout.apply(self, args);
}, Zone);
return idInfo.timeoutId;
};
};
exports.clearTimeout = function(clearTimeout, Zone){
return function(timeoutId){
var args = arguments, self = this;
return removeTimer(timeoutId, function(){
return clearTimeout.apply(self, args);
}, Zone);
};
};
exports.setImmediate = function(setImmediate, Zone){
return function(fn){
var idInfo;
var zone = Zone.current;
var callback = zone.waitFor(function(){
delete zone.ids[idInfo.id];
return fn.apply(this, arguments);
});
var self = this, args = slice.call(arguments, 1);
idInfo = addTimer(function(){
return setImmediate.apply(self, [callback].concat(args));
}, Zone);
return idInfo.timeoutId;
};
};
exports.clearImmediate = function(clearImmediate, Zone){
return function(immediateId){
var args = arguments, self = this;
return removeTimer(immediateId, function(){
return clearImmediate.apply(self, args);
}, Zone);
};
};
exports.requestAnimationFrame = function(rAF, Zone){
return function(fn){
var callback = Zone.current.waitFor(fn);
return rAF.call(this, callback);
};
};
exports.then = function(then, Zone){
return function(onFulfilled, onRejected){
var fn;
var rejected;
var callback = Zone.current.waitFor(function(val){
if(fn) {
return fn.apply(this, arguments);
} else if(rejected) {
return Promise.reject(val);
}
return val;
}, false);
var callWith = function(cb, isError){
return function(){
fn = cb;
rejected = !!isError;
return callback.apply(this, arguments);
};
};
return then.call(this, callWith(onFulfilled),
callWith(onRejected, true));
};
};
var supportsOnload = undefined;
exports.send = function(send, Zone){
if(typeof supportsOnload === "undefined") {
supportsOnload = ("onload" in new XMLHttpRequest());
}
return function(){
var onreadystatechange = this.onreadystatechange,
onload = this.onload,
onerror = this.onerror,
thisXhr = this,
zone = Zone.current;
zone.addWait();
if(supportsOnload && this.onload) {
this.onload = createCallback(onload);
this.onerror = createCallback(onerror);
} else {
onreadystatechange = onreadystatechange || function(){};
var callback = createCallback(onreadystatechange);
this.onreadystatechange = function(ev){
var xhr = ev ? ev.target : thisXhr;
if(xhr.readyState === 4) {
return callback.apply(this, arguments);
} else {
return onreadystatechange.apply(this, arguments);
}
};
}
function createCallback(fn){
fn = fn || function(){};
return function(){
var task = new Zone.Task(zone, fn);
var res = task.run(this, arguments);
zone.removeWait();
return res;
};
}
return send.apply(this, arguments);
};
};
exports.nextTick = function(nextTick, Zone){
return function(fn/*, ...args */){
var callback = Zone.current.waitFor(fn);
var args = slice.call(arguments, 1);
args.unshift(callback);
return nextTick.apply(process, args);
};
};
exports.MutationObserver = function(MutationObserver, Zone){
return function(fn){
fn = Zone.current.wrap(fn);
return new MutationObserver(fn);
};
};
var EVENT_HANDLER = util.symbol("zone-eventhandler");
exports.addEventListener = function(addEventListener, Zone){
return function(eventName, handler, useCapture){
var outHandler = handler[EVENT_HANDLER];
if(outHandler === undefined) {
outHandler = Zone.current.wrap(handler);
handler[EVENT_HANDLER] = outHandler;
}
return addEventListener.call(this, eventName, outHandler, useCapture);
};
};
exports.removeEventListener = function(removeEventListener, Zone){
return function(eventName, handler, useCapture){
var outHandler = handler[EVENT_HANDLER] || handler;
return removeEventListener.call(this, eventName, outHandler, useCapture);
};
};