Skip to content

ModelEventBus

ModelEventBus

ModelEventBus()

Per-model publish/subscribe bus owned by a Model.

A Model creates one ModelEventBus and exposes it through model.events. Managers and advanced components use that bus to coordinate lifecycle work without tightly coupling themselves to one another.

Callbacks are registered per FemoraEvent and are invoked with keyword-argument payload data supplied by the emitter.

Example
from femora.core.model import Model
from femora.components.event.event_bus import FemoraEvent

model = Model()

def on_post_assemble(**payload):
    assembled_mesh = payload.get("assembled_mesh")
    if assembled_mesh is not None:
        print(assembled_mesh.n_points)

model.events.subscribe(FemoraEvent.POST_ASSEMBLE, on_post_assemble)

Initialize an empty per-model subscriber registry.

Attributes

_subscribers instance-attribute

_subscribers: Dict[FemoraEvent, List[Callable]] = defaultdict(list)

Methods:

subscribe

subscribe(event: FemoraEvent, callback: Callable) -> None

Register a callback for one lifecycle event.

If the callback is already subscribed to the same event, it is not added a second time.

Parameters:

Name Type Description Default
event FemoraEvent

Lifecycle event to listen for.

required
callback Callable

Callable invoked when event is emitted. The callback receives keyword arguments from the emitter.

required

unsubscribe

unsubscribe(event: FemoraEvent, callback: Callable) -> None

Remove a previously registered callback from one event.

Parameters:

Name Type Description Default
event FemoraEvent

Lifecycle event previously used during subscription.

required
callback Callable

Callback to remove.

required

emit

emit(event: FemoraEvent, **payload) -> None

Dispatch one lifecycle event to its current subscribers.

The bus iterates over a snapshot of the subscriber list so callbacks can safely unsubscribe or adjust registrations during dispatch without corrupting the iteration order.

Parameters:

Name Type Description Default
event FemoraEvent

Lifecycle event being emitted.

required
**payload

Keyword arguments forwarded directly to each callback. Emitters commonly provide objects such as assembled_mesh.

{}

clear

clear() -> None

Remove all subscribers from this bus instance.