Debian Codegraph @

packages/disposable/src/index.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 /*-----------------------------------------------------------------------------
4 | Copyright (c) 2014-2017, PhosphorJS Contributors
5 |
6 | Distributed under the terms of the BSD 3-Clause License.
7 |
8 | The full license is in the file LICENSE, distributed with this software.
9 |----------------------------------------------------------------------------*/
10 /**
11 * @packageDocumentation
12 * @module disposable
13 */
14 import { ISignal, Signal } from '@lumino/signaling';
15
16 /**
17 * An object which implements the disposable pattern.
18 */
19 export interface IDisposable {
20 /**
21 * Test whether the object has been disposed.
22 *
23 * #### Notes
24 * This property is always safe to access.
25 */
26 readonly isDisposed: boolean;
27
28 /**
29 * Dispose of the resources held by the object.
30 *
31 * #### Notes
32 * If the object's `dispose` method is called more than once, all
33 * calls made after the first will be a no-op.
34 *
35 * #### Undefined Behavior
36 * It is undefined behavior to use any functionality of the object
37 * after it has been disposed unless otherwise explicitly noted.
38 */
39 dispose(): void;
40 }
41
42 /**
43 * A disposable object with an observable `disposed` signal.
44 */
45 export interface IObservableDisposable extends IDisposable {
46 /**
47 * A signal emitted when the object is disposed.
48 */
49 readonly disposed: ISignal<this, void>;
50 }
51
52 /**
53 * A disposable object which delegates to a callback function.
54 */
55 export class DisposableDelegate implements IDisposable {
56 /**
57 * Construct a new disposable delegate.
58 *
59 * @param fn - The callback function to invoke on dispose.
60 */
61 constructor(fn: () => void) {
62 this._fn = fn;
63 }
64
65 /**
66 * Test whether the delegate has been disposed.
67 */
68 get isDisposed(): boolean {
69 return !this._fn;
70 }
71
72 /**
73 * Dispose of the delegate and invoke the callback function.
74 */
75 dispose(): void {
76 if (!this._fn) {
77 return;
78 }
79 let fn = this._fn;
80 this._fn = null;
81 fn();
82 }
83
84 private _fn: (() => void) | null;
85 }
86
87 /**
88 * An observable disposable object which delegates to a callback function.
89 */
90 export class ObservableDisposableDelegate
91 extends DisposableDelegate
92 implements IObservableDisposable
93 {
94 /**
95 * A signal emitted when the delegate is disposed.
96 */
97 get disposed(): ISignal<this, void> {
98 return this._disposed;
99 }
100
101 /**
102 * Dispose of the delegate and invoke the callback function.
103 */
104 dispose(): void {
105 if (this.isDisposed) {
106 return;
107 }
108 super.dispose();
109 this._disposed.emit(undefined);
110 Signal.clearData(this);
111 }
112
113 private _disposed = new Signal<this, void>(this);
114 }
115
116 /**
117 * An object which manages a collection of disposable items.
118 */
119 export class DisposableSet implements IDisposable {
120 /**
121 * Test whether the set has been disposed.
122 */
123 get isDisposed(): boolean {
124 return this._isDisposed;
125 }
126
127 /**
128 * Dispose of the set and the items it contains.
129 *
130 * #### Notes
131 * Items are disposed in the order they are added to the set.
132 */
133 dispose(): void {
134 if (this._isDisposed) {
135 return;
136 }
137 this._isDisposed = true;
138 this._items.forEach(item => {
139 item.dispose();
140 });
141 this._items.clear();
142 }
143
144 /**
145 * Test whether the set contains a specific item.
146 *
147 * @param item - The item of interest.
148 *
149 * @returns `true` if the set contains the item, `false` otherwise.
150 */
151 contains(item: IDisposable): boolean {
152 return this._items.has(item);
153 }
154
155 /**
156 * Add a disposable item to the set.
157 *
158 * @param item - The item to add to the set.
159 *
160 * #### Notes
161 * If the item is already contained in the set, this is a no-op.
162 */
163 add(item: IDisposable): void {
164 this._items.add(item);
165 }
166
167 /**
168 * Remove a disposable item from the set.
169 *
170 * @param item - The item to remove from the set.
171 *
172 * #### Notes
173 * If the item is not contained in the set, this is a no-op.
174 */
175 remove(item: IDisposable): void {
176 this._items.delete(item);
177 }
178
179 /**
180 * Remove all items from the set.
181 */
182 clear(): void {
183 this._items.clear();
184 }
185
186 private _isDisposed = false;
187 private _items = new Set<IDisposable>();
188 }
189
190 /**
191 * The namespace for the `DisposableSet` class statics.
192 */
193 export namespace DisposableSet {
194 /**
195 * Create a disposable set from an iterable of items.
196 *
197 * @param items - The iterable object of interest.
198 *
199 * @returns A new disposable initialized with the given items.
200 */
201 export function from(items: Iterable<IDisposable>): DisposableSet {
202 let set = new DisposableSet();
203 for (const item of items) {
204 set.add(item);
205 }
206 return set;
207 }
208 }
209
210 /**
211 * An observable object which manages a collection of disposable items.
212 */
213 export class ObservableDisposableSet
214 extends DisposableSet
215 implements IObservableDisposable
216 {
217 /**
218 * A signal emitted when the set is disposed.
219 */
220 get disposed(): ISignal<this, void> {
221 return this._disposed;
222 }
223
224 /**
225 * Dispose of the set and the items it contains.
226 *
227 * #### Notes
228 * Items are disposed in the order they are added to the set.
229 */
230 dispose(): void {
231 if (this.isDisposed) {
232 return;
233 }
234 super.dispose();
235 this._disposed.emit(undefined);
236 Signal.clearData(this);
237 }
238
239 private _disposed = new Signal<this, void>(this);
240 }
241
242 /**
243 * The namespace for the `ObservableDisposableSet` class statics.
244 */
245 export namespace ObservableDisposableSet {
246 /**
247 * Create an observable disposable set from an iterable of items.
248 *
249 * @param items - The iterable object of interest.
250 *
251 * @returns A new disposable initialized with the given items.
252 */
253 export function from(items: Iterable<IDisposable>): ObservableDisposableSet {
254 let set = new ObservableDisposableSet();
255 for (const item of items) {
256 set.add(item);
257 }
258 return set;
259 }
260 }