Last active
March 1, 2020 02:03
-
-
Save mattpodwysocki/5118923 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @name Rx | |
* @type Object | |
*/ | |
var Rx = { Internals: {} }; | |
/** | |
* @name CompositeDisposable | |
* @constructor | |
* @memberOf Rx | |
* Represents a group of disposable resources that are disposed together. | |
*/ | |
var CompositeDisposable = Rx.CompositeDisposable = function () { | |
this.disposables = argsOrArray(arguments, 0); | |
this.isDisposed = false; | |
this.length = this.disposables.length; | |
}; | |
/** | |
* Adds a disposable to the CompositeDisposable or disposes the disposable if the CompositeDisposable is disposed. | |
* @memberOf CompositeDisposable | |
* @param item Disposable to add. | |
*/ | |
CompositeDisposable.prototype.add = function (item) { | |
if (this.isDisposed) { | |
item.dispose(); | |
} else { | |
this.disposables.push(item); | |
this.length++; | |
} | |
}; | |
/** | |
* Removes and disposes the first occurrence of a disposable from the CompositeDisposable. | |
* @memberOf CompositeDisposable | |
* @param item Disposable to remove. | |
* @returns true if found; false otherwise. | |
*/ | |
CompositeDisposable.prototype.remove = function (item) { | |
var shouldDispose = false; | |
if (!this.isDisposed) { | |
var idx = this.disposables.indexOf(item); | |
if (idx !== -1) { | |
shouldDispose = true; | |
this.disposables.splice(idx, 1); | |
this.length--; | |
item.dispose(); | |
} | |
} | |
return shouldDispose; | |
}; | |
/** | |
* Disposes all disposables in the group and removes them from the group. | |
* @memberOf CompositeDisposable | |
*/ | |
CompositeDisposable.prototype.dispose = function () { | |
if (!this.isDisposed) { | |
this.isDisposed = true; | |
var currentDisposables = this.disposables.slice(0); | |
this.disposables = []; | |
this.length = 0; | |
for (var i = 0, len = currentDisposables.length; i < len; i++) { | |
currentDisposables[i].dispose(); | |
} | |
} | |
}; | |
/** | |
* Removes and disposes all disposables from the CompositeDisposable, but does not dispose the CompositeDisposable. | |
* @memberOf CompositeDisposable | |
*/ | |
CompositeDisposable.prototype.clear = function () { | |
var currentDisposables = this.disposables.slice(0); | |
this.disposables = []; | |
this.length = 0; | |
for (var i = 0, len = currentDisposables.length; i < len; i++) { | |
currentDisposables[i].dispose(); | |
} | |
}; | |
/** | |
* Determines whether the CompositeDisposable contains a specific disposable. | |
* @memberOf CompositeDisposable | |
* @param item Disposable to search for. | |
* @returns {Boolean} true if the disposable was found; otherwise, false. | |
*/ | |
CompositeDisposable.prototype.contains = function (item) { | |
return this.disposables.indexOf(item) !== -1; | |
}; | |
/** | |
* Converts the existing CompositeDisposable to an array of disposables | |
* @memberOf CompositeDisposable | |
* @returns {Array} An array of disposable objects. | |
*/ | |
CompositeDisposable.prototype.toArray = function () { | |
return this.disposables.slice(0); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment