-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproxy.js
251 lines (187 loc) · 6.7 KB
/
proxy.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
248
249
250
251
import Registry from './registry';
let proxyOptions = {
noPreserveState: false
};
function capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}
function getDebugName(id) {
const posixID = id.replace(/[/\\]/g, '/');
const name = posixID.split('/').pop().split('.').shift();
return `<${capitalize(name)}>`;
}
function groupStart(msg) {
console.group && console.group(msg);
}
function groupEnd() {
console.groupEnd && console.groupEnd();
}
export { Registry };
export function configure(_options) {
proxyOptions = Object.assign(proxyOptions, _options);
}
export function getConfig() {
return proxyOptions;
}
/*
creates a proxy object that
decorates the original component with trackers
and ensures resolution to the
latest version of the component
*/
export function createProxy(id) {
const handledMethods = '_mount,_unmount,destroy'.split(',');
const forwardedMethods = 'get,fire,observe,on,set,teardown,_recompute,_set,_bind'.split(',');
class proxyComponent {
constructor(options) {
this.id = id;
this.__mountpoint = null;
this.__anchor = null;
this.__insertionPoint = null;
this.__mounted = false;
this._register(options);
this._debugName = this.proxyTarget._debugName || getDebugName(this.id);
// ---- forwarded methods ----
const self = this;
forwardedMethods.forEach(function(method) {
self[method] = function() {
return self.proxyTarget[method].apply(self.proxyTarget, arguments);
};
});
// ---- END forwarded methods ----
}
// ---- augmented methods ----
_mount(target, anchor, insertionPoint) {
this.__mountpoint = target;
this.__anchor = anchor;
if (insertionPoint) {
this.__insertionPoint = insertionPoint;
} else {
// eslint-disable-next-line no-undef
this.__insertionPoint = document.createComment(this._debugName);
target.insertBefore(this.__insertionPoint, anchor);
}
this.__insertionPoint.__component__ = this;
anchor = this.__insertionPoint.nextSibling;
if (target.nodeName == '#document-fragment' && insertionPoint) {
//handles #4 by forcing a target
//if original target was a document fragment
target = this.__insertionPoint.parentNode;
}
this.__mounted = true;
return this.proxyTarget._mount(target, anchor);
}
destroy(detach, keepInsertionPoint) {
Registry.deRegisterInstance(this);
if (!keepInsertionPoint && this.__insertionPoint) {
//deref for GC before removal of node
this.__insertionPoint.__component__ = null;
const ip = this.__insertionPoint;
ip && ip.parentNode && ip.parentNode.removeChild(ip);
}
return this.proxyTarget.destroy(detach);
}
_unmount() {
this.__mounted = false;
return this.proxyTarget._unmount.apply(this.proxyTarget, arguments);
}
// ---- END augmented methods ----
// ---- extra methods ----
_register(options) {
const record = Registry.get(this.id);
try {
//resolve to latest version of component
this.proxyTarget = new record.component(options);
} catch (e) {
const rb = record.rollback;
if (!rb) {
console.error(e);
console.warn('Full reload required. Please fix component errors and reload the whole page');
return;
}
groupStart(this._debugName + ' Errors');
console.warn(e);
console.warn(this._debugName + ' could not be hot-loaded because it has an error');
//resolve to previous working version of component
this.proxyTarget = new rb(options);
console.info('%c' + this._debugName + ' rolled back to previous working version', 'color:green');
//set latest version as the rolled-back version
record.component = rb;
groupEnd();
}
Registry.set(this.id, record);
//register current instance, so that
//we can re-render it when required
Registry.registerInstance(this);
//proxy custom methods
const self = this;
let methods = Object.getOwnPropertyNames(Object.getPrototypeOf(self.proxyTarget));
methods.forEach(function(method) {
if (!handledMethods.includes(method) && !forwardedMethods.includes(method)) {
self[method] = function() {
return self.proxyTarget[method].apply(self.proxyTarget, arguments);
};
}
});
//(re)expose properties that might be used from outside
this.refs = this.proxyTarget.refs || {};
this._fragment = this.proxyTarget._fragment;
this._slotted = this.proxyTarget._slotted;
this.root = this.proxyTarget.root;
this.store = this.proxyTarget.store || null;
}
_rerender() {
const mountpoint = this.__mountpoint || null,
anchor = this.__anchor || null,
options = this.proxyTarget.options,
oldstate = this.get(),
isMounted = this.__mounted,
insertionPoint = this.__insertionPoint,
handlers = this.proxyTarget._handlers;
this.destroy(true, true);
this._register(options);
//re-attach event handlers
const self = this;
for (const ev in handlers) {
const _handlers = handlers[ev];
_handlers.forEach (function(item) {
if (item.toString().includes('component.fire(')) {
self.proxyTarget.on(ev, item);
}
});
}
if (mountpoint && isMounted) {
this.proxyTarget._fragment.c();
this._mount(mountpoint, anchor, insertionPoint);
//work around _checkReadOnly in svelte (for computed properties)
this.proxyTarget._updatingReadonlyProperty = true;
//preserve local state (unless noPreserveState is true)
if (
!this.proxyTarget.constructor.noPreserveState
&& !proxyOptions.noPreserveState) {
//manually flush computations and re-render changes
let changed = {};
for (let k in oldstate) {
changed[k] = true;
}
this.proxyTarget._recompute(changed, oldstate);
this.proxyTarget._fragment && this.proxyTarget._fragment.p(changed, oldstate);
//set old state back
this.set(oldstate);
} else {
//we have to call .set() here
//otherwise oncreate is not fired
this.set(this.get());
}
this.proxyTarget._updatingReadonlyProperty = false;
}
}
// ---- END extra methods ----
}
//forward static properties and methods
const originalComponent = Registry.get(id).component;
for (let key in originalComponent) {
proxyComponent[key] = originalComponent[key];
}
return proxyComponent;
}