11import { router , type VisitOptions } from '@inertiajs/core'
22import { onDestroy , onMount } from 'svelte'
3- import { readonly , writable } from 'svelte/store'
43
54export default function usePrefetch ( options : VisitOptions = { } ) {
6- const isPrefetched = writable ( false )
7- const isPrefetching = writable ( false )
8- const lastUpdatedAt = writable < number | null > ( null )
5+ let isPrefetched = $state ( false )
6+ let isPrefetching = $state ( false )
7+ let lastUpdatedAt = $state < number | null > ( null )
98
109 const cached = typeof window === 'undefined' ? null : router . getCached ( window . location . pathname , options )
1110 const inFlight = typeof window === 'undefined' ? null : router . getPrefetching ( window . location . pathname , options )
1211
13- isPrefetched . set ( cached !== null )
14- isPrefetching . set ( inFlight !== null )
15- lastUpdatedAt . set ( cached ?. staleTimestamp || null )
12+ isPrefetched = cached !== null
13+ isPrefetching = inFlight !== null
14+ lastUpdatedAt = cached ?. staleTimestamp || null
1615
1716 let removePrefetchedListener : ( ) => void
1817 let removePrefetchingListener : ( ) => void
1918
2019 onMount ( ( ) => {
2120 removePrefetchingListener = router . on ( 'prefetching' , ( { detail } ) => {
2221 if ( detail . visit . url . pathname === window . location . pathname ) {
23- isPrefetching . set ( true )
22+ isPrefetching = true
2423 }
2524 } )
2625
2726 removePrefetchedListener = router . on ( 'prefetched' , ( { detail } ) => {
2827 if ( detail . visit . url . pathname === window . location . pathname ) {
29- isPrefetched . set ( true )
30- isPrefetching . set ( false )
28+ isPrefetched = true
29+ isPrefetching = false
3130 }
3231 } )
3332 } )
@@ -43,9 +42,15 @@ export default function usePrefetch(options: VisitOptions = {}) {
4342 } )
4443
4544 return {
46- isPrefetched : readonly ( isPrefetched ) ,
47- isPrefetching : readonly ( isPrefetching ) ,
48- lastUpdatedAt : readonly ( lastUpdatedAt ) ,
45+ get isPrefetched ( ) {
46+ return isPrefetched
47+ } ,
48+ get isPrefetching ( ) {
49+ return isPrefetching
50+ } ,
51+ get lastUpdatedAt ( ) {
52+ return lastUpdatedAt
53+ } ,
4954 flush ( ) {
5055 router . flush ( window . location . pathname , options )
5156 } ,
0 commit comments