Skip to content

Commit

Permalink
Merge pull request #10 from rdkcentral/fix/double_dispose
Browse files Browse the repository at this point in the history
Fix/double dispose
  • Loading branch information
wouterlucas authored Jun 3, 2020
2 parents d825e57 + 7d1c7dd commit 069dab3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dist/thunderJS.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions module/thunderJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ function listener(plugin, event, callback, errorCallback) {
return {
dispose() {
const listener_id = makeListenerId(plugin, event);
if (listeners[listener_id] === undefined) return
listeners[listener_id].splice(index, 1);
if (listeners[listener_id].length === 0) {
unregister.call(thunder, plugin, event, errorCallback);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Metrological, Wouter <[email protected]>"
],
"name": "ThunderJS",
"version": "1.2.3",
"version": "1.2.4",
"license": "apache",
"browser": "dist/thunderJS.js",
"main": "src/thunderJS.js",
Expand Down
3 changes: 3 additions & 0 deletions src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export default function(plugin, event, callback, errorCallback) {
return {
dispose() {
const listener_id = makeListenerId(plugin, event)
//early return if the listener is already deleted and someone is calling dispose twice
if (listeners[listener_id] === undefined) return

listeners[listener_id].splice(index, 1)

if (listeners[listener_id].length === 0) {
Expand Down
19 changes: 19 additions & 0 deletions tests/thunderJS.notifications.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,25 @@ test('thunderJS - notifications - error on invalid event', assert => {
}, 250)
})

// should execute callback each time when notification listener is called
test('thunderJS - notifications - call dispose twice', assert => {
resetStubsAndSpies()
let thunderJS = ThunderJS(options)

const TestListener = thunderJS.FooPlugin.on(
'bar',
() => {},
() => {}
)

assert.doesNotThrow(TestListener.dispose, 'First dispose should work')
assert.doesNotThrow(
TestListener.dispose,
'Second dispose should not an error, even though it no longer exists'
)
assert.end()
})

test('Teardown - thunderJS - calls', assert => {
makeBodySpy.restore()
apiRequestSpy.restore()
Expand Down

0 comments on commit 069dab3

Please sign in to comment.