c# - Correct way to merge observable sequences for events fired from multiple instances -



c# - Correct way to merge observable sequences for events fired from multiple instances -

say have mill method churns out instances of type t, , want rx observable sequence events fired instances originating out of mill method.

is using merge() have done below right , optimal way accomplish this?

the other way did utilize static event , create observable sequence out of that, don't using static events , curious rx experts think optimal in situation?

public t makefoo<t>() t: foo, new() { this.instanceofobservable.merge(new t()); homecoming self; } public class observablesequence : iobservable<eventargs>, idisposable { private iobservable<eventargs> stream; public observablesequence() { } // method called in mill method each new instance of t public void merge(foo instance) { if (this.stream == null) { this.stream = init(instance); } else { iobservable<eventargs> next = init(instance); this.stream.merge(next); } } private iobservable<eventargs> init(foo instance) { homecoming observable.fromevent <eventhandler<eventargs>, eventargs>(handler => { eventhandler<eventargs> eh = (sender, e) => { handler(e); }; homecoming eh; }, eh => instance.someevent += eh , eh => instance.someevent -= eh ) .repeat() .retry() .publish() .refcount(); } public void dispose() { } public idisposable subscribe(iobserver<eventargs> observer) { homecoming stream.subscribe(observer); } }

abusing merge not efficient. straightforward way via subject, iobservable command hand.

public static class myawesomefoofactory { readonly static subject<someeventargs> someevents = new subject<someeventargs>(); public static iobservable<someeventargs> notificationsfromalltheevents { { homecoming someevent; }} public static foo makeanewfoo() { var ret = new foo(); ret.someevent.subscribe(someevents); // nb: never unsubscribe, *evar* homecoming ret; } }

c# .net events system.reactive observer-pattern

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -