Skip to content

Commit 898b289

Browse files
committed
Add HMR status handlers
1 parent 5e99866 commit 898b289

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

packages/app/src/sandbox/eval/manager.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ const SHIMMED_MODULE: Module = {
7373
};
7474
const debug = _debug('cs:compiler:manager');
7575

76+
type HMRStatus = 'idle' | 'check' | 'apply' | 'fail' | 'dispose';
77+
7678
export default class Manager {
7779
id: string;
7880
transpiledModules: {
@@ -91,7 +93,8 @@ export default class Manager {
9193
dependencies: Object;
9294
webpackHMR: boolean;
9395
hardReload: boolean;
94-
hmrStatus: 'idle' | 'check' | 'apply' | 'fail' | 'dispose' = 'idle';
96+
hmrStatus: HMRStatus = 'idle';
97+
hmrStatusChangeListeners: Set<Function>;
9598
testRunner: TestRunner;
9699
isFirstLoad: boolean;
97100

@@ -114,6 +117,7 @@ export default class Manager {
114117
this.webpackHMR = false;
115118
this.hardReload = false;
116119
this.hmrStatus = 'idle';
120+
this.hmrStatusChangeListeners = new Set();
117121
this.isFirstLoad = true;
118122
this.transpiledModulesByHash = {};
119123
this.configurations = {};
@@ -230,7 +234,7 @@ export default class Manager {
230234
try {
231235
const exports = this.evaluateTranspiledModule(transpiledModule);
232236

233-
this.hmrStatus = 'idle';
237+
this.setHmrStatus('idle');
234238

235239
return exports;
236240
} catch (e) {
@@ -357,7 +361,7 @@ export default class Manager {
357361
* @param {*} entry
358362
*/
359363
async transpileModules(entry: Module, isTestFile: boolean = false) {
360-
this.hmrStatus = 'check';
364+
this.setHmrStatus('check');
361365
this.setEnvironmentVariables();
362366
const transpiledModule = this.getTranspiledModule(entry);
363367

@@ -565,6 +569,21 @@ export default class Manager {
565569
}
566570
};
567571

572+
setHmrStatus = (status: HMRStatus) => {
573+
this.hmrStatusChangeListeners.forEach(v => {
574+
v(status);
575+
});
576+
this.hmrStatus = status;
577+
};
578+
579+
addStatusHandler = (cb: Function) => {
580+
this.hmrStatusChangeListeners.add(cb);
581+
};
582+
583+
removeStatusHandler = (cb: Function) => {
584+
this.hmrStatusChangeListeners.delete(cb);
585+
};
586+
568587
/**
569588
* Resolve the transpiled module from the path, note that the path can actually
570589
* include loaders. That's why we're focussing on first extracting this query
@@ -729,7 +748,7 @@ export default class Manager {
729748
* continuing
730749
*/
731750
markHardReload() {
732-
this.hmrStatus = 'fail';
751+
this.setHmrStatus('fail');
733752
this.hardReload = true;
734753
}
735754

packages/app/src/sandbox/eval/transpiled-module.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,11 +719,11 @@ export default class TranspiledModule {
719719

720720
if (this.hmrConfig) {
721721
/* eslint-disable no-param-reassign */
722-
manager.hmrStatus = 'dispose';
722+
manager.setHmrStatus('dispose');
723723
// Call module.hot.dispose handler
724724
// https://webpack.js.org/api/hot-module-replacement/#dispose-or-adddisposehandler-
725725
this.hmrConfig.callDisposeHandler();
726-
manager.hmrStatus = 'idle';
726+
manager.setHmrStatus('idle');
727727
/* eslint-enable */
728728
}
729729

@@ -734,7 +734,10 @@ export default class TranspiledModule {
734734
exports: {},
735735
hot: {
736736
accept: (path: string | Array<string>, cb) => {
737-
if (typeof path === 'undefined') {
737+
if (
738+
typeof path === 'undefined' ||
739+
(typeof path !== 'string' || !Array.isArray(path))
740+
) {
738741
// Self mark hot
739742
this.hmrConfig = this.hmrConfig || new HMR();
740743
if (this.hmrConfig) {
@@ -784,6 +787,8 @@ export default class TranspiledModule {
784787
},
785788
data: hotData,
786789
status: () => manager.hmrStatus,
790+
addStatusHandler: manager.addStatusHandler,
791+
removeStatusHandler: manager.removeStatusHandler,
787792
},
788793
};
789794
this.compilation.hot.data = hotData;
@@ -845,13 +850,13 @@ export default class TranspiledModule {
845850
);
846851

847852
/* eslint-disable no-param-reassign */
848-
manager.hmrStatus = 'apply';
853+
manager.setHmrStatus('apply');
849854
const hmrConfig = this.hmrConfig;
850855
if (hmrConfig && hmrConfig.isHot()) {
851856
hmrConfig.setDirty(false);
852857
hmrConfig.callAcceptCallback();
853858
}
854-
manager.hmrStatus = 'idle';
859+
manager.setHmrStatus('idle');
855860
/* eslint-enable */
856861

857862
return exports;

0 commit comments

Comments
 (0)