Interface for FSM storage for the dispatcher.

All of the officially supported storages already implement this interface, so you can just re-use it.

Current scene is a special case of a string state, Most of the time you can just store it the same way as normal state, prefixing with something like $current_state_ (scene name can't start with $). Alternatively, you can store them as simple strings

interface IStateRepository {
    deleteState: ((key: string) => MaybePromise<void>);
    getRateLimit: ((key: string, now: number, limit: number, window: number) => MaybePromise<[number, number]>);
    getState: ((key: string, now: number) => MaybePromise<null | string>);
    resetRateLimit: ((key: string) => MaybePromise<void>);
    setState: ((key: string, state: string, ttl?: number) => MaybePromise<void>);
    vacuum: ((now: number) => MaybePromise<void>);
}

Properties

deleteState: ((key: string) => MaybePromise<void>)

Delete state from the storage

Type declaration

getRateLimit: ((key: string, now: number, limit: number, window: number) => MaybePromise<[number, number]>)

Get information about a rate limit.

It is recommended that you use sliding window or leaky bucket to implement rate limiting (learn more),

Type declaration

    • (key, now, limit, window): MaybePromise<[number, number]>
    • Parameters

      • key: string

        Key of the rate limit

      • now: number

        Current unix time in ms

      • limit: number

        Maximum number of requests in window

      • window: number

        Window size in seconds

      Returns MaybePromise<[number, number]>

      Tuple containing the number of remaining and unix time in ms when the user can try again

getState: ((key: string, now: number) => MaybePromise<null | string>)

Retrieve state from the storage If state is not found or has expired, return null

Type declaration

resetRateLimit: ((key: string) => MaybePromise<void>)

Reset a rate limit.

Type declaration

setState: ((key: string, state: string, ttl?: number) => MaybePromise<void>)

Save state to the storage

Type declaration

    • (key, state, ttl?): MaybePromise<void>
    • Parameters

      • key: string

        Key of the state, as defined by StateKeyDelegate

      • state: string

        String representing the state

      • Optionalttl: number

        TTL for the state, in seconds

      Returns MaybePromise<void>

vacuum: ((now: number) => MaybePromise<void>)

Clean up expired states and rate limits.

Type declaration