Skip to content

Commit c74cc97

Browse files
committed
add experimental instance level HMR update hooks
1 parent 681354a commit c74cc97

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

runtime/svelte-hooks.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ export const createProxiedComponent = (
8787
{ onInstance, onMount, onDestroy }
8888
) => {
8989
let cmp
90-
let last
9190
let options = initialOptions
9291

9392
const isCurrent = _cmp => cmp === _cmp
@@ -139,6 +138,9 @@ export const createProxiedComponent = (
139138
return comp
140139
}
141140

141+
targetCmp.$$.on_before_hmr = []
142+
targetCmp.$$.on_hmr = []
143+
142144
// `conservative: true` means we want to be sure that the new component has
143145
// actually been successfuly created before destroying the old instance.
144146
// This could be useful for preventing runtime errors in component init to
@@ -161,6 +163,11 @@ export const createProxiedComponent = (
161163
) => {
162164
const restore = captureState(targetCmp)
163165
assignOptions(target, anchor, restore, preserveLocalState)
166+
167+
const callbacks = cmp.$$.on_hmr
168+
169+
const afterCallbacks = callbacks.map(fn => fn(cmp)).filter(Boolean)
170+
164171
const previous = cmp
165172
if (conservative) {
166173
try {
@@ -180,9 +187,17 @@ export const createProxiedComponent = (
180187
// previous can be null if last constructor has crashed
181188
previous.$destroy()
182189
}
183-
cmp = createComponent(Component, restore, last)
184-
last = cmp
190+
cmp = createComponent(Component, restore, cmp)
185191
}
192+
193+
cmp.$$.hmr_cmp = cmp
194+
195+
for (const fn of afterCallbacks) {
196+
fn(cmp)
197+
}
198+
199+
cmp.$$.on_hmr = callbacks
200+
186201
return cmp
187202
}
188203

@@ -243,6 +258,7 @@ export const createProxiedComponent = (
243258
const parentComponent = get_current_component_safe()
244259

245260
cmp = new Component(options)
261+
cmp.$$.hmr_cmp = cmp
246262

247263
instrument(cmp)
248264

0 commit comments

Comments
 (0)