How about more "reactive" observers ?
[https://gist.github.com/PixeyeHQ/fbec35b25b667b847b4eac413a8539a5](https://gist.github.com/PixeyeHQ/fbec35b25b667b847b4eac413a8539a5)
The idea is to subscribe for variable change. The proc you register will
trigger every time this variable changes.
I can do this but it's not convenient to have all the subscribers be of the
same class
I think you could also do it in OOP style by defining a Subscriber object with
a ‘changed’ method, then having the real subscribers subclass that and override
the method. (Requires ‘pragma multimethods’, I think? I have to admit I haven’t
tried any real OOP in Nim yet! I’d try it out in a playgr
Thank you! Works great:
import tables
type EventEmitter = ref object
eventsMap: Table[string, seq[proc(event: string)]]
proc subscribe(emitter: EventEmitter, event: string, callback: proc(event:
string)) =
if not emitter.eventsMap.hasKey(event):
This should be enough, you can capture whatever type you want inside a closure.
type
Publisher = ref object
subs: seq[proc(data: int)]
Run
The publisher has to maintain the list of subscribers.
It's easy to implement if all the subscribers are of the same type (or of the
multiple pre-defined types).
But is it possible to do not specify all possible types of subscribers?