@@ -33,6 +33,49 @@ export function loaderPlugin<
3333 SyncInspectableDeferred < unknown >
3434 > ( ) ;
3535
36+ const resolveLoadPathLoaderData = ( actions : LoaderPluginActions ) => {
37+ actions
38+ . getStack ( )
39+ . activities . filter (
40+ ( activity ) => activity . transitionState !== "exit-done" ,
41+ )
42+ . forEach ( ( activity ) => {
43+ const matchActivity = input . config . activities . find (
44+ ( candidate ) => candidate . name === activity . name ,
45+ ) ;
46+
47+ if ( ! matchActivity ?. loader ) {
48+ return ;
49+ }
50+
51+ const loaderData = ( activity . context as any ) ?. loaderData as
52+ | SyncInspectablePromise < unknown >
53+ | undefined ;
54+ const deferred = loaderData
55+ ? loadPathDeferreds . get ( loaderData )
56+ : undefined ;
57+
58+ if ( ! loaderData || ! deferred ) {
59+ return ;
60+ }
61+
62+ loadPathDeferreds . delete ( loaderData ) ;
63+
64+ Promise . allSettled ( [ loaderData ] ) . then ( ( [ loaderDataPromiseResult ] ) => {
65+ printLoaderDataPromiseError ( {
66+ promiseResult : loaderDataPromiseResult ,
67+ activityName : matchActivity . name ,
68+ } ) ;
69+ } ) ;
70+
71+ try {
72+ deferred . resolve ( loadData ( activity . name , activity . params ) ) ;
73+ } catch ( error ) {
74+ deferred . reject ( error ) ;
75+ }
76+ } ) ;
77+ } ;
78+
3679 return {
3780 key : "plugin-loader" ,
3881 overrideInitialEvents ( { initialEvents, initialContext, initInfo } ) {
@@ -117,48 +160,16 @@ export function loaderPlugin<
117160 return ;
118161 }
119162
120- actions
121- . getStack ( )
122- . activities . filter (
123- ( activity ) => activity . transitionState !== "exit-done" ,
124- )
125- . forEach ( ( activity ) => {
126- const matchActivity = input . config . activities . find (
127- ( candidate ) => candidate . name === activity . name ,
128- ) ;
129-
130- if ( ! matchActivity ?. loader ) {
131- return ;
132- }
133-
134- const loaderData = ( activity . context as any ) ?. loaderData as
135- | SyncInspectablePromise < unknown >
136- | undefined ;
137- const deferred = loaderData
138- ? loadPathDeferreds . get ( loaderData )
139- : undefined ;
140-
141- if ( ! loaderData || ! deferred ) {
142- throw new Error (
143- `Missing deferred loader data for the restored "${ activity . name } " activity` ,
144- ) ;
145- }
146-
147- Promise . allSettled ( [ loaderData ] ) . then (
148- ( [ loaderDataPromiseResult ] ) => {
149- printLoaderDataPromiseError ( {
150- promiseResult : loaderDataPromiseResult ,
151- activityName : matchActivity . name ,
152- } ) ;
153- } ,
154- ) ;
155-
156- try {
157- deferred . resolve ( loadData ( activity . name , activity . params ) ) ;
158- } catch ( error ) {
159- deferred . reject ( error ) ;
160- }
161- } ) ;
163+ resolveLoadPathLoaderData ( actions ) ;
164+ } ,
165+ onResumed ( { actions } ) {
166+ resolveLoadPathLoaderData ( actions ) ;
167+ } ,
168+ onPushed ( { actions } ) {
169+ resolveLoadPathLoaderData ( actions ) ;
170+ } ,
171+ onReplaced ( { actions } ) {
172+ resolveLoadPathLoaderData ( actions ) ;
162173 } ,
163174 onBeforePush : createBeforeRouteHandler ( input , loadData ) ,
164175 onBeforeReplace : createBeforeRouteHandler ( input , loadData ) ,
@@ -170,6 +181,10 @@ type OnBeforeRoute = NonNullable<
170181 | ReturnType < StackflowReactPlugin > [ "onBeforePush" ]
171182 | ReturnType < StackflowReactPlugin > [ "onBeforeReplace" ]
172183> ;
184+ type LoaderPluginActions = Parameters <
185+ NonNullable < ReturnType < StackflowReactPlugin > [ "onInit" ] >
186+ > [ 0 ] [ "actions" ] ;
187+
173188function createBeforeRouteHandler <
174189 T extends ActivityDefinition < RegisteredActivityName > ,
175190 R extends {
0 commit comments