Module: StateMachines::AsyncMode::AsyncMachine
- Defined in:
- lib/state_machines/async_mode/async_machine.rb
Overview
Enhanced machine class with async capabilities
Instance Method Summary collapse
-
#async_fire_event(object, event_name, *args) ⇒ Object
Fires an event asynchronously on the given object Returns an Async::Task for concurrent execution.
-
#create_async_transition_collection(transitions, options = {}) ⇒ Object
Creates an async-aware transition collection Supports concurrent transition execution with proper synchronization.
-
#read_safely(object, attribute, ivar = false) ⇒ Object
Thread-safe state reading for machines.
-
#run_callbacks_safely(type, object, context, transition) ⇒ Object
Thread-safe callback execution for async operations.
-
#write_safely(object, attribute, value, ivar = false) ⇒ Object
Thread-safe state writing for machines.
Instance Method Details
#async_fire_event(object, event_name, *args) ⇒ Object
Fires an event asynchronously on the given object Returns an Async::Task for concurrent execution
27 28 29 30 31 32 33 34 35 |
# File 'lib/state_machines/async_mode/async_machine.rb', line 27 def async_fire_event(object, event_name, *args) unless defined?(::Async::Task) && ::Async::Task.current? raise RuntimeError, "async_fire_event must be called within an Async context" end Async do events[event_name].fire(object, *args) end end |
#create_async_transition_collection(transitions, options = {}) ⇒ Object
Creates an async-aware transition collection Supports concurrent transition execution with proper synchronization
39 40 41 42 43 44 45 46 |
# File 'lib/state_machines/async_mode/async_machine.rb', line 39 def create_async_transition_collection(transitions, = {}) if defined?(AsyncTransitionCollection) AsyncTransitionCollection.new(transitions, ) else # Fallback to regular collection if async collection isn't available TransitionCollection.new(transitions, ) end end |
#read_safely(object, attribute, ivar = false) ⇒ Object
Thread-safe state reading for machines
8 9 10 11 12 13 14 |
# File 'lib/state_machines/async_mode/async_machine.rb', line 8 def read_safely(object, attribute, ivar = false) if object.respond_to?(:read_state_safely) object.read_state_safely(self, attribute, ivar) else read(object, attribute, ivar) end end |
#run_callbacks_safely(type, object, context, transition) ⇒ Object
Thread-safe callback execution for async operations
49 50 51 52 53 54 55 56 57 |
# File 'lib/state_machines/async_mode/async_machine.rb', line 49 def run_callbacks_safely(type, object, context, transition) if object.respond_to?(:state_machine_mutex) object.state_machine_mutex.with_read_lock do callbacks[type].each { |callback| callback.call(object, context, transition) } end else callbacks[type].each { |callback| callback.call(object, context, transition) } end end |
#write_safely(object, attribute, value, ivar = false) ⇒ Object
Thread-safe state writing for machines
17 18 19 20 21 22 23 |
# File 'lib/state_machines/async_mode/async_machine.rb', line 17 def write_safely(object, attribute, value, ivar = false) if object.respond_to?(:write_state_safely) object.write_state_safely(self, attribute, value, ivar) else write(object, attribute, value, ivar) end end |