Examples

Here a few examples to use this library functionality and type hints.

Simple overview to instance the connectable objects and connect them a function.
 1from emitcallback import Single, Signal, Queue
 2
 3# example method we want to connect.
 4def callback():
 5	print("Hello World!")
 6
 7# create a single and connect the callback.
 8single = Single()
 9single.connect(callback)
10
11# create a signal and connect the callback.
12signal = Signal()
13signal.connect(callback)
14
15# create a queue and connect the callback.
16queue = Queue()
17queue.connect(callback)
Connects twince the function using mutiple flags at once.
 1from emitcallback import Signal, SINGLE_ONE_SHOT, SIGNAL_MULTI_CONNECT
 2
 3def callback():
 4	print("Hello World!")
 5
 6s = Signal()
 7
 8# allows the function to be connected more than once and that on the next emittion will be disconnected.
 9s.connect(callback, flags = SINGLE_ONE_SHOT|SIGNAL_MULTI_CONNECT)
10s.connect(callback, flags = SIGNAL_MULTI_CONNECT)
11
12# calls both function.
13s.emit()
14# calls only the one that wasn't connected with oneshot.
15s.emit()
# This on the first emittion.
> "Hello World"
> "Hello World"

# This on the second emittiong, the first connection has been removed by oneshot.
> "Hello World"
Shows how each kind of callables can be connected.
 1from emitcallback import Signal
 2
 3def function():
 4	print("Hello Function!")
 5
 6class Class:
 7
 8	def method(self):
 9		print("Hello Class!")
10
11obj = Class()
12
13# any connections happens the same way!
14s = Signal()
15s.connect(lambda: print("Hello Lambda!"))
16s.connect(function)
17s.connect(obj.method)
18
19s.emit()
Shows how a method weak connection works when the garbage collector destroy the object.
 1from emitcallback import Signal
 2
 3class Class:
 4
 5	def method(self):
 6		print("Hello Class!")
 7
 8# here object is created.
 9obj = Class()
10
11# here method (using weak reference is connected) is connected.
12s = Signal()
13s.connect(obj.method)
14
15# garbage collector destroy the object because there are no more references of it.
16# emittion won't call method because it got disconnected here.
17del obj
18
19s.emit()
Shows how a method strong connection can be created.
 1from emitcallback import Signal, SIGNAL_NO_WEAK
 2
 3class Class:
 4
 5	def method(self):
 6		print("Hello Class!")
 7
 8# here object is created.
 9obj = Class()
10
11# here method (using weak reference is connected) is connected.
12s = Signal()
13s.connect(obj.method, flags = SIGNAL_NO_WEAK)
14
15# garbage collector reduces reference but object has still one left from the signal.
16del obj
17
18# calls the method. here the obj variable is no longer avaiable but object still exist.
19s.emit()

Note

Functions and lambdas always use a strong connection, weak connections are only for methods.

Shows how to create a signal and function with type hints.
 1from emitcallback import Signal, Single, Queue
 2
 3def callback(value: int) -> None:
 4	print(f"Value is {value} !")
 5
 6# put list of argouments type you expect separated by commas.
 7s1 = Signal[int]()
 8
 9# type hints will show a warning if the callback doesn't match his signature
10# or if emit doesn't have all argouments.
11s1.connect(callback)
12s1.emit(10)
> "Value is 10!"
More showcase of type hints.
 1from emitcallback import Signal, Single, Queue
 2
 3# other combinations of type hints:
 4Signal[int]()
 5Signal[str, int]()
 6Signal[int, str, bool]()
 7Signal[list[int], bool]()
 8
 9Single[int]()
10Single[str, int]()
11Single[int, str, bool]()
12Single[list[int], bool]()
13
14Queue[int]()
15Queue[str, int]()
16Queue[int, str, bool]()
17Queue[list[int], bool]()
18
19# using it with variables:
20s1 = Signal[int]()
21
22s2: Signal[int] = Signal()