Events and publish-subscribe

Application modules can interact by publishing and subscribing to messages. Events can be published using a TransactionContext, and received by an event handler. There can be multiple handlers bound to a single event. If the application is receiving an event from an external source, it should be processed using Application.publish():

from lato import Application, ApplicationModule, Event, Command, TransactionContext

class SampleCommand(Command):

class FooHappened(Event):
    source: str

foo_module = ApplicationModule(name="foo")
def call_foo(command: SampleCommand, ctx: TransactionContext):
    print("handling foo")

bar_module = ApplicationModule(name="bar")
def on_foo_happened(event: FooHappened):
    print(f"handling event from {event.source}")

foobar = Application()

foobar.publish(FooHappened(source="external source"))

And the output is:

handling foo
handling event from foo
handling event from external source