@acutmore/rxjs
Version:
Reactive Extensions for modern JavaScript
202 lines • 4.97 kB
JavaScript
// v4-backwards-compatibility
export class DisposableImpl {
constructor(work) {
this.work = work;
this._disposed = false;
}
get closed() {
return this._disposed;
}
get isDisposed() {
return this._disposed;
}
unsubscribe() {
if (!this._disposed) {
this._disposed = true;
if (this.work !== undefined) {
this.work();
}
}
}
}
;
DisposableImpl.prototype.dispose = function () { this.unsubscribe(); };
export class Disposable {
constructor() { }
static create(fn) {
return new DisposableImpl(fn);
}
get closed() {
return true;
}
get isDisposed() {
return true;
}
unsubscribe() {
/* no op */
}
}
Disposable.empty = new DisposableImpl(() => { });
;
Disposable.prototype.dispose = function () { this.unsubscribe(); };
export class CompositeDisposable {
constructor(...disposables) {
if (disposables.length > 0 && Array.isArray(disposables[0])) {
this.disposables = disposables[0];
}
else {
this.disposables = disposables;
}
}
add(v) {
if (this.closed) {
v.dispose();
}
else {
this.disposables.push(v);
}
}
remove(v) {
const index = this.disposables.indexOf(v);
if (index !== -1) {
const d = this.disposables.splice(index, 1)[0];
d.dispose();
return true;
}
return false;
}
toArray() {
return this.disposables.slice();
}
get length() {
return this.disposables.length;
}
unsubscribe() {
if (this.closed) {
return;
}
this.closed = true;
const arr = this.disposables.slice();
this.disposables = [];
const length = arr.length;
for (let i = 0; i < length; i++) {
arr[i].dispose();
}
}
get isDisposed() {
return this.closed;
}
}
CompositeDisposable.prototype.dispose = function () { this.unsubscribe(); };
export class SerialDisposable {
constructor() {
this.closed = false;
}
unsubscribe() {
if (this.closed) {
return;
}
this.closed = true;
const current = this.current;
this.current = undefined;
if (current !== undefined) {
current.dispose();
}
}
getDisposable() {
return this.current;
}
setDisposable(disposable) {
if (this.closed) {
if (disposable !== undefined) {
disposable.dispose();
}
}
else {
const old = this.current;
this.current = disposable;
if (old !== undefined) {
old.dispose();
}
}
}
get isDisposed() {
return this.closed;
}
}
;
SerialDisposable.prototype.dispose = function () { this.unsubscribe(); };
export class SingleAssignmentDisposable {
constructor() {
this.closed = false;
}
unsubscribe() {
if (this.closed) {
return;
}
this.closed = true;
const disposable = this.disposable;
this.disposable = undefined;
if (disposable !== undefined) {
disposable.dispose();
}
}
getDisposable() {
return this.disposable;
}
setDisposable(v) {
if (this.disposable !== undefined) {
throw new Error(`SerialDisposable already set`);
}
if (v !== undefined) {
if (this.closed) {
v.dispose();
}
else {
this.disposable = v;
}
}
}
get isDisposed() {
return this.closed;
}
}
SingleAssignmentDisposable.prototype.dispose = function () { this.unsubscribe(); };
export class RefCountDisposable {
constructor(subscription) {
this.subscription = subscription;
this.closed = false;
this.count = 0;
this.primaryDisposed = false;
}
get isDisposed() {
return this.closed;
}
unsubscribe() {
if (this.primaryDisposed) {
return;
}
this.primaryDisposed = true;
this._disposeCheck();
}
_disposeCheck() {
if (!this.closed && this.primaryDisposed && this.count === 0) {
this.closed = true;
const sub = this.subscription;
this.subscription = undefined;
sub.dispose();
}
}
getDisposable() {
if (this.isDisposed) {
return Disposable.empty;
}
this.count++;
return new DisposableImpl(() => {
this.count--;
this._disposeCheck();
});
}
}
RefCountDisposable.prototype.dispose = function () { this.unsubscribe(); };
//# sourceMappingURL=Disposable.js.map