import { assign, createMachine, sendParent } from 'xstate'; export type DebounceContext = { id: string; debounceFor: number; value?: any; action: { name: string, payload: string }; } export const debounceMachine = createMachine( { tsTypes: {} as import("./debounce.machine.typegen").Typegen0, schema: { context: {} as DebounceContext, events: {} as { type: 'GO'; value: any } }, id: '', initial: 'idle', context: { id: '', value: undefined, debounceFor: 200, action: { name: 'CHANGE', payload: 'value'} }, states: { idle: { on: { GO: { target: 'debouncing', actions: 'assignValueToContext' } } }, debouncing: { on: { GO: { actions: 'assignValueToContext', target: 'debouncing', }, }, after: { delay_current: { target: 'idle', actions: 'performValue', }, }, }, }, }, { actions: { assignValueToContext: assign((context, event) => { return { value: event.value, }; }), performValue: sendParent((context, event) => { return { type: context.action.name, [ context.action.payload ]: context.value }; }) }, delays: { delay_current: (context, event) => context.debounceFor } }, );