|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Ruby Engine Compatibility Check |
| 4 | +# The async gem requires native extensions and Fiber scheduler support |
| 5 | +# which are not available on JRuby or TruffleRuby |
| 6 | +if RUBY_ENGINE == 'jruby' || RUBY_ENGINE == 'truffleruby' |
| 7 | + raise LoadError, <<~ERROR |
| 8 | + StateMachines::AsyncMode is not available on #{RUBY_ENGINE}. |
| 9 | +
|
| 10 | + The async gem requires native extensions (io-event) and Fiber scheduler support |
| 11 | + which are not implemented in #{RUBY_ENGINE}. AsyncMode is only supported on: |
| 12 | +
|
| 13 | + • MRI Ruby (CRuby) 3.2+ |
| 14 | + • Other Ruby engines with full Fiber scheduler support |
| 15 | +
|
| 16 | + If you need async support on #{RUBY_ENGINE}, consider using: |
| 17 | + • java.util.concurrent classes (JRuby) |
| 18 | + • Native threading libraries for your platform |
| 19 | + • Or stick with synchronous state machines |
| 20 | + ERROR |
| 21 | +end |
| 22 | + |
| 23 | +# Load required gems with version constraints |
| 24 | +gem 'async', '>= 2.25.0' |
| 25 | +gem 'concurrent-ruby', '>= 1.3.5' # Security is not negotiable - enterprise-grade thread safety required |
| 26 | + |
| 27 | +require 'async' |
| 28 | +require 'concurrent-ruby' |
| 29 | + |
| 30 | +# Load all async mode components |
| 31 | +require_relative 'async_mode/thread_safe_state' |
| 32 | +require_relative 'async_mode/async_events' |
| 33 | +require_relative 'async_mode/async_event_extensions' |
| 34 | +require_relative 'async_mode/async_machine' |
| 35 | +require_relative 'async_mode/async_transition_collection' |
| 36 | + |
| 37 | +module StateMachines |
| 38 | + # AsyncMode provides asynchronous state machine capabilities using the async gem |
| 39 | + # This module enables concurrent, thread-safe state operations for high-performance applications |
| 40 | + # |
| 41 | + # @example Basic usage |
| 42 | + # class AutonomousDrone < StarfleetShip |
| 43 | + # state_machine :teleporter_status, async: true do |
| 44 | + # event :power_up do |
| 45 | + # transition offline: :charging |
| 46 | + # end |
| 47 | + # end |
| 48 | + # end |
| 49 | + # |
| 50 | + # drone = AutonomousDrone.new |
| 51 | + # Async do |
| 52 | + # result = drone.fire_event_async(:power_up) # => true |
| 53 | + # task = drone.power_up_async! # => Async::Task |
| 54 | + # end |
| 55 | + # |
| 56 | + module AsyncMode |
| 57 | + # All components are loaded from separate files: |
| 58 | + # - ThreadSafeState: Mutex-based thread safety |
| 59 | + # - AsyncEvents: Async event firing methods |
| 60 | + # - AsyncEventExtensions: Event method generation |
| 61 | + # - AsyncMachine: Machine-level async capabilities |
| 62 | + # - AsyncTransitionCollection: Concurrent transition execution |
| 63 | + end |
| 64 | +end |
0 commit comments