Properly using multiple resources with Rx

rx catch
try-with multiple resources java 8 example
forkjoin
forkjoin error handling
rxjs zip
multiple resources can be used in try-with-resources
managing multiple medications
nested try-with-resources

I need to use multiple disposable resources with Rx. This is how I have nested the Observable.Using statements (the inner source is just for testing).

var obs = Observable.Using(
    () => new FileStream("file.txt", FileMode.Open),
    fs => Observable.Using(
        () => new StreamReader(fs),
        sr => Observable.Create<string>(o =>
            TaskPoolScheduler.Default.ScheduleAsync(async (sch, ct) =>
            {
                while (!ct.IsCancellationRequested)
                {
                    var s = await sr.ReadLineAsync().ConfigureAwait(false);
                    if (s is null) break;

                    o.OnNext(s);
                }
                o.OnCompleted();
            }))));

obs.Subscribe(Console.WriteLine);

Is there a more concise way to use multiple disposable resources?

I can't think of a general way to use unlimited number of resources, but at least you could make helper methods for the common cases of 2-3 resources. Here is an implementation for two:

public static IObservable<TResult> Using<TResult, TResource1, TResource2>(
    Func<TResource1> resourceFactory1,
    Func<TResource1, TResource2> resourceFactory2,
    Func<TResource1, TResource2, IObservable<TResult>> observableFactory)
    where TResource1 : IDisposable
    where TResource2 : IDisposable
{
    return Observable.Using(resourceFactory1, resource1 => Observable.Using(
        () => resourceFactory2(resource1),
        resource2 => observableFactory(resource1, resource2)));
}

Usage example:

var obs = Using(
    () => new FileStream("file.txt", FileMode.Open),
    (fs) => new StreamReader(fs),
    (fs, sr) => Observable.Create<string>(o =>
        TaskPoolScheduler.Default.ScheduleAsync(async (sch, ct) =>
        {
            while (!ct.IsCancellationRequested)
            {
                var s = await sr.ReadLineAsync().ConfigureAwait(false);
                if (s is null) break;

                o.OnNext(s);
            }
            o.OnCompleted();
        })));

Advanced error handling, A useful method in Rx for handling an OnError notification is the Catch The Using factory method allows you to bind the lifetime of a resource to the lifetime //Repeats the source observable sequence until it successfully terminates. the use of marble diagrams to help us visualize the combination of multiple sequences. Properly using multiple resources with Rx I need to use multiple disposable resources with Rx. This is how I have nested the Observable.Using statements (the inner source is just for testing).

Usually when I have a set of resources to be used in a cold observable, I stick with the create pattern which is much easier to read.

var obs = Observable.Create<string>(observer =>
{

    var fs = new FileStream("file.txt", FileMode.Open);
    var sr = new StreamReader(fs);
    var task = TaskPoolScheduler.Default.ScheduleAsync(async (_, ct) =>
    {
        while (!ct.IsCancellationRequested)
        {
            var s = await sr.ReadLineAsync().ConfigureAwait(false);
            if (s is null) break;

            observer.OnNext(s);
        }
        observer.OnCompleted();
    });


    return new CompositeDisposable(task, fs, sr);
});

How to Specify Multiple Resources in a Single Try-With-Resources , With a real resource, the implication of this is that the resource is not closed properly. The next code listing demonstrates the correct approach for� This page has been locked due to a period of inactivity. Please enter your password to continue. Username: * Password:

I'd just continue to use the double Using pattern, but I'd clean up the internals of how you're reading the lines. Try this:

var obs =
    Observable.Using(() => new FileStream("file.txt", FileMode.Open),
        fs => Observable.Using(() => new StreamReader(fs),
            sr =>
                Observable
                    .Defer(() => Observable.FromAsync(() => sr.ReadLineAsync()))
                    .Repeat()
                    .TakeWhile(x => x != null)));

You should try to avoid Observable.Create where possible.

Cleaner observable queries means that the Using operators don't look so out-of-place.

forkJoin, One common use case for this is if you wish to issue multiple requests on and you are concerned with the previous emissions forkJoin is not the correct choice. The ability to specify multiple resources in a single try-with-resources statement is a feature introduced in Java 7. But, it can be fraught with peril if not used carefully.

Help for Managing Multiple Medications, Nearly 40 percent of older adults take five or more prescription drugs. The best approach: “When you're using several medications, be proactive,” says Jessica� I usually do better when I only concentrate in 1-2 resource(s) instead of using multiple, and I am afraid if I use too many resources to prepare for step 1, I might not master the materials that I need to know cold for the exam My question is: If you had to choose between

[PDF] Rx for Understanding: Preventing Prescription Drug Abuse, Lesson 3: Understanding Proper Use of Prescription Drugs . is predicated on proper use. The objective of this resource is to change the way teens view prescription drugs, use in multiple subject areas as the main source of instruction or� Use the following 9 tips to use your drugs properly and to stay healthy! Know what you are taking. When it comes to medications–especially powerful medications for serious conditions–what you don’t know can hurt you. The more information you have about the drugs you are taking, the more you will be able to be sure that you are using it

[PDF] Standardize the Dosing Designations on Prescription , Resource. Standardize the Dosing Designations on Prescription Container Labels for prescription container label dosing designations to guide measurement of the proper The use of multiple volumetric units (e.g., teaspoons, tablespoons,� Multiple LEDs & Breadboards With Arduino in Tinkercad: Let's learn how to control multiple LEDs using Arduino’s digital outputs and a breadboard. Expanding upon the last lesson on blinking an LED, We'll connect some LEDs to the Arduino Uno and compose a simple program to light them up in a pattern.

Comments
  • Will this work if a subscriber neglects to dispose the subscription? Or in that case the resources will remain undisposed?
  • When it completes, it will auto-detach and dispose.
  • You can test this by adding a Disposable.Create(() => Console.WriteLine("Done!")) to the list.
  • Yes, observables will automatically dispose of all subscriptions when they error or complete.
  • While this is way more readable it has an issue when any of the lines after the creation of a disposable resource throw: Normally, with a using statement it would properly dispose the resource, but in this case that won't happen. As the returned disposable object is not created in that case either, the resource is not properly disposed.