@@ -373,7 +373,7 @@ export interface Resource<T> {
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
@@ -445,39 +445,53 @@ export function createResource<T, S>(
445
445
if ( sharedConfig . context ! . async && options . ssrLoadFrom !== "initial" ) {
446
446
resource = sharedConfig . context ! . resources [ id ] || ( sharedConfig . context ! . resources [ id ] = { } ) ;
447
447
if ( resource . ref ) {
448
- if ( ! resource . data && ! resource . ref [ 0 ] . loading && ! resource . ref [ 0 ] . error )
448
+ if ( ! resource . data && ! resource . ref [ 0 ] . _loading && ! resource . ref [ 0 ] . error )
449
449
resource . ref [ 1 ] . refetch ( ) ;
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 ;
@@ -523,7 +537,10 @@ export function createResource<T, S>(
523
537
return ctx . resources [ id ] . data ;
524
538
}
525
539
if ( options . ssrLoadFrom !== "initial" ) load ( ) ;
526
- const ref = [ read , { refetch : load , mutate : ( v : T ) => ( value = v ) } ] as ResourceReturn < T > ;
540
+ const ref = [
541
+ read as unknown as Resource < T > ,
542
+ { refetch : load , mutate : ( v : T ) => ( value = v ) }
543
+ ] as ResourceReturn < T > ;
527
544
if ( p ) resource . ref = ref ;
528
545
return ref ;
529
546
}
@@ -550,15 +567,15 @@ export function lazy<T extends Component<any>>(
550
567
else load ( id ) ;
551
568
if ( p . resolved ) return p . resolved ( props ) ;
552
569
const ctx = useContext ( SuspenseContext ) ;
553
- const track = { loading : true , error : undefined } ;
570
+ const track = { _loading : true , error : undefined } ;
554
571
if ( ctx ) {
555
572
ctx . resources . set ( id , track ) ;
556
573
contexts . add ( ctx ) ;
557
574
}
558
575
if ( sharedConfig . context ! . async ) {
559
576
sharedConfig . context ! . block (
560
577
p . then ( ( ) => {
561
- track . loading = false ;
578
+ track . _loading = false ;
562
579
notifySuspense ( contexts ) ;
563
580
} )
564
581
) ;
@@ -571,7 +588,7 @@ export function lazy<T extends Component<any>>(
571
588
572
589
function suspenseComplete ( c : SuspenseContextType ) {
573
590
for ( const r of c . resources . values ( ) ) {
574
- if ( r . loading ) return false ;
591
+ if ( r . _loading ) return false ;
575
592
}
576
593
return true ;
577
594
}
@@ -642,7 +659,7 @@ export function Suspense(props: { fallback?: string; children: string }) {
642
659
const value : SuspenseContextType =
643
660
ctx . suspense [ id ] ||
644
661
( ctx . suspense [ id ] = {
645
- resources : new Map < string , { loading : boolean ; error : any } > ( ) ,
662
+ resources : new Map < string , { _loading : boolean ; error : any } > ( ) ,
646
663
completed : ( ) => {
647
664
const res = runSuspense ( ) ;
648
665
if ( suspenseComplete ( value ) ) {
0 commit comments