Module: RobotLab::Web::EventSink
- Defined in:
- lib/robot_lab/web/event_sink.rb
Overview
The bridge between robot_lab's synchronous hook dispatch and a consumer (an SSE stream, a collecting array, anything responding to #call).
robot_lab hooks are class-level singletons, so there is no instance on which to hang a per-connection callback. We instead stash the consumer in a thread-local for the duration of a run. Because Robot#run dispatches its hooks synchronously on the calling thread, the StreamHook sees the right sink. (Caveat: a robot that executes tools on other threads/Ractors would not propagate this — fine for the core synchronous path.)
In effect this gives a robot run an on_event: callback without changing
robot_lab's API.
Constant Summary collapse
- KEY =
:robot_lab_web_event_sink
Class Method Summary collapse
-
.capture(sink) ⇒ Object
Run the block with
sink(any callable taking an Event) installed as the current consumer. - .current ⇒ Object
-
.emit(event) ⇒ Object
Deliver an Event to the current sink (if any).
Class Method Details
.capture(sink) ⇒ Object
Run the block with sink (any callable taking an Event) installed as
the current consumer. Restores the previous sink afterward.
24 25 26 27 28 29 30 |
# File 'lib/robot_lab/web/event_sink.rb', line 24 def capture(sink) previous = Thread.current[KEY] Thread.current[KEY] = sink yield ensure Thread.current[KEY] = previous end |
.current ⇒ Object
32 33 34 |
# File 'lib/robot_lab/web/event_sink.rb', line 32 def current Thread.current[KEY] end |
.emit(event) ⇒ Object
Deliver an Event to the current sink (if any). Never raises into the run — a broken consumer must not break the robot.
38 39 40 41 42 43 |
# File 'lib/robot_lab/web/event_sink.rb', line 38 def emit(event) sink = current sink&.call(event) rescue StandardError nil end |