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