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

chore (client): refactoring subscribe methods of the event notifier #708

Merged
merged 3 commits into from
Dec 4, 2023

Conversation

kevin-dp
Copy link
Contributor

@kevin-dp kevin-dp commented Nov 29, 2023

This PR refactors the various subscribe methods provided by the event notifier. For every listener that was being subscribed, the event notifier would generate a random key, store the listener under that key, and return that key. To unsubscribe, the user had to pass that key to the corresponding unsubscribe method which would lookup the listener and then unsubscribe it from the underlying event emitter. Storing the listener under a key is not needed and is a bit redundant as the underlying event emitter already stores them.

This PR modified the event notifier such that it does not keep track of registered callbacks but instead returns an unsubscribe function from the subscribe methods. The unsubscribe function closes over the callback that needs to be unregistered from the underlying event emitter.

Copy link
Contributor

@icehaunter icehaunter left a comment

Choose a reason for hiding this comment

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

I love it

@samwillis
Copy link
Contributor

I love the concept here too, however we need to consider this in relation to a multi-process/thread notifier.

The subscription keys are ugly, but it is the style of API needed for a cross process notifier (at least at a protocol level)

@kevin-dp
Copy link
Contributor Author

kevin-dp commented Dec 4, 2023

I love the concept here too, however we need to consider this in relation to a multi-process/thread notifier.

The subscription keys are ugly, but it is the style of API needed for a cross process notifier (at least at a protocol level)

Could you elaborate a bit more on the problem?
With multi-process notifier, the problem is that the listener does not run on the process that registered it but rather on the notifier's process? I believe this will always be a problem with this kind of notifier API (where a callback is registered) and solving it requires changing the notifier to send a message to the subscribed processes.

I believe this is out of scope for this PR and we can implement a multi-process notifier when we move toward that architecture. Do you agree?

@samwillis
Copy link
Contributor

The multi-process notifier will have a server + client component, the client receives a notification of event with a subscription key and running the callback locally. It will also use that key to unsubscribe. Hiding that behind a closure would be nice DX, as you have here! My only thought was that some of the existing machinery around the keys could be a good starting point for an initial multi process notifier.

However, you're probably right that it's out of scope, ignore me as you see fit.

@kevin-dp kevin-dp merged commit e091bbf into main Dec 4, 2023
6 checks passed
@kevin-dp kevin-dp deleted the kevindp/event-notifier-refactoring branch December 4, 2023 15:43
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.

3 participants