xterm
[VSoRC/.git] / node_modules / xterm / src / common / Lifecycle.ts
1 /**
2  * Copyright (c) 2018 The xterm.js authors. All rights reserved.
3  * @license MIT
4  */
5
6 import { IDisposable } from 'common/Types';
7
8 /**
9  * A base class that can be extended to provide convenience methods for managing the lifecycle of an
10  * object and its components.
11  */
12 export abstract class Disposable implements IDisposable {
13   protected _disposables: IDisposable[] = [];
14   protected _isDisposed: boolean = false;
15
16   constructor() {
17   }
18
19   /**
20    * Disposes the object, triggering the `dispose` method on all registered IDisposables.
21    */
22   public dispose(): void {
23     this._isDisposed = true;
24     this._disposables.forEach(d => d.dispose());
25     this._disposables.length = 0;
26   }
27
28   /**
29    * Registers a disposable object.
30    * @param d The disposable to register.
31    */
32   public register<T extends IDisposable>(d: T): void {
33     this._disposables.push(d);
34   }
35
36   /**
37    * Unregisters a disposable object if it has been registered, if not do
38    * nothing.
39    * @param d The disposable to unregister.
40    */
41   public unregister<T extends IDisposable>(d: T): void {
42     const index = this._disposables.indexOf(d);
43     if (index !== -1) {
44       this._disposables.splice(index, 1);
45     }
46   }
47 }