@@ -33,10 +33,7 @@ if (__DEV__) {
33
33
} as HMRRuntime
34
34
}
35
35
36
- interface HMRRecord {
37
- comp : ComponentOptions
38
- instances : Set < ComponentInternalInstance >
39
- }
36
+ type HMRRecord = Set < ComponentInternalInstance >
40
37
41
38
const map : Map < string , HMRRecord > = new Map ( )
42
39
@@ -47,21 +44,18 @@ export function registerHMR(instance: ComponentInternalInstance) {
47
44
createRecord ( id , instance . type as ComponentOptions )
48
45
record = map . get ( id ) !
49
46
}
50
- record . instances . add ( instance )
47
+ record . add ( instance )
51
48
}
52
49
53
50
export function unregisterHMR ( instance : ComponentInternalInstance ) {
54
- map . get ( instance . type . __hmrId ! ) ! . instances . delete ( instance )
51
+ map . get ( instance . type . __hmrId ! ) ! . delete ( instance )
55
52
}
56
53
57
54
function createRecord ( id : string , comp : ComponentOptions ) : boolean {
58
55
if ( map . has ( id ) ) {
59
56
return false
60
57
}
61
- map . set ( id , {
62
- comp,
63
- instances : new Set ( )
64
- } )
58
+ map . set ( id , new Set ( ) )
65
59
return true
66
60
}
67
61
@@ -70,7 +64,7 @@ function rerender(id: string, newRender?: Function) {
70
64
if ( ! record ) return
71
65
// Array.from creates a snapshot which avoids the set being mutated during
72
66
// updates
73
- Array . from ( record . instances ) . forEach ( instance => {
67
+ Array . from ( record ) . forEach ( instance => {
74
68
if ( newRender ) {
75
69
instance . render = newRender as InternalRenderFunction
76
70
}
@@ -85,22 +79,29 @@ function rerender(id: string, newRender?: Function) {
85
79
function reload ( id : string , newComp : ComponentOptions ) {
86
80
const record = map . get ( id )
87
81
if ( ! record ) return
88
- // 1. Update existing comp definition to match new one
89
- const comp = record . comp
90
- Object . assign ( comp , newComp )
91
- for ( const key in comp ) {
92
- if ( ! ( key in newComp ) ) {
93
- delete ( comp as any ) [ key ]
94
- }
95
- }
96
- // 2. Mark component dirty. This forces the renderer to replace the component
97
- // on patch.
98
- comp . __hmrUpdated = true
99
82
// Array.from creates a snapshot which avoids the set being mutated during
100
83
// updates
101
- Array . from ( record . instances ) . forEach ( instance => {
84
+ Array . from ( record ) . forEach ( instance => {
85
+ const comp = instance . type
86
+ if ( ! comp . __hmrUpdated ) {
87
+ // 1. Update existing comp definition to match new one
88
+ Object . assign ( comp , newComp )
89
+ for ( const key in comp ) {
90
+ if ( ! ( key in newComp ) ) {
91
+ delete ( comp as any ) [ key ]
92
+ }
93
+ }
94
+ // 2. Mark component dirty. This forces the renderer to replace the component
95
+ // on patch.
96
+ comp . __hmrUpdated = true
97
+ // 3. Make sure to unmark the component after the reload.
98
+ queuePostFlushCb ( ( ) => {
99
+ comp . __hmrUpdated = false
100
+ } )
101
+ }
102
+
102
103
if ( instance . parent ) {
103
- // 3 . Force the parent instance to re-render. This will cause all updated
104
+ // 4 . Force the parent instance to re-render. This will cause all updated
104
105
// components to be unmounted and re-mounted. Queue the update so that we
105
106
// don't end up forcing the same parent to re-render multiple times.
106
107
queueJob ( instance . parent . update )
@@ -116,10 +117,6 @@ function reload(id: string, newComp: ComponentOptions) {
116
117
)
117
118
}
118
119
} )
119
- // 4. Make sure to unmark the component after the reload.
120
- queuePostFlushCb ( ( ) => {
121
- comp . __hmrUpdated = false
122
- } )
123
120
}
124
121
125
122
function tryWrap ( fn : ( id : string , arg : any ) => any ) : Function {
0 commit comments