function fnToDeferredObservable(fn) {
return Rx.Observable.defer(() => {
const result = fn();
let observable;
if (result instanceof Rx.Observable) {
observable = result;
} else if (result instanceof Promise) {
observable = Rx.Observable.fromPromise(result);
} else {
observable = Rx.Observable.return(result);
}
return observable;
});
}
function transactionEmmiter() {
const eventStream = RxReact.FuncSubject.create(() => () => action(...attr));
this.transactionStream = this.transactionStream.merge(eventStream);
}
function componentDidMount() {
this.transactionStream
.map(fnToDeferredObservable)
.concatAll()
.subscribe((result) => console.log(result),
(err) => console.log(err),
() => console.log('complete'));
}
<button className="btn btn-default" onClick={this.transactionEmmiter(this.props.increment, 1)}>
Increment
</button>
{' '}
<button className="btn btn-default" onClick={this.transactionEmmiter(this.props.double)}>
Double (Async)
</button>
<hr />
<button className="btn btn-default" onClick={this.transactionEmmiter(this.props.random)}>
Random Increment (Async)
</button>
The eventStream applies for DomEvents, merging them into the transaction stream, which by itself defers any given handler (and creating observables from their return values) concatAll, concats these new observables. The effect is, that any async function is queued and waits for the before event to resolve. Works so far, but .concatAll somehow just strips all events after I initially emitted a DomEvent, it’s just a short period of time (until the first Observable completes) where the emmiting Dom Events are handled by the transaction subscription at all.
Click Events
———2——>
—1————>
————3—>
Map Value to function expression
———y2——>
—y1————>
————y3—>
Merge
—y1—y2—y3—>
Map to deferred Observable
—y1—y2—y3—>
—x1—x2—x3—>
Concat all
—>xy1—>xy2—>xy2
expected output is:
result = y()
—>result1—>result2—>result3———>
example:
with input:
———2——>
—1————>
————3—>
——————————4—>
expected:
—>result1—>result2—>result3—>result4
actual:
—>result1—>result2—>result3