UNPKG

selenium-state-machine

Version:
180 lines 6.26 kB
import { DependencyMap } from './Dependency'; import { ProvideComplete, ProvideFunction, ProvidePublic } from './Provide'; export interface BaseContext { /** * Remaining time for fsm */ timeout: number; /** * The machine name */ name?: string; } /** * State machine implementation which is capable of recovering stale dependencies. * The first added state is considered as starting state. The last added is finish state. * To add states call a {@link state} method. */ export declare class StateMachine<TContext extends BaseContext, TDependencyMap extends DependencyMap> { private dependencies; /** * State machine context */ private _context; /** * Current state index */ private _i; /** * Number of iterations on state */ private _stateCounter; /** * Map state name => state index */ private _nameMap; /** * Promise which is resolved when state machines is done */ private _promise; /** * Set of reached states */ private _reachedStates; /** * Running flag */ private _running; /** * List of all states */ private _states; /** * Spent time on current state */ private _timeOnState; /** * List of transition callbacks */ private _transitionCallbacks; constructor(context: TContext, dependencies: TDependencyMap); /** * Get context values. Please take in mind timeout will be the same during run. */ get context(): TContext; /** * Update context values. Make sure you are using immutable types. * @param data which will take part in new context */ updateContext(data: Partial<TContext>): void; /** * Get name of current state */ get currentState(): string; /** * Get time spent on current state */ get timeOnCurrentState(): number; /** * Get remaining timeout */ get timeout(): number; /** * Set timeout. Please note this cannot be done when state machine is running. */ set timeout(v: number); /** * Create new timer. Useful when it is not desirable perform WebElement click every state transition. * @param name new name of the timer * @param timeout time after timer will be in state 'elapsed' */ createTimer(name: string | ProvideFunction<never, never>, timeout: number): void; /** * Clear set timer with name. * @param name name of the timer */ clearTimer(name: string | ProvideFunction<never, never>): void; /** * Check if timer is set. * @param name name of the timer in question * @returns boolean signalling availability */ hasTimer(name: string | ProvideFunction<never, never>): boolean; /** * Check if timer has elapsed. * @param name name of the timer in question * @returns boolean signaling its state */ hasElapsedTimer(name: string | ProvideFunction<never, never>): boolean; /** * Add new state * @param state state to be added * @returns self */ private addState; /** * Notify all transition listeners * @returns */ private notify; /** * Register new on transition callback * @param callback function to be called */ onTransition(callback: typeof this._transitionCallbacks[0]): void; /** * Wait until state has been reached. It may return result from past so check {@link currentState} as well. * @param name name of state or function which is called * @param timeout timeout in ms */ waitUntilReached(name: string | ProvideFunction<TContext, TDependencyMap>, timeout?: number): Promise<void>; /** * Add new state to the state machine and infer state name. * @param f State functions which is called each tick. The function must return ProvideComplete object by using given DSL. * In case no dependencies are provides, use provide.nothing() otherwise use provide.dependency(dependencyObject, value). * After that select one of next, previous or transition. If nothing was provides tryAgain is available. Depending on selected option * the state machine will perform transition to next/previous/selected state or repeat itself. * @param timeout * @returns self */ state(f: ((provide: ProvidePublic<TContext, TDependencyMap>, dependencies: TDependencyMap) => Promise<ProvideComplete<TContext, TDependencyMap>> | ProvideComplete<TContext, TDependencyMap>), timeout?: number): this; /** * Add new state to the state machine. * @param name Name of the state * @param f State functions which is called each tick. The function must return ProvideComplete object by using given DSL. * In case no dependencies are provides, use provide.nothing() otherwise use provide.dependency(dependencyObject, value). * After that select one of next, previous or transition. If nothing was provides tryAgain is available. Depending on selected option * the state machine will perform transition to next/previous/selected state or repeat itself. * @param timeout timeout on the state * @returns self */ namedState(name: string, f: ((provide: ProvidePublic<TContext, TDependencyMap>, dependencies: TDependencyMap) => Promise<ProvideComplete<TContext, TDependencyMap>> | ProvideComplete<TContext, TDependencyMap>), timeout?: number): this; /** * Perform transition. * @param i new state index * @returns void */ private changeIndex; /** * Stop the state machine. */ stop(): void; /** * Start the state machine. * @returns promise which resolved when the state machine is on end state */ start(): Promise<void>; /** * Wait until the end state is reached. * @returns */ wait(): Promise<void>; private helperStart; } /** * Declare the state machine dependencies. It is capable of inferring names. * @param dependencies * @returns the same dependencies but with name set as their key */ export declare function declareDependencies<T extends DependencyMap>(dependencies: T): T; //# sourceMappingURL=StateMachine.d.ts.map