]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(hmr): simplify usage
authorEvan You <yyx990803@gmail.com>
Mon, 20 Apr 2020 08:03:00 +0000 (04:03 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 20 Apr 2020 17:39:47 +0000 (13:39 -0400)
packages/runtime-core/src/hmr.ts

index 86115133c82d85e6cfd2185f65ec2e47a64cff5f..eff67e6373e1297a49321d90b69aad12ea30e3b2 100644 (file)
@@ -41,7 +41,13 @@ interface HMRRecord {
 const map: Map<string, HMRRecord> = new Map()
 
 export function registerHMR(instance: ComponentInternalInstance) {
-  map.get(instance.type.__hmrId!)!.instances.add(instance)
+  const id = instance.type.__hmrId!
+  let record = map.get(id)
+  if (!record) {
+    createRecord(id, instance.type as ComponentOptions)
+    record = map.get(id)!
+  }
+  record.instances.add(instance)
 }
 
 export function unregisterHMR(instance: ComponentInternalInstance) {
@@ -60,9 +66,11 @@ function createRecord(id: string, comp: ComponentOptions): boolean {
 }
 
 function rerender(id: string, newRender?: RenderFunction) {
+  const record = map.get(id)
+  if (!record) return
   // Array.from creates a snapshot which avoids the set being mutated during
   // updates
-  Array.from(map.get(id)!.instances).forEach(instance => {
+  Array.from(record.instances).forEach(instance => {
     if (newRender) {
       instance.render = newRender
     }
@@ -75,7 +83,8 @@ function rerender(id: string, newRender?: RenderFunction) {
 }
 
 function reload(id: string, newComp: ComponentOptions) {
-  const record = map.get(id)!
+  const record = map.get(id)
+  if (!record) return
   // 1. Update existing comp definition to match new one
   const comp = record.comp
   Object.assign(comp, newComp)