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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer decorators for emitting awareness events #249

Open
dlqqq opened this issue Mar 14, 2024 · 0 comments
Open

Prefer decorators for emitting awareness events #249

dlqqq opened this issue Mar 14, 2024 · 0 comments

Comments

@dlqqq
Copy link
Collaborator

dlqqq commented Mar 14, 2024

          @jzhang20133 Thank you opening this PR! I left a comment below, but David Brochart's review covers the areas of feedback blocking this PR from being merged. 馃榿 

All I would like to add is that it's possible to emit events through a decorator interface. Emitting events imperatively (as done in this PR) is less preferable, since it's really easy for a developer to accidentally delete the line emitting the event. Using a decorator also allows for this feature to be added without any changes to the method definition, which I take as a sign of clean code.

To implement a decorator interface, you would want to define an @emit_awareness_event decorator that looks something like:

def emit_awareness_event(action: str, message: Optional[str] = None):
    def decorator(method):
        async def method_stub(self, *args, **kwargs):
            return_value = await method(self, *args, **kwargs)
            data = { "action": action, ... }
            self.event_logger.emit(..., data=data)
            return return_value
        return method_stub
    return decorator

After doing so, the method declarations would look like:

    @emit_awareness_event("join")
    async def open(self, room_id):
        ...

    @emit_awareness_event("leave")
    def on_close(self) -> None:
        ...

Let me know if this all sounds like a good idea for future work. If so, I can track this in a new issue so as to not block this PR. Thanks! 馃

Originally posted by @dlqqq in #246 (review)

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

No branches or pull requests

1 participant