This repository has been archived by the owner on Jan 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
hmr.js
373 lines (332 loc) · 11 KB
/
hmr.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//////////////////// HMR BEGIN ////////////////////
var _elm_hot_loader_init = function () {}
if (module.hot) {
(function(Elm) {
"use strict";
var version = detectElmVersion()
console.log('[elm-hot] Elm version:', version)
if (version === '0.17') {
throw new Error('[elm-hot] Please use elm-hot-loader@0.4.x')
} else if (version !== '0.18') {
throw new Error('[elm-hot] Elm version not supported.')
}
//polyfill for IE: https://github.com/fluxxu/elm-hot-loader/issues/16
if (typeof Object.assign != 'function') {
Object.assign = function(target) {
'use strict';
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
target = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source != null) {
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
}
return target;
};
}
var instances = module.hot.data
? module.hot.data.instances || {}
: {};
var uid = module.hot.data
? module.hot.data.uid || 0
: 0;
var cancellers = [];
var initializingInstance = null, swappingInstance = null;
module.hot.accept();
module.hot.dispose(function(data) {
data.instances = instances;
data.uid = uid;
// disable current instance
_elm_lang$core$Native_Scheduler.nativeBinding = function() {
return _elm_lang$core$Native_Scheduler.fail(new Error('[elm-hot] Inactive Elm instance.'))
}
if (cancellers.length) {
console.log('[elm-hot] Killing ' + cancellers.length + ' running processes...');
try {
cancellers.forEach(function (cancel) {
cancel();
});
} catch (e) {
console.warn('[elm-hot] Kill process error: ' + e.message);
}
}
});
function getId() {
return ++uid;
}
function detectElmVersion() {
try {
if (_elm_lang$core$Native_Platform.initialize) {
return '0.18'
}
} catch (_) {}
try {
// 0.17 function programWithFlags(details)
if (_elm_lang$virtual_dom$VirtualDom$programWithFlags.length === 1) {
return '0.17'
}
} catch (_) {}
return 'unknown'
}
function findPublicModules(parent, path) {
var modules = [];
for (var key in parent) {
var child = parent[key];
var currentPath = path ? path + '.' + key : key;
if ('fullscreen' in child) {
modules.push({
path: currentPath,
module: child
});
} else {
modules = modules.concat(findPublicModules(child, currentPath));
}
}
return modules;
}
function getPublicModule(Elm, path) {
var parts = path.split('.');
var parent = Elm;
for (var i = 0; i < parts.length; ++i) {
var part = parts[i];
if (part in parent) {
parent = parent[part]
}
if (!parent) {
return null;
}
}
return parent
}
function registerInstance(domNode, flags, path, portSubscribes) {
var id = getId();
var instance = {
id: id,
path: path,
domNode: domNode,
flags: flags,
portSubscribes: portSubscribes,
elmProxy: null,
lastState: null, // last elm app state
callbacks: []
}
instance.subscribe = function (cb) {
instance.callbacks.push(cb)
return function () {
instance.callbacks.splice(instance.callbacks.indexOf(cb), 1)
}
}
instance.dispatch = function (event) {
instance.callbacks.forEach(function (cb) {
cb(event, {
flags: instance.flags,
state: '_0' in instance.lastState
? instance.lastState._0 //debugger state
: instance.lastState //normal state
})
})
}
return instances[id] = instance
}
function wrapPublicModule(path, module) {
var embed = module.embed;
var fullscreen = module.fullscreen;
module.embed = function(domNode, flags) {
var elm;
var portSubscribes = {};
initializingInstance = registerInstance(domNode, flags, path, portSubscribes)
elm = embed(domNode, flags);
wrapPorts(elm, portSubscribes)
elm = initializingInstance.elmProxy = {
ports: elm.ports,
hot: {
subscribe: initializingInstance.subscribe
}
};
initializingInstance = null;
return elm;
};
module.fullscreen = function (flags) {
var elm
var portSubscribes = {};
initializingInstance = registerInstance(document.body, flags, path, portSubscribes)
elm = fullscreen(flags);
wrapPorts(elm, portSubscribes)
elm = initializingInstance.elmProxy = {
ports: elm.ports,
hot: {
subscribe: initializingInstance.subscribe
}
};
initializingInstance = null;
return elm;
}
}
function swap(Elm, instance) {
console.log('[elm-hot] Hot-swapping module: ' + instance.path)
swappingInstance = instance;
var domNode = instance.domNode;
while (domNode.lastChild) {
domNode.removeChild(domNode.lastChild);
}
var m = getPublicModule(Elm, instance.path)
var elm;
if (m) {
instance.dispatch('swap')
var flags = instance.flags
if (instance.isFullscreen) {
elm = m.fullscreen(flags);
} else {
elm = m.embed(domNode, flags);
}
instance.elmProxy.ports = elm.ports;
Object.keys(instance.portSubscribes).forEach(function(portName) {
if (portName in elm.ports && 'subscribe' in elm.ports[portName]) {
var handlers = instance.portSubscribes[portName];
if (!handlers.length) {
return;
}
console.log('[elm-hot] Reconnect ' + handlers.length + ' handler(s) to port \'' + portName + '\' (' + instance.path + ').');
handlers.forEach(function(handler) {
elm.ports[portName].subscribe(handler);
});
} else {
delete instance.portSubscribes[portName];
console.log('[elm-hot] Port was removed: ' + portName);
}
});
} else {
console.log('[elm-hot] Module was removed: ' + instance.path);
}
swappingInstance = null;
}
function wrapPorts(elm, portSubscribes) {
var portNames = Object.keys(elm.ports || {});
//hook ports
if (portNames.length) {
portNames
.filter(function(name) {
return 'subscribe' in elm.ports[name];
})
.forEach(function(portName) {
var port = elm.ports[portName];
var subscribe = port.subscribe;
var unsubscribe = port.unsubscribe;
elm.ports[portName] = Object.assign(port, {
subscribe: function(handler) {
console.log('[elm-hot] ports.' + portName + '.subscribe called.');
if (!portSubscribes[portName]) {
portSubscribes[portName] = [ handler ];
} else {
//TODO handle subscribing to single handler more than once?
portSubscribes[portName].push(handler);
}
return subscribe.call(port, handler);
},
unsubscribe: function(handler) {
console.log('[elm-hot] ports.' + portName + '.unsubscribe called.');
var list = portSubscribes[portName];
if (list && list.indexOf(handler) !== -1) {
list.splice(list.lastIndexOf(handler), 1);
} else {
console.warn('[elm-hot] ports.' + portName + '.unsubscribe: handler not subscribed');
}
return unsubscribe.call(port, handler);
}
});
});
}
return portSubscribes;
}
// hook program creation
var initialize = _elm_lang$core$Native_Platform.initialize
_elm_lang$core$Native_Platform.initialize = function (stateTuple, update, view, renderer) {
var instance = initializingInstance
var swapping = swappingInstance
var tryFirstRender = !!swappingInstance
var isInitialRender = true
var debuggerEnabled = isDebuggerState(stateTuple._0)
if (swappingInstance) {
if (debuggerEnabled) {
stateTuple._0.state = swappingInstance.lastState
} else {
stateTuple._0 = swappingInstance.lastState
}
}
return initialize(stateTuple, update, function (model) {
var result;
// first render may fail if shape of model changed too much
if (tryFirstRender) {
tryFirstRender = false
try {
result = view(model)
} catch (e) {
throw new Error('[elm-hot] Hot-swapping is not possible, please reload page. Error: ' + e.message)
}
} else {
result = view(model)
}
if (instance) {
if (isDebuggerState(model)) {
instance.lastState = model.state
} else {
instance.lastState = model
}
} else {
instance = swapping
}
isInitialRender = false
return result
}, renderer)
function isDebuggerState(state) {
return state && typeof state === 'object' && typeof state.isDebuggerOpen === 'boolean' && 'state' in state
}
}
// hook process creation
var nativeBinding = _elm_lang$core$Native_Scheduler.nativeBinding
_elm_lang$core$Native_Scheduler.nativeBinding = function() {
var def = nativeBinding.apply(this, arguments);
var callback = def.callback
def.callback = function() {
var result = callback.apply(this, arguments)
if (result) {
cancellers.push(result);
return function() {
cancellers.splice(cancellers.indexOf(result), 1);
return result();
};
}
return result;
};
return def;
};
_elm_hot_loader_init = function (Elm) {
// swap instances
var removedInstances = [];
for (var id in instances) {
var instance = instances[id]
if (instance.domNode.parentNode) {
swap(Elm, instance);
} else {
removedInstances.push(id);
}
}
removedInstances.forEach(function (id) {
delete instance[id];
});
// wrap all public modules
var publicModules = findPublicModules(Elm);
publicModules.forEach(function (m) {
wrapPublicModule(m.path, m.module);
});
}
})(Elm);
}
//////////////////// HMR END ////////////////////