TypeState icon indicating copy to clipboard operation
TypeState copied to clipboard

State Machine Timeout

Open stefnotch opened this issue 6 years ago • 0 comments

Timeouts are reasonably common in state machines, however manually implementing them requires quite a number of lines of code. It might be nice if this library had built-in support for them.

Maybe something along the lines of:

public stateTimeout<U extends keyof T>(state: U, to: keyof T, timeout: number) {
         this.on(state, (from, context) => {
            (<any>context).timeout = setTimeout(() => {
              this.go(to);
            }, timeout);
          });
        
          this.onExit(state, (from, context) => {
            if ((<any>context).timeout) {
              clearTimeout((<any>context).timeout);
              (<any>context).timeout = 0;
            }
            return true;
          });
      }

stefnotch avatar Mar 10 '19 08:03 stefnotch