/** * Copyright (c) 2018-present, tarant * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import Message from '../mailbox/message'; import ISubscriber from '../mailbox/subscriber'; import ActorMessage from './actor-message'; import ActorSystem from './actor-system'; import { Topic } from '..'; import IActorSupervisor, { SupervisionStrategies } from './supervision/actor-supervisor'; type Cancellable = string; export interface IActor extends ISubscriber, IActorSupervisor { id: string; } /** * Class that must be extended by all actors. All defined public methods in actors should be * asynchronous (return a Promise) or return void. */ export default abstract class Actor implements IActor { readonly id: string; readonly partitions: string[]; protected readonly self: this; protected readonly system?: ActorSystem; private readonly materializers; private readonly supervisor?; private readonly scheduled; private readonly topicSubscriptions; private busy; protected constructor(id?: string); /** * Method called by the mailbox when there are messages to be processed. This should * not be overriden by the actor. * * @param message Message received from the mailbox */ onReceiveMessage(message: Message): Promise; /** * Supervision method, called when a child actor failed. * The default strategy is to delegate the supervision to this actor supervisor. * * @param actor Actor that raised the exception * @param exception Exception that raised the actor * @param message Message that we failed to process */ supervise(actor: Actor, exception: any, message: any): SupervisionStrategies; /** * Schedules a message to this actor every {interval} milliseconds. Returns a cancellable, that * can be passed to #cancel to stop the scheduled message. * * @param interval Interval, in ms, between messages * @param fn Message to send to this current actor, in form of a method reference (like this.myMethod) * @param values Parameters to pass with the message (parameters of the method to call) * * @see Actor#cancel */ protected schedule(interval: number, fn: (...args: any[]) => void, values: any[]): Cancellable; /** * Schedules a message to this actor once, after {timeout} milliseconds. Returns a cancellable, that * can be passed to #cancel to stop the scheduled message. * * @param timeout Time to send the message, in ms * @param fn Message to send to this current actor, in form of a method reference (like this.myMethod) * @param values Parameters to pass with the message (parameters of the method to call) * * @see Actor#cancel */ protected scheduleOnce(timeout: number, fn: (...args: any[]) => void, values: any[]): Cancellable; /** * Cancels a scheduled action created by #schedule or #scheduleOnce * * @param cancellable Cancellable reference * @see Actor#schedule * @see Actor#scheduleOnce */ protected cancel(cancellable: Cancellable): void; /** * Creates a child actor of this actor. The current actor will behave as the supervisor * of the created actor. * * @param classFn Constructor of the actor to build * @param values Values to pass as the constructor parameters */ protected actorOf(classFn: new (...args: any[]) => T, values: any[]): T; protected subscribeToTopic(topic: Topic): void; protected unsubscribeFromTopic(topic: Topic): void; private dispatchAndPromisify; private initialized; } export {};