import safeStringify from 'fast-safe-stringify' ; import { State , createMachine } from 'xstate' ; import { ParsedReceiverEvent , ReceiverEvent } from './types' ; export function getLazy < T > ( value ) { return typeof value === 'function' ? value ( ) : value ; } export function stringify ( value , replacer ? ) { try { return JSON . stringify ( value , replacer ) ; } catch ( e ) { return safeStringify ( value , replacer ) ; } } export function isReceiverEvent ( event ) { if ( ! event ) { return false ; } try { if ( typeof event === 'object' && 'type' in event && ( event . type as string ) . startsWith ( 'service.' ) ) { return true ; } } catch ( e ) { return false ; } return false ; } export function parseState ( stateJSON ) { const state = State . create ( JSON . parse ( stateJSON ) ) ; delete state . history ; return state ; } export function parseReceiverEvent ( event ) { switch ( event . type ) { case 'service.event' : return { ... event , event : JSON . parse ( event . event ) } ; case 'service.register' : return { ... event , machine : createMachine ( JSON . parse ( event . machine ) ) , state : parseState ( event . state ) } ; case 'service.state' : return { ... event , state : parseState ( event . state ) } ; default : return event ; } }