@rivetkit/core
Version:
157 lines (140 loc) • 2.86 kB
text/typescript
import type { ActorKey } from "@/actor/mod";
import type { Client } from "@/client/client";
import type { Logger } from "@/common/log";
import type { Registry } from "@/registry/mod";
import type { Conn, ConnId } from "./connection";
import type { AnyDatabaseProvider, InferDatabaseClient } from "./database";
import type { ActorInstance, SaveStateOptions } from "./instance";
import type { Schedule } from "./schedule";
/**
* ActorContext class that provides access to actor methods and state
*/
export class ActorContext<
TState,
TConnParams,
TConnState,
TVars,
TInput,
TAuthData,
TDatabase extends AnyDatabaseProvider,
> {
TState,
TConnParams,
TConnState,
TVars,
TInput,
TAuthData,
TDatabase
>;
constructor(
actor: ActorInstance<
TState,
TConnParams,
TConnState,
TVars,
TInput,
TAuthData,
TDatabase
>,
) {
this.
}
/**
* Get the actor state
*/
get state(): TState {
return this.
}
/**
* Get the actor variables
*/
get vars(): TVars {
return this.
}
/**
* Broadcasts an event to all connected clients.
* @param name - The name of the event.
* @param args - The arguments to send with the event.
*/
broadcast<Args extends Array<unknown>>(name: string, ...args: Args): void {
this.
return;
}
/**
* Gets the logger instance.
*/
get log(): Logger {
return this.
}
/**
* Gets actor ID.
*/
get actorId(): string {
return this.
}
/**
* Gets the actor name.
*/
get name(): string {
return this.
}
/**
* Gets the actor key.
*/
get key(): ActorKey {
return this.
}
/**
* Gets the region.
*/
get region(): string {
return this.
}
/**
* Gets the scheduler.
*/
get schedule(): Schedule {
return this.
}
/**
* Gets the map of connections.
*/
get conns(): Map<
ConnId,
Conn<TState, TConnParams, TConnState, TVars, TInput, TAuthData, TDatabase>
> {
return this.
}
/**
* Returns the client for the given registry.
*/
client<R extends Registry<any>>(): Client<R> {
return this.
}
/**
* Gets the database.
* @experimental
* @throws {DatabaseNotEnabled} If the database is not enabled.
*/
get db(): InferDatabaseClient<TDatabase> {
return this.
}
/**
* Forces the state to get saved.
*
* @param opts - Options for saving the state.
*/
async saveState(opts: SaveStateOptions): Promise<void> {
return this.
}
/**
* Runs a promise in the background.
*
* @param promise - The promise to run in the background.
*/
runInBackground(promise: Promise<void>): void {
this.
return;
}
}