Saturday, 15 September 2012

c# - Observable that produces a single item per subscription - one event per handler -



c# - Observable that produces a single item per subscription - one event per handler -

let's expose observable notifies either immediately, if net connection available right now, or if device not connected net notification pushed out when become available:

iobservable<datetime> internetbecameavailablesignalledoncepersubscriber { get; }

furthermore, there should 1 notification per subscription without requiring subscriber .take(1) or that.

i.e. client depends on net resource utilize observable right or net becomes available, not more 1 time - there no more signalling subscriber if net became unavailable , available sec time..

how implemented reactive extensions (rx) ?

please ignore flawed notion net can become available such, think of google.com beingness reachable or whatever flavored implementation prefer

this should solved rx. question how know if net available? based on other consumers subscribing, based on method (like connect() method) or event beingness pushed (like wcf channel state changed event)?

depending on answer, seems need encapsulate take(1) , replay(1).

public class iserviceclient { iobservable<datetime> lastconnnected { get; } } public class serviceclient : iserviceclient, idisposable { private readonly idisposable _connection; private readonly iobservable<datetime> _lastconnnected; public serviceclient() { //question 1) 'connected' sequence come i.e. tells you have net connectivity? //question 2) when should subscription made 'connected'? here cheat , in ctor, not great. var connected = connected.replay(1) .where(isconnected=>isconnected) .take(1) .select(_=>datetime.utcnow); _lastconnnected = connected; _connection = connected.connect(); } public iobservable<datetime> lastconnnected{ {return _lastconnnected; } } public void dispose() { _connection.dispose(); } }

this leave other questions reply e.g. thing tells if have net connectivity , resource management plan this?

updated code

public interface iserviceclient { iobservable<datetime> lastconnnected { get; } } public class serviceclient : iserviceclient, idisposable { private readonly idisposable _connection; private readonly iobservable<bool> _lastconnnected; public serviceclient(iobservable<connectionstate> connectedstates) { var cachedstates = connectedstates.select(state=>state.isconnected).replay(1); _lastconnnected = cachedstates; _connection = cachedstates.connect(); } public iobservable<datetime> lastconnnected { { homecoming _lastconnnected.startwith(isconnected()) .where(isconnected=>isconnected) .take(1) .select(_=>datetime.utcnow); } } //.... public void dispose() { _connection.dispose(); } }

c# .net linq system.reactive

No comments:

Post a Comment