Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global event bus #1051

Merged
merged 6 commits into from
Aug 8, 2024
Merged

Add global event bus #1051

merged 6 commits into from
Aug 8, 2024

Conversation

collindutter
Copy link
Member

@collindutter collindutter commented Aug 7, 2024

Describe your changes

Added

  • Global event bus, griptape.events.EventBus, for publishing and subscribing to events.

Changed

  • BREAKING: Removed all uses of EventPublisherMixin in favor of EventBus.
  • BREAKING: Removed EventPublisherMixin.

A couple things I'm unsure about:

  1. Should this be a singleton or should we have a namespaced factory method similar to python's logging module? Something like:
def get_event_bus(namespace: str = DEFAULT_NAMESPACE) ...

I can't think of any scenario where you'd want multiple busses, especially since you can pass in many EventListeners.
2. If we stick with a singleton, does the PascalCase naming of the variable make sense? The idea was that it might convey the global nature of it, similar to how you might access static fields.

Issue ticket number and link

NA


📚 Documentation preview 📚: https://griptape--1051.org.readthedocs.build//1051/

Copy link

codecov bot commented Aug 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

Copy link
Contributor

@dylanholmes dylanholmes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the direction!

The changes look functionally good, but I'd like to chat before approving.

About the multiple buses scenario -- I can't really imagine a use case, would events get published to all of them or just some of them (if some of them, how would the publisher decide which)? If you just want to listen to a subset of events, the event listener should be able to filter out events it doesn't care about.

@collindutter
Copy link
Member Author

@dylanholmes what do you think about dropping the event portion on all methods/fields in EventBus? EventBus.add_listener, for example.

class EventPublisherMixin:
event_listeners: list[EventListener] = field(factory=list, kw_only=True)
class _EventBus:
_event_listeners: list[EventListener] = field(factory=list, kw_only=True, alias="_event_listeners")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is uglier than just not using attrs imo. Plus it still adds _event_listeners as an keyword arg in init.

I don't care that much though

@dylanholmes
Copy link
Contributor

@dylanholmes what do you think about dropping the event portion on all methods/fields in EventBus? EventBus.add_listener, for example.

Not sure, I kind of like add_event_listener, but maybe I'm brainwashed by javascript in the browser. I don't really care either way honestly though. Another alternative could be EventBus.subscribe(...).

This seems like a good opportunity to mention another "syntax golfing" idea I had that I wasn't going to share publicly. Accept the EventListener init params directly in the method to avoid the boilerplate (...EventListener(...)...)

event_bus.add_event_listener(my_callback, event_types=[...])

@collindutter
Copy link
Member Author

collindutter commented Aug 8, 2024

Not sure, I kind of like add_event_listener

Let's just leave it as is.

Accept the EventListener init params directly in the method to avoid the boilerplate

I like the conciseness, but I'd rather introduce even more breaking changes at this point. Let's evaluate in the future.

Copy link
Contributor

@dylanholmes dylanholmes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

Just one optional suggestion

Comment on lines +22 to 24
event_bus.add_event_listeners(
[
EventListener(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be plural? Can there be a singular add_event_listener too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not _need_to be plural, no. Let's just leave it as is, docs will get an overhaul at some point soon.

@collindutter collindutter merged commit 443cf29 into dev Aug 8, 2024
15 of 16 checks passed
@collindutter collindutter deleted the feature/event-bus branch August 8, 2024 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants