hunk.code_window:     }
     }
     onMounted(() => {
       state.value = service.state;
     });
     return { state, send: service.send, service };
   }
hunk.labels: keep keep keep replace replace replace replace keep keep
hunk.file_path: packages/xstate-vue/src/useMachine.ts
hunk.type: replace
file: import {
    interpret,
    EventObject,
    StateMachine,
    State,
    Interpreter,
    InterpreterOptions,
    MachineOptions,
    Typestate,
    Observer
  } from 'xstate';
  import { UseMachineOptions } from './types';
  import { onBeforeUnmount, onMounted } from 'vue';
  
  // copied from core/src/utils.ts
  // it avoids a breaking change between this package and XState which is its peer dep
  function toObserver<T>(
    nextHandler: Observer<T> | ((value: T) => void),
    errorHandler?: (error: any) => void,
    completionHandler?: () => void
  ): Observer<T> {
    if (typeof nextHandler === 'object') {
      return nextHandler;
    }
  
    const noop = () => void 0;
  
    return {
      next: nextHandler,
      error: errorHandler || noop,
      complete: completionHandler || noop
    };
  }
  
  export function useInterpret<
    TContext,
    TEvent extends EventObject,
    TTypestate extends Typestate<TContext> = { value: any; context: TContext }
  >(
    machine: StateMachine<TContext, any, TEvent, TTypestate>,
    options: Partial<InterpreterOptions> &
      Partial<UseMachineOptions<TContext, TEvent>> &
      Partial<MachineOptions<TContext, TEvent>> = {},
    observerOrListener?:
      | Observer<State<TContext, TEvent, any, TTypestate>>
      | ((value: State<TContext, TEvent, any, TTypestate>) => void)
  ): Interpreter<TContext, any, TEvent, TTypestate> {
    const {
      context,
      guards,
      actions,
      activities,
      services,
      delays,
      state: rehydratedState,
      ...interpreterOptions
    } = options;
  
    const machineConfig = {
      context,
      guards,
      actions,
      activities,
      services,
      delays
    };
  
    const machineWithConfig = machine.withConfig(machineConfig, {
      ...machine.context,
      ...context
    } as TContext);
  
    const service = interpret(machineWithConfig, interpreterOptions).start(
      rehydratedState ? (State.create(rehydratedState) as any) : undefined
    );
  
    let sub;
    onMounted(() => {
      if (observerOrListener) {
        sub = service.subscribe(toObserver(observerOrListener));
      }
    });
  
    onBeforeUnmount(() => {
      service.stop();
      sub?.unsubscribe();
    });
  
    return service;
  }
  