Skip to content

Commit def65da

Browse files
committed
add __SVELTE_HMR global event source
1 parent 2e05cec commit def65da

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

runtime/proxy.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,33 @@ const copyStatics = (component, proxy) => {
270270
}
271271
}
272272

273+
const globalListeners = {}
274+
275+
const onGlobal = (event, fn) => {
276+
event = event.toLowerCase()
277+
if (!globalListeners[event]) globalListeners[event] = []
278+
globalListeners[event].push(fn)
279+
}
280+
281+
const fireGlobal = (event, ...args) => {
282+
const listeners = globalListeners[event]
283+
if (!listeners) return
284+
for (const fn of listeners) {
285+
fn(...args)
286+
}
287+
}
288+
289+
const fireBeforeUpdate = () => fireGlobal('beforeupdate')
290+
291+
const fireAfterUpdate = () => fireGlobal('afterupdate')
292+
293+
if (typeof window !== 'undefined') {
294+
// eslint-disable-next-line no-undef
295+
window.__SVELTE_HMR = {
296+
on: onGlobal,
297+
}
298+
}
299+
273300
let fatalError = false
274301

275302
export const hasFatalError = () => fatalError
@@ -348,6 +375,8 @@ export function createProxy(Adapter, id, Component, hotOptions, canAccept) {
348375

349376
// reload all existing instances of this component
350377
const reload = () => {
378+
fireBeforeUpdate()
379+
351380
// copy statics before doing anything because a static prop/method
352381
// could be used somewhere in the create/render call
353382
copyStatics(current.Component, proxy)
@@ -367,6 +396,8 @@ export function createProxy(Adapter, id, Component, hotOptions, canAccept) {
367396
return false
368397
}
369398

399+
fireAfterUpdate()
400+
370401
return true
371402
}
372403

0 commit comments

Comments
 (0)