Using

create a disposable resource that has the same lifespan as the Observable

Using

The Using operator is a way you can instruct an Observable to create a resource that exists only during the lifespan of the Observable and is disposed of when the Observable terminates.

See Also

Language-Specific Information:

using

You pass the using operator three parameters:

  1. a factory function that creates a disposable resource
  2. a factory function that creates an Observable
  3. a function that disposes of the resource

When an observer subscribes to the Observable returned from using, using will use the Observable factory function to create the Observable the observer will observe, while at the same time using the resource factory function to create whichever resource you have designed it to make. When the observer unsubscribes from the Observable, or when the Observable terminates (normally or with an error), using will call the third function to dispose of the resource it created.

using does not by default operate on any particular Scheduler.

using

You pass the using operator three parameters:

  1. a factory function that creates a disposable resource
  2. a factory function that creates an Observable
  3. a function that disposes of the resource

When an observer subscribes to the Observable returned from using, using will use the Observable factory function to create the Observable the observer will observe, while at the same time using the resource factory function to create whichever resource you have designed it to make. When the observer unsubscribes from the Observable, or when the Observable terminates (normally or with an error), using will call the third function to dispose of the resource it created.

using does not by default operate on any particular Scheduler.

using

You pass the using operator two parameters:

  1. a factory function that creates a disposable resource
  2. a factory function that creates an Observable

When an observer subscribes to the Observable returned from using, using will use the Observable factory function to create the Observable the observer will observe, while at the same time using the resource factory function to create whichever resource you have designed it to make. To dispose of the resource, call the dispose method of the subscription that was returned from the subscribe call you used to subscribe an observer to the Observable that you modified with using.

Sample Code

/* Using an AsyncSubject as a resource which supports the .dispose method */
function DisposableResource(value) {
  this.value = value;
  this.disposed = false;
}

DisposableResource.prototype.getValue = function () {
  if (this.disposed) {
    throw new Error('Object is disposed');
  }
  return this.value;
};

DisposableResource.prototype.dispose = function () {
  if (!this.disposed) {
    this.disposed = true;
    this.value = null;
  }
  console.log('Disposed');
};

var source = Rx.Observable.using(
  function () { return new DisposableResource(42); },
  function (resource) {
    var subject = new Rx.AsyncSubject();
    subject.onNext(resource.getValue());
    subject.onCompleted();
    return subject;
  }
);

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); });
Next: 42
Completed
subscription.dispose();
Disposed

using is found in each of the following distributions:

  • rx.js
  • rx.compat.js