Class: Teek::BackgroundRactor4x::BackgroundWork::TaskContext
- Inherits:
-
Object
- Object
- Teek::BackgroundRactor4x::BackgroundWork::TaskContext
- Defined in:
- lib/teek/background_ractor4x.rb
Overview
Context object passed to the worker block inside the Ractor. Provides methods for yielding results, sending/receiving messages, and responding to pause/stop signals. :nocov: – TaskContext runs exclusively inside a Ractor; invisible to Coverage.so
Instance Method Summary collapse
-
#check_message ⇒ Object?
Non-blocking check for a message from the main thread.
-
#check_pause ⇒ Object
Block while paused, returning immediately if not paused.
-
#initialize(output_port, msg_queue) ⇒ TaskContext
constructor
private
A new instance of TaskContext.
-
#send_message(msg) ⇒ Object
Send a custom message back to the main thread.
-
#wait_message ⇒ Object
Blocking wait for the next message from the main thread.
-
#yield(value) ⇒ Object
Send a result to the main thread.
Constructor Details
#initialize(output_port, msg_queue) ⇒ TaskContext
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of TaskContext.
367 368 369 370 371 |
# File 'lib/teek/background_ractor4x.rb', line 367 def initialize(output_port, msg_queue) @output_port = output_port @msg_queue = msg_queue @paused = false end |
Instance Method Details
#check_message ⇒ Object?
Non-blocking check for a message from the main thread. Handles built-in control messages (:pause, :resume, :stop) automatically; :stop raises StopIteration.
385 386 387 388 389 390 391 392 |
# File 'lib/teek/background_ractor4x.rb', line 385 def return nil if @msg_queue.empty? msg = @msg_queue.pop(true) (msg) msg rescue ThreadError nil end |
#check_pause ⇒ Object
Block while paused, returning immediately if not paused. Call this periodically in long-running loops to honor pause requests.
412 413 414 |
# File 'lib/teek/background_ractor4x.rb', line 412 def check_pause check_pause_loop end |
#send_message(msg) ⇒ Object
Send a custom message back to the main thread. Arrives in the Teek::BackgroundRactor4x::BackgroundWork#on_message callback.
406 407 408 |
# File 'lib/teek/background_ractor4x.rb', line 406 def (msg) @output_port.send([:message, msg]) end |
#wait_message ⇒ Object
Blocking wait for the next message from the main thread. Handles control messages automatically.
397 398 399 400 401 |
# File 'lib/teek/background_ractor4x.rb', line 397 def msg = @msg_queue.pop (msg) msg end |
#yield(value) ⇒ Object
Send a result to the main thread. Blocks while paused. The value arrives in the Teek::BackgroundRactor4x::BackgroundWork#on_progress callback.
376 377 378 379 |
# File 'lib/teek/background_ractor4x.rb', line 376 def yield(value) check_pause_loop @output_port.send([:result, value]) end |