Skip to content

Commit

Permalink
add isDisposable, and assertNotDisposable, #436
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Jun 22, 2023
1 parent 575901a commit e8f3917
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions js/Disposable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class Disposable {
// `this.disposeMyClass()` pattern.
public readonly _disposeEmitter: TEmitter = new TinyEmitter();

// Keep track if this instance supports disposing. If set to false, then an assertion will fire if trying to dispose
// this instance.
private _isDisposable = true;

// Marked true when this Disposable has had dispose() called on it (after disposeEmitter is fired)
private _isDisposed = false;

Expand Down Expand Up @@ -51,12 +55,25 @@ class Disposable {
return this._isDisposed;
}

public get isDisposable(): boolean {
return this._isDisposable;
}

public set isDisposable( isDisposable: boolean ) {
this._isDisposable = isDisposable;
}

public dispose(): void {
assert && !this.isDisposable && Disposable.assertNotDisposable();
assert && assert( !this._isDisposed, 'Disposable can only be disposed once' );
this._disposeEmitter.emit();
this._disposeEmitter.dispose();
this._isDisposed = true;
}

public static assertNotDisposable(): void {
assert && assert( false, 'dispose is not supported, exists for the lifetime of the sim' );
}
}

axon.register( 'Disposable', Disposable );
Expand Down

0 comments on commit e8f3917

Please sign in to comment.