Re: How to implement observer (publish/subscribe) pattern?

2020-05-27 Thread Pixeye
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.

Re: How to implement observer (publish/subscribe) pattern?

2020-05-27 Thread Kosteg
I can do this but it's not convenient to have all the subscribers be of the same class

Re: How to implement observer (publish/subscribe) pattern?

2020-05-23 Thread snej
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

Re: How to implement observer (publish/subscribe) pattern?

2020-05-21 Thread Kosteg
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):

Re: How to implement observer (publish/subscribe) pattern?

2020-05-20 Thread dawkot
This should be enough, you can capture whatever type you want inside a closure. type Publisher = ref object subs: seq[proc(data: int)] Run

How to implement observer (publish/subscribe) pattern?

2020-05-20 Thread Kosteg
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?