Skip to content

Commit 9a897e8

Browse files
committed
Merge branch 'master' into monorepo-and-tests
2 parents 07b2f3a + 809c701 commit 9a897e8

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

packages/svelte-hmr/lib/make-hot.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ const defaultHotOptions = {
4040

4141
// disable runtime error overlay
4242
noOverlay: false,
43+
44+
// set to true on systems that supports them, to avoid useless caught /
45+
// managed (but still...) exceptions, by using Svelte's current_component
46+
// instead of get_current_component
47+
allowLiveBinding: false,
4348
}
4449

4550
const defaultHotApi = 'hot-api-esm.js'

packages/svelte-hmr/runtime/proxy.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ class ProxyComponent {
237237
try {
238238
let lastProperties
239239
const _cmp = createProxiedComponent(current.Component, options, {
240+
allowLiveBinding: current.hotOptions.allowLiveBinding,
240241
onDestroy,
241242
onMount: afterMount,
242243
onInstance: comp => {

packages/svelte-hmr/runtime/svelte-hooks.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,9 @@ const get_current_component_safe = () => {
8484
export const createProxiedComponent = (
8585
Component,
8686
initialOptions,
87-
{ onInstance, onMount, onDestroy }
87+
{ allowLiveBinding, 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

@@ -240,9 +255,12 @@ export const createProxiedComponent = (
240255
}
241256
}
242257

243-
const parentComponent = get_current_component_safe()
258+
const parentComponent = allowLiveBinding
259+
? current_component
260+
: get_current_component_safe()
244261

245262
cmp = new Component(options)
263+
cmp.$$.hmr_cmp = cmp
246264

247265
instrument(cmp)
248266

0 commit comments

Comments
 (0)