@@ -58,122 +58,49 @@ export function createBrowserApplication(config: {
5858 const hatchUpdate = domain . createEvent < HatchParams > ( { name : `hatchUpdate:${ path } ` } ) ;
5959 const hatchExit = domain . createEvent < void > ( { name : `hatchExit:${ path } ` } ) ;
6060
61- // Triggered when hatch is used from the main bundle
62- const dontNeedLoadChunk = domain . createEvent ( { name : `dontNeedLoadChunk:${ path } ` } ) ;
63-
64- const $chunkLoaded = domain . createStore ( false , { name : `$chunkLoaded:${ path } ` } ) ;
65- const $hasHatch = domain . createStore ( getHatch ( component ) !== undefined , {
66- name : `$hasHatch:${ path } ` ,
67- } ) ;
68-
69- const loadPageFx = domain . createEffect ( {
70- name : `loadPageFx:${ path } ` ,
71- handler : async ( ) => {
72- // eslint-disable-next-line @typescript-eslint/no-explicit-any
73- const loader = ( component as any ) . load ;
74- if ( typeof loader === 'function' ) {
75- const module = await loader ( ) ;
76- if ( ! module . default ) {
77- console . info ( `Not found default export for "${ path } " route` ) ;
78- return null ;
79- }
80- // eslint-disable-next-line @typescript-eslint/ban-types
81- return module . default as { } ;
82- }
83- return component ;
84- } ,
85- } ) ;
86-
87- // eslint-disable-next-line @typescript-eslint/no-explicit-any
88- const setupHatchLinksFx = domain . createEffect ( {
89- name : `setupHatchLinksFx:${ path } ` ,
90- handler : ( page : any ) => {
91- const hatch = getHatch ( page ) ;
92- if ( hatch ) {
93- forward ( { from : hatchEnter , to : hatch . enter } ) ;
94- forward ( { from : hatchUpdate , to : hatch . update } ) ;
95- forward ( { from : hatchExit , to : hatch . exit } ) ;
96- // Refork here?
97- // Because computations still in the old scope, we can't switch to a new scope where page's events already forked
98- // We need effector v22
99- // https://share.effector.dev/HEXzaOU0
100- return true ;
101- }
102- return false ;
103- } ,
104- } ) ;
61+ const componentHatch = getHatch ( component ) ;
62+ if ( componentHatch ) {
63+ forward ( { from : hatchEnter , to : componentHatch . enter } ) ;
64+ forward ( { from : hatchUpdate , to : componentHatch . update } ) ;
65+ forward ( { from : hatchExit , to : componentHatch . exit } ) ;
66+ }
10567
10668 // Shows that user is on the route
107- const $onRoute = domain
108- . createStore ( false , { name : `$onRoute:${ path } ` } )
109- . on ( routeMatched , ( ) => true )
110- . on ( notMatched , ( ) => false ) ;
111-
112- // Shows that user visited route and wait for page
113- // If true, page.hatch.enter is triggered and logic is ran
114- const $onPage = domain
115- . createStore ( false , { name : `$onPage:${ path } ` } )
116- . on ( hatchEnter , ( ) => true )
117- . on ( hatchExit , ( ) => false ) ;
118-
119- $chunkLoaded . on ( loadPageFx . done , ( ) => true ) . on ( dontNeedLoadChunk , ( ) => true ) ;
120- $hasHatch . on ( setupHatchLinksFx . doneData , ( _ , has ) => has ) ;
121-
122- // When hatch not found on component from route and chunk don't load before
123- guard ( {
124- source : routeMatched ,
125- filter : combine (
126- $hasHatch ,
127- $chunkLoaded ,
128- ( hasHatch , chunkLoaded ) => ! hasHatch && ! chunkLoaded ,
129- ) ,
130- target : loadPageFx ,
131- } ) ;
69+ const $onRoute = domain . createStore ( false , { name : `$onRoute:${ path } ` } ) ;
13270
133- // After loading page chunk check that it has Page and try to connect with local events
134- guard ( {
135- source : loadPageFx . doneData ,
136- filter : ( value ) => value !== null ,
137- target : setupHatchLinksFx ,
138- } ) ;
139-
140- loadPageFx . failData . watch ( ( error ) => {
141- console . error ( `Failed to load page for ${ path } ` , error ) ;
142- } ) ;
71+ // Shows that user visited route and waited for page
72+ // If true, page.hatch.enter is triggered and logic was run
73+ const $onPage = domain . createStore ( false , { name : `$onPage:${ path } ` } ) ;
14374
144- setupHatchLinksFx . failData . watch ( ( error ) => {
145- console . error ( `Failed to setup hatch links for ${ path } ` , error ) ;
146- } ) ;
75+ //#region route matched
76+ $onRoute . on ( routeMatched , ( ) => true ) ;
14777
148- // Hatch found on component from route, but chunk never loaded
149- // We need to setup connections between hatch from component and local triggers
15078 guard ( {
151- source : routeMatched ,
152- filter : combine ( $hasHatch , $chunkLoaded , ( hasHatch , chunkLoaded ) => hasHatch && ! chunkLoaded ) ,
153- target : [ setupHatchLinksFx . prepend ( ( ) => component ) , dontNeedLoadChunk ] ,
79+ clock : routeMatched ,
80+ filter : $onPage ,
81+ target : hatchUpdate ,
15482 } ) ;
15583
156- // Trigger local unit only after loading chunk and setup connections
157- // Set onPage = true
15884 guard ( {
159- source : routeMatched ,
160- clock : setupHatchLinksFx . doneData ,
161- filter : $onRoute ,
85+ clock : routeMatched ,
86+ filter : combine ( $onPage , $onRoute , ( page , route ) => ! page && route ) ,
16287 target : hatchEnter ,
16388 } ) ;
16489
165- guard ( {
166- source : routeMatched ,
167- filter : $onPage ,
168- target : hatchUpdate ,
169- } ) ;
90+ $onPage . on ( hatchEnter , ( ) => true ) ;
91+ //#endregion route matched
92+
93+ //#region NOT matched
94+ $onRoute . on ( notMatched , ( ) => false ) ;
17095
171- // onPage = false should set only after exit logic is run
17296 guard ( {
173- source : notMatched ,
97+ clock : notMatched ,
17498 filter : $onPage ,
17599 target : hatchExit ,
176100 } ) ;
101+
102+ $onPage . on ( hatchExit , ( ) => false ) ;
103+ //#endregion NOT matched
177104 }
178105
179106 return { navigation } ;
0 commit comments