1
- import { Registry , configure as configureProxy , createProxy } from 'svelte-dev-helper ' ;
1
+ import { makeApplyHmr } from 'svelte-hmr/runtime ' ;
2
2
3
- let hotOptions = {
4
- noPreserveState : false
5
- } ;
3
+ // eslint-disable-next-line no-undef
4
+ const g = typeof window !== 'undefined' ? window : global ;
6
5
7
- export function configure ( options ) {
8
- hotOptions = Object . assign ( hotOptions , options ) ;
9
- configureProxy ( hotOptions ) ;
10
- }
6
+ const globalKey =
7
+ typeof Symbol !== 'undefined'
8
+ ? Symbol ( 'SVELTE_LOADER_HOT' )
9
+ : '__SVELTE_LOADER_HOT' ;
11
10
12
- export function register ( id , component ) {
11
+ if ( ! g [ globalKey ] ) {
12
+ // do updating refs counting to know when a full update has been applied
13
+ let updatingCount = 0 ;
13
14
14
- //store original component in registry
15
- Registry . set ( id , {
16
- rollback : null ,
17
- component,
18
- instances : [ ]
19
- } ) ;
15
+ const notifyStart = ( ) => {
16
+ updatingCount ++ ;
17
+ } ;
20
18
21
- //create the proxy itself
22
- const proxy = createProxy ( id ) ;
19
+ const notifyError = reload => err => {
20
+ const errString = ( err && err . stack ) || err ;
21
+ // eslint-disable-next-line no-console
22
+ console . error (
23
+ '[HMR] Failed to accept update (nollup compat mode)' ,
24
+ errString
25
+ ) ;
26
+ reload ( ) ;
27
+ notifyEnd ( ) ;
28
+ } ;
23
29
24
- //patch the registry record with proxy constructor
25
- const record = Registry . get ( id ) ;
26
- record . proxy = proxy ;
27
- Registry . set ( id , record ) ;
30
+ const notifyEnd = ( ) => {
31
+ updatingCount -- ;
32
+ if ( updatingCount === 0 ) {
33
+ // NOTE this message is important for timing in tests
34
+ // eslint-disable-next-line no-console
35
+ console . log ( '[HMR:Svelte] Up to date' ) ;
36
+ }
37
+ } ;
28
38
29
- return proxy ;
39
+ g [ globalKey ] = {
40
+ hotStates : { } ,
41
+ notifyStart,
42
+ notifyError,
43
+ notifyEnd,
44
+ } ;
30
45
}
31
46
32
- export function reload ( id , component ) {
47
+ const runAcceptHandlers = acceptHandlers => {
48
+ const queue = [ ...acceptHandlers ] ;
49
+ const next = ( ) => {
50
+ const cur = queue . shift ( ) ;
51
+ if ( cur ) {
52
+ return cur ( null ) . then ( next ) ;
53
+ } else {
54
+ return Promise . resolve ( null ) ;
55
+ }
56
+ } ;
57
+ return next ( ) ;
58
+ } ;
33
59
34
- const record = Registry . get ( id ) ;
60
+ export const applyHmr = makeApplyHmr ( args => {
61
+ const { notifyStart, notifyError, notifyEnd } = g [ globalKey ] ;
62
+ const { m, reload } = args ;
35
63
36
- //keep reference to previous version to enable rollback
37
- record . rollback = record . component ;
64
+ let acceptHandlers = ( m . hot . data && m . hot . data . acceptHandlers ) || [ ] ;
65
+ let nextAcceptHandlers = [ ] ;
38
66
39
- //replace component in registry with newly loaded component
40
- record . component = component ;
67
+ m . hot . dispose ( data => {
68
+ data . acceptHandlers = nextAcceptHandlers ;
69
+ } ) ;
41
70
42
- Registry . set ( id , record ) ;
71
+ const dispose = ( ... args ) => m . hot . dispose ( ... args ) ;
43
72
44
- //re-render the proxy instances
45
- record . instances . slice ( ) . forEach ( function ( instance ) {
46
- instance && instance . _rerender ( ) ;
73
+ const accept = handler => {
74
+ if ( nextAcceptHandlers . length === 0 ) {
75
+ m . hot . accept ( ) ;
76
+ }
77
+ nextAcceptHandlers . push ( handler ) ;
78
+ } ;
79
+
80
+ const check = status => {
81
+ if ( status === 'ready' ) {
82
+ notifyStart ( ) ;
83
+ } else if ( status === 'idle' ) {
84
+ runAcceptHandlers ( acceptHandlers )
85
+ . then ( notifyEnd )
86
+ . catch ( notifyError ( reload ) ) ;
87
+ }
88
+ } ;
89
+
90
+ m . hot . addStatusHandler ( check ) ;
91
+
92
+ m . hot . dispose ( ( ) => {
93
+ m . hot . removeStatusHandler ( check ) ;
47
94
} ) ;
48
95
49
- //return the original proxy constructor that was `register()`-ed
50
- return record . proxy ;
51
- }
96
+ const hot = {
97
+ data : m . hot . data ,
98
+ dispose,
99
+ accept,
100
+ } ;
101
+
102
+ return Object . assign ( { } , args , { hot } ) ;
103
+ } ) ;
0 commit comments