// Available variables: // - Machine // - interpret // - assign // - send // - sendParent // - spawn // - raise // - actions // - XState (all XState exports) const fetchMachine = Machine({ id: 'timer', initial: 'stopped', context: { duration: 0, endDate: null }, states: { stopped: { on: { START: { cond: (_, event) => event.endDate, target: 'running', actions: assign({ duration: (context, event) => new Date(event.endDate) - (new Date()), endDate: (context, event) => new Date(event.endDate), }) } } }, running: { on: { TICK: [ { cond: (context) => context.endDate <= (new Date()), target: 'stopped' }, { actions: assign({ duration: (context, event) => context.endDate - (new Date()) }) } ], PAUSE: 'paused' } }, paused: { on: { START: 'running', STOP: { target: 'stopped', actions: assign({ duration: 0 }) } } } } });