Skip to content

Commit

Permalink
feat: AsyncEvent 支持同步函数
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo2011 committed Jan 31, 2025
1 parent e0084ac commit b288fb2
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 12 deletions.
4 changes: 3 additions & 1 deletion bilibili_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
# api
HEADERS,
)
from .utils.AsyncEvent import AsyncEvent
from .utils.geetest import Geetest, GeetestMeta, GeetestType
from .exceptions import (
ApiException,
Expand Down Expand Up @@ -102,7 +103,7 @@
)


BILIBILI_API_VERSION = "17.0.0"
BILIBILI_API_VERSION = "17.0.0+"


def __register_all_clients():
Expand Down Expand Up @@ -130,6 +131,7 @@ def __register_all_clients():

__all__ = [
"ApiException",
"AsyncEvent",
"ArgsException",
"BILIBILI_API_VERSION",
"BiliAPIClient",
Expand Down
22 changes: 12 additions & 10 deletions bilibili_api/utils/AsyncEvent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

import asyncio
from typing import Callable, Coroutine
from typing import Callable, Coroutine, Union


class AsyncEvent:
Expand All @@ -19,13 +19,13 @@ def __init__(self):
self.__handlers = {}
self.__ignore_events = []

def add_event_listener(self, name: str, handler: Coroutine) -> None:
def add_event_listener(self, name: str, handler: Union[Callable, Coroutine]) -> None:
"""
注册事件监听器。
Args:
name (str): 事件名。
handler (Coroutine): 回调异步函数
name (str) : 事件名。
handler (Union[Callable, Coroutine]): 回调函数
"""
name = name.upper()
if name not in self.__handlers:
Expand All @@ -40,7 +40,7 @@ def on(self, event_name: str) -> Callable:
event_name (str): 事件名。
"""

def decorator(func: Coroutine):
def decorator(func: Union[Callable, Coroutine]):
self.add_event_listener(event_name, func)
return func

Expand All @@ -52,13 +52,13 @@ def remove_all_event_listener(self) -> None:
"""
self.__handlers = {}

def remove_event_listener(self, name: str, handler: Coroutine) -> bool:
def remove_event_listener(self, name: str, handler: Union[Callable, Coroutine]) -> bool:
"""
移除事件监听函数。
Args:
name (str): 事件名。
handler (Coroutine): 要移除的函数。
name (str): 事件名。
handler (Union[Callable, Coroutine]): 要移除的函数。
Returns:
bool, 是否移除成功。
Expand Down Expand Up @@ -101,8 +101,10 @@ def dispatch(self, name: str, *args, **kwargs) -> None:

name = name.upper()
if name in self.__handlers:
for coroutine in self.__handlers[name]:
asyncio.create_task(coroutine(*args, **kwargs))
for callableorcoroutine in self.__handlers[name]:
obj = callableorcoroutine(*args, **kwargs)
if isinstance(obj, Coroutine):
asyncio.create_task(obj)

if name != "__ALL__":
kwargs.update({"name": name, "data": args})
Expand Down
2 changes: 1 addition & 1 deletion bilibili_api/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def set_on(self, status: bool) -> None:
"""
self.__on = status

async def __handle_events(self, data: dict) -> None:
def __handle_events(self, data: dict) -> None:
evt = data["name"]
desc, real_data = data["data"]
if (
Expand Down
115 changes: 115 additions & 0 deletions docs/modules/bilibili_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ from bilibili_api import bilibili_api

- [class ApiException()](#class-ApiException)
- [class ArgsException()](#class-ArgsException)
- [class AsyncEvent()](#class-AsyncEvent)
- [def \_\_init\_\_()](#def-\_\_init\_\_)
- [def add\_event\_listener()](#def-add\_event\_listener)
- [def dispatch()](#def-dispatch)
- [def ignore\_event()](#def-ignore\_event)
- [def on()](#def-on)
- [def remove\_all\_event\_listener()](#def-remove\_all\_event\_listener)
- [def remove\_event\_listener()](#def-remove\_event\_listener)
- [def remove\_ignore\_events()](#def-remove\_ignore\_events)
- [class BiliAPIClient()](#class-BiliAPIClient)
- [class BiliAPIFile()](#class-BiliAPIFile)
- [class BiliAPIResponse()](#class-BiliAPIResponse)
Expand Down Expand Up @@ -141,6 +150,112 @@ API 基类异常。



---

## class AsyncEvent()

发布-订阅模式异步事件类支持。

特殊事件:__ALL__ 所有事件均触发




### def \_\_init\_\_()





### def add_event_listener()

注册事件监听器。


| name | type | description |
| - | - | - |
| name | str | 事件名。 |
| handler | Union[Callable, Coroutine] | 回调函数。 |

**Returns:** None



### def dispatch()

异步发布事件。


| name | type | description |
| - | - | - |
| name | str | 事件名。 |
| *args, **kwargs: 要传递给函数的参数。 | | 要传递给函数的参数。 |

**Returns:** None



### def ignore_event()

忽略指定事件


| name | type | description |
| - | - | - |
| name | str | 事件名。 |

**Returns:** None



### def on()

装饰器注册事件监听器。


| name | type | description |
| - | - | - |
| event_name | str | 事件名。 |

**Returns:** None



### def remove_all_event_listener()

移除所有事件监听函数



**Returns:** None



### def remove_event_listener()

移除事件监听函数。


| name | type | description |
| - | - | - |
| name | str | 事件名。 |
| handler | Union[Callable, Coroutine] | 要移除的函数。 |

**Returns:** bool, 是否移除成功。




### def remove_ignore_events()

移除所有忽略事件



**Returns:** None



---

## class BiliAPIClient()
Expand Down

0 comments on commit b288fb2

Please sign in to comment.