about summary refs log tree commit diff stats
path: root/discord/utils
diff options
context:
space:
mode:
Diffstat (limited to 'discord/utils')
-rw-r--r--discord/utils/event_emitter.py22
1 files changed, 8 insertions, 14 deletions
diff --git a/discord/utils/event_emitter.py b/discord/utils/event_emitter.py
index 77fff72..08b6060 100644
--- a/discord/utils/event_emitter.py
+++ b/discord/utils/event_emitter.py
@@ -1,29 +1,23 @@
 import asyncio
-from types import NoneType
-from typing import Coroutine
+from typing import Optional, Coroutine, Any, Callable, Dict
 
 class EventEmitter():
-    def __init__(self):
-        self.listeners = {}
+    def __init__(self, loop: Optional[asyncio.AbstractEventLoop]=None):
+        self.listeners: Dict[str, Optional[Callable[..., Coroutine[Any, Any, Any]]]] = {}
+        self.loop = loop if loop else asyncio.get_event_loop()
 
-    def add_listener(self, event_name: str, func: Coroutine):
+    def add_listener(self, event_name: str, func: Optional[Callable[..., Coroutine[Any, Any, Any]]]=None):
         if not self.listeners.get(event_name, None):
             self.listeners[event_name] = {func}
         else:
             self.listeners[event_name].add(func)
 
-    def remove_listener(self, event_name: str, func: Coroutine):
+    def remove_listener(self, event_name: str, func: Optional[Callable[..., Coroutine[Any, Any, Any]]]=None):
         self.listeners[event_name].remove(func)
         if len(self.listeners[event_name]) == 0:
             del self.listeners[event_name]
 
-    def emit(self, event_name: str, args_required=False, *args, **kwargs):
+    def emit(self, event_name: str, *args: Any, **kwargs: Any) -> None:
         listeners = self.listeners.get(event_name, [])
         for func in listeners:
-            if args_required:
-                if len(args) == 0:
-                    raise TypeError('event registered must have arguments')
-                else:
-                    asyncio.create_task(func(*args, **kwargs))
-            else:
-                asyncio.create_task(func(*args, **kwargs))
+            asyncio.run_coroutine_threadsafe(func(*args, **kwargs), self.loop)
.org> 2020-03-15 17:45:02 +0530 Fix wording in readme' href='/andinus/cetus/commit/README.org?id=03197e4385388a4f20d393a20448e8996a80dd56'>03197e4 ^
16bcb7f ^




















50871fc ^

16842fa ^



50871fc ^















16842fa ^


50871fc ^






3326769 ^



50871fc ^





f0a905d ^
b34b873 ^


a7e66bc ^


b34b873 ^

17ca26f ^
bfa2918 ^
6849d7f ^
b34b873 ^
f755d1d ^

7552361 ^
72441e4 ^


7552361 ^
f755d1d ^










16bcb7f ^

f755d1d ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123