123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- import { once } from './functional.js';
- import { Iterable } from './iterator.js';
- /**
- * Enables logging of potentially leaked disposables.
- *
- * A disposable is considered leaked if it is not disposed or not registered as the child of
- * another disposable. This tracking is very simple an only works for classes that either
- * extend Disposable or use a DisposableStore. This means there are a lot of false positives.
- */
- const TRACK_DISPOSABLES = false;
- let disposableTracker = null;
- export function setDisposableTracker(tracker) {
- disposableTracker = tracker;
- }
- if (TRACK_DISPOSABLES) {
- const __is_disposable_tracked__ = '__is_disposable_tracked__';
- setDisposableTracker(new class {
- trackDisposable(x) {
- const stack = new Error('Potentially leaked disposable').stack;
- setTimeout(() => {
- if (!x[__is_disposable_tracked__]) {
- console.log(stack);
- }
- }, 3000);
- }
- setParent(child, parent) {
- if (child && child !== Disposable.None) {
- try {
- child[__is_disposable_tracked__] = true;
- }
- catch (_a) {
- // noop
- }
- }
- }
- markAsDisposed(disposable) {
- if (disposable && disposable !== Disposable.None) {
- try {
- disposable[__is_disposable_tracked__] = true;
- }
- catch (_a) {
- // noop
- }
- }
- }
- markAsSingleton(disposable) { }
- });
- }
- function trackDisposable(x) {
- disposableTracker === null || disposableTracker === void 0 ? void 0 : disposableTracker.trackDisposable(x);
- return x;
- }
- function markAsDisposed(disposable) {
- disposableTracker === null || disposableTracker === void 0 ? void 0 : disposableTracker.markAsDisposed(disposable);
- }
- function setParentOfDisposable(child, parent) {
- disposableTracker === null || disposableTracker === void 0 ? void 0 : disposableTracker.setParent(child, parent);
- }
- function setParentOfDisposables(children, parent) {
- if (!disposableTracker) {
- return;
- }
- for (const child of children) {
- disposableTracker.setParent(child, parent);
- }
- }
- /**
- * Indicates that the given object is a singleton which does not need to be disposed.
- */
- export function markAsSingleton(singleton) {
- disposableTracker === null || disposableTracker === void 0 ? void 0 : disposableTracker.markAsSingleton(singleton);
- return singleton;
- }
- export class MultiDisposeError extends Error {
- constructor(errors) {
- super(`Encountered errors while disposing of store. Errors: [${errors.join(', ')}]`);
- this.errors = errors;
- }
- }
- export function isDisposable(thing) {
- return typeof thing.dispose === 'function' && thing.dispose.length === 0;
- }
- export function dispose(arg) {
- if (Iterable.is(arg)) {
- let errors = [];
- for (const d of arg) {
- if (d) {
- try {
- d.dispose();
- }
- catch (e) {
- errors.push(e);
- }
- }
- }
- if (errors.length === 1) {
- throw errors[0];
- }
- else if (errors.length > 1) {
- throw new MultiDisposeError(errors);
- }
- return Array.isArray(arg) ? [] : arg;
- }
- else if (arg) {
- arg.dispose();
- return arg;
- }
- }
- export function combinedDisposable(...disposables) {
- const parent = toDisposable(() => dispose(disposables));
- setParentOfDisposables(disposables, parent);
- return parent;
- }
- export function toDisposable(fn) {
- const self = trackDisposable({
- dispose: once(() => {
- markAsDisposed(self);
- fn();
- })
- });
- return self;
- }
- export class DisposableStore {
- constructor() {
- this._toDispose = new Set();
- this._isDisposed = false;
- trackDisposable(this);
- }
- /**
- * Dispose of all registered disposables and mark this object as disposed.
- *
- * Any future disposables added to this object will be disposed of on `add`.
- */
- dispose() {
- if (this._isDisposed) {
- return;
- }
- markAsDisposed(this);
- this._isDisposed = true;
- this.clear();
- }
- /**
- * Dispose of all registered disposables but do not mark this object as disposed.
- */
- clear() {
- try {
- dispose(this._toDispose.values());
- }
- finally {
- this._toDispose.clear();
- }
- }
- add(o) {
- if (!o) {
- return o;
- }
- if (o === this) {
- throw new Error('Cannot register a disposable on itself!');
- }
- setParentOfDisposable(o, this);
- if (this._isDisposed) {
- if (!DisposableStore.DISABLE_DISPOSED_WARNING) {
- console.warn(new Error('Trying to add a disposable to a DisposableStore that has already been disposed of. The added object will be leaked!').stack);
- }
- }
- else {
- this._toDispose.add(o);
- }
- return o;
- }
- }
- DisposableStore.DISABLE_DISPOSED_WARNING = false;
- export class Disposable {
- constructor() {
- this._store = new DisposableStore();
- trackDisposable(this);
- setParentOfDisposable(this._store, this);
- }
- dispose() {
- markAsDisposed(this);
- this._store.dispose();
- }
- _register(o) {
- if (o === this) {
- throw new Error('Cannot register a disposable on itself!');
- }
- return this._store.add(o);
- }
- }
- Disposable.None = Object.freeze({ dispose() { } });
- /**
- * Manages the lifecycle of a disposable value that may be changed.
- *
- * This ensures that when the disposable value is changed, the previously held disposable is disposed of. You can
- * also register a `MutableDisposable` on a `Disposable` to ensure it is automatically cleaned up.
- */
- export class MutableDisposable {
- constructor() {
- this._isDisposed = false;
- trackDisposable(this);
- }
- get value() {
- return this._isDisposed ? undefined : this._value;
- }
- set value(value) {
- var _a;
- if (this._isDisposed || value === this._value) {
- return;
- }
- (_a = this._value) === null || _a === void 0 ? void 0 : _a.dispose();
- if (value) {
- setParentOfDisposable(value, this);
- }
- this._value = value;
- }
- clear() {
- this.value = undefined;
- }
- dispose() {
- var _a;
- this._isDisposed = true;
- markAsDisposed(this);
- (_a = this._value) === null || _a === void 0 ? void 0 : _a.dispose();
- this._value = undefined;
- }
- /**
- * Clears the value, but does not dispose it.
- * The old value is returned.
- */
- clearAndLeak() {
- const oldValue = this._value;
- this._value = undefined;
- if (oldValue) {
- setParentOfDisposable(oldValue, null);
- }
- return oldValue;
- }
- }
- export class ImmortalReference {
- constructor(object) {
- this.object = object;
- }
- dispose() { }
- }
|