eonarheim / TypeState

A strongly typed finite state machine for TypeScript
http://eonarheim.github.io/TypeState/example/
BSD 2-Clause "Simplified" License
269 stars 28 forks source link

Support async transition #30

Closed alex-knyazev closed 5 years ago

alex-knyazev commented 5 years ago

It is very good library, but i need to have more powrful control on function:

onEnter(state: T, callback: (from?: T, event?: any) => boolean): FiniteStateMachine<T>;

It is very good to be:

type SyncFunction = callback: (from?: T, event?: any) => boolean 
Type AsyncFunction =  callback: (from?: T, event?: any) => Promise<boolean> 
onEnter(state: T, callback: SyncFunction | AsyncFunction  ): FiniteStateMachine<T>
stefnotch commented 5 years ago

So, in the case of an async transition, it shouldn't enter/exit the state until the promise finishes?

Would async functions also make sense here and what should their behaviour be?

on(state: T, callback: (from?: T, event?: any) => any): FiniteStateMachine<T> 

I would be willing to implement it and create a PR.

alex-knyazev commented 5 years ago

@stefnotch
I think in case of onEnter promise must be resolved to go to new state. But in case of 'on' it isn;t required

eonarheim commented 5 years ago

@stefnotch Async transitions would be an excellent addition! I would gladly accept such a PR!

@alex-knyazev Agreed, I would expect onEnter to require the promise to resolve before entering the new state. on() does not limit state transition so fire and forget is okay there.