-
Notifications
You must be signed in to change notification settings - Fork 3
Added compression, better emitter /w tests #4
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| from .connector import * | ||
| from .exceptions import * | ||
| from .opcodes import * | ||
| from .emitter import * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .emitter import * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| from collections import defaultdict | ||
| from enum import Enum | ||
| from inspect import iscoroutinefunction | ||
| from functools import wraps | ||
| from typing import Callable, Coroutine, Union, Any, Dict | ||
|
|
||
| from clamor.gateway.exceptions import InvalidListener | ||
|
|
||
| from anyio import create_task_group | ||
|
|
||
|
|
||
| def check_coroutine(func): | ||
| @wraps(func) | ||
| def wrapper(self, listener: Callable[..., Coroutine[Any, Any, None]]): | ||
| if not iscoroutinefunction(listener): | ||
| raise InvalidListener("Listener must be a coroutine") | ||
| return func(self, listener) | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| class Priority(Enum): | ||
| BEFORE = 0 | ||
| NORMAL = 1 | ||
| AFTER = 2 | ||
|
|
||
|
|
||
| class ListenerPod: | ||
| """Event listener module | ||
|
|
||
| Listeners that all follow a certain event will exist in the same pod. Pods will separate | ||
| listeners into self-explanatory categories, before, normal, and after. The listeners will | ||
| trigger in order from before to after, with each listener triggering synchronously with | ||
| listeners from the same category. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| before : set | ||
| Listeners that will trigger 1st | ||
| normal : set | ||
| Listeners that will trigger 2nd | ||
| after : set | ||
| Listeners that will trigger 3rd | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| self.before = set() | ||
| self.normal = set() | ||
| self.after = set() | ||
|
|
||
| def __bool__(self): | ||
| return bool(self.before) or bool(self.normal) or bool(self.after) | ||
|
|
||
| @check_coroutine | ||
| def add_before(self, listener: Callable[..., Coroutine[Any, Any, None]]): | ||
| """Add listener (Before) | ||
|
|
||
| Add a coroutine to the before category | ||
|
|
||
| Parameters | ||
| ---------- | ||
| listener : Coroutine | ||
| A coroutine to be triggered on it's respective event | ||
| """ | ||
| self.before.add(listener) | ||
|
|
||
| @check_coroutine | ||
| def add_normal(self, listener: Callable[..., Coroutine[Any, Any, None]]): | ||
| """Add listener (Normal) | ||
|
|
||
| Add a coroutine to the normal category | ||
|
|
||
| Parameters | ||
| ---------- | ||
| listener : Coroutine | ||
| A coroutine to be triggered on it's respective event | ||
| """ | ||
| self.normal.add(listener) | ||
|
|
||
| @check_coroutine | ||
| def add_after(self, listener: Callable[..., Coroutine[Any, Any, None]]): | ||
| """Add listener (After) | ||
|
|
||
| Add a coroutine to the after category | ||
|
|
||
| Parameters | ||
| ---------- | ||
| listener : Coroutine | ||
| A coroutine to be triggered on it's respective event | ||
| """ | ||
| self.after.add(listener) | ||
|
|
||
| async def emit(self, data: Dict[str, Any]): | ||
| """Trigger listeners in the pod | ||
|
|
||
| All listeners in the before category will be spawned with the appropriate payload, and | ||
| once all those have finished, the normal category is triggered, and then the after category. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| data : dict | ||
| The payload provided by discord to be distributed | ||
| """ | ||
| async with create_task_group() as tg: | ||
| for listener in self.before: | ||
| await tg.spawn(listener, data) | ||
| async with create_task_group() as tg: | ||
| for listener in self.normal: | ||
| await tg.spawn(listener, data) | ||
| async with create_task_group() as tg: | ||
| for listener in self.after: | ||
| await tg.spawn(listener, data) | ||
|
|
||
|
|
||
| class Emitter: | ||
| """Main event emitter | ||
|
|
||
| This is what orchestrates all the event pods, adds listeners, removes them and triggers events. | ||
| Events can be either an op code, or a name (for opcode 0). | ||
|
|
||
| Attributes | ||
| ---------- | ||
| listeners : defaultdict(:class `clamor.gateway.emitter.ListenerPod`:) | ||
| A default dict that holders event namess to listener pods. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| self.listeners = defaultdict(ListenerPod) | ||
|
|
||
| def add_listener(self, event: Union[int, str], | ||
| listener: Callable[..., Coroutine[Any, Any, None]], | ||
| order: Priority = Priority.NORMAL): | ||
| """Add a listener | ||
|
|
||
| Add a listener to the correct pod and category, which by default is the normal priority. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| event : int or str | ||
| The op code or event to listen too | ||
| listener : Coroutine | ||
| A coroutine to be triggered on it's respective event | ||
| order : :class `clamor.gateway.emitter.Priority` | ||
| The order this listener should be triggered in | ||
| """ | ||
|
|
||
| # Create a pod if one does not exist for the event, then add the listener | ||
| # using the respective method, based on the priority. | ||
| getattr(self.listeners[event], "add_" + order.name.lower())(listener) | ||
|
|
||
| async def emit(self, event: Union[int, str], data): | ||
| """Emit an event | ||
|
|
||
| Trigger the corresponding ListenerPod if one exists. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| event: int or str | ||
| The op code or event to listen too | ||
| data : dict | ||
| The payload provided by discord to be distributed | ||
| """ | ||
| if self.listeners[event]: | ||
| await self.listeners[event].emit(data) | ||
|
|
||
| def clear_event(self, event: Union[str, int]): | ||
| """Clear all listeners | ||
|
|
||
| Removes all listeners, to matter the category, from the provided event. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| event : str or int | ||
| The op code or event to remove | ||
| """ | ||
| self.listeners.pop(event) | ||
|
|
||
| def remove_listener(self, event: Union[str, int], | ||
| listener: Callable[..., Coroutine[Any, Any, None]]): | ||
| """Remove a specific listener from an event | ||
|
|
||
| Removes a the provided listener from an event, no matter the category. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| event : int or str | ||
| The op code or event to search | ||
| listener : Coroutine | ||
| Listener to remove | ||
| """ | ||
| if self.listeners[event]: | ||
| if listener in self.listeners[event].before: | ||
| self.listeners[event].before.remove(listener) | ||
| if listener in self.listeners[event].normal: | ||
| self.listeners[event].normal.remove(listener) | ||
| if listener in self.listeners[event].after: | ||
| self.listeners[event].after.remove(listener) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.