lifecycle.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { once } from './functional.js';
  6. import { Iterable } from './iterator.js';
  7. /**
  8. * Enables logging of potentially leaked disposables.
  9. *
  10. * A disposable is considered leaked if it is not disposed or not registered as the child of
  11. * another disposable. This tracking is very simple an only works for classes that either
  12. * extend Disposable or use a DisposableStore. This means there are a lot of false positives.
  13. */
  14. const TRACK_DISPOSABLES = false;
  15. let disposableTracker = null;
  16. export function setDisposableTracker(tracker) {
  17. disposableTracker = tracker;
  18. }
  19. if (TRACK_DISPOSABLES) {
  20. const __is_disposable_tracked__ = '__is_disposable_tracked__';
  21. setDisposableTracker(new class {
  22. trackDisposable(x) {
  23. const stack = new Error('Potentially leaked disposable').stack;
  24. setTimeout(() => {
  25. if (!x[__is_disposable_tracked__]) {
  26. console.log(stack);
  27. }
  28. }, 3000);
  29. }
  30. setParent(child, parent) {
  31. if (child && child !== Disposable.None) {
  32. try {
  33. child[__is_disposable_tracked__] = true;
  34. }
  35. catch (_a) {
  36. // noop
  37. }
  38. }
  39. }
  40. markAsDisposed(disposable) {
  41. if (disposable && disposable !== Disposable.None) {
  42. try {
  43. disposable[__is_disposable_tracked__] = true;
  44. }
  45. catch (_a) {
  46. // noop
  47. }
  48. }
  49. }
  50. markAsSingleton(disposable) { }
  51. });
  52. }
  53. function trackDisposable(x) {
  54. disposableTracker === null || disposableTracker === void 0 ? void 0 : disposableTracker.trackDisposable(x);
  55. return x;
  56. }
  57. function markAsDisposed(disposable) {
  58. disposableTracker === null || disposableTracker === void 0 ? void 0 : disposableTracker.markAsDisposed(disposable);
  59. }
  60. function setParentOfDisposable(child, parent) {
  61. disposableTracker === null || disposableTracker === void 0 ? void 0 : disposableTracker.setParent(child, parent);
  62. }
  63. function setParentOfDisposables(children, parent) {
  64. if (!disposableTracker) {
  65. return;
  66. }
  67. for (const child of children) {
  68. disposableTracker.setParent(child, parent);
  69. }
  70. }
  71. /**
  72. * Indicates that the given object is a singleton which does not need to be disposed.
  73. */
  74. export function markAsSingleton(singleton) {
  75. disposableTracker === null || disposableTracker === void 0 ? void 0 : disposableTracker.markAsSingleton(singleton);
  76. return singleton;
  77. }
  78. export class MultiDisposeError extends Error {
  79. constructor(errors) {
  80. super(`Encountered errors while disposing of store. Errors: [${errors.join(', ')}]`);
  81. this.errors = errors;
  82. }
  83. }
  84. export function isDisposable(thing) {
  85. return typeof thing.dispose === 'function' && thing.dispose.length === 0;
  86. }
  87. export function dispose(arg) {
  88. if (Iterable.is(arg)) {
  89. const errors = [];
  90. for (const d of arg) {
  91. if (d) {
  92. try {
  93. d.dispose();
  94. }
  95. catch (e) {
  96. errors.push(e);
  97. }
  98. }
  99. }
  100. if (errors.length === 1) {
  101. throw errors[0];
  102. }
  103. else if (errors.length > 1) {
  104. throw new MultiDisposeError(errors);
  105. }
  106. return Array.isArray(arg) ? [] : arg;
  107. }
  108. else if (arg) {
  109. arg.dispose();
  110. return arg;
  111. }
  112. }
  113. export function combinedDisposable(...disposables) {
  114. const parent = toDisposable(() => dispose(disposables));
  115. setParentOfDisposables(disposables, parent);
  116. return parent;
  117. }
  118. export function toDisposable(fn) {
  119. const self = trackDisposable({
  120. dispose: once(() => {
  121. markAsDisposed(self);
  122. fn();
  123. })
  124. });
  125. return self;
  126. }
  127. export class DisposableStore {
  128. constructor() {
  129. this._toDispose = new Set();
  130. this._isDisposed = false;
  131. trackDisposable(this);
  132. }
  133. /**
  134. * Dispose of all registered disposables and mark this object as disposed.
  135. *
  136. * Any future disposables added to this object will be disposed of on `add`.
  137. */
  138. dispose() {
  139. if (this._isDisposed) {
  140. return;
  141. }
  142. markAsDisposed(this);
  143. this._isDisposed = true;
  144. this.clear();
  145. }
  146. /**
  147. * Returns `true` if this object has been disposed
  148. */
  149. get isDisposed() {
  150. return this._isDisposed;
  151. }
  152. /**
  153. * Dispose of all registered disposables but do not mark this object as disposed.
  154. */
  155. clear() {
  156. try {
  157. dispose(this._toDispose.values());
  158. }
  159. finally {
  160. this._toDispose.clear();
  161. }
  162. }
  163. add(o) {
  164. if (!o) {
  165. return o;
  166. }
  167. if (o === this) {
  168. throw new Error('Cannot register a disposable on itself!');
  169. }
  170. setParentOfDisposable(o, this);
  171. if (this._isDisposed) {
  172. if (!DisposableStore.DISABLE_DISPOSED_WARNING) {
  173. 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);
  174. }
  175. }
  176. else {
  177. this._toDispose.add(o);
  178. }
  179. return o;
  180. }
  181. }
  182. DisposableStore.DISABLE_DISPOSED_WARNING = false;
  183. export class Disposable {
  184. constructor() {
  185. this._store = new DisposableStore();
  186. trackDisposable(this);
  187. setParentOfDisposable(this._store, this);
  188. }
  189. dispose() {
  190. markAsDisposed(this);
  191. this._store.dispose();
  192. }
  193. _register(o) {
  194. if (o === this) {
  195. throw new Error('Cannot register a disposable on itself!');
  196. }
  197. return this._store.add(o);
  198. }
  199. }
  200. Disposable.None = Object.freeze({ dispose() { } });
  201. /**
  202. * Manages the lifecycle of a disposable value that may be changed.
  203. *
  204. * This ensures that when the disposable value is changed, the previously held disposable is disposed of. You can
  205. * also register a `MutableDisposable` on a `Disposable` to ensure it is automatically cleaned up.
  206. */
  207. export class MutableDisposable {
  208. constructor() {
  209. this._isDisposed = false;
  210. trackDisposable(this);
  211. }
  212. get value() {
  213. return this._isDisposed ? undefined : this._value;
  214. }
  215. set value(value) {
  216. var _a;
  217. if (this._isDisposed || value === this._value) {
  218. return;
  219. }
  220. (_a = this._value) === null || _a === void 0 ? void 0 : _a.dispose();
  221. if (value) {
  222. setParentOfDisposable(value, this);
  223. }
  224. this._value = value;
  225. }
  226. clear() {
  227. this.value = undefined;
  228. }
  229. dispose() {
  230. var _a;
  231. this._isDisposed = true;
  232. markAsDisposed(this);
  233. (_a = this._value) === null || _a === void 0 ? void 0 : _a.dispose();
  234. this._value = undefined;
  235. }
  236. /**
  237. * Clears the value, but does not dispose it.
  238. * The old value is returned.
  239. */
  240. clearAndLeak() {
  241. const oldValue = this._value;
  242. this._value = undefined;
  243. if (oldValue) {
  244. setParentOfDisposable(oldValue, null);
  245. }
  246. return oldValue;
  247. }
  248. }
  249. export class RefCountedDisposable {
  250. constructor(_disposable) {
  251. this._disposable = _disposable;
  252. this._counter = 1;
  253. }
  254. acquire() {
  255. this._counter++;
  256. return this;
  257. }
  258. release() {
  259. if (--this._counter === 0) {
  260. this._disposable.dispose();
  261. }
  262. return this;
  263. }
  264. }
  265. /**
  266. * A safe disposable can be `unset` so that a leaked reference (listener)
  267. * can be cut-off.
  268. */
  269. export class SafeDisposable {
  270. constructor() {
  271. this.dispose = () => { };
  272. this.unset = () => { };
  273. this.isset = () => false;
  274. trackDisposable(this);
  275. }
  276. set(fn) {
  277. let callback = fn;
  278. this.unset = () => callback = undefined;
  279. this.isset = () => callback !== undefined;
  280. this.dispose = () => {
  281. if (callback) {
  282. callback();
  283. callback = undefined;
  284. markAsDisposed(this);
  285. }
  286. };
  287. return this;
  288. }
  289. }
  290. export class ImmortalReference {
  291. constructor(object) {
  292. this.object = object;
  293. }
  294. dispose() { }
  295. }