disposable.js 3.5 KB

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