mirror of
https://github.com/captbaritone/webamp.git
synced 2026-07-23 18:17:38 +00:00
25 lines
650 B
JavaScript
25 lines
650 B
JavaScript
export default class Disposable {
|
|
_teardowns = [];
|
|
disposed = false;
|
|
add(...teardowns) {
|
|
this._teardowns.push(...teardowns);
|
|
}
|
|
dispose() {
|
|
if (this.disposed) {
|
|
throw new Error(
|
|
"Cannot dispose an observable that has already been dispsoed"
|
|
);
|
|
}
|
|
this._teardowns.forEach(teardown => {
|
|
if (typeof teardown === "function") {
|
|
teardown();
|
|
} else if (typeof teardown.unsubscribe === "function") {
|
|
teardown.unsubscribe();
|
|
} else if (typeof teardown.dispose === "function") {
|
|
teardown.dispose();
|
|
}
|
|
});
|
|
this._teardowns = null;
|
|
this.disposed = true;
|
|
}
|
|
}
|