Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] define fluent transitions with junction support for multiple paths and also fluent transitions itself #14

Open
spali opened this issue Nov 21, 2016 · 0 comments

Comments

@spali
Copy link

spali commented Nov 21, 2016

I adapted your code to support a second level of states but stripped some other stuff out which I don't need, so this is why I don't just send a complete code of mine.
But I tried to extract the important part, with it you should be able to introduce this feature without a lot of work.
The only problem is, because I stripped it down to only allow to provide a single state per transition, I don't know how easy it is to adapt it for multiple states. But even you get it, I think due the advantage of the incredible nice reading code by fluent chaining multiple transitions with junctions , would go away if you use this feature with multiple states. I didn't get it to be readable and always clear which transitions are possible with multiple states per transition.
Probably you provide an separate Transition interface and API for single fluent definition to keep your API backward compatible.

But with single states it makes really fun to read the flow of the states as defined.
And yes, due your duplicate check in the code, you can just for fun define duplicate transitions to keep the flow nice and easy to red.

i.e.

fsm.from(State.disconnected)
  .to(State.connecting)
  .to(State.connected)
  .to(State.disconnecting)
  .toMulti(
    from => from
      .to(State.autoReconnect)
      .to(State.connecting)
      .to(State.connected)
   ,from => from
      .to(State.disconnected)
   .....
   );
export class Transitions<T> {
...
  /**
   * Specify the end state(s) of a transition function
   */
  public to(state: T): Transitions<T> {
    this.toState = state;
    this.fsm.addTransitions(this);
    return new Transitions<T>(this.fsm, this.toState);
  }

  /**
   * allows to fluent define a states switch transition to multiple allowed states from a single source state.
   * example:
   * fsm.from(State1)
   *   .toMulti(
   *    from => from.to(State1A).to ...
   *   ,from => from.to(State1B).to ...
   *   )
   */
  public toMulti(...from: ((from: Transitions<T>) => Transitions<T>)[]): Transitions<T> {
    from.forEach(fnc => {
      var x = new Transitions<T>(this.fsm, this.fromState);
      fnc.call(x, x);
    });
    return this;
  }

}

second:

just add a return this in the go() method allows to also fluently switch states like:

// connect
fsm
  .go(State.connecting)
  .go(State.connected);

// disconnect
fsm
  .go(State.disconnecting)
  .go(State.disconnected);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant