Textualize/textual

Allow `Signal.publish` to include some data

Closed this issue · 1 comments

Right now, when a Signal is published, there's no data attached to it.

Subscribers are informed that the publish happened, but it's up to them to go off and fetch any associated data.

Often that data is associated with the node that published it, so the subscriber is having to know how to access data from the publisher and is coupled to it. Ideally we'd avoid this coupling as it's one of the main benefits of a pubsub system.

It'd be nice to offer a means for publish to an optional extra piece of data to the subscribers callback.

To retain typing, Signal would become generic.

Example:

# Creation of a signal
config_changed_signal = Signal[Config](self, "config-changed")

# Publishing that signal
config_changed_signal.publish(Config(a=1, b=2))

# A callback to run when the signal is published
def callback(config: Config) -> None:
    """Does something with the Config object"""

# Subscribing and passing in the callback
config_changed_signal.subscribe(self, callback)

Don't forget to star the repository!

Follow @textualizeio for Textual updates.