disposable.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. /* eslint-disable no-underscore-dangle */
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.DisposableSet = exports.DisposableDelegate = exports.Disposable = void 0;
  5. class Disposable {
  6. get disposed() {
  7. return this._disposed === true;
  8. }
  9. dispose() {
  10. this._disposed = true;
  11. }
  12. }
  13. exports.Disposable = Disposable;
  14. (function (Disposable) {
  15. function dispose() {
  16. return (target, methodName, descriptor) => {
  17. const raw = descriptor.value;
  18. const proto = target.__proto__; // eslint-disable-line
  19. descriptor.value = function (...args) {
  20. if (this.disposed) {
  21. return;
  22. }
  23. raw.call(this, ...args);
  24. proto.dispose.call(this);
  25. };
  26. };
  27. }
  28. Disposable.dispose = dispose;
  29. })(Disposable = exports.Disposable || (exports.Disposable = {}));
  30. /**
  31. * A disposable object which delegates to a callback function.
  32. */
  33. class DisposableDelegate {
  34. /**
  35. * Construct a new disposable delegate.
  36. *
  37. * @param callback - The callback function to invoke on dispose.
  38. */
  39. constructor(callback) {
  40. this.callback = callback;
  41. }
  42. /**
  43. * Test whether the delegate has been disposed.
  44. */
  45. get disposed() {
  46. return !this.callback;
  47. }
  48. /**
  49. * Dispose of the delegate and invoke the callback function.
  50. */
  51. dispose() {
  52. if (!this.callback) {
  53. return;
  54. }
  55. const callback = this.callback;
  56. this.callback = null;
  57. callback();
  58. }
  59. }
  60. exports.DisposableDelegate = DisposableDelegate;
  61. /**
  62. * An object which manages a collection of disposable items.
  63. */
  64. class DisposableSet {
  65. constructor() {
  66. this.isDisposed = false; // eslint-disable-line:variable-name
  67. this.items = new Set();
  68. }
  69. /**
  70. * Test whether the set has been disposed.
  71. */
  72. get disposed() {
  73. return this.isDisposed;
  74. }
  75. /**
  76. * Dispose of the set and the items it contains.
  77. *
  78. * #### Notes
  79. * Items are disposed in the order they are added to the set.
  80. */
  81. dispose() {
  82. if (this.isDisposed) {
  83. return;
  84. }
  85. this.isDisposed = true;
  86. this.items.forEach((item) => {
  87. item.dispose();
  88. });
  89. this.items.clear();
  90. }
  91. /**
  92. * Test whether the set contains a specific item.
  93. *
  94. * @param item - The item of interest.
  95. *
  96. * @returns `true` if the set contains the item, `false` otherwise.
  97. */
  98. contains(item) {
  99. return this.items.has(item);
  100. }
  101. /**
  102. * Add a disposable item to the set.
  103. *
  104. * @param item - The item to add to the set.
  105. *
  106. * #### Notes
  107. * If the item is already contained in the set, this is a no-op.
  108. */
  109. add(item) {
  110. this.items.add(item);
  111. }
  112. /**
  113. * Remove a disposable item from the set.
  114. *
  115. * @param item - The item to remove from the set.
  116. *
  117. * #### Notes
  118. * If the item is not contained in the set, this is a no-op.
  119. */
  120. remove(item) {
  121. this.items.delete(item);
  122. }
  123. /**
  124. * Remove all items from the set.
  125. */
  126. clear() {
  127. this.items.clear();
  128. }
  129. }
  130. exports.DisposableSet = DisposableSet;
  131. (function (DisposableSet) {
  132. /**
  133. * Create a disposable set from an iterable of items.
  134. *
  135. * @param items - The iterable or array-like object of interest.
  136. *
  137. * @returns A new disposable initialized with the given items.
  138. */
  139. function from(items) {
  140. const set = new DisposableSet();
  141. items.forEach((item) => {
  142. set.add(item);
  143. });
  144. return set;
  145. }
  146. DisposableSet.from = from;
  147. })(DisposableSet = exports.DisposableSet || (exports.DisposableSet = {}));
  148. //# sourceMappingURL=disposable.js.map