@reactive-js/disposable
Advanced tools
Comparing version 0.0.20 to 0.0.21
@@ -11,5 +11,13 @@ export declare type DisposableOrTeardown = DisposableLike | (() => void); | ||
export declare const throwIfDisposed: (disposable: DisposableLike) => void; | ||
export interface DelegatingDisposableLike { | ||
readonly disposable: DisposableLike; | ||
} | ||
export declare const disposableMixin: { | ||
add(this: DelegatingDisposableLike, disposable: DisposableOrTeardown, ...disposables: DisposableOrTeardown[]): any; | ||
dispose(this: DelegatingDisposableLike): void; | ||
remove(this: DelegatingDisposableLike, disposable: DisposableOrTeardown, ...disposables: DisposableOrTeardown[]): any; | ||
}; | ||
export interface SerialDisposableLike extends DisposableLike { | ||
disposable: DisposableLike; | ||
inner: DisposableLike; | ||
} | ||
export declare const createSerialDisposable: () => SerialDisposableLike; |
@@ -97,24 +97,43 @@ "use strict"; | ||
}; | ||
class SerialDisposableImpl extends DisposableImpl { | ||
exports.disposableMixin = { | ||
add(disposable, ...disposables) { | ||
this.disposable.add(disposable, ...disposables); | ||
return this; | ||
}, | ||
dispose() { | ||
this.disposable.dispose(); | ||
}, | ||
remove(disposable, ...disposables) { | ||
this.disposable.remove(disposable, ...disposables); | ||
return this; | ||
}, | ||
}; | ||
class SerialDisposableImpl { | ||
constructor() { | ||
super(...arguments); | ||
this._disposable = exports.disposed; | ||
this._inner = exports.disposed; | ||
this.disposable = exports.createDisposable(); | ||
this.add = exports.disposableMixin.add; | ||
this.dispose = exports.disposableMixin.dispose; | ||
this.remove = exports.disposableMixin.remove; | ||
} | ||
get disposable() { | ||
return this._disposable; | ||
get inner() { | ||
return this._inner; | ||
} | ||
set disposable(newDisposable) { | ||
set inner(newInner) { | ||
if (this.isDisposed) { | ||
newDisposable.dispose(); | ||
newInner.dispose(); | ||
} | ||
else { | ||
const oldDisposable = this.disposable; | ||
this._disposable = newDisposable; | ||
if (oldDisposable !== newDisposable) { | ||
this.add(newDisposable).remove(oldDisposable); | ||
const oldInner = this._inner; | ||
this._inner = newInner; | ||
if (oldInner !== newInner) { | ||
this.add(newInner).remove(oldInner); | ||
} | ||
} | ||
} | ||
get isDisposed() { | ||
return this.disposable.isDisposed; | ||
} | ||
} | ||
exports.createSerialDisposable = () => new SerialDisposableImpl(); | ||
//# sourceMappingURL=index.js.map |
@@ -11,5 +11,13 @@ export declare type DisposableOrTeardown = DisposableLike | (() => void); | ||
export declare const throwIfDisposed: (disposable: DisposableLike) => void; | ||
export interface DelegatingDisposableLike { | ||
readonly disposable: DisposableLike; | ||
} | ||
export declare const disposableMixin: { | ||
add(this: DelegatingDisposableLike, disposable: DisposableOrTeardown, ...disposables: DisposableOrTeardown[]): any; | ||
dispose(this: DelegatingDisposableLike): void; | ||
remove(this: DelegatingDisposableLike, disposable: DisposableOrTeardown, ...disposables: DisposableOrTeardown[]): any; | ||
}; | ||
export interface SerialDisposableLike extends DisposableLike { | ||
disposable: DisposableLike; | ||
inner: DisposableLike; | ||
} | ||
export declare const createSerialDisposable: () => SerialDisposableLike; |
@@ -95,24 +95,43 @@ const doDispose = (disposable) => { | ||
}; | ||
class SerialDisposableImpl extends DisposableImpl { | ||
export const disposableMixin = { | ||
add(disposable, ...disposables) { | ||
this.disposable.add(disposable, ...disposables); | ||
return this; | ||
}, | ||
dispose() { | ||
this.disposable.dispose(); | ||
}, | ||
remove(disposable, ...disposables) { | ||
this.disposable.remove(disposable, ...disposables); | ||
return this; | ||
}, | ||
}; | ||
class SerialDisposableImpl { | ||
constructor() { | ||
super(...arguments); | ||
this._disposable = disposed; | ||
this._inner = disposed; | ||
this.disposable = createDisposable(); | ||
this.add = disposableMixin.add; | ||
this.dispose = disposableMixin.dispose; | ||
this.remove = disposableMixin.remove; | ||
} | ||
get disposable() { | ||
return this._disposable; | ||
get inner() { | ||
return this._inner; | ||
} | ||
set disposable(newDisposable) { | ||
set inner(newInner) { | ||
if (this.isDisposed) { | ||
newDisposable.dispose(); | ||
newInner.dispose(); | ||
} | ||
else { | ||
const oldDisposable = this.disposable; | ||
this._disposable = newDisposable; | ||
if (oldDisposable !== newDisposable) { | ||
this.add(newDisposable).remove(oldDisposable); | ||
const oldInner = this._inner; | ||
this._inner = newInner; | ||
if (oldInner !== newInner) { | ||
this.add(newInner).remove(oldInner); | ||
} | ||
} | ||
} | ||
get isDisposed() { | ||
return this.disposable.isDisposed; | ||
} | ||
} | ||
export const createSerialDisposable = () => new SerialDisposableImpl(); | ||
//# sourceMappingURL=index.js.map |
@@ -11,6 +11,14 @@ export declare type DisposableOrTeardown = DisposableLike | (() => void); | ||
export declare const throwIfDisposed: (disposable: DisposableLike) => void; | ||
export interface DelegatingDisposableLike { | ||
readonly disposable: DisposableLike; | ||
} | ||
export declare const disposableMixin: { | ||
add(this: DelegatingDisposableLike, disposable: DisposableOrTeardown, ...disposables: DisposableOrTeardown[]): any; | ||
dispose(this: DelegatingDisposableLike): void; | ||
remove(this: DelegatingDisposableLike, disposable: DisposableOrTeardown, ...disposables: DisposableOrTeardown[]): any; | ||
}; | ||
export interface SerialDisposableLike extends DisposableLike { | ||
disposable: DisposableLike; | ||
inner: DisposableLike; | ||
} | ||
export declare const createSerialDisposable: () => SerialDisposableLike; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -18,10 +18,10 @@ | ||
* [disposable](serialdisposablelike.md#disposable) | ||
* [inner](serialdisposablelike.md#inner) | ||
## Properties | ||
### disposable | ||
### inner | ||
• **disposable**: *[DisposableLike](disposablelike.md)* | ||
• **inner**: *[DisposableLike](disposablelike.md)* | ||
The inner disposable that may be get or set. |
@@ -8,2 +8,3 @@ | ||
* [DelegatingDisposableLike](interfaces/delegatingdisposablelike.md) | ||
* [DisposableLike](interfaces/disposablelike.md) | ||
@@ -26,2 +27,6 @@ * [SerialDisposableLike](interfaces/serialdisposablelike.md) | ||
### Object literals | ||
* [disposableMixin](README.md#const-disposablemixin) | ||
## Type aliases | ||
@@ -82,1 +87,47 @@ | ||
**Returns:** *void* | ||
## Object literals | ||
### `Const` disposableMixin | ||
### ▪ **disposableMixin**: *object* | ||
### add | ||
▸ **add**(`this`: [DelegatingDisposableLike](interfaces/delegatingdisposablelike.md), `disposable`: [DisposableOrTeardown](README.md#disposableorteardown), ...`disposables`: [DisposableOrTeardown](README.md#disposableorteardown)[]): *any* | ||
**Parameters:** | ||
Name | Type | | ||
------ | ------ | | ||
`this` | [DelegatingDisposableLike](interfaces/delegatingdisposablelike.md) | | ||
`disposable` | [DisposableOrTeardown](README.md#disposableorteardown) | | ||
`...disposables` | [DisposableOrTeardown](README.md#disposableorteardown)[] | | ||
**Returns:** *any* | ||
### dispose | ||
▸ **dispose**(`this`: [DelegatingDisposableLike](interfaces/delegatingdisposablelike.md)): *void* | ||
**Parameters:** | ||
Name | Type | | ||
------ | ------ | | ||
`this` | [DelegatingDisposableLike](interfaces/delegatingdisposablelike.md) | | ||
**Returns:** *void* | ||
### remove | ||
▸ **remove**(`this`: [DelegatingDisposableLike](interfaces/delegatingdisposablelike.md), `disposable`: [DisposableOrTeardown](README.md#disposableorteardown), ...`disposables`: [DisposableOrTeardown](README.md#disposableorteardown)[]): *any* | ||
**Parameters:** | ||
Name | Type | | ||
------ | ------ | | ||
`this` | [DelegatingDisposableLike](interfaces/delegatingdisposablelike.md) | | ||
`disposable` | [DisposableOrTeardown](README.md#disposableorteardown) | | ||
`...disposables` | [DisposableOrTeardown](README.md#disposableorteardown)[] | | ||
**Returns:** *any* |
{ | ||
"name": "@reactive-js/disposable", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"main": "dist/cjs/index.js", | ||
@@ -65,3 +65,3 @@ "module": "dist/esm5/index.js", | ||
}, | ||
"gitHead": "d78cd2b94c3a6a1a4eac41b8c9ca81bd9b98167d" | ||
"gitHead": "3457dad433e17a7335b6c9ba083762ea3ad6ef31" | ||
} |
@@ -156,2 +156,28 @@ export type DisposableOrTeardown = DisposableLike | (() => void); | ||
export interface DelegatingDisposableLike { | ||
readonly disposable: DisposableLike; | ||
} | ||
export const disposableMixin = { | ||
add( | ||
this: DelegatingDisposableLike, | ||
disposable: DisposableOrTeardown, | ||
...disposables: DisposableOrTeardown[] | ||
): any { | ||
this.disposable.add(disposable, ...disposables); | ||
return this; | ||
}, | ||
dispose(this: DelegatingDisposableLike) { | ||
this.disposable.dispose(); | ||
}, | ||
remove( | ||
this: DelegatingDisposableLike, | ||
disposable: DisposableOrTeardown, | ||
...disposables: DisposableOrTeardown[] | ||
): any { | ||
this.disposable.remove(disposable, ...disposables); | ||
return this; | ||
}, | ||
}; | ||
/** | ||
@@ -166,25 +192,32 @@ * A Disposable container that allows replacing a contained Disposable with another, | ||
/** The inner disposable that may be get or set. */ | ||
disposable: DisposableLike; | ||
inner: DisposableLike; | ||
} | ||
class SerialDisposableImpl extends DisposableImpl | ||
implements SerialDisposableLike { | ||
private _disposable: DisposableLike = disposed; | ||
class SerialDisposableImpl implements SerialDisposableLike { | ||
_inner: DisposableLike = disposed; | ||
readonly disposable = createDisposable(); | ||
get disposable(): DisposableLike { | ||
return this._disposable; | ||
add = disposableMixin.add; | ||
dispose = disposableMixin.dispose; | ||
remove = disposableMixin.remove; | ||
get inner() { | ||
return this._inner; | ||
} | ||
set disposable(newDisposable: DisposableLike) { | ||
set inner(newInner: DisposableLike) { | ||
if (this.isDisposed) { | ||
newDisposable.dispose(); | ||
newInner.dispose(); | ||
} else { | ||
const oldDisposable = this.disposable; | ||
this._disposable = newDisposable; | ||
const oldInner = this._inner; | ||
this._inner = newInner; | ||
if (oldDisposable !== newDisposable) { | ||
this.add(newDisposable).remove(oldDisposable); | ||
if (oldInner !== newInner) { | ||
this.add(newInner).remove(oldInner); | ||
} | ||
} | ||
} | ||
get isDisposed() { | ||
return this.disposable.isDisposed; | ||
} | ||
} | ||
@@ -191,0 +224,0 @@ |
@@ -81,8 +81,8 @@ import { | ||
serialDisposable.disposable = disposable; | ||
expect(serialDisposable.disposable).toEqual(disposable); | ||
serialDisposable.inner = disposable; | ||
expect(serialDisposable.inner).toEqual(disposable); | ||
const anotherDisposable = createDisposable(); | ||
serialDisposable.disposable = anotherDisposable; | ||
expect(serialDisposable.disposable).toEqual(anotherDisposable); | ||
serialDisposable.inner = anotherDisposable; | ||
expect(serialDisposable.inner).toEqual(anotherDisposable); | ||
expect(disposable.isDisposed).toBeTruthy(); | ||
@@ -95,5 +95,5 @@ | ||
const yetAnotherDisposable = createDisposable(); | ||
serialDisposable.disposable = yetAnotherDisposable; | ||
serialDisposable.inner = yetAnotherDisposable; | ||
expect(yetAnotherDisposable.isDisposed).toBeTruthy(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
352738
25
665