Skip to content

Commit 822b78e

Browse files
committed
✨ add propagators param for scope.register
1 parent 252ebb7 commit 822b78e

File tree

2 files changed

+55
-53
lines changed

2 files changed

+55
-53
lines changed

arclet/letoderea/scope.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def __call__(self, func: Callable, /) -> Subscriber[T]:
5858

5959
class Scope:
6060
global_skip_req_missing = False
61+
__wrapper_class__ = RegisterWrapper
6162

6263
@staticmethod
6364
def of(id_: str | None = None):
@@ -97,10 +98,11 @@ def remove_subscriber(self, subscriber: Subscriber) -> None:
9798
"""移除订阅者"""
9899
self.subscribers.pop(subscriber.id, None)
99100

100-
def register(self, func: Callable[..., Any] | None = None, event: type | None = None, *, priority: int = 16, providers: TProviders | None = None, publisher: str | Publisher | None = None, once: bool = False, skip_req_missing: bool | None = None):
101+
def register(self, func: Callable[..., Any] | None = None, event: type | None = None, *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, publisher: str | Publisher | None = None, once: bool = False, skip_req_missing: bool | None = None):
101102
"""注册一个订阅者"""
102103
_skip_req_missing = self.global_skip_req_missing if skip_req_missing is None else skip_req_missing
103104
providers = providers or []
105+
propagators = propagators or []
104106
if isinstance(publisher, Publisher):
105107
pub_id = publisher.id
106108
event_providers = publisher.providers
@@ -121,7 +123,7 @@ def register(self, func: Callable[..., Any] | None = None, event: type | None =
121123
event_providers = _pub.providers
122124
_listen = event
123125

124-
register_wrapper = RegisterWrapper(self, _listen, priority, [*global_providers, *event_providers, *self.providers, *providers], [*global_propagators, *self.propagators], _pub, pub_id, once, _skip_req_missing)
126+
register_wrapper = self.__wrapper_class__(self, _listen, priority, [*global_providers, *event_providers, *self.providers, *providers], [*global_propagators, *self.propagators, *propagators], _pub, pub_id, once, _skip_req_missing)
125127
if func:
126128
return register_wrapper(func)
127129
return register_wrapper
@@ -157,12 +159,12 @@ def configure(skip_req_missing: bool = False):
157159
global_skip_req_missing = skip_req_missing
158160

159161

160-
def on(event: type, func: Callable[..., Any] | None = None, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None):
162+
def on(event: type, func: Callable[..., Any] | None = None, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None):
161163
if not (scope := scope_ctx.get()):
162164
scope = _scopes["$global"]
163165
if not func:
164-
return scope.register(event=event, priority=priority, providers=providers, skip_req_missing=skip_req_missing, once=once)
165-
return scope.register(func, event=event, priority=priority, providers=providers, skip_req_missing=skip_req_missing, once=once)
166+
return scope.register(event=event, priority=priority, providers=providers, propagators=propagators, skip_req_missing=skip_req_missing, once=once)
167+
return scope.register(func, event=event, priority=priority, providers=providers, propagators=propagators, skip_req_missing=skip_req_missing, once=once)
166168

167169

168170
def on_global(func: Callable[..., Any] | None = None, priority: int = 16, once: bool = False, skip_req_missing: bool | None = None):
@@ -173,9 +175,9 @@ def on_global(func: Callable[..., Any] | None = None, priority: int = 16, once:
173175
return scope.register(func, event=None, priority=priority, skip_req_missing=skip_req_missing, once=once)
174176

175177

176-
def use(pub: str | Publisher, func: Callable[..., Any] | None = None, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None):
178+
def use(pub: str | Publisher, func: Callable[..., Any] | None = None, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None):
177179
if not (scope := scope_ctx.get()):
178180
scope = _scopes["$global"]
179181
if not func:
180-
return scope.register(priority=priority, providers=providers, once=once, skip_req_missing=skip_req_missing, publisher=pub)
181-
return scope.register(func, priority=priority, providers=providers, once=once, skip_req_missing=skip_req_missing, publisher=pub)
182+
return scope.register(priority=priority, providers=providers, propagators=propagators, once=once, skip_req_missing=skip_req_missing, publisher=pub)
183+
return scope.register(func, priority=priority, providers=providers, propagators=propagators, once=once, skip_req_missing=skip_req_missing, publisher=pub)

arclet/letoderea/scope.pyi

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,6 @@ scope_ctx: ContextModel[Scope]
2121
global_propagators: list[Propagator]
2222

2323

24-
class Scope:
25-
global_skip_req_missing: ClassVar[bool]
26-
id: str
27-
subscribers: dict[str, tuple[Subscriber, str]]
28-
available: bool
29-
providers: list[Provider[Any] | ProviderFactory]
30-
propagators: list[Propagator]
31-
32-
@staticmethod
33-
def of(id_: str | None = None) -> Scope: ...
34-
def __init__(self, id_: str | None = None): ...
35-
def bind(self, *args: Provider | type[Provider] | ProviderFactory | type[ProviderFactory]) -> None: ...
36-
def unbind(self, arg: Provider | type[Provider] | ProviderFactory | type[ProviderFactory]) -> None: ...
37-
@contextmanager
38-
def context(self) -> Generator[Scope, None, None]: ...
39-
def remove_subscriber(self, subscriber: Subscriber) -> None: ...
40-
@overload
41-
def register(self, func: Callable[..., T], event: type | None = None, *, priority: int = 16, providers: TProviders | None = None, publisher: str | Publisher | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T]: ...
42-
@overload
43-
def register(self, *, event: type | None = None, priority: int = 16, providers: TProviders | None = None, publisher: str | Publisher | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Callable[[Callable[..., T]], Subscriber[T]]: ...
44-
def iter(self, pub_ids: set[str], pass_backend: bool = True) -> Generator[Subscriber, None, None]: ...
45-
def disable(self) -> None: ...
46-
def enable(self) -> None: ...
47-
def dispose(self) -> None: ...
48-
49-
50-
def configure(skip_req_missing: bool = False) -> None: ...
51-
52-
5324
class RegisterWrapper(Generic[T, TC]):
5425
_scope: Scope
5526
_event: type | None
@@ -77,39 +48,68 @@ class RegisterWrapper(Generic[T, TC]):
7748
def __call__(self: RegisterWrapper[T, None], func: Callable[..., T | ExitState | None]) -> Subscriber[T]: ...
7849

7950

51+
class Scope:
52+
global_skip_req_missing: ClassVar[bool]
53+
__wrapper_class__: ClassVar[type[RegisterWrapper]]
54+
id: str
55+
subscribers: dict[str, tuple[Subscriber, str]]
56+
available: bool
57+
providers: list[Provider[Any] | ProviderFactory]
58+
propagators: list[Propagator]
59+
60+
@staticmethod
61+
def of(id_: str | None = None) -> Scope: ...
62+
def __init__(self, id_: str | None = None): ...
63+
def bind(self, *args: Provider | type[Provider] | ProviderFactory | type[ProviderFactory]) -> None: ...
64+
def unbind(self, arg: Provider | type[Provider] | ProviderFactory | type[ProviderFactory]) -> None: ...
65+
@contextmanager
66+
def context(self) -> Generator[Scope, None, None]: ...
67+
def remove_subscriber(self, subscriber: Subscriber) -> None: ...
68+
@overload
69+
def register(self, func: Callable[..., T], event: type | None = None, *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, publisher: str | Publisher | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T]: ...
70+
@overload
71+
def register(self, *, event: type | None = None, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, publisher: str | Publisher | None = None, once: bool = False, skip_req_missing: bool | None = None) -> RegisterWrapper[None, Callable]: ...
72+
def iter(self, pub_ids: set[str], pass_backend: bool = True) -> Generator[Subscriber, None, None]: ...
73+
def disable(self) -> None: ...
74+
def enable(self) -> None: ...
75+
def dispose(self) -> None: ...
76+
77+
78+
def configure(skip_req_missing: bool = False) -> None: ...
79+
8080
@overload
81-
def on(event: type[Resultable[T1]], func: Callable[..., Generator[T1 | ExitState | None, None, None]], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[Generator[T1, None, None]]: ...
81+
def on(event: type[Resultable[T1]], func: Callable[..., Generator[T1 | ExitState | None, None, None]], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[Generator[T1, None, None]]: ...
8282
@overload
83-
def on(event: type[Resultable[T1]], func: Callable[..., AsyncGenerator[T1 | ExitState | None, None]], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[AsyncGenerator[T1, None]]: ...
83+
def on(event: type[Resultable[T1]], func: Callable[..., AsyncGenerator[T1 | ExitState | None, None]], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[AsyncGenerator[T1, None]]: ...
8484
@overload
85-
def on(event: type[Resultable[T1]], func: Callable[..., Awaitable[T1 | ExitState | None]], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[Awaitable[T1]]: ...
85+
def on(event: type[Resultable[T1]], func: Callable[..., Awaitable[T1 | ExitState | None]], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[Awaitable[T1]]: ...
8686
@overload
87-
def on(event: type[Resultable[T1]], func: Callable[..., T1 | ExitState | None], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T1]: ...
87+
def on(event: type[Resultable[T1]], func: Callable[..., T1 | ExitState | None], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T1]: ...
8888
@overload
89-
def on(event: type[Resultable[T1]], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> RegisterWrapper[T1, None]: ...
89+
def on(event: type[Resultable[T1]], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> RegisterWrapper[T1, None]: ...
9090
@overload
91-
def on(event: type[Any], func: Callable[..., T], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T]: ... # type: ignore
91+
def on(event: type[Any], func: Callable[..., T], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T]: ... # type: ignore
9292
@overload
93-
def on(event: type[Any], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> RegisterWrapper[None, Callable]: ... # type: ignore
93+
def on(event: type[Any], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> RegisterWrapper[None, Callable]: ... # type: ignore
9494
@overload
9595
def on_global(func: Callable[..., T], *, priority: int = 16, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T]: ...
9696
@overload
9797
def on_global(*, priority: int = 16, once: bool = False, skip_req_missing: bool | None = None) -> RegisterWrapper[None, Callable]: ...
9898
@overload
99-
def use(pub: Publisher[Resultable[T1]], func: Callable[..., Generator[T1 | ExitState | None, None, None]], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[Generator[T1, None, None]]: ...
99+
def use(pub: Publisher[Resultable[T1]], func: Callable[..., Generator[T1 | ExitState | None, None, None]], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[Generator[T1, None, None]]: ...
100100
@overload
101-
def use(pub: Publisher[Resultable[T1]], func: Callable[..., AsyncGenerator[T1 | ExitState | None, None]], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[AsyncGenerator[T1, None]]: ...
101+
def use(pub: Publisher[Resultable[T1]], func: Callable[..., AsyncGenerator[T1 | ExitState | None, None]], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[AsyncGenerator[T1, None]]: ...
102102
@overload
103-
def use(pub: Publisher[Resultable[T1]], func: Callable[..., Awaitable[T1 | ExitState | None]], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[Awaitable[T1]]: ...
103+
def use(pub: Publisher[Resultable[T1]], func: Callable[..., Awaitable[T1 | ExitState | None]], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[Awaitable[T1]]: ...
104104
@overload
105-
def use(pub: Publisher[Resultable[T1]], func: Callable[..., T1 | ExitState | None], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T1]: ...
105+
def use(pub: Publisher[Resultable[T1]], func: Callable[..., T1 | ExitState | None], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T1]: ...
106106
@overload
107-
def use(pub: Publisher[Resultable[T1]], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> RegisterWrapper[T1, None]: ...
107+
def use(pub: Publisher[Resultable[T1]], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> RegisterWrapper[T1, None]: ...
108108
@overload
109-
def use(pub: Publisher[Any], func: Callable[..., T], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T]: ...
109+
def use(pub: Publisher[Any], func: Callable[..., T], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T]: ...
110110
@overload
111-
def use(pub: Publisher[Any], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> RegisterWrapper[None, Callable]: ...
111+
def use(pub: Publisher[Any], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> RegisterWrapper[None, Callable]: ...
112112
@overload
113-
def use(pub: str, func: Callable[..., T], *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T]: ...
113+
def use(pub: str, func: Callable[..., T], *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> Subscriber[T]: ...
114114
@overload
115-
def use(pub: str, *, priority: int = 16, providers: TProviders | None = None, once: bool = False, skip_req_missing: bool | None = None) -> RegisterWrapper[None, Callable]: ...
115+
def use(pub: str, *, priority: int = 16, providers: TProviders | None = None, propagators: list[Propagator] | None = None, once: bool = False, skip_req_missing: bool | None = None) -> RegisterWrapper[None, Callable]: ...

0 commit comments

Comments
 (0)