> For the complete documentation index, see [llms.txt](https://soufatn.gitbook.io/rxjs-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soufatn.gitbook.io/rxjs-book/summary/disposables/ref_count_disposable.md).

# Rx.RefCountDisposable

Represents a disposable resource that only disposes its underlying disposable resource when all `getDisposable` dependent disposable objects have been disposed.

## Usage #\#

The follow example shows the basic usage of an `Rx.RefCountDisposable`.

```javascript
var disposable = Rx.Disposable.create(() => console.log('disposed'));

var refCountDisposable = new Rx.RefCountDisposable(disposable);

// Try disposing before the underlying is disposed
refCountDisposable.dispose();

console.log(refCountDisposable.isDisposed);
// => false

// Dispose the underlying disposable
disposable.dispose();
// => disposed

// Now dispose the primary
refCountDisposable.dispose();

console.log(refCountDisposable.isDisposed);
// => true
```

### Location

* rx.js

## `RefCountDisposable Constructor` #\#

* [`constructor`](/rxjs-book/summary/disposables/ref_count_disposable.md#rxrefcountdisposabledisposable)

## `RefCountDisposable Instance Methods` #\#

* [`dispose`](/rxjs-book/summary/disposables/ref_count_disposable.md#rxrefcountdisposableprototypedispose)
* [`getDisposable`](/rxjs-book/summary/disposables/ref_count_disposable.md#rxrefcountdisposableprototypegetdisposable)

## `RefCountDisposable Instance Properties` #\#

* [`isDisposed`](/rxjs-book/summary/disposables/ref_count_disposable.md#isdisposed)

## *RefCountDisposable Constructor* #\#

### `Rx.RefCountDisposable(disposable)` <a href="#rxrefcountdisposable" id="rxrefcountdisposable"></a>

[#](/rxjs-book/summary/disposables/ref_count_disposable.md#rxrefcountdisposable) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/disposables/rxrefcountdisposable.js#L7-L10)

Initializes a new instance of the `Rx.RefCountDisposable` class with the specified disposable

#### Arguments

1. `disposable` *(Disposable)*: Underlying disposable.

#### Example

```javascript
var disposable = Rx.Disposable.create(() => console.log('disposed'));

var refCountDisposable = new Rx.RefCountDisposable(disposable);

console.log(refCountDisposable.isDisposed);
// => false
```

### Location

* rx.js

## *RefCountDisposable Instance Methods* #\#

### `Rx.RefCountDisposable.prototype.dispose()` <a href="#rxrefcountdisposableprototypedispose" id="rxrefcountdisposableprototypedispose"></a>

[#](/rxjs-book/summary/disposables/ref_count_disposable.md#rxrefcountdisposableprototypedispose) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/disposables/rxrefcountdisposable.js#L30-L35)

Disposes the underlying disposable only when all dependent disposables have been disposed.

#### Example

```javascript
var disposable = Rx.Disposable.create(() => console.log('disposed'));

var refCountDisposable = new Rx.RefCountDisposable(disposable);

// Try disposing before the underlying is disposed
refCountDisposable.dispose();

console.log(refCountDisposable.isDisposed);
// => false

// Dispose the underlying disposable
disposable.dispose();
// => disposed

// Now dispose the primary
refCountDisposable.dispose();

console.log(refCountDisposable.isDisposed);
// => true
```

### Location

* rx.js

### `Rx.RefCountDisposable.prototype.getDisposable()` <a href="#rxrefcountdisposableprototypegetdisposable" id="rxrefcountdisposableprototypegetdisposable"></a>

[#](/rxjs-book/summary/disposables/ref_count_disposable.md#rxrefcountdisposableprototypegetdisposable) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/disposables/rxrefcountdisposable.js#L18-L20)

Returns a dependent disposable that when disposed decreases the refcount on the underlying disposable.

#### Returns

*(Disposable)*: A dependent disposable contributing to the reference count that manages the underlying disposable's lifetime.

#### Example

```javascript
var disposable = Rx.Disposable.create(() => console.log('disposed'));

var refCountDisposable = new Rx.RefCountDisposable(disposable);

var d = refCountDisposable.getDisposable();

console.log(d === disposable);
// => false

// Clean up disposables
disposable.dispose();
d.dispose();

// Now try to dispose the main
refCountDisposable.dispose();

console.log(refCountDisposable.isDisposed);
// => true
```

### Location

* rx.js

## *RefCountDisposable Instance Properties* #\#

### `isDisposed` <a href="#isdisposed" id="isdisposed"></a>

[#](/rxjs-book/summary/disposables/ref_count_disposable.md#isdisposed) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/disposables/rxrefcountdisposable.js#L8)

Gets a value that indicates whether the object is disposed.

#### Example

```javascript
var disposable = Rx.Disposable.create(() => console.log('disposed'));

var refCountDisposable = new Rx.RefCountDisposable(disposable);

disposable.dispose();

console.log(refCountDisposable.isDisposed);
// => false

refCountDisposable.dispose();
// => disposed

console.log(refCountDisposable.isDisposed);
// => true
```

### Location

* rx.js
