File tree 3 files changed +24
-10
lines changed
packages/qwik/src/core/shared
3 files changed +24
-10
lines changed Original file line number Diff line number Diff line change @@ -138,14 +138,18 @@ export const createQRL = <TYPE>(
138
138
return fn ;
139
139
}
140
140
return function ( this : unknown , ...args : QrlArgs < TYPE > ) {
141
- let context = tryGetInvokeContext ( ) ;
142
- if ( context ) {
143
- return fn . apply ( this , args ) ;
144
- }
145
- context = newInvokeContext ( ) ;
141
+ const context = tryGetInvokeContext ( ) || newInvokeContext ( ) ;
142
+ const prevQrl = context . $qrl$ ;
143
+ const prevEvent = context . $event$ ;
146
144
context . $qrl$ = qrl ;
145
+ // TODO do we use this?
147
146
context . $event$ = this as Event ;
148
- return invoke . call ( this , context , fn as any , ...args ) ;
147
+ try {
148
+ return invoke . call ( this , context , fn as any , ...args ) ;
149
+ } finally {
150
+ context . $qrl$ = prevQrl ;
151
+ context . $event$ = prevEvent ;
152
+ }
149
153
} as TYPE ;
150
154
} ;
151
155
Original file line number Diff line number Diff line change @@ -177,6 +177,7 @@ export const createScheduler = (
177
177
journalFlush : ( ) => void
178
178
) => {
179
179
const choreQueue : Chore [ ] = [ ] ;
180
+ const qrlRuns : Promise < any > [ ] = [ ] ;
180
181
181
182
let currentChore : Chore | null = null ;
182
183
let drainScheduled : boolean = false ;
@@ -323,6 +324,9 @@ export const createScheduler = (
323
324
}
324
325
executeChore ( nextChore , isServer ) ;
325
326
if ( nextChore === runUptoChore ) {
327
+ if ( nextChore . $type$ === ChoreType . WAIT_FOR_ALL && qrlRuns . length ) {
328
+ return Promise . all ( qrlRuns ) . then ( ( ) => drainUpTo ( runUptoChore , isServer ) ) ;
329
+ }
326
330
break ;
327
331
}
328
332
}
@@ -399,9 +403,15 @@ export const createScheduler = (
399
403
try {
400
404
const result = retryOnPromise ( ( ) => fn ( ...( chore . $payload$ as unknown [ ] ) ) ) ;
401
405
if ( isPromise ( result ) ) {
402
- result . catch ( ( error ) => {
403
- emitEvent ( 'qerror' , { error } ) ;
404
- } ) ;
406
+ qrlRuns . push (
407
+ result
408
+ . catch ( ( error ) => {
409
+ emitEvent ( 'qerror' , { error } ) ;
410
+ } )
411
+ . then ( ( ) => {
412
+ qrlRuns . splice ( qrlRuns . indexOf ( result ) , 1 ) ;
413
+ } )
414
+ ) ;
405
415
}
406
416
} catch ( error ) {
407
417
emitEvent ( 'qerror' , { error } ) ;
Original file line number Diff line number Diff line change @@ -111,7 +111,7 @@ export function retryOnPromise<T>(
111
111
fn : ( ) => ValueOrPromise < T > ,
112
112
retryCount : number = 0
113
113
) : ValueOrPromise < T > {
114
- const retryOrThrow = ( e : any ) => {
114
+ const retryOrThrow = ( e : any ) : ValueOrPromise < T > => {
115
115
if ( isPromise ( e ) && retryCount < MAX_RETRY_ON_PROMISE_COUNT ) {
116
116
return e . then ( retryOnPromise . bind ( null , fn , retryCount ++ ) ) as ValueOrPromise < T > ;
117
117
}
You can’t perform that action at this time.
0 commit comments