Skip to content

Commit

Permalink
Fix signals
Browse files Browse the repository at this point in the history
  • Loading branch information
mekanix committed Feb 19, 2024
1 parent 6b26b42 commit 6ec0849
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions docs/signals.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class AlbumAuditor:
auditor = AlbumAuditor()
pre_save(Album)(auditor.before_save)
# call above has same result like the one below
Album.Meta.signals.pre_save.connect(auditor.before_save)
Album.ormar_config.signals.pre_save.connect(auditor.before_save)
# signals are also exposed on instance
album = Album(name='Miami')
album.signals.pre_save.connect(auditor.before_save)
Expand All @@ -127,7 +127,7 @@ async def before_update(sender, instance, **kwargs):
instance.is_best_seller = True

# disconnect given function from signal for given Model
Album.Meta.signals.pre_save.disconnect(before_save)
Album.ormar_config.signals.pre_save.disconnect(before_save)
# signals are also exposed on instance
album = Album(name='Miami')
album.signals.pre_save.disconnect(before_save)
Expand Down Expand Up @@ -266,8 +266,8 @@ class Album(ormar.Model):
is_best_seller: bool = ormar.Boolean(default=False)
play_count: int = ormar.Integer(default=0)

Album.Meta.signals.your_custom_signal = ormar.Signal()
Album.Meta.signals.your_custom_signal.connect(your_receiver_name)
Album.ormar_config.signals.your_custom_signal = ormar.Signal()
Album.ormar_config.signals.your_custom_signal.connect(your_receiver_name)
```

Actually under the hood signal is a `SignalEmitter` instance that keeps a dictionary of know signals, and allows you
Expand All @@ -276,20 +276,20 @@ to access them as attributes. When you try to access a signal that does not exis
So example above can be simplified to. The `Signal` will be created for you.

```
Album.Meta.signals.your_custom_signal.connect(your_receiver_name)
Album.ormar_config.signals.your_custom_signal.connect(your_receiver_name)
```

Now to trigger this signal you need to call send method of the Signal.

```python
await Album.Meta.signals.your_custom_signal.send(sender=Album)
await Album.ormar_config.signals.your_custom_signal.send(sender=Album)
```

Note that sender is the only required parameter and it should be ormar Model class.

Additional parameters have to be passed as keyword arguments.

```python
await Album.Meta.signals.your_custom_signal.send(sender=Album, my_param=True)
await Album.ormar_config.signals.your_custom_signal.send(sender=Album, my_param=True)
```

0 comments on commit 6ec0849

Please sign in to comment.