-
Notifications
You must be signed in to change notification settings - Fork 4
/
register.js
175 lines (147 loc) · 4.08 KB
/
register.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
"use strict";
"format cjs";
(function(){
var isNode = typeof process !== "undefined" && {}.toString.call(process) === "[object process]";
var isWorker = typeof WorkerGlobalScope !== "undefined" &&
typeof self !== "undefined" &&
self instanceof WorkerGlobalScope;
var g = typeof WorkerGlobalScope !== "undefined" && (self instanceof WorkerGlobalScope)
? self
: isNode
? global
: window;
if(typeof module !== "undefined" && !!module.exports) {
module.exports = wrapAll;
}
var forEach = Array.prototype.forEach || function(cb){
var i = 0, len = this.length;
for(; i < len; i++) {
cb.call(this, this[i], i);
}
};
var props = Array.prototype.concat.call(
[
"setTimeout",
"clearTimeout",
"requestAnimationFrame",
"cancelAnimationFrame",
"Promise.prototype.then",
"XMLHttpRequest.prototype.send",
"Node.prototype.addEventListener",
"Node.prototype.removeEventListener",
"process.nextTick",
"fetch",
"setImmediate",
"clearImmediate",
{
prop: "MutationObserver",
fn: function(MutationObserver) {
return function(fn) {
return new MutationObserver(fn);
};
}
}
],
// add all event handlers methods like `onclick` and `onload`.
getGlobalEventHandlersNames().map(function(name) {
return "HTMLElement.prototype." + name;
})
);
wrapAll(g);
if(g.Promise) {
monitor(g, "Promise", "Promise.prototype.then", g);
}
function extract(obj, prop) {
var parts = prop.split(".");
while(parts.length > 1) {
prop = parts.shift();
obj = obj[prop];
if(!obj) break;
if(parts.length === 1) prop = parts[0];
}
return [obj, prop];
}
function wrapAll(globalObj){
var global = globalObj || g;
var wrapped = global.__canZoneWrapped;
if(!wrapped) {
wrapped = global.__canZoneWrapped = {};
}
forEach.call(props, function(prop){
var fn;
if(typeof prop === "object") {
fn = prop.fn;
prop = prop.prop;
}
var key = prop;
// If this has already been wrapped
if(wrapped[key]) {
return;
}
var results = extract(global, prop);
var obj = results[0];
prop = results[1];
// This happens if the environment doesn't support this property
// obj[prop] throws Illegal invocation when prop is an event handler
if(!isGlobalEventHandler(prop) && (!obj || !obj[prop])) {
return;
} else {
wrapped[key] = true;
}
wrapInZone(obj, prop, fn, global);
});
}
function wrapInZone(object, property, fn, global) {
var wrappedFn = function() {
var Zone = global.CanZone;
if (typeof Zone !== "undefined" && !!Zone.current) {
return Zone.tasks[property](fn, Zone).apply(this, arguments);
}
return fn.apply(this, arguments);
};
var descriptor = Object.getOwnPropertyDescriptor(object, property) || {};
if (isGlobalEventHandler(property)) {
fn = descriptor.set;
descriptor.set = wrappedFn;
} else {
fn = fn ? fn(object[property]) : object[property];
descriptor.value = wrappedFn;
descriptor.writable = true; // fix: allow third-party libraries to also wrap functions
}
Object.defineProperty(object, property, descriptor);
wrappedFn.zoneWrapped = true;
}
function isGlobalEventHandler(property) {
return property.substr(0, 2) === "on";
}
// Returns an array of global event handlers names
// https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers
function getGlobalEventHandlersNames() {
var names = [];
if (!isNode && !isWorker) {
names = Object.getOwnPropertyNames(HTMLElement.prototype).filter(
isGlobalEventHandler
);
}
return names;
}
function monitor(object, property, thingToRewrap, global) {
var current = object[property];
Object.defineProperty(object, property, {
get: function(){
return current;
},
set: function(val){
var hasChanged = !val.zoneWrapped && val !== current;
current = val;
if(hasChanged) {
var results = extract(object, thingToRewrap);
var localObject = results[0];
var localProperty = results[1];
wrapInZone(localObject, localProperty, null, global);
monitor(object, property, thingToRewrap, global);
}
}
});
}
})();