Skip to content

Commit

Permalink
🐛 fix dispatcher replaced by same id
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Jul 26, 2024
1 parent 64f8549 commit 57ce051
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
3 changes: 1 addition & 2 deletions arclet/entari/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
def dispatch(*events: type[Event], predicate: Callable[[Event], bool] | None = None):
if not (plugin := _current_plugin.get()):
raise LookupError("no plugin context found")
disp = PluginDispatcher(plugin, *events, predicate=predicate)
return disp
return plugin.dispatch(*events, predicate=predicate)


def load_plugin(path: str) -> Plugin | None:
Expand Down
13 changes: 8 additions & 5 deletions arclet/entari/plugin/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dataclasses import dataclass, field
from types import ModuleType
from typing import TYPE_CHECKING, Any, Callable
from weakref import WeakValueDictionary, finalize
from weakref import finalize

from arclet.letoderea import BaseAuxiliary, Provider, Publisher, StepOut, system_ctx
from arclet.letoderea.builtin.breakpoint import R
Expand All @@ -28,9 +28,8 @@ def __init__(
*events: type[Event],
predicate: Callable[[Event], bool] | None = None,
):
super().__init__(f"{plugin.id}@{id(plugin)}", *events, predicate=predicate) # type: ignore
super().__init__(f"{plugin.id}@{id(self)}", *events, predicate=predicate) # type: ignore
self.plugin = plugin
plugin.dispatchers[self.id] = self
self._run_by_system = False
if es := system_ctx.get():
es.register(self)
Expand Down Expand Up @@ -93,7 +92,7 @@ class PluginMetadata:
class Plugin:
id: str
module: ModuleType
dispatchers: WeakValueDictionary[str, PluginDispatcher] = field(default_factory=WeakValueDictionary)
dispatchers: dict[str, PluginDispatcher] = field(default_factory=dict)
metadata: PluginMetadata | None = None
_is_disposed: bool = False

Expand Down Expand Up @@ -142,4 +141,8 @@ def dispose(self):
del self.module

def dispatch(self, *events: type[Event], predicate: Callable[[Event], bool] | None = None):
return PluginDispatcher(self, *events, predicate=predicate)
disp = PluginDispatcher(self, *events, predicate=predicate)
if disp.id in self.dispatchers:
return self.dispatchers[disp.id]
self.dispatchers[disp.id] = disp
return disp
7 changes: 4 additions & 3 deletions arclet/entari/plugin/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ def stages(self) -> set[Phase]:
async def launch(self, manager: Launart):
_preparing = []
_cleanup = []
for plug in self.plugins.values():
_preparing.extend([func() for func in plug._preparing])
_cleanup.extend([func() for func in plug._cleanup])
async with self.stage("preparing"):
for plug in self.plugins.values():
_preparing.extend([func() for func in plug._preparing])
await asyncio.gather(*_preparing, return_exceptions=True)
async with self.stage("cleanup"):
for plug in self.plugins.values():
_cleanup.extend([func() for func in plug._cleanup])
await asyncio.gather(*_cleanup, return_exceptions=True)
ids = [*self.plugins.keys()]
for plug_id in ids:
Expand Down

0 comments on commit 57ce051

Please sign in to comment.