Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@
"test:watch": "jest --updateSnapshot --watchAll"
},
"types": "dist/index.d.ts",
"version": "2.0.2"
"version": "2.0.3"
}
9 changes: 9 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,22 @@ describe( 'Immutable class', () => {
expect( closeListener ).toHaveBeenCalledWith();
} );
test( 'can stop monitoring its closing event', () => {
// 1. by using returned unsubscribed function
const im = new Immutable({});
const closeListener = jest.fn();
const stopWatching = im.onClose( closeListener );
expect( closeListener ).not.toHaveBeenCalled();
stopWatching();
im.close();
expect( closeListener ).not.toHaveBeenCalled();
// 2. `by directly unsubscribing from the close event
const im1 = new Immutable({});
const closeListener1 = jest.fn();
im1.onClose( closeListener1 );
expect( closeListener1 ).not.toHaveBeenCalled();
im1.offClose( closeListener1 );
im1.close();
expect( closeListener1 ).not.toHaveBeenCalled();
} );
test( 'disconnects all of its currently held connections on close', () => {
const NUM_CONNECTIONS = 4;
Expand Down
11 changes: 4 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,15 @@ export class Closable {

@invoke
close() {
this._listeners.forEach( f => f() )
this._listeners.forEach( fn => fn() );
this._listeners.clear();
this._closed = true;
}

@invoke
onClose( fn : Fn ) {
const _fn = () => {
fn();
this.offClose( _fn );
}
this._listeners.add( _fn );
return () => this.offClose( _fn );
this._listeners.add( fn );
return () => this.offClose( fn );
}

@invoke
Expand Down