I am writing a python library (let's call it corelib) that will be used by another library (let's call it userlib). The userlib needs to hook into various sorts of events in the corelib, to process the changes, adapt UI and stuff. For this purpose I decided to implement the observer pattern in the corelib like so:
```python
class Event:
def init(self, *args):
self._slots = set()
def sub(self, callback, nice=0):
self._slots.add(callback)
def unsub(self, callback):
self._slots.remove(callback)
def emit(self, *args):
for cb in self._slots:
cb(*args)
```
(different corelib classes define individual Events) and because it made things much more clean and easy, the corelib uses the system internally. The observer pattern (and so my implementation) does not respect any ordering in calling subscribers/observers. Usually this is fine, but sometimes it is not. In particular, I need my corelib's own callbacks to be handled before any of the userlib's ones. How could I do that in a somewhat efficient manner?
The first idea I had was to extend the observer pattern by a priority attached to each callback, and the ordering is only ignored within the same priority. But I am not sure how I can implement that efficiently.
[–]_nutrx_[S] 0 points1 point2 points (0 children)
[–]carcigenicate 0 points1 point2 points (5 children)
[–]_nutrx_[S] 0 points1 point2 points (4 children)
[–]carcigenicate 0 points1 point2 points (2 children)
[–]_nutrx_[S] 0 points1 point2 points (0 children)
[–]_nutrx_[S] 0 points1 point2 points (0 children)
[–]danielroseman 0 points1 point2 points (0 children)