Skip to content

Commit 7c49e88

Browse files
committed
Update the example to show how to receive only changed messages
Now that `ChangedOnly` was removed it makes more sense to use that as an example instead of a more obscure example using `is` for comparison. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent e122ec7 commit 7c49e88

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/frequenz/channels/experimental/_predicates.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,25 @@ class WithPrevious(Generic[ChannelMessageT]):
2828
process messages only if they satisfy a particular condition with respect to the
2929
previous message.
3030
31-
Example: Receiving only messages that are not the same instance as the previous one.
31+
Example: Receiving only messages that are different from the previous one.
3232
```python
3333
from frequenz.channels import Broadcast
3434
from frequenz.channels.experimental import WithPrevious
3535
36-
channel = Broadcast[int | bool](name="example")
37-
receiver = channel.new_receiver().filter(WithPrevious(lambda old, new: old is not new))
36+
channel = Broadcast[int](name="example")
37+
receiver = channel.new_receiver().filter(WithPrevious(lambda old, new: old != new))
3838
sender = channel.new_sender()
3939
4040
# This message will be received as it is the first message.
4141
await sender.send(1)
4242
assert await receiver.receive() == 1
4343
44-
# This message will be skipped as it is the same instance as the previous one.
44+
# This message will be skipped as it equals to the previous one.
4545
await sender.send(1)
4646
47-
# This message will be received as it is a different instance from the previous
48-
# one.
49-
await sender.send(True)
50-
assert await receiver.receive() is True
47+
# This message will be received as it is a different from the previous one.
48+
await sender.send(0)
49+
assert await receiver.receive() == 0
5150
```
5251
5352
Example: Receiving only messages if they are bigger than the previous one.

0 commit comments

Comments
 (0)