Skip to content

Commit 9dbfcf2

Browse files
Kasper Jungeclaude
authored andcommitted
refactor: encapsulate emitter composition inside ManagedRun
RunManager.start_run was reaching into ManagedRun._extra_emitters (a private field) to build the fanout emitter. Move this logic into ManagedRun.build_emitter() so the emitter composition is owned by the class that holds the data. Also avoids wrapping a single emitter in FanoutEmitter when there are no extra listeners. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0d7d5f9 commit 9dbfcf2

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

src/ralphify/manager.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ def add_listener(self, emitter: EventEmitter) -> None:
3434
"""Register an additional emitter to receive events from this run."""
3535
self._extra_emitters.append(emitter)
3636

37+
def build_emitter(self) -> EventEmitter:
38+
"""Build the composite emitter for this run.
39+
40+
Returns a :class:`FanoutEmitter` when extra listeners are registered,
41+
or the queue emitter directly when there are none. This keeps the
42+
emitter composition logic inside ``ManagedRun`` so callers don't need
43+
to reach into private fields.
44+
"""
45+
if self._extra_emitters:
46+
return FanoutEmitter([self.emitter, *self._extra_emitters])
47+
return self.emitter
48+
3749

3850
class RunManager:
3951
"""Thread-safe registry for managing concurrent background runs.
@@ -70,15 +82,15 @@ def create_run(self, config: RunConfig) -> ManagedRun:
7082
def start_run(self, run_id: str) -> None:
7183
"""Start the run loop in a daemon thread.
7284
73-
The thread calls :func:`engine.run_loop` with a fanout emitter
74-
that broadcasts events to the run's queue and any extra listeners.
85+
The thread calls :func:`engine.run_loop` with the emitter built
86+
by :meth:`ManagedRun.build_emitter`, which fans out to the queue
87+
and any extra listeners.
7588
"""
7689
managed = self._get_run(run_id)
77-
all_emitters: list[EventEmitter] = [managed.emitter] + managed._extra_emitters
78-
fanout = FanoutEmitter(all_emitters)
90+
emitter = managed.build_emitter()
7991
thread = threading.Thread(
8092
target=run_loop,
81-
args=(managed.config, managed.state, fanout),
93+
args=(managed.config, managed.state, emitter),
8294
daemon=True,
8395
name=f"run-{run_id}",
8496
)

0 commit comments

Comments
 (0)