Skip to content

Commit 36d77f9

Browse files
committed
refactor(hmr): simplify usage
1 parent 19223f5 commit 36d77f9

File tree

1 file changed

+12
-3
lines changed
  • packages/runtime-core/src

1 file changed

+12
-3
lines changed

packages/runtime-core/src/hmr.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ interface HMRRecord {
4141
const map: Map<string, HMRRecord> = new Map()
4242

4343
export function registerHMR(instance: ComponentInternalInstance) {
44-
map.get(instance.type.__hmrId!)!.instances.add(instance)
44+
const id = instance.type.__hmrId!
45+
let record = map.get(id)
46+
if (!record) {
47+
createRecord(id, instance.type as ComponentOptions)
48+
record = map.get(id)!
49+
}
50+
record.instances.add(instance)
4551
}
4652

4753
export function unregisterHMR(instance: ComponentInternalInstance) {
@@ -60,9 +66,11 @@ function createRecord(id: string, comp: ComponentOptions): boolean {
6066
}
6167

6268
function rerender(id: string, newRender?: RenderFunction) {
69+
const record = map.get(id)
70+
if (!record) return
6371
// Array.from creates a snapshot which avoids the set being mutated during
6472
// updates
65-
Array.from(map.get(id)!.instances).forEach(instance => {
73+
Array.from(record.instances).forEach(instance => {
6674
if (newRender) {
6775
instance.render = newRender
6876
}
@@ -75,7 +83,8 @@ function rerender(id: string, newRender?: RenderFunction) {
7583
}
7684

7785
function reload(id: string, newComp: ComponentOptions) {
78-
const record = map.get(id)!
86+
const record = map.get(id)
87+
if (!record) return
7988
// 1. Update existing comp definition to match new one
8089
const comp = record.comp
8190
Object.assign(comp, newComp)

0 commit comments

Comments
 (0)