upsies.utils.signal
Managing callbacks
Classes
- class upsies.utils.signal.Signal(id=None, *, signals)[source]
Bases:
objectSimple callback registry
- property id
Instance ID used for debugging
- add(signal, *, record=False)[source]
Create new signal
- Parameters:
signal (hashable) – Any hashable object
record (bool) – Whether emissions of this signal are recorded in
emissions_recorded
- Raises:
TypeError – if signal is not hashable
RuntimeError – if signal has been added previously
- property signals
Mutable dictionary of signals mapped to lists of callbacks
- property recording
class:list of signals that are recorded
- register(signal, callback)[source]
Call callback when signal is emited
- Parameters:
signal (hashable) – Previously added signal
callback (callable) – Any callback. The signature depends on the caller of
emit().
- Raises:
ValueError – if signal was not added first
TypeError – if callback is not callable
- emit(signal, *args, **kwargs)[source]
Call callbacks that are registered to signal and make any ongoing
receive_all()calls iterate and any ongoingreceive_one()calls return- Parameters:
signal (hashable) – Previously added signal
Any other arguments are passed on to the callbacks.
- async receive_all(signal, *, only_posargs=False)[source]
Iterate over
(args, kwargs)fromemit()calls for signalThis will always produce all emitted signals, both from the past and the future, until
stop()is called. The only exception is whenstop()is called withimmediately=True, in which case iteration stops immediately.
- async receive_one(signal, *, only_posargs=False)[source]
Same as
receive_all(), but return just one emission
- stop(*, immediately=False)[source]
Do not
emit()any more signalsCalling this method disallows further calls to
emit(), iterating over any ongoingreceive_all()calls stops, and any ongoingreceive_one()calls return None- Parameters:
immediately (bool) –
If truthy
receive_all()calls stop iterating NOW without producing any more emissions andreceive_one()calls return NoneOtherwise, any emissions made before :meth`stop` is called are processed normally before iteration stops.
- property is_stopped
Whether
stop()was called (seeStopped)This means
emit()can no longer be called, any ongoingreceive_all()calls will stop iterating, and any ongoingreceive_one()calls return None.
- property emissions
Sequence of
emit()calls made so farEach call is stored as a tuple like this:
(<signal>, {"args": <positional arguments>, "kwargs": <keyword arguments>})
- property emissions_recorded
Sequence of only recorded
emit()calls made so farEach call is stored as a tuple like this:
(<signal>, {"args": <positional arguments>, "kwargs": <keyword arguments>})
- suspend(*signals)[source]
Context manager that blocks certain signals in its body
- Parameters:
signals – Which signals to block
- Raises:
ValueError – if any signal in signals is not registered