mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-09-21 05:12:34 +00:00
✨ Feature: 支持主动销毁事件响应器 (#1444)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
9826bc29ca
commit
06c33ad6ea
@ -247,6 +247,11 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
|
|
||||||
return NewMatcher
|
return NewMatcher
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def destroy(cls) -> None:
|
||||||
|
"""销毁当前的事件响应器"""
|
||||||
|
matchers[cls.priority].remove(cls)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def check_perm(
|
async def check_perm(
|
||||||
cls,
|
cls,
|
||||||
|
@ -112,7 +112,6 @@ def run_postprocessor(func: T_RunPostProcessor) -> T_RunPostProcessor:
|
|||||||
|
|
||||||
|
|
||||||
async def _check_matcher(
|
async def _check_matcher(
|
||||||
priority: int,
|
|
||||||
Matcher: Type[Matcher],
|
Matcher: Type[Matcher],
|
||||||
bot: "Bot",
|
bot: "Bot",
|
||||||
event: "Event",
|
event: "Event",
|
||||||
@ -122,7 +121,7 @@ async def _check_matcher(
|
|||||||
) -> None:
|
) -> None:
|
||||||
if Matcher.expire_time and datetime.now() > Matcher.expire_time:
|
if Matcher.expire_time and datetime.now() > Matcher.expire_time:
|
||||||
with contextlib.suppress(Exception):
|
with contextlib.suppress(Exception):
|
||||||
matchers[priority].remove(Matcher)
|
Matcher.destroy()
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -138,7 +137,7 @@ async def _check_matcher(
|
|||||||
|
|
||||||
if Matcher.temp:
|
if Matcher.temp:
|
||||||
with contextlib.suppress(Exception):
|
with contextlib.suppress(Exception):
|
||||||
matchers[priority].remove(Matcher)
|
Matcher.destroy()
|
||||||
await _run_matcher(Matcher, bot, event, state, stack, dependency_cache)
|
await _run_matcher(Matcher, bot, event, state, stack, dependency_cache)
|
||||||
|
|
||||||
|
|
||||||
@ -294,7 +293,7 @@ async def handle_event(bot: "Bot", event: "Event") -> None:
|
|||||||
|
|
||||||
pending_tasks = [
|
pending_tasks = [
|
||||||
_check_matcher(
|
_check_matcher(
|
||||||
priority, matcher, bot, event, state.copy(), stack, dependency_cache
|
matcher, bot, event, state.copy(), stack, dependency_cache
|
||||||
)
|
)
|
||||||
for matcher in matchers[priority]
|
for matcher in matchers[priority]
|
||||||
]
|
]
|
||||||
|
@ -187,25 +187,19 @@ async def test_expire(app: App, load_plugin):
|
|||||||
async with app.test_api() as ctx:
|
async with app.test_api() as ctx:
|
||||||
bot = ctx.create_bot()
|
bot = ctx.create_bot()
|
||||||
assert test_temp_matcher in matchers[test_temp_matcher.priority]
|
assert test_temp_matcher in matchers[test_temp_matcher.priority]
|
||||||
await _check_matcher(
|
await _check_matcher(test_temp_matcher, bot, event, {})
|
||||||
test_temp_matcher.priority, test_temp_matcher, bot, event, {}
|
|
||||||
)
|
|
||||||
assert test_temp_matcher not in matchers[test_temp_matcher.priority]
|
assert test_temp_matcher not in matchers[test_temp_matcher.priority]
|
||||||
|
|
||||||
event = make_fake_event()()
|
event = make_fake_event()()
|
||||||
async with app.test_api() as ctx:
|
async with app.test_api() as ctx:
|
||||||
bot = ctx.create_bot()
|
bot = ctx.create_bot()
|
||||||
assert test_datetime_matcher in matchers[test_datetime_matcher.priority]
|
assert test_datetime_matcher in matchers[test_datetime_matcher.priority]
|
||||||
await _check_matcher(
|
await _check_matcher(test_datetime_matcher, bot, event, {})
|
||||||
test_datetime_matcher.priority, test_datetime_matcher, bot, event, {}
|
|
||||||
)
|
|
||||||
assert test_datetime_matcher not in matchers[test_datetime_matcher.priority]
|
assert test_datetime_matcher not in matchers[test_datetime_matcher.priority]
|
||||||
|
|
||||||
event = make_fake_event()()
|
event = make_fake_event()()
|
||||||
async with app.test_api() as ctx:
|
async with app.test_api() as ctx:
|
||||||
bot = ctx.create_bot()
|
bot = ctx.create_bot()
|
||||||
assert test_timedelta_matcher in matchers[test_timedelta_matcher.priority]
|
assert test_timedelta_matcher in matchers[test_timedelta_matcher.priority]
|
||||||
await _check_matcher(
|
await _check_matcher(test_timedelta_matcher, bot, event, {})
|
||||||
test_timedelta_matcher.priority, test_timedelta_matcher, bot, event, {}
|
|
||||||
)
|
|
||||||
assert test_timedelta_matcher not in matchers[test_timedelta_matcher.priority]
|
assert test_timedelta_matcher not in matchers[test_timedelta_matcher.priority]
|
||||||
|
Loading…
Reference in New Issue
Block a user