/// <summary> /// Queue an operation to run in the background that returns a stream of values. All operations with the same key will run in sequence, /// waiting for the previous operation to complete. /// If you want to queue an operation that already returns IObservable, this is your guy. /// </summary> /// <param name="key">The key to use.</param> /// <param name="asyncCalculationFunc">A method to run in the background that returns a stream of values.</param> /// <typeparam name="T">The type of value in the queue.</typeparam> /// <returns>A future stream of values.</returns> public IObservable <T> EnqueueObservableOperation <T>(string key, Func <IObservable <T> > asyncCalculationFunc) { int id = Interlocked.Increment(ref _sequenceNumber); key ??= "__NONE__"; this.Log().Debug(CultureInfo.InvariantCulture, "Queuing operation {0} with key {1}", id, key); var item = new KeyedOperation <T>(asyncCalculationFunc, key, id); _queuedOps.OnNext(item); return(item.Result); }
/// <summary> /// Queue an operation to run in the background that returns a stream of values. All operations with the same key will run in sequence, /// waiting for the previous operation to complete. /// If you want to queue an operation that already returns IObservable, this is your guy. /// </summary> /// <param name="key">The key to use</param> /// <param name="asyncCalculationFunc">A method to run in the background that returns a stream of values</param> /// <returns>A future stream of values</returns> public IObservable <T> EnqueueObservableOperation <T>(string key, Func <IObservable <T> > asyncCalculationFunc) { int id = Interlocked.Increment(ref sequenceNumber); key = key ?? "__NONE__"; this.Log().Debug("Queuing operation {0} with key {1}", id, key); var item = new KeyedOperation <T> { Key = key, Id = id, Func = asyncCalculationFunc, }; queuedOps.OnNext(item); return(item.Result); }
static IObservable <KeyedOperation> ProcessOperation(KeyedOperation operation) { return(Observable.Defer(operation.EvaluateFunc) .Select(_ => operation) .Catch(Observable.Return(operation))); }