Skip to content

Commit 40c409e

Browse files
committed
added applyOnUpdate
1 parent 9aa00a6 commit 40c409e

1 file changed

Lines changed: 71 additions & 54 deletions

File tree

‎lib/HotModuleReplacementPlugin.js‎

Lines changed: 71 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -181,53 +181,6 @@ var hotInitCode = function() {
181181
}
182182
}
183183

184-
function hotUpdateDownloaded() {
185-
var outdatedDependencies = hotUpdateOutdatedDependencies = {};
186-
var outdatedModules = hotUpdateOutdatedModules = Object.keys(hotUpdate).map(function(id) {
187-
return +id;
188-
});
189-
var queue = outdatedModules.slice();
190-
while(queue.length > 0) {
191-
var moduleId = queue.pop();
192-
var module = installedModules[moduleId];
193-
if(!module || module.hot._selfAccepted)
194-
continue;
195-
if(module.hot._selfDeclined) {
196-
hotSetStatus("abort");
197-
return hotCallback(new Error("Aborted because of self decline: " + moduleId));
198-
}
199-
if(moduleId === 0) {
200-
hotSetStatus("abort");
201-
return hotCallback(new Error("Aborted because of bubbling"));
202-
}
203-
for(var i = 0; i < module.parents.length; i++) {
204-
var parentId = module.parents[i];
205-
var parent = installedModules[parentId];
206-
if(parent.hot._declinedDependencies[moduleId]) {
207-
hotSetStatus("abort");
208-
return hotCallback(new Error("Aborted because of declined dependency: " + moduleId + " in " + parentId));
209-
}
210-
if(outdatedModules.indexOf(parentId) >= 0) continue;
211-
if(parent.hot._acceptedDependencies[moduleId]) {
212-
if(!outdatedDependencies[parentId]) outdatedDependencies[parentId] = [];
213-
if(outdatedDependencies[parentId].indexOf(moduleId) >= 0) continue;
214-
outdatedDependencies[parentId].push(moduleId);
215-
continue;
216-
}
217-
delete outdatedDependencies[parentId];
218-
outdatedModules.push(parentId);
219-
queue.push(parentId);
220-
}
221-
}
222-
223-
hotSetStatus("ready");
224-
if(hotApplyOnUpdate) {
225-
hotApply(hotCallback);
226-
} else {
227-
hotCallback(null, outdatedModules);
228-
}
229-
}
230-
231184
var hotApplyOnUpdate = true;
232185
var hotCurrentHash = $hash$;
233186
var hotCurrentModuleData = {};
@@ -244,11 +197,12 @@ var hotInitCode = function() {
244197
return $require$(request);
245198
};
246199
fn.e = function(chunkId, callback) {
247-
if(hotStatus === "ready") throw new Error("Cannot load chunks when update is ready");
200+
if(hotStatus === "ready")
201+
hotSetStatus("prepare");
248202
hotChunksLoading++;
249203
$require$.e(chunkId, function() {
250204
try {
251-
callback(fn);
205+
callback.call(null, fn);
252206
} catch(e) {
253207
finishChunkLoading();
254208
throw e;
@@ -312,14 +266,17 @@ var hotInitCode = function() {
312266
// Management API
313267
check: hotCheck,
314268
apply: hotApply,
269+
setApplyOnUpdate: function(applyOnUpdate) {
270+
hotApplyOnUpdate = applyOnUpdate;
271+
},
315272
status: function(l) {
316273
if(!l) return hotStatus;
317274
hotStatusHandlers.push(l);
318275
},
319276
addStatusHandler: function(l) {
320277
hotStatusHandlers.push(l);
321278
},
322-
removeDisposeHandler: function(l) {
279+
removeStatusHandler: function(l) {
323280
var idx = hotStatusHandlers.indexOf(l);
324281
if(idx >= 0) hotStatusHandlers.splice(idx, 1);
325282
}
@@ -329,15 +286,23 @@ var hotInitCode = function() {
329286

330287
var hotStatusHandlers = [];
331288
var hotStatus = "idle";
289+
332290
function hotSetStatus(newStatus) {
291+
var oldStatus = hotStatus;
333292
hotStatus = newStatus;
334-
// TODO notify listeners
293+
for(var i = 0; i < hotStatusHandlers.length; i++)
294+
hotStatusHandlers[i].call(null, newStatus);
335295
}
336296

297+
// while downloading
337298
var hotWaitingFiles = 0;
338299
var hotChunksLoading = 0;
339300
var hotWaitingFilesMap = {};
340301
var hotCallback;
302+
303+
// The update info
304+
var hotUpdate, hotUpdateNewHash;
305+
341306
function hotCheck(callback) {
342307
callback = callback || function(err) { if(err) throw err };
343308
if(hotStatus !== "idle") throw new Error("check() is only allowed in idle status");
@@ -374,6 +339,7 @@ var hotInitCode = function() {
374339
}
375340
};
376341
}
342+
377343
function hotDownloadUpdateChunk(chunkId) {
378344
hotWaitingFiles++;
379345
var head = document.getElementsByTagName('head')[0];
@@ -385,11 +351,62 @@ var hotInitCode = function() {
385351
hotWaitingFilesMap[chunkId] = true;
386352
}
387353

388-
var hotUpdate, hotUpdateOutdatedDependencies, hotUpdateOutdatedModules, hotUpdateNewHash;
354+
function hotUpdateDownloaded() {
355+
hotSetStatus("ready");
356+
var callback = hotCallback;
357+
hotCallback = null;
358+
if(!callback) return;
359+
if(hotApplyOnUpdate) {
360+
hotApply(callback);
361+
} else {
362+
var outdatedModules = Object.keys(hotUpdate).map(function(id) {
363+
return +id;
364+
});
365+
callback(null, outdatedModules);
366+
}
367+
}
389368

390369
function hotApply(callback) {
391-
var outdatedModules = hotUpdateOutdatedModules;
392-
var outdatedDependencies = hotUpdateOutdatedDependencies;
370+
callback = callback || function(err) { if(err) throw err };
371+
if(hotStatus !== "ready") throw new Error("apply() is only allowed in ready status");
372+
var outdatedDependencies = {};
373+
var outdatedModules = Object.keys(hotUpdate).map(function(id) {
374+
return +id;
375+
});
376+
var queue = outdatedModules.slice();
377+
while(queue.length > 0) {
378+
var moduleId = queue.pop();
379+
var module = installedModules[moduleId];
380+
if(!module || module.hot._selfAccepted)
381+
continue;
382+
if(module.hot._selfDeclined) {
383+
hotSetStatus("abort");
384+
return callback(new Error("Aborted because of self decline: " + moduleId));
385+
}
386+
if(moduleId === 0) {
387+
hotSetStatus("abort");
388+
return callback(new Error("Aborted because of bubbling"));
389+
}
390+
for(var i = 0; i < module.parents.length; i++) {
391+
var parentId = module.parents[i];
392+
var parent = installedModules[parentId];
393+
if(parent.hot._declinedDependencies[moduleId]) {
394+
hotSetStatus("abort");
395+
return callback(new Error("Aborted because of declined dependency: " + moduleId + " in " + parentId));
396+
}
397+
if(outdatedModules.indexOf(parentId) >= 0) continue;
398+
if(parent.hot._acceptedDependencies[moduleId]) {
399+
if(!outdatedDependencies[parentId]) outdatedDependencies[parentId] = [];
400+
if(outdatedDependencies[parentId].indexOf(moduleId) >= 0) continue;
401+
outdatedDependencies[parentId].push(moduleId);
402+
continue;
403+
}
404+
delete outdatedDependencies[parentId];
405+
outdatedModules.push(parentId);
406+
queue.push(parentId);
407+
}
408+
}
409+
393410
var outdatedSelfAcceptedModules = outdatedModules.filter(function(moduleId) {
394411
return installedModules[moduleId] && installedModules[moduleId].hot._selfAccepted;
395412
});

0 commit comments

Comments
 (0)