import { GrpcClientLookup } from './grpc-util'; import { Entity, EntityOptions, PreStartSettings } from './kalix'; import { Serializable } from './serializable'; import { EntityCommandContext, CommandReply } from './command'; /** @public */ export declare namespace ValueEntity { /** * Context for a value entity command. * * @typeParam State - the type of {@link Serializable} state */ type ValueEntityCommandContext = CommandContext; /** * Context for a value entity command. * * @typeParam State - the type of {@link Serializable} state */ interface CommandContext extends EntityCommandContext { /** * Persist the updated state. * * @remarks * * The state won't be persisted until the reply is sent to the proxy. Then, the state will be persisted * before the reply is sent back to the client. * * @param newState - The state to store */ updateState(newState: State): void; /** * Delete this entity. */ deleteState(): void; } /** * A command handler for one service call to the value entity. * * @typeParam State - the type of {@link Serializable} state * @param command - The command message, this will be of the type of the gRPC service call input type * @param state - The entity state * @param context - The command context * @returns The message to reply with, it must match the gRPC service call output type for this * command, or if a Reply is returned, contain an object that matches the output type. */ type CommandHandler = (command: any, state: State, context: CommandContext) => Promise | CommandReply; /** * Value entity command handlers. * * @remarks * The names of the properties must match the names of the service calls specified in the gRPC * descriptor for this value entity's service. * * @typeParam State - the type of {@link Serializable} state */ type CommandHandlers = { [commandName: string]: CommandHandler; }; /** * Initial state callback. * * @remarks * This is invoked if the entity is started with no snapshot. * * @typeParam State - the type of {@link Serializable} state * @param entityId - The entity id * @returns The entity state */ type InitialCallback = (entityId: string) => State; /** * Options for a value entity. */ interface Options extends Omit { /** * Whether serialization of primitives should be supported when serializing the state. * * @defaultValue `false` */ serializeAllowPrimitives?: boolean; /** * Whether serialization should fallback to using JSON if the state can't be serialized as a protobuf. * * @defaultValue `false` */ serializeFallbackToJson?: boolean; } } /** * Value Entity. * * @typeParam State - the type of {@link Serializable} state * @typeParam CommandHandlers - optional type of CommandHandlers * * @public */ export declare class ValueEntity = ValueEntity.CommandHandlers> implements Entity { readonly serviceName: string; readonly service: protobuf.Service; readonly options: Required; clients?: GrpcClientLookup; /** * The initial state callback. */ initial?: ValueEntity.InitialCallback; /** * The command handlers. */ commandHandlers: CommandHandlers; /** * Create a new value entity. * * @remarks * Never change the entityType after deploying a service that stored data of this type. * * @param desc - A descriptor or list of descriptors to parse, containing the service to serve * @param serviceName - The fully qualified name of the service that provides this entities interface * @param entityType - The entity type name for all value entities of this type * @param options - The options for this entity */ constructor(desc: string | string[], serviceName: string, entityType: string, options?: ValueEntity.Options); preStart(settings: PreStartSettings): void; lookupType(messageType: string): protobuf.Type; /** * Set the initial state callback. * * @param callback - The initial state callback * @returns This entity */ setInitial(callback: ValueEntity.InitialCallback): ValueEntity; /** * Set the command handlers of the entity. * * @param commandHandlers - The command handler callbacks * @returns This entity */ setCommandHandlers(commandHandlers: CommandHandlers): ValueEntity; }