@@ -367,13 +367,13 @@ export function ErrorBoundary(props: {
367
367
export interface Resource < T > {
368
368
( ) : T | undefined ;
369
369
state : "unresolved" | "pending" | "ready" | "refreshing" | "errored" ;
370
- loading : boolean ;
370
+ _loading : boolean ;
371
371
error : any ;
372
372
latest : T | undefined ;
373
373
}
374
374
375
375
type SuspenseContextType = {
376
- resources : Map < string , { loading : boolean ; error : any } > ;
376
+ resources : Map < string , { _loading : boolean ; error : any } > ;
377
377
completed : ( ) => void ;
378
378
} ;
379
379
@@ -435,7 +435,6 @@ export function createResource<T, S>(
435
435
fetcher = source as ResourceFetcher < S , T > ;
436
436
source = true as ResourceSource < S > ;
437
437
}
438
-
439
438
const contexts = new Set < SuspenseContextType > ( ) ;
440
439
const id = sharedConfig . getNextContextId ( ) ;
441
440
let resource : { ref ?: any ; data ?: T } = { } ;
@@ -450,34 +449,48 @@ export function createResource<T, S>(
450
449
return resource . ref ;
451
450
}
452
451
}
453
- const read = ( ) => {
452
+ const prepareResource = ( ) => {
454
453
if ( error ) throw error ;
455
454
const resolved =
456
455
options . ssrLoadFrom !== "initial" &&
457
456
sharedConfig . context ! . async &&
458
457
"data" in sharedConfig . context ! . resources [ id ] ;
459
458
if ( ! resolved && resourceContext ) resourceContext . push ( id ) ;
460
- if ( ! resolved && read . loading ) {
459
+ if ( ! resolved && read . _loading ) {
461
460
const ctx = useContext ( SuspenseContext ) ;
462
461
if ( ctx ) {
463
462
ctx . resources . set ( id , read ) ;
464
463
contexts . add ( ctx ) ;
465
464
}
466
465
}
467
- return resolved ? sharedConfig . context ! . resources [ id ] . data : value ;
466
+ return resolved ;
467
+ } ;
468
+ const read = ( ) => {
469
+ return prepareResource ( ) ? sharedConfig . context ! . resources [ id ] . data : value ;
468
470
} ;
469
- read . loading = false ;
471
+ const loading = ( ) => {
472
+ prepareResource ( ) ;
473
+ return read . _loading ;
474
+ } ;
475
+ read . _loading = false ;
470
476
read . error = undefined as any ;
471
477
read . state = "initialValue" in options ? "ready" : "unresolved" ;
472
- Object . defineProperty ( read , "latest" , {
473
- get ( ) {
474
- return read ( ) ;
478
+ Object . defineProperties ( read , {
479
+ latest : {
480
+ get ( ) {
481
+ return read ( ) ;
482
+ }
483
+ } ,
484
+ loading : {
485
+ get ( ) {
486
+ return loading ( ) ;
487
+ }
475
488
}
476
489
} ) ;
477
490
function load ( ) {
478
491
const ctx = sharedConfig . context ! ;
479
492
if ( ! ctx . async )
480
- return ( read . loading = ! ! ( typeof source === "function" ? ( source as ( ) => S ) ( ) : source ) ) ;
493
+ return ( read . _loading = ! ! ( typeof source === "function" ? ( source as ( ) => S ) ( ) : source ) ) ;
481
494
if ( ctx . resources && id in ctx . resources && "data" in ctx . resources [ id ] ) {
482
495
value = ctx . resources [ id ] . data ;
483
496
return ;
@@ -495,19 +508,19 @@ export function createResource<T, S>(
495
508
p = ( fetcher as ResourceFetcher < S , T > ) ( lookup , { value } ) ;
496
509
}
497
510
if ( p != undefined && typeof p === "object" && "then" in p ) {
498
- read . loading = true ;
511
+ read . _loading = true ;
499
512
read . state = "pending" ;
500
513
p = p
501
514
. then ( res => {
502
- read . loading = false ;
515
+ read . _loading = false ;
503
516
read . state = "ready" ;
504
517
ctx . resources [ id ] . data = res ;
505
518
p = null ;
506
519
notifySuspense ( contexts ) ;
507
520
return res ;
508
521
} )
509
522
. catch ( err => {
510
- read . loading = false ;
523
+ read . _loading = false ;
511
524
read . state = "errored" ;
512
525
read . error = error = castError ( err ) ;
513
526
p = null ;
@@ -550,15 +563,15 @@ export function lazy<T extends Component<any>>(
550
563
else load ( id ) ;
551
564
if ( p . resolved ) return p . resolved ( props ) ;
552
565
const ctx = useContext ( SuspenseContext ) ;
553
- const track = { loading : true , error : undefined } ;
566
+ const track = { _loading : true , error : undefined } ;
554
567
if ( ctx ) {
555
568
ctx . resources . set ( id , track ) ;
556
569
contexts . add ( ctx ) ;
557
570
}
558
571
if ( sharedConfig . context ! . async ) {
559
572
sharedConfig . context ! . block (
560
573
p . then ( ( ) => {
561
- track . loading = false ;
574
+ track . _loading = false ;
562
575
notifySuspense ( contexts ) ;
563
576
} )
564
577
) ;
@@ -571,7 +584,7 @@ export function lazy<T extends Component<any>>(
571
584
572
585
function suspenseComplete ( c : SuspenseContextType ) {
573
586
for ( const r of c . resources . values ( ) ) {
574
- if ( r . loading ) return false ;
587
+ if ( r . _loading ) return false ;
575
588
}
576
589
return true ;
577
590
}
@@ -642,7 +655,7 @@ export function Suspense(props: { fallback?: string; children: string }) {
642
655
const value : SuspenseContextType =
643
656
ctx . suspense [ id ] ||
644
657
( ctx . suspense [ id ] = {
645
- resources : new Map < string , { loading : boolean ; error : any } > ( ) ,
658
+ resources : new Map < string , { _loading : boolean ; error : any } > ( ) ,
646
659
completed : ( ) => {
647
660
const res = runSuspense ( ) ;
648
661
if ( suspenseComplete ( value ) ) {
0 commit comments