Skip to content

Commit

Permalink
Merge pull request #8 from rdkcentral/wouterlucas/listener-errors
Browse files Browse the repository at this point in the history
Listeners: add error handling & minor other updates
  • Loading branch information
wouterlucas authored Apr 27, 2020
2 parents 81f807f + 52a0fad commit d825e57
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 22 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.

28 changes: 19 additions & 9 deletions module/thunderJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ var connect = options => {
if (socket === null) {
socket = new ws_1(makeWebsocketAddress(options), protocols);
socket.addEventListener('message', message => {
if (options.debug) {
console.log(' ');
console.log('API REPONSE:');
console.log(JSON.stringify(message.data, null, 2));
console.log(' ');
}
requestQueueResolver(message.data);
});
socket.addEventListener('message', message => {
Expand Down Expand Up @@ -178,8 +184,8 @@ var API = options => {
resolve,
reject,
};
execRequest(options, body).catch(m => {
reject(m);
execRequest(options, body).catch(e => {
reject(e);
});
})
},
Expand All @@ -203,23 +209,23 @@ var plugins = {
DeviceInfo,
};

function listener(plugin, event, callback) {
function listener(plugin, event, callback, errorCallback) {
const thunder = this;
const index = register.call(this, plugin, event, callback);
const index = register.call(this, plugin, event, callback, errorCallback);
return {
dispose() {
const listener_id = makeListenerId(plugin, event);
listeners[listener_id].splice(index, 1);
if (listeners[listener_id].length === 0) {
unregister.call(thunder, plugin, event);
unregister.call(thunder, plugin, event, errorCallback);
}
},
}
}
const makeListenerId = (plugin, event) => {
return ['client', plugin, 'events', event].join('.')
};
const register = function(plugin, event, callback) {
const register = function(plugin, event, callback, errorCallback) {
const listener_id = makeListenerId(plugin, event);
if (!listeners[listener_id]) {
listeners[listener_id] = [];
Expand All @@ -233,13 +239,15 @@ const register = function(plugin, event, callback) {
event,
id: request_id,
};
this.api.request(plugin, method, params);
this.api.request(plugin, method, params).catch(e => {
if (typeof errorCallback === 'function') errorCallback(e.message);
});
}
}
listeners[listener_id].push(callback);
return listeners[listener_id].length - 1
};
const unregister = function(plugin, event) {
const unregister = function(plugin, event, errorCallback) {
const listener_id = makeListenerId(plugin, event);
delete listeners[listener_id];
if (plugin !== 'ThunderJS') {
Expand All @@ -252,7 +260,9 @@ const unregister = function(plugin, event) {
event,
id: request_id,
};
this.api.request(plugin, method, params);
this.api.request(plugin, method, params).catch(e => {
if (typeof errorCallback === 'function') errorCallback(e.message);
});
}
};

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

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.2",
"version": "1.2.3",
"license": "apache",
"browser": "dist/thunderJS.js",
"main": "src/thunderJS.js",
Expand Down
31 changes: 30 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Thunder (WPEframework) broadcasts notifications when events ocur in the system.

ThunderJS makes it easy to subscribe to specific events, and execute a _callback-function_ upon every notification of each event.

Simply define a listener, passing in the `plugin` as a first argument and the `event` as a second. As a third argument you can pass in the callback function (that receives the `notification` as an argument) every time a notification is received.
Simply define a listener, passing in the `plugin` as a first argument and the `event` as a second. As a third argument you can pass in the callback function (that receives the `notification` as an argument) every time a notification is received. Optionally a fourth `error` callback can be provided which will be called when the notification failed to register.

```js
const listener = thunderJS.on('Controller', 'statechange', (notification) => {
Expand Down Expand Up @@ -289,6 +289,35 @@ const listener2 = thunderJS.Controller.on('statechange', (notification) => {
})
```

If the event does not exist (or there is another thunder issue) the error callback will be called.

```js
const errorListener = thunderJS.Controller.on('thisdoesnotexist', () => {}, (error) => {
console.log('This is an error callback', notification)
}))
```
#### ThunderJS connection events

Aside from the Thunder provided event system the same syntax can be used to listen for the `connect`, `disconnect` or `error` events which will be fired if there are state changes on the socket connection between ThunderJS and Thunder.

For example:

```js
thunderJS.on('connect', () => {
console.log('Connect event!')
})

thunderJS.on('disconnect', () => {
console.log('Disconnect event!')
})

thunderJS.on('error', () => {
console.log('Error event!')
})
```

The `connect`, `disconnect` and `error` events are tied to the websocket events. For more information please see the [browser](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) or [nodejs](https://github.com/websockets/ws/blob/master/doc/ws.md#class-websocket) documentation respectively.

> **Proposal / Work in progress!**
If you want or need more control over listeners - for example because you need multiple listeners and want to keep track of them individually - you could also create a _subscription_ oject.
Expand Down
4 changes: 2 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export default options => {
reject,
}

execRequest(options, body).catch(m => {
reject(m)
execRequest(options, body).catch(e => {
reject(e)
})
})
},
Expand Down
8 changes: 8 additions & 0 deletions src/api/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ export default options => {
if (socket === null) {
socket = new WebSocket(makeWebsocketAddress(options), protocols)
socket.addEventListener('message', message => {
if (options.debug) {
console.log(' ')
console.log('API REPONSE:')
console.log(JSON.stringify(message.data, null, 2))
console.log(' ')
}

requestQueueResolver(message.data)
})

socket.addEventListener('message', message => {
notificationListener(message.data)
})
Expand Down
19 changes: 12 additions & 7 deletions src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@

import { listeners } from './store'

export default function(plugin, event, callback) {
export default function(plugin, event, callback, errorCallback) {
const thunder = this

// register and keep track of the index
const index = register.call(this, plugin, event, callback)
const index = register.call(this, plugin, event, callback, errorCallback)

return {
dispose() {
const listener_id = makeListenerId(plugin, event)
listeners[listener_id].splice(index, 1)

if (listeners[listener_id].length === 0) {
unregister.call(thunder, plugin, event)
unregister.call(thunder, plugin, event, errorCallback)
}
},
}
Expand All @@ -42,7 +42,7 @@ const makeListenerId = (plugin, event) => {
return ['client', plugin, 'events', event].join('.')
}

const register = function(plugin, event, callback) {
const register = function(plugin, event, callback, errorCallback) {
const listener_id = makeListenerId(plugin, event)

// no listener registered for this plugin/event yet
Expand All @@ -65,7 +65,10 @@ const register = function(plugin, event, callback) {
event,
id: request_id,
}
this.api.request(plugin, method, params)

this.api.request(plugin, method, params).catch(e => {
if (typeof errorCallback === 'function') errorCallback(e.message)
})
}
}

Expand All @@ -76,7 +79,7 @@ const register = function(plugin, event, callback) {
return listeners[listener_id].length - 1
}

const unregister = function(plugin, event) {
const unregister = function(plugin, event, errorCallback) {
const listener_id = makeListenerId(plugin, event)

delete listeners[listener_id]
Expand All @@ -96,6 +99,8 @@ const unregister = function(plugin, event) {
event,
id: request_id,
}
this.api.request(plugin, method, params)
this.api.request(plugin, method, params).catch(e => {
if (typeof errorCallback === 'function') errorCallback(e.message)
})
}
}
Loading

0 comments on commit d825e57

Please sign in to comment.