Skip to content

Commit

Permalink
Change where to condition
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Mar 14, 2021
1 parent f0687bb commit 5c1e146
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,13 @@ def dispatch(
self,
event: str,
*,
where: Optional[Dict[str, str]] = None,
condition: Optional[Dict[str, str]] = None,
context: Optional[Dict[str, Any]] = None,
) -> Coroutine[Any, Any, Awaitable[Any]]:
return self.signal_router.dispatch(
event,
context=context,
where=where,
condition=condition,
)

def event(self, event: str, timeout: Optional[Union[int, float]] = None):
Expand Down
8 changes: 4 additions & 4 deletions sanic/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def register(self, app, options):
listeners[listener.event].append(app._apply_listener(listener))

for signal in self._future_signals:
signal.where.update({"blueprint": self.name})
signal.condition.update({"blueprint": self.name})
app._apply_signal(signal)

self.routes = [route for route in routes if isinstance(route, Route)]
Expand All @@ -239,9 +239,9 @@ def register(self, app, options):
self.listeners = dict(listeners)

async def dispatch(self, *args, **kwargs):
where = kwargs.pop("where", {})
where.update({"blueprint": self.name})
kwargs["where"] = where
condition = kwargs.pop("condition", {})
condition.update({"blueprint": self.name})
kwargs["condition"] = condition
await asyncio.gather(
*[app.dispatch(*args, **kwargs) for app in self.apps]
)
Expand Down
8 changes: 4 additions & 4 deletions sanic/mixins/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def signal(
event: str,
*,
apply: bool = True,
where: Dict[str, Any] = None,
condition: Dict[str, Any] = None,
) -> Callable[[SignalHandler], FutureSignal]:
"""
For creating a signal handler, used similar to a route handler:
Expand All @@ -37,17 +37,17 @@ async def signal_handler(thing, **kwargs):
:type event: str
:param apply: For lazy evaluation, defaults to True
:type apply: bool, optional
:param where: For use with the ``where`` argument in dispatch
:param condition: For use with the ``condition`` argument in dispatch
filtering, defaults to None
:type where: Dict[str, Any], optional
:type condition: Dict[str, Any], optional
"""

def decorator(handler: SignalHandler):
nonlocal event
nonlocal apply

future_signal = FutureSignal(
handler, event, HashableDict(where or {})
handler, event, HashableDict(condition or {})
)
self._future_signals.add(future_signal)

Expand Down
2 changes: 1 addition & 1 deletion sanic/models/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ class FutureStatic(NamedTuple):
class FutureSignal(NamedTuple):
handler: SignalHandler
event: str
where: Optional[Dict[str, str]]
condition: Optional[Dict[str, str]]
20 changes: 10 additions & 10 deletions sanic/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def __init__(self) -> None:
def get( # type: ignore
self,
event: str,
where: Optional[Dict[str, str]] = None,
condition: Optional[Dict[str, str]] = None,
):
extra = where or {}
extra = condition or {}
try:
return self.resolve(f".{event}", extra=extra)
except NotFound:
Expand All @@ -56,9 +56,9 @@ async def _dispatch(
self,
event: str,
context: Optional[Dict[str, Any]] = None,
where: Optional[Dict[str, str]] = None,
condition: Optional[Dict[str, str]] = None,
) -> None:
signal, handlers, params = self.get(event, where=where)
signal, handlers, params = self.get(event, condition=condition)

signal_event = signal.ctx.event
signal_event.set()
Expand All @@ -67,7 +67,7 @@ async def _dispatch(

try:
for handler in handlers:
if where is None or where == handler.__requirements__:
if condition is None or condition == handler.__requirements__:
maybe_coroutine = handler(**params)
if isawaitable(maybe_coroutine):
await maybe_coroutine
Expand All @@ -79,13 +79,13 @@ async def dispatch(
event: str,
*,
context: Optional[Dict[str, Any]] = None,
where: Optional[Dict[str, str]] = None,
condition: Optional[Dict[str, str]] = None,
) -> asyncio.Task:
task = self.ctx.loop.create_task(
self._dispatch(
event,
context=context,
where=where,
condition=condition,
)
)
await asyncio.sleep(0)
Expand All @@ -95,7 +95,7 @@ def add( # type: ignore
self,
handler: SignalHandler,
event: str,
where: Optional[Dict[str, Any]] = None,
condition: Optional[Dict[str, Any]] = None,
) -> Signal:
parts = path_to_parts(event, self.delimiter)

Expand All @@ -111,12 +111,12 @@ def add( # type: ignore
else:
name = event

handler.__requirements__ = where # type: ignore
handler.__requirements__ = condition # type: ignore

return super().add(
event,
handler,
requirements=where,
requirements=condition,
name=name,
overwrite=True,
) # type: ignore
Expand Down
6 changes: 3 additions & 3 deletions tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def sync_signal(baz):
async def test_dispatch_signal_triggers_with_requirements(app):
counter = 0

@app.signal("foo.bar.baz", where={"one": "two"})
@app.signal("foo.bar.baz", condition={"one": "two"})
def sync_signal(*_):
nonlocal counter
counter += 1
Expand All @@ -110,7 +110,7 @@ def sync_signal(*_):

await app.dispatch("foo.bar.baz")
assert counter == 0
await app.dispatch("foo.bar.baz", where={"one": "two"})
await app.dispatch("foo.bar.baz", condition={"one": "two"})
assert counter == 1


Expand Down Expand Up @@ -216,7 +216,7 @@ async def do_wait():
app.blueprint(bp)
app.signal_router.finalize()
signal, *_ = app.signal_router.get(
"foo.bar.baz", where={"blueprint": "bp"}
"foo.bar.baz", condition={"blueprint": "bp"}
)

await bp.dispatch("foo.bar.baz")
Expand Down

0 comments on commit 5c1e146

Please sign in to comment.