Class: Teek::RactorStream

Inherits:
Object
  • Object
show all
Defined in:
lib/teek/ractor_support.rb

Overview

Simplified streaming API without pause/resume support. Uses Ractor on Ruby 4.x+, falls back to threads on 3.x.

Examples:

Teek::RactorStream.new(app, files) do |yielder, data|
  data.each { |f| yielder.yield(process(f)) }
end.on_progress { |r| update_ui(r) }
   .on_done { puts "Done!" }

Defined Under Namespace

Classes: StreamYielder

Instance Method Summary collapse

Constructor Details

#initialize(app, data, &block) ⇒ RactorStream

Returns a new instance of RactorStream.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/teek/ractor_support.rb', line 202

def initialize(app, data, &block)
  # Ruby 4.x: use Ractor with shareable_proc for true parallelism
  # Ruby 3.x: use threads (Ractor mode not supported)
  if BackgroundWork::RACTOR_SUPPORTED
    shareable_block = Ractor.shareable_proc(&block)
    wrapped_block = Ractor.shareable_proc do |task, d|
      yielder = StreamYielder.new(task)
      shareable_block.call(yielder, d)
    end
    @impl = Teek::BackgroundRactor4x::BackgroundWork.new(app, data, &wrapped_block)
  else
    wrapped_block = proc do |task, d|
      yielder = StreamYielder.new(task)
      block.call(yielder, d)
    end
    @impl = Teek::BackgroundThread::BackgroundWork.new(app, data, &wrapped_block)
  end
end

Instance Method Details

#cancelObject



231
232
233
# File 'lib/teek/ractor_support.rb', line 231

def cancel
  @impl.stop
end

#on_done(&block) ⇒ Object



226
227
228
229
# File 'lib/teek/ractor_support.rb', line 226

def on_done(&block)
  @impl.on_done(&block)
  self
end

#on_progress(&block) ⇒ Object



221
222
223
224
# File 'lib/teek/ractor_support.rb', line 221

def on_progress(&block)
  @impl.on_progress(&block)
  self
end