Module: Solana::Ruby::Kit::Subscribable

Extended by:
T::Sig
Defined in:
lib/solana/ruby/kit/subscribable.rb,
lib/solana/ruby/kit/subscribable/async_iterable.rb,
lib/solana/ruby/kit/subscribable/data_publisher.rb,
lib/solana/ruby/kit/subscribable/reactive_action_store.rb,
lib/solana/ruby/kit/subscribable/reactive_stream_store.rb,
lib/solana/ruby/kit/subscribable/bridge_store_to_async_iterable.rb

Defined Under Namespace

Modules: AsyncIterable Classes: DataPublisher, ReactiveActionState, ReactiveActionStore, ReactiveState, ReactiveStreamStore, RetryableReactiveStreamStore

Constant Summary collapse

ReactiveActionStatus =

Lifecycle status of a ReactiveActionStore. Mirrors TypeScript's ReactiveActionStatus.

T.type_alias { String }
REACTIVE_ACTION_IDLE_STATE =
T.let(
  ReactiveActionState.new(status: 'idle').freeze,
  ReactiveActionState
)
REACTIVE_LOADING_STATE =
T.let(
  ReactiveState.new(status: 'loading').freeze,
  ReactiveState
)
ReactiveStore =

Deprecated alias — use ReactiveStreamStore.

ReactiveStreamStore

Class Method Summary collapse

Class Method Details

.bridge_store_to_async_iterable(store, signal: nil, should_yield: nil, poll_interval: 0.05) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/solana/ruby/kit/subscribable/bridge_store_to_async_iterable.rb', line 79

def bridge_store_to_async_iterable(store, signal: nil, should_yield: nil, poll_interval: 0.05)
  Enumerator.new do |yielder|
    mutex = Mutex.new
    cond  = ConditionVariable.new
    # Latest-wins single-slot buffer. Both are one-element arrays used as
    # "present or nil" boxes so a legitimately-nil value is distinguishable
    # from "nothing buffered".
    latest  = T.let(nil, T.nilable(T::Array[T.untyped]))
    failure = T.let(nil, T.nilable(T::Array[T.untyped]))

    on_change = Kernel.lambda do
      state = store.get_unified_state
      case state.status
      when 'loaded'
        # Drop a value the gate rejects; park again rather than yielding it.
        next if should_yield && !should_yield.call(state.data)
        mutex.synchronize do
          latest = [state.data]
          cond.broadcast
        end
      when 'error'
        # A nil error would otherwise surface as a value-less success;
        # substitute a sentinel so the failure propagates.
        err = state.error || SolanaError.new(SolanaError::SUBSCRIBABLE__STREAM_CLOSED_WITHOUT_ERROR)
        mutex.synchronize do
          failure = [err]
          cond.broadcast
        end
      end
    end

    aborted = Kernel.lambda do
      next false if signal.nil?

      begin
        signal.call
        false
      rescue StandardError
        true
      end
    end

    unsubscribe = store.subscribe(&on_change)
    # Seed from the store's current snapshot: the caller may already have
    # connected (and a value or error may already be present) before
    # iteration began. The bridge never connects the store itself.
    on_change.call

    begin
      Kernel.loop do
        # Abort wins over everything: end cleanly without raising.
        break if aborted.call

        action  = T.let(:park, Symbol)
        payload = T.let(nil, T.untyped)

        mutex.synchronize do
          if failure
            action  = :raise
            payload = failure[0]
          elsif latest
            action  = :yield
            payload = latest[0]
            latest  = nil
          else
            cond.wait(mutex, signal ? poll_interval : nil)
          end
        end

        case action
        when :yield then yielder.yield(payload)
        when :raise then Kernel.raise(coerce_error(payload))
        end
      end
    ensure
      unsubscribe.call
    end
  end
end

.create_reactive_action_store(&fn) ⇒ Object



203
204
205
# File 'lib/solana/ruby/kit/subscribable/reactive_action_store.rb', line 203

def create_reactive_action_store(&fn)
  ReactiveActionStore.new(T.let(fn, T.proc.params(args: T.untyped).returns(T.untyped)))
end

.create_reactive_store_from_data_publisher(publisher, data_channel:, error_channel:, signal: nil) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/solana/ruby/kit/subscribable/reactive_stream_store.rb', line 216

def create_reactive_store_from_data_publisher(publisher, data_channel:, error_channel:, signal: nil)
  store = ReactiveStreamStore.new

  publisher.on(data_channel, signal: signal) do |data|
    store._set_state(ReactiveState.new(status: 'loaded', data: data))
  end

  publisher.on(error_channel, signal: signal) do |err|
    unified = store.get_unified_state
    next if unified.status == 'error'
    store._set_state(ReactiveState.new(status: 'error', data: unified.data, error: err))
  end

  store
end

.create_reactive_store_from_data_publisher_factory(data_channel:, error_channel:, signal: nil, &blk) ⇒ Object



244
245
246
247
248
249
250
251
252
253
# File 'lib/solana/ruby/kit/subscribable/reactive_stream_store.rb', line 244

def create_reactive_store_from_data_publisher_factory(
  data_channel:, error_channel:, signal: nil, &blk
)
  RetryableReactiveStreamStore.new(
    data_channel:     data_channel,
    error_channel:    error_channel,
    create_publisher: T.let(blk, T.proc.returns(DataPublisher)),
    signal:           signal
  )
end