UNPKG

@provydon/vue-auto-save

Version:

A Vue 3 composable that autosaves forms with debounce, optional field skipping, and blockable watchers.

70 lines (69 loc) 2.37 kB
import { Ref } from 'vue'; export interface UseAutoSaveFormOptions { /** * Delay in milliseconds before auto-saving after changes (default: 3000ms) */ debounce?: number; /** * List of form field keys to exclude from tracking */ skipFields?: string[]; /** * Whether to skip common Inertia form fields (default: true) */ skipInertiaFields?: boolean; /** * Whether to deeply watch the form (default: true) */ deep?: boolean; /** * Enable debug logs in the console (default: false) */ debug?: boolean; /** * Custom serializer function (default: JSON.stringify) * Note: Functions and non-serializable fields won't survive JSON.stringify. * Circular references will also cause JSON.stringify to throw. * Use a custom serializer if you need to handle these cases. */ serialize?: (obj: Record<string, unknown>) => string; /** * Custom comparator function (optional) * If provided, this will be used instead of string comparison */ compare?: (a: Record<string, unknown>, b: Record<string, unknown>) => boolean; /** * Whether to save on initial mount (default: false) */ saveOnInit?: boolean; /** * Called when a save should be triggered (required) */ onSave: () => void | Promise<void>; /** * Called just before auto-saving starts */ onBeforeSave?: () => void; /** * Called after a successful auto-save */ onAfterSave?: () => void; /** * Called if auto-saving throws or fails */ onError?: (err: unknown) => void; } /** * Automatically watches a Vue 3 form object and triggers save on change with debounce. * Includes support for skipping specific fields and Inertia form helpers. * * @param form - The form object to watch (typically a reactive or ref object) * @param options - Configuration for debounce, lifecycle hooks, and field skipping * @returns An object with `isAutoSaving` and `blockWatcher()` for temporary disable */ export declare function useAutoSaveForm(form: Record<string, unknown> | Ref<Record<string, unknown>>, options: UseAutoSaveFormOptions): { isAutoSaving: Ref<boolean, boolean>; blockWatcher: (ms?: number) => void; unblockWatcher: (ms?: number | null) => void; stop: import('vue').WatchHandle; };