UNPKG

saltfish

Version:

An interactive video-guided tour system for web applications

119 lines 3.15 kB
import type { PlayerState, Step } from '../types'; export type PlayerEvent = { type: 'INITIALIZE'; } | { type: 'LOAD_MANIFEST'; } | { type: 'MANIFEST_LOADED'; step: Step; } | { type: 'PLAY'; } | { type: 'PAUSE'; } | { type: 'MINIMIZE'; } | { type: 'MAXIMIZE'; } | { type: 'VIDEO_ENDED'; step: Step; } | { type: 'TRANSITION_TO_STEP'; step: Step; } | { type: 'WAIT_FOR_INTERACTION'; } | { type: 'INTERACTION_COMPLETED'; } | { type: 'ERROR'; error: Error; } | { type: 'AUTOPLAY_FALLBACK'; } | { type: 'COMPLETE_PLAYLIST'; }; export interface PlayerContext { currentStep: Step | null; error: Error | null; } export type ActionFunction = (context: PlayerContext, event?: PlayerEvent) => void; export type Action = string | ActionFunction; export type StateTransition = { target: PlayerState; actions?: Action[]; }; export type StateConfig = { on: { [key in PlayerEvent['type']]?: StateTransition; }; entry?: Action[]; exit?: Action[]; }; export type StateMachineConfig = { states: { [key in PlayerState]: StateConfig; }; initial: PlayerState; }; export interface ActionHandlers { [actionName: string]: ActionFunction; } export declare class PlayerStateMachine { private currentState; private config; private context; private actionHandlers; constructor(config: StateMachineConfig, initialContext: PlayerContext); /** * Set up default action handlers for common operations */ private setupDefaultActions; /** * Register custom action handlers * @param actions - Object mapping action names to handler functions */ registerActions(actions: ActionHandlers): void; /** * Execute an action (either named or inline function) * @param action - The action to execute * @param event - Optional event that triggered the action */ private executeAction; /** * Send an event to the state machine to trigger a transition * @param event - The event to send * @returns The new state after the transition */ send(event: PlayerEvent): PlayerState; /** * Update context based on the event * @param event - The event to process */ private updateContextFromEvent; /** * Get the current state of the state machine * @returns The current state */ getState(): PlayerState; /** * Get the current context of the state machine * @returns The current context */ getContext(): PlayerContext; /** * Update the context directly (use with caution) * @param updater Function that takes the current context and returns an updated one */ updateContext(updater: (context: PlayerContext) => Partial<PlayerContext>): void; /** * Run entry actions for a state * @param state - The state to run entry actions for */ private runEntryActions; /** * Run exit actions for a state * @param state - The state to run exit actions for */ private runExitActions; } //# sourceMappingURL=stateMachine.d.ts.map