Comments (7)
Agree, the on_enter
, on_exit
, on_transition
would serve well in the context of states and on_before
and on_after
would feel better in describing event callbacks.
My rational was simply to provide uniform interface and let the code sweat out what the user means, whether on_enter
refers to state or event. However, as you have rightly pointed out this can lead to confusion when state and event are named the same.
from finite_machine.
This is implemented in the master as agreed, I've also updated docs
from finite_machine.
Nice. So now I have the following:
callbacks {
on_exit_state { |event| puts "exit_#{event.from}" }
on_before_event { |event| puts "\tbefore_#{event.name}" }
on_transition_state { |event| puts "\t\ttransition: #{event.name}: #{event.from} -> #{event.to}" }
on_enter_state { |event| puts "\tenter_#{event.to}" }
on_after_event { |event| puts "after_#{event.name}" }
}
Since events and states are now independent (from callback perspective), I guess you could drop the trailing _event
and _state
. This would be the same as an implicit :any
argument which would mean any event or any state dependent on the context. Could that work?
callbacks {
on_exit { |event| puts "exit_#{event.from}" }
on_before { |event| puts "\tbefore_#{event.name}" }
on_transition { |event| puts "\t\ttransition: #{event.name}: #{event.from} -> #{event.to}" }
on_enter { |event| puts "\tenter_#{event.to}" }
on_after { |event| puts "after_#{event.name}" }
}
Mind you if I try that, I get the following InvalidCallbackNameError
exception, which indicates that internally there is still some confusion about states and events (on_exit
argument can only be state but exception mentions 'any' event name).
"on_exit" callback is a state listener and cannot be used with "any" event name. Please use on_before or on_after instead.
from finite_machine.
Very good point - working on it. I've added some validations yesterday to throw InvalidCallbackNameError
for when state callback is used with event name etc.... This kind of works now 😄
from finite_machine.
Seems your callback error checking is a bit too strict now. Because state and event namespace is now separate, you should be able to have a state and an event with the same name.
The following state machine is perfectly reasonable and works when the callbacks are removed.
require 'finite_machine'
phone = FiniteMachine.define do
initial :on_hook
events {
event :off_hook, :on_hook => :off_hook
event :on_hook, :off_hook => :on_hook
}
callbacks {
on_before(:on_hook) { puts "receive on_hook event" }
on_enter(:on_hook) { puts "entering on_hook state" }
}
end
phone.off_hook
phone.on_hook
Event names and state names should be allowed to be the same I think.
from finite_machine.
Fixed on master!
from finite_machine.
Thanks.
from finite_machine.
Related Issues (20)
- Remove on_transition callback HOT 5
- Cancelling inside callbacks HOT 3
- Consider removing the conversion of event names into methods. HOT 1
- Repo description url is broken HOT 1
- Website for the gem is not working HOT 1
- Terminal state HOT 4
- When an error occurs on transition the current state is not "rollback" HOT 2
- https://travis-ci.org/minad/moneta
- Ruby 2.6.0 support (ruby-2.6.0-preview3) HOT 1
- Defining helper methods on machine, not on target HOT 2
- Setting target that responds to to_hash HOT 1
- v0.12.1 missing sync dependency HOT 4
- Java::JavaUtilConcurrent::RejectedExecutionException on jruby HOT 1
- Message Queue Spec sometimes hangs on jruby HOT 2
- alias_target doesn't seem to work HOT 2
- [ Feature Request ] Handling For Multiple State Machines HOT 3
- Unable to define constructors on custom classes HOT 4
- Base class events are not overrided in subclass.
- Errors with Ruby 3.1.2 HOT 2
- Transition does not happen if any_state is used in the definition. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from finite_machine.