import { Temporal } from "@js-temporal/polyfill"; import DaprClient from "../../implementation/Client/DaprClient"; import ActorId from "../ActorId"; import ActorStateManager from "./ActorStateManager"; import StateProvider from "./StateProvider"; /** * Represents the base class for actors. * The base type for actors, that provides the common functionality for actors. * The state is preserved across actor garbage collections and fail-overs. * * Example * * export default interface IDemoCounterActor extends IActor { * increment(amount: number): void; * } * * export default class DemoActorImpl extends AbstractActor implements IDemoActor { * increment(amount: number): void { * throw new Error("Method not implemented."); * } * } */ export default abstract class AbstractActor { private readonly stateManager; private readonly id; private readonly daprClient; private readonly actorClient; private readonly daprStateProvider; private readonly actorType; private readonly logger; /** * Instantiates a new Actor * * @param runtimeContext context for the runtime * @param id actor identifier */ constructor(daprClient: DaprClient, id: ActorId); /** * Registers a reminder for this actor * * Reminders are a mechanism to trigger persistent callbacks on an actor at specified times. * Their functionality is similar to timers. But unlike timers, reminders are triggered under * all circumstances until the actor explicitly unregisters them or the actor is explicitly * deleted. Specifically, reminders are triggered across actor deactivations and failovers * because the Actors runtime persists information about the actor's reminders using actor * state provider. Also existing reminders can be updated by calling this registration method * again using the same reminderName. * * @todo: * https://github.com/dapr/java-sdk/blob/master/sdk-actors/src/main/java/io/dapr/actors/runtime/AbstractActor.java * https://github.com/dapr/python-sdk/blob/46c5664d2e75c20122120dab3be882c4d059a987/dapr/actor/runtime/actor.py#L93 * * @param reminderName name of the reminder * @param state the state to be send along with the reminder trigger * @param dueTime Specifies the time after which the reminder is invoked * @param period Specifies the period between different invocations * @param ttl time to duration after which the reminder will be expired and deleted * @param Type of the state object * @return Async void response */ registerActorReminder<_Type>(reminderName: string, dueTime: Temporal.Duration, period?: Temporal.Duration, ttl?: Temporal.Duration, state?: any): Promise; unregisterActorReminder(reminderName: string): Promise; registerActorTimer(timerName: string, callback: string, dueTime: Temporal.Duration, period?: Temporal.Duration, ttl?: Temporal.Duration, state?: any): Promise; unregisterActorTimer(timerName: string): Promise; /** * Clears all state cache, calls the overridden onActivate and then saves the states * This method gets called when the actor is activated * Note: we require this to save the state so that we know the actor got activated! */ onActivateInternal(): Promise; /** * Clears all state cache and calls the overridden method onDeactivate * This callback is called when an actor is deactivated */ onDeactivateInternal(): Promise; /** * Calls the onActorMethodPre hook on the actor implementation * This gets called just before executing a method */ onActorMethodPreInternal(): Promise; /** * Calls the onActorMethodPost hook on the actor implementation * This gets called just after executing a method * It also persists the state changes of the actor */ onActorMethodPostInternal(): Promise; /** * This will be called when an actor method invocation failed or the actor is activated */ resetStateInternal(): Promise; /** * Saves all the state changes (ADD/UPDATE/REMOVE) that were made since the last call * to the actor state provider associated with the actor */ saveStateInternal(): Promise; /** * This method gets called right after an actor gets activated * and before a method call or reminders are dispatched on it */ onActivate(): Promise; /** * This method gets called right before an actor gets deactivated */ onDeactivate(): Promise; /** * Gets called before executing a method * @returns */ onActorMethodPre(): Promise; /** * Gets called after executing a method * @returns */ onActorMethodPost(): Promise; receiveReminder(_data: string): Promise; getDaprClient(): DaprClient; getStateProvider(): StateProvider; getStateManager(): ActorStateManager; getActorId(): ActorId; getActorType(): any; }