Prevent overriding of Disposable.dispose
A common mistake made was overriding dispose and not calling super.dispose, so a TS trick is now used to move it to a readonly property instead of a member method which prevents it being overridden.
This commit is contained in:
@@ -3,35 +3,28 @@
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import { EventEmitter, IEvent } from 'common/EventEmitter';
|
||||
import { Disposable } from 'common/Lifecycle';
|
||||
import { EventEmitter } from 'common/EventEmitter';
|
||||
import { Disposable, toDisposable } from 'common/Lifecycle';
|
||||
import { IMarker } from 'common/Types';
|
||||
|
||||
export class Marker extends Disposable implements IMarker {
|
||||
private static _nextId = 1;
|
||||
|
||||
private _id: number = Marker._nextId++;
|
||||
public isDisposed: boolean = false;
|
||||
|
||||
public get id(): number { return this._id; }
|
||||
|
||||
private readonly _onDispose = new EventEmitter<void>();
|
||||
public readonly onDispose = this._onDispose.event;
|
||||
|
||||
public get isDisposed(): boolean { return this._isDisposed; };
|
||||
|
||||
constructor(
|
||||
public line: number
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
if (this.isDisposed) {
|
||||
return;
|
||||
}
|
||||
this.isDisposed = true;
|
||||
this.line = -1;
|
||||
// Emit before super.dispose such that dispose listeners get a change to react
|
||||
this._onDispose.fire();
|
||||
super.dispose();
|
||||
this.register(toDisposable(() => {
|
||||
this.line = -1;
|
||||
this._onDispose.fire();
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user