Movatterモバイル変換


[0]ホーム

URL:


ReactiveX
  1. Operators
  2. Utility
  3. Using

Using

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

Using

TheUsing 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 theusing 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 fromusing,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 particularScheduler.

using

You pass theusing 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 fromusing,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 particularScheduler.

using

You pass theusing 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 fromusing,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 thedispose method of the subscription that was returned from thesubscribe call you used to subscribe an observer to the Observable that you modified withusing.

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: 42Completed
subscription.dispose();
Disposed

using is found in each of the following distributions:

  • rx.js
  • rx.compat.js

[8]ページ先頭

©2009-2025 Movatter.jp