1313
1414let React ;
1515let ReactDOM ;
16+ let ReactFreshRuntime ;
1617let Scheduler ;
1718let act ;
19+ let lastRoot ;
1820
1921describe ( 'ReactFresh' , ( ) => {
2022 let container ;
21- let familiesByID ;
22- let familiesByType ;
23- let newFamilies ;
24- let updatedFamilies ;
25- let performHotReload ;
26- let signaturesByType ;
23+ let scheduleHotUpdate ;
2724
2825 beforeEach ( ( ) => {
29- let scheduleHotUpdate ;
30- let lastRoot ;
3126 global . __REACT_DEVTOOLS_GLOBAL_HOOK__ = {
3227 supportsFiber : true ,
3328 inject : injected => {
@@ -42,126 +37,43 @@ describe('ReactFresh', () => {
4237 jest . resetModules ( ) ;
4338 React = require ( 'react' ) ;
4439 ReactDOM = require ( 'react-dom' ) ;
40+ ReactFreshRuntime = require ( 'react-fresh/runtime' ) ;
4541 Scheduler = require ( 'scheduler' ) ;
4642 act = require ( 'react-dom/test-utils' ) . act ;
4743 container = document . createElement ( 'div' ) ;
4844 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- }
6345 } ) ;
6446
6547 afterEach ( ( ) => {
6648 document . body . removeChild ( container ) ;
6749 } ) ;
6850
6951 function prepare ( version ) {
70- newFamilies = new Set ( ) ;
71- updatedFamilies = new Set ( ) ;
72- signaturesByType = new Map ( ) ;
7352 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-
8553 return Component ;
8654 }
8755
8856 function render ( version , props ) {
89- const Component = prepare ( version ) ;
57+ const Component = version ( ) ;
9058 act ( ( ) => {
9159 ReactDOM . render ( < Component { ...props } /> , container ) ;
9260 } ) ;
9361 return Component ;
9462 }
9563
9664 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 ( ) ;
10165 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 ) ;
12368 return Component ;
12469 }
12570
12671 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 ) ;
16173 }
16274
163- function __signature__ ( type , signature ) {
164- signaturesByType . set ( type , signature ) ;
75+ function __signature__ ( type , id ) {
76+ ReactFreshRuntime . setSignature ( type , id ) ;
16577 }
16678
16779 it ( 'can preserve state for compatible types' , ( ) => {
0 commit comments