piotrmurach / finite_machine

A minimal finite state machine with a straightforward syntax.
https://piotrmurach.github.io/finite_machine/
MIT License
808 stars 38 forks source link

Remove on_transition callback #44

Open piotrmurach opened 8 years ago

piotrmurach commented 8 years ago

@craiglittle I feel as though we should drop the on_transition callback, I haven't used it myself and don't really see a reason any more for its existence?

craiglittle commented 8 years ago

I use on_transition in our app, but I don't see any reason why on_enter can't work in its stead.

What was your reasoning for including on_transition in the first place?

In any case, if you want to remove it, it'd probably make sense at v2.0.0.

piotrmurach commented 8 years ago

The semantics of on_transition sounded good, and explained probably well the intention. However, looking at the trigger implementation, the on_transiton is equivalent to on_enter, hence thought I will just drop it. Keep the codebase to minimum. The finite_machine hasn't reached v1.0.0 yet :)

craiglittle commented 8 years ago

Er, yeah, I meant v1.0.0 (or, I guess, v0.12.0). :)

dmgr commented 7 years ago

Actually I think on_transition callback is quite useful, but limited due to its lack of recognition of from and to states. It can be a lot more useful if it would let you specify from and to states like that:

on_transition :started => :stopped do |event| ...
on_transition any => :broken do |event| ... # `any` state as a singleton object (see below)
on_transition %i[stopped broken] => :started do |event| ...

Currently I use the if conditional to test from/to states:

on_transition do |event|
  if event.from == :started && event.to == :stopped ...

# or:

on_transition :stopped do |event|
  if event.from == :started ...

The other thing is that internally any state shouldn't be a symbol, but a unique singleton object, to never collide with any user defined states:

any = String.new('any').frozen
# or:
any = 'any'.frozen # it generates unique object, but it may change in the future Ruby releases
# or:
any = String('any').frozen
piotrmurach commented 6 years ago

@dmgr I've released v0.12.0 which includes any_state to prevent any clashes with :any keys. Your idea about on_transition to allow from states is intriguing but I'm kind of worried that it will exponentially increase internal complexity for the storage of callbacks.