@@ -450,34 +450,48 @@ export function createResource<T, S>(
450450 return resource . ref ;
451451 }
452452 }
453- const read = ( ) => {
453+ const prepareResource = ( ) => {
454454 if ( error ) throw error ;
455455 const resolved =
456456 options . ssrLoadFrom !== "initial" &&
457457 sharedConfig . context ! . async &&
458458 "data" in sharedConfig . context ! . resources [ id ] ;
459459 if ( ! resolved && resourceContext ) resourceContext . push ( id ) ;
460- if ( ! resolved && read . loading ) {
460+ if ( ! resolved && read . _loading ) {
461461 const ctx = useContext ( SuspenseContext ) ;
462462 if ( ctx ) {
463- ctx . resources . set ( id , read ) ;
463+ ctx . resources . set ( id , read as unknown as { loading : boolean ; error : any } ) ;
464464 contexts . add ( ctx ) ;
465465 }
466466 }
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 ;
468471 } ;
469- read . loading = false ;
472+ const loading = ( ) => {
473+ prepareResource ( ) ;
474+ return read . _loading ;
475+ } ;
476+ read . _loading = false ;
470477 read . error = undefined as any ;
471478 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+ }
475489 }
476490 } ) ;
477491 function load ( ) {
478492 const ctx = sharedConfig . context ! ;
479493 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 ) ) ;
481495 if ( ctx . resources && id in ctx . resources && "data" in ctx . resources [ id ] ) {
482496 value = ctx . resources [ id ] . data ;
483497 return ;
@@ -495,19 +509,19 @@ export function createResource<T, S>(
495509 p = ( fetcher as ResourceFetcher < S , T > ) ( lookup , { value } ) ;
496510 }
497511 if ( p != undefined && typeof p === "object" && "then" in p ) {
498- read . loading = true ;
512+ read . _loading = true ;
499513 read . state = "pending" ;
500514 p = p
501515 . then ( res => {
502- read . loading = false ;
516+ read . _loading = false ;
503517 read . state = "ready" ;
504518 ctx . resources [ id ] . data = res ;
505519 p = null ;
506520 notifySuspense ( contexts ) ;
507521 return res ;
508522 } )
509523 . catch ( err => {
510- read . loading = false ;
524+ read . _loading = false ;
511525 read . state = "errored" ;
512526 read . error = error = castError ( err ) ;
513527 p = null ;
@@ -523,7 +537,10 @@ export function createResource<T, S>(
523537 return ctx . resources [ id ] . data ;
524538 }
525539 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 > ;
527544 if ( p ) resource . ref = ref ;
528545 return ref ;
529546}
0 commit comments