Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ export type LocationVisit = {
preserveScroll: boolean
}

export type CancelToken = {
cancel: VoidFunction
}

export type CancelTokenCallback = (cancelToken: CancelToken) => void

export type Visit<T extends RequestPayload = RequestPayload> = {
method: Method
data: T
Expand Down Expand Up @@ -330,7 +336,7 @@ export type GlobalEventCallback<TEventName extends GlobalEventNames<T>, T extend
export type InternalEvent = 'missingHistoryItem' | 'loadDeferredProps'

export type VisitCallbacks<T extends RequestPayload = RequestPayload> = {
onCancelToken: { ({ cancel }: { cancel: VoidFunction }): void }
onCancelToken: CancelTokenCallback
onBefore: GlobalEventCallback<'before', T>
onBeforeUpdate: GlobalEventCallback<'beforeUpdate', T>
onStart: GlobalEventCallback<'start', T>
Expand Down Expand Up @@ -420,9 +426,8 @@ export interface LinkComponentBaseProps
| 'queryStringArrayFormat'
| 'async'
> &
Omit<VisitCallbacks, 'onCancelToken'> & {
VisitCallbacks & {
href: string | UrlMethodPair
onCancelToken: (cancelToken: import('axios').CancelTokenSource) => void
prefetch: boolean | LinkPrefetchOption | LinkPrefetchOption[]
cacheFor: CacheForOption | CacheForOption[]
cacheTags: string | string[]
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/useForm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CancelToken,
ErrorValue,
FormDataErrors,
FormDataKeys,
Expand Down Expand Up @@ -70,7 +71,7 @@ export default function useForm<TForm extends FormDataType<TForm>>(
const [defaults, setDefaults] = useState(
(typeof rememberKeyOrInitialValues === 'string' ? maybeInitialValues : rememberKeyOrInitialValues) || ({} as TForm),
)
const cancelToken = useRef(null)
const cancelToken = useRef<CancelToken | null>(null)
const recentlySuccessfulTimeoutId = useRef(null)
const [data, setData] = rememberKey ? useRemember(defaults, `${rememberKey}:data`) : useState(defaults)
const [errors, setErrors] = rememberKey
Expand Down
6 changes: 3 additions & 3 deletions packages/svelte/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {
shouldIntercept,
shouldNavigate,
type CacheForOption,
type CancelToken,
type GlobalEventsMap,
type LinkComponentBaseProps,
type LinkPrefetchOption,
type Method,
type VisitOptions,
} from '@inertiajs/core'
import type { CancelTokenSource } from 'axios'
import type { ActionReturn } from 'svelte/action'

type ActionEventHandlers = {
Expand Down Expand Up @@ -40,8 +40,8 @@ type ActionAttributes = {
event: CustomEvent<SelectedGlobalEventsMap[K]['details']>,
) => void
} & {
'on:cancel-token'?: (event: CustomEvent<CancelTokenSource>) => void
oncanceltoken?: (event: CustomEvent<CancelTokenSource>) => void
'on:cancel-token'?: (event: CustomEvent<CancelToken>) => void
oncanceltoken?: (event: CustomEvent<CancelToken>) => void
}

function link(
Expand Down
5 changes: 3 additions & 2 deletions packages/svelte/src/useForm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
ActiveVisit,
CancelToken,
Errors,
ErrorValue,
FormDataErrors,
Expand Down Expand Up @@ -70,7 +71,7 @@ export default function useForm<TForm extends FormDataType<TForm>>(
? (router.restore(rememberKey) as { data: TForm; errors: Record<FormDataKeys<TForm>, string> } | null)
: null
let defaults = cloneDeep(data)
let cancelToken: { cancel: () => void } | null = null
let cancelToken: CancelToken | null = null
let recentlySuccessfulTimeoutId: ReturnType<typeof setTimeout> | null = null
let transform = (data: TForm) => data as object
// Track if defaults was called manually during onSuccess to avoid
Expand Down Expand Up @@ -169,7 +170,7 @@ export default function useForm<TForm extends FormDataType<TForm>>(

const _options: Omit<VisitOptions, 'method'> = {
...options,
onCancelToken: (token: { cancel: () => void }) => {
onCancelToken: (token: CancelToken) => {
cancelToken = token

if (options.onCancelToken) {
Expand Down
3 changes: 2 additions & 1 deletion packages/vue3/src/link.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ActiveVisit,
CacheForOption,
CancelTokenCallback,
GlobalEventCallback,
isUrlMethodPair,
LinkComponentBaseProps,
Expand Down Expand Up @@ -115,7 +116,7 @@ const Link: InertiaLink = defineComponent({
default: noop,
},
onCancelToken: {
type: Function as PropType<(cancelToken: import('axios').CancelTokenSource) => void>,
type: Function as PropType<CancelTokenCallback>,
default: noop,
},
onPrefetching: {
Expand Down
3 changes: 2 additions & 1 deletion packages/vue3/src/useForm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CancelToken,
ErrorValue,
FormDataErrors,
FormDataKeys,
Expand Down Expand Up @@ -59,7 +60,7 @@ export default function useForm<TForm extends FormDataType<TForm>>(
? (router.restore(rememberKey) as { data: TForm; errors: Record<FormDataKeys<TForm>, string> })
: null
let defaults = typeof data === 'function' ? cloneDeep(data()) : cloneDeep(data)
let cancelToken = null
let cancelToken: CancelToken | null = null
let recentlySuccessfulTimeoutId = null
let transform = (data) => data

Expand Down