Frans-Willem / stlab-extras

Library with extra helper functions in an stlab::concurrency world
1 stars 1 forks source link

Observable API questions #2

Open Frans-Willem opened 6 years ago

Frans-Willem commented 6 years ago

stlab::future's have proven to be immensely useful to me, although one thing missing is a future-like object that can change value over time.

ReactiveX has some nice documentation on Observables, which are close to what I'm seeking, and some of it's properties: http://reactivex.io/documentation/observable.html Unfortunately, I don't think it's current C++ api plays well with an asynchronous world like in boost::asio and stlab's concurrency libraries, which is why I'd like to write something else.

Something I'm not yet sure about is the choice between what I'd call a dynamic value and an observable. The former would pertain an object that would have a value over time, which could change, but the value of the object would 'remain' that of the last update. The latter is more like a stream of items, where items are emitted, possibly duplicate, but there is no 'state' inbetween those emmissions.

The most important contraints on the API to me are:

The first constraint means that if you take one observable A, map a transformation function F over it, and get a new observable B, that there may be a (small or large, depending on F) period where B does not yet have a value.

The second constraint means that there should be a 'return' function, which takes a normal value a of type A, and returns something of type observable<A>, as well as a >>=, which takes an observable and a function from A to observable<B>, and returns an observable<B>. The former (return) should be relatively easy, but the latter is more difficult. ReactiveX's FlatMap comes close for the 'stream' kind of object, but the interleaving of green and blue in the example makes no sense in the dynamic-value world.

Another issue that needs attention is that of exceptions. If you apply a function to an observable, and at some point it throws an exception, will the resulting observable stay in an excepted state? or can it recover once a new value is emitted on the original observable?

Frans-Willem commented 6 years ago

The >>= function for the case of dynamic values is also slightly more complex. It appears simple that whenever the input observable changes value, we should unsubscribe from the previous transformed observable and subscribe to whatever the transform function spits out. But what value does the resulting dynamic value have while the newly returned observable does not yet contain a value ? Or should we remain subscribed to the first transformed observable, and only when the new transformed observable spits out a value unsubscribe and start following that one ?