import { EventObject , Subscribable , InvokeDefinition , AnyEventObject , StateMachine , Spawnable , SCXML } from './types' ; import { interopSymbols , isMachine , mapContext , toInvokeSource } from './utils' ; import * as serviceScope from './serviceScope' ; import { ActorRef , BaseActorRef } from '.' ; export interface Actor < TContext = any , TEvent extends EventObject = AnyEventObject > extends Subscribable < TContext > { id : string ; send : ( event : TEvent ) => any ; stop ? : ( ) => any | undefined ; toJSON : ( ) => { id : string ; } ; meta ? : InvokeDefinition < TContext , TEvent > ; state ? : any ; deferred ? : boolean ; } export function createNullActor ( id ) { return { id , send : ( ) => void 0 , subscribe : ( ) => ( { unsubscribe : ( ) => void 0 } ) , getSnapshot : ( ) => undefined , toJSON : ( ) => ( { id } ) , ... interopSymbols } ; } param invokeDefinition export function createInvocableActor < TC , TE extends EventObject > ( invokeDefinition , machine , context , _event ) { const invokeSrc = toInvokeSource ( invokeDefinition . src ) ; const serviceCreator = machine ?. options . services ?. [ invokeSrc . type ] ; const resolvedData = invokeDefinition . data ? mapContext ( invokeDefinition . data , context , _event ) : undefined ; const tempActor = serviceCreator ? createDeferredActor ( serviceCreator as Spawnable , invokeDefinition . id , resolvedData ) : createNullActor ( invokeDefinition . id ) ; tempActor . meta = invokeDefinition ; return tempActor ; } export function createDeferredActor ( entity , id , data ? ) { const tempActor = createNullActor ( id ) ; tempActor . deferred = true ; if ( isMachine ( entity ) ) { const initialState = ( ( tempActor as any ) . state = serviceScope . provide ( undefined , ( ) => ( data ? entity . withContext ( data ) : entity ) . initialState ) ) ; tempActor . getSnapshot = ( ) => initialState ; } return tempActor ; } export function isActor ( item ) { try { return typeof item . send === 'function' ; } catch ( e ) { return false ; } } export function isSpawnedActor ( item ) { return isActor ( item ) && 'id' in item ; } export function toActorRef < TEvent extends EventObject , TEmitted = any , TActorRefLike extends BaseActorRef < TEvent > = BaseActorRef < TEvent > > ( actorRefLike ) { return { subscribe : ( ) => ( { unsubscribe : ( ) => void 0 } ) , id : 'anonymous' , getSnapshot : ( ) => undefined , ... interopSymbols , ... actorRefLike } ; }