13
13
14
14
let React ;
15
15
let ReactDOM ;
16
+ let ReactFreshRuntime ;
16
17
let Scheduler ;
17
18
let act ;
19
+ let lastRoot ;
18
20
19
21
describe ( 'ReactFresh' , ( ) => {
20
22
let container ;
21
- let familiesByID ;
22
- let familiesByType ;
23
- let newFamilies ;
24
- let updatedFamilies ;
25
- let performHotReload ;
26
- let signaturesByType ;
23
+ let scheduleHotUpdate ;
27
24
28
25
beforeEach ( ( ) => {
29
- let scheduleHotUpdate ;
30
- let lastRoot ;
31
26
global . __REACT_DEVTOOLS_GLOBAL_HOOK__ = {
32
27
supportsFiber : true ,
33
28
inject : injected => {
@@ -42,126 +37,43 @@ describe('ReactFresh', () => {
42
37
jest . resetModules ( ) ;
43
38
React = require ( 'react' ) ;
44
39
ReactDOM = require ( 'react-dom' ) ;
40
+ ReactFreshRuntime = require ( 'react-fresh/runtime' ) ;
45
41
Scheduler = require ( 'scheduler' ) ;
46
42
act = require ( 'react-dom/test-utils' ) . act ;
47
43
container = document . createElement ( 'div' ) ;
48
44
document . body . appendChild ( container ) ;
49
-
50
- familiesByID = new Map ( ) ;
51
- familiesByType = new WeakMap ( ) ;
52
-
53
- if ( __DEV__ ) {
54
- performHotReload = function ( staleFamilies ) {
55
- scheduleHotUpdate ( {
56
- root : lastRoot ,
57
- familiesByType,
58
- updatedFamilies,
59
- staleFamilies,
60
- } ) ;
61
- } ;
62
- }
63
45
} ) ;
64
46
65
47
afterEach ( ( ) => {
66
48
document . body . removeChild ( container ) ;
67
49
} ) ;
68
50
69
51
function prepare ( version ) {
70
- newFamilies = new Set ( ) ;
71
- updatedFamilies = new Set ( ) ;
72
- signaturesByType = new Map ( ) ;
73
52
const Component = version ( ) ;
74
-
75
- // Fill in the signatures.
76
- for ( let family of newFamilies ) {
77
- const latestSignature = signaturesByType . get ( family . currentType ) || null ;
78
- family . currentSignature = latestSignature ;
79
- }
80
-
81
- newFamilies = null ;
82
- updatedFamilies = null ;
83
- signaturesByType = null ;
84
-
85
53
return Component ;
86
54
}
87
55
88
56
function render ( version , props ) {
89
- const Component = prepare ( version ) ;
57
+ const Component = version ( ) ;
90
58
act ( ( ) => {
91
59
ReactDOM . render ( < Component { ...props } /> , container ) ;
92
60
} ) ;
93
61
return Component ;
94
62
}
95
63
96
64
function patch ( version ) {
97
- // Will be filled in by __register__ calls in user code.
98
- newFamilies = new Set ( ) ;
99
- updatedFamilies = new Set ( ) ;
100
- signaturesByType = new Map ( ) ;
101
65
const Component = version ( ) ;
102
-
103
- // Fill in the signatures.
104
- for ( let family of newFamilies ) {
105
- const latestSignature = signaturesByType . get ( family . currentType ) || null ;
106
- family . currentSignature = latestSignature ;
107
- }
108
- // Now that all registration and signatures are collected,
109
- // find which registrations changed their signatures since last time.
110
- const staleFamilies = new Set ( ) ;
111
- for ( let family of updatedFamilies ) {
112
- const latestSignature = signaturesByType . get ( family . currentType ) || null ;
113
- if ( family . currentSignature !== latestSignature ) {
114
- family . currentSignature = latestSignature ;
115
- staleFamilies . add ( family ) ;
116
- }
117
- }
118
-
119
- performHotReload ( staleFamilies ) ;
120
- newFamilies = null ;
121
- updatedFamilies = null ;
122
- signaturesByType = null ;
66
+ const hotUpdate = ReactFreshRuntime . prepareUpdate ( ) ;
67
+ scheduleHotUpdate ( lastRoot , hotUpdate ) ;
123
68
return Component ;
124
69
}
125
70
126
71
function __register__ ( type , id ) {
127
- if ( familiesByType . has ( type ) ) {
128
- return ;
129
- }
130
- let family = familiesByID . get ( id ) ;
131
- let isNew = false ;
132
- if ( family === undefined ) {
133
- isNew = true ;
134
- family = { currentType : type , currentSignature : null } ;
135
- familiesByID . set ( id , family ) ;
136
- }
137
- const prevType = family . currentType ;
138
- if ( isNew ) {
139
- // The first time a type is registered, we don't need
140
- // any special reconciliation logic. So we won't add it to the map.
141
- // Instead, this will happen the firt time it is edited.
142
- newFamilies . add ( family ) ;
143
- } else {
144
- family . currentType = type ;
145
- // Point both previous and next types to this family.
146
- familiesByType . set ( prevType , family ) ;
147
- familiesByType . set ( type , family ) ;
148
- updatedFamilies . add ( family ) ;
149
- }
150
-
151
- if ( typeof type === 'object' && type !== null ) {
152
- switch ( type . $$typeof ) {
153
- case Symbol . for ( 'react.forward_ref' ) :
154
- __register__ ( type . render , id + '$render' ) ;
155
- break ;
156
- case Symbol . for ( 'react.memo' ) :
157
- __register__ ( type . type , id + '$type' ) ;
158
- break ;
159
- }
160
- }
72
+ ReactFreshRuntime . register ( type , id ) ;
161
73
}
162
74
163
- function __signature__ ( type , signature ) {
164
- signaturesByType . set ( type , signature ) ;
75
+ function __signature__ ( type , id ) {
76
+ ReactFreshRuntime . setSignature ( type , id ) ;
165
77
}
166
78
167
79
it ( 'can preserve state for compatible types' , ( ) => {
0 commit comments