Skip to content

Commit

Permalink
refactor(hmr): simplify usage
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 20, 2020
1 parent 19223f5 commit 36d77f9
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/runtime-core/src/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit 36d77f9

Please sign in to comment.