disposable.d.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * An object which implements the disposable pattern.
  3. */
  4. export interface IDisposable {
  5. /**
  6. * Test whether the object has been disposed.
  7. *
  8. * #### Notes
  9. * This property is always safe to access.
  10. */
  11. readonly disposed: boolean;
  12. /**
  13. * Dispose of the resources held by the object.
  14. *
  15. * #### Notes
  16. * If the object's `dispose` method is called more than once, all
  17. * calls made after the first will be a no-op.
  18. *
  19. * #### Undefined Behavior
  20. * It is undefined behavior to use any functionality of the object
  21. * after it has been disposed unless otherwise explicitly noted.
  22. */
  23. dispose(): void;
  24. }
  25. export declare class Disposable implements IDisposable {
  26. private _disposed?;
  27. get disposed(): boolean;
  28. dispose(): void;
  29. }
  30. export declare namespace Disposable {
  31. function dispose(): (target: any, methodName: string, descriptor: PropertyDescriptor) => void;
  32. }
  33. /**
  34. * A disposable object which delegates to a callback function.
  35. */
  36. export declare class DisposableDelegate implements IDisposable {
  37. private callback;
  38. /**
  39. * Construct a new disposable delegate.
  40. *
  41. * @param callback - The callback function to invoke on dispose.
  42. */
  43. constructor(callback: () => void);
  44. /**
  45. * Test whether the delegate has been disposed.
  46. */
  47. get disposed(): boolean;
  48. /**
  49. * Dispose of the delegate and invoke the callback function.
  50. */
  51. dispose(): void;
  52. }
  53. /**
  54. * An object which manages a collection of disposable items.
  55. */
  56. export declare class DisposableSet implements IDisposable {
  57. private isDisposed;
  58. private items;
  59. /**
  60. * Test whether the set has been disposed.
  61. */
  62. get disposed(): boolean;
  63. /**
  64. * Dispose of the set and the items it contains.
  65. *
  66. * #### Notes
  67. * Items are disposed in the order they are added to the set.
  68. */
  69. dispose(): void;
  70. /**
  71. * Test whether the set contains a specific item.
  72. *
  73. * @param item - The item of interest.
  74. *
  75. * @returns `true` if the set contains the item, `false` otherwise.
  76. */
  77. contains(item: IDisposable): boolean;
  78. /**
  79. * Add a disposable item to the set.
  80. *
  81. * @param item - The item to add to the set.
  82. *
  83. * #### Notes
  84. * If the item is already contained in the set, this is a no-op.
  85. */
  86. add(item: IDisposable): void;
  87. /**
  88. * Remove a disposable item from the set.
  89. *
  90. * @param item - The item to remove from the set.
  91. *
  92. * #### Notes
  93. * If the item is not contained in the set, this is a no-op.
  94. */
  95. remove(item: IDisposable): void;
  96. /**
  97. * Remove all items from the set.
  98. */
  99. clear(): void;
  100. }
  101. export declare namespace DisposableSet {
  102. /**
  103. * Create a disposable set from an iterable of items.
  104. *
  105. * @param items - The iterable or array-like object of interest.
  106. *
  107. * @returns A new disposable initialized with the given items.
  108. */
  109. function from(items: IDisposable[]): DisposableSet;
  110. }