Skip to content

Commit

Permalink
Disposable to have a protected constructor, #433
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Apr 4, 2023
1 parent adea0b2 commit 8869d2d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion js/Disposable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Disposable {
public readonly _disposeEmitter: TEmitter = new TinyEmitter();
public isDisposed = false;

public constructor() {
// Disposable should only be used by subtypes, no need to instantiate one on its own.
protected constructor() {
if ( assert ) {

// Wrap the prototype dispose method with a check. NOTE: We will not catch devious cases where the dispose() is
Expand Down
8 changes: 6 additions & 2 deletions js/DisposableTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ QUnit.module( 'Disposable' );
QUnit.test( 'Disposable basics', assert => {
assert.ok( true, 'initial test' );

const object1 = new Disposable();
class MyDisposable extends Disposable {
public constructor() { super();}
}

const object1 = new MyDisposable();
assert.ok( !!object1.disposeEmitter, 'disposeEmitter needed' );
const object2 = new Disposable();
const object2 = new MyDisposable();
object1.disposeEmitter.addListener( () => object2.dispose() );

assert.ok( !object1.isDisposed, '1 is not disposed' );
Expand Down

0 comments on commit 8869d2d

Please sign in to comment.