pyapp-kit / psygnal

Python observer pattern (callback/event system). Modeled after Qt Signals & Slots (but independent of Qt)
https://psygnal.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
84 stars 13 forks source link

feat: add priority to connect, to control callback order #285

Closed tlambert03 closed 7 months ago

tlambert03 commented 8 months ago

This adds the ability to control the order in which callbacks are called at connection time. Higher priority connections are called first, and negative priorities are allowed. with default 0

sig = SignalInstance()

calls = []

sig.connect(lambda: calls.append(1))
sig.connect(lambda: calls.append(2), priority=5)
sig.connect(lambda: calls.append(3), priority=-5)
sig.connect(lambda: calls.append(4), priority=10)

sig.emit()
assert calls == [4, 2, 1, 3]
codecov[bot] commented 8 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 100.00%. Comparing base (63573f3) to head (8696e69).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #285 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 22 22 Lines 1919 1931 +12 ========================================= + Hits 1919 1931 +12 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codspeed-hq[bot] commented 8 months ago

CodSpeed Performance Report

Merging #285 will not alter performance

Comparing tlambert03:priority (8696e69) with main (63573f3)

Summary

✅ 66 untouched benchmarks

tlambert03 commented 8 months ago

@Czaki, let me know if you have any feelings about this feature or its implementation. This is similar to position in vispy's events... but without the whole before/after positioning functions. The connector simply gives a priority number by which to order the callbacks; with zero being the default, and higher priority callbacks getting placed before lower priority ones.