MLightCAD
    Preparing search index...

    Class AcEdPromptStateMachine<TPromptOptions, TResult>

    A lightweight state machine for chaining editor prompts.

    It repeatedly:

    1. Builds a prompt from the current state,
    2. Awaits a result from an injected getResult function,
    3. Lets the state decide whether to continue or finish.

    The state itself can also mutate the machine by calling setState(...) from within handleResult.

    type Prompt = { message: string }
    type Result = { status: 'ok' | 'cancel'; value?: number }

    class FirstState implements AcEdPromptState<Prompt, Result> {
    constructor(private sm: AcEdPromptStateMachine<Prompt, Result>) {}

    buildPrompt() {
    return { message: 'Enter first value:' }
    }

    handleResult(result: Result) {
    if (result.status !== 'ok') return 'finish'
    this.sm.setState(new SecondState(this.sm, result.value ?? 0))
    return 'continue'
    }
    }

    class SecondState implements AcEdPromptState<Prompt, Result> {
    constructor(
    private sm: AcEdPromptStateMachine<Prompt, Result>,
    private firstValue: number
    ) {}

    buildPrompt() {
    return { message: 'Enter second value:' }
    }

    handleResult(result: Result) {
    if (result.status !== 'ok') return 'finish'
    const sum = this.firstValue + (result.value ?? 0)
    console.log('sum', sum)
    return 'finish'
    }
    }

    const sm = new AcEdPromptStateMachine<Prompt, Result>()
    sm.setState(new FirstState(sm))
    await sm.run(async prompt => {
    // Editor interaction happens here:
    return { status: 'ok', value: 1 }
    })

    Type Parameters

    • TPromptOptions
    • TResult
    Index

    Constructors

    Properties

    Methods

    Constructors

    Properties

    The current active state. When undefined, the machine is idle.

    Methods