Skip to content

Commit d825e57

Browse files
authored
Merge pull request #8 from rdkcentral/wouterlucas/listener-errors
Listeners: add error handling & minor other updates
2 parents 81f807f + 52a0fad commit d825e57

9 files changed

+99
-22
lines changed

dist/thunderJS.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

module/thunderJS.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ var connect = options => {
8585
if (socket === null) {
8686
socket = new ws_1(makeWebsocketAddress(options), protocols);
8787
socket.addEventListener('message', message => {
88+
if (options.debug) {
89+
console.log(' ');
90+
console.log('API REPONSE:');
91+
console.log(JSON.stringify(message.data, null, 2));
92+
console.log(' ');
93+
}
8894
requestQueueResolver(message.data);
8995
});
9096
socket.addEventListener('message', message => {
@@ -178,8 +184,8 @@ var API = options => {
178184
resolve,
179185
reject,
180186
};
181-
execRequest(options, body).catch(m => {
182-
reject(m);
187+
execRequest(options, body).catch(e => {
188+
reject(e);
183189
});
184190
})
185191
},
@@ -203,23 +209,23 @@ var plugins = {
203209
DeviceInfo,
204210
};
205211

206-
function listener(plugin, event, callback) {
212+
function listener(plugin, event, callback, errorCallback) {
207213
const thunder = this;
208-
const index = register.call(this, plugin, event, callback);
214+
const index = register.call(this, plugin, event, callback, errorCallback);
209215
return {
210216
dispose() {
211217
const listener_id = makeListenerId(plugin, event);
212218
listeners[listener_id].splice(index, 1);
213219
if (listeners[listener_id].length === 0) {
214-
unregister.call(thunder, plugin, event);
220+
unregister.call(thunder, plugin, event, errorCallback);
215221
}
216222
},
217223
}
218224
}
219225
const makeListenerId = (plugin, event) => {
220226
return ['client', plugin, 'events', event].join('.')
221227
};
222-
const register = function(plugin, event, callback) {
228+
const register = function(plugin, event, callback, errorCallback) {
223229
const listener_id = makeListenerId(plugin, event);
224230
if (!listeners[listener_id]) {
225231
listeners[listener_id] = [];
@@ -233,13 +239,15 @@ const register = function(plugin, event, callback) {
233239
event,
234240
id: request_id,
235241
};
236-
this.api.request(plugin, method, params);
242+
this.api.request(plugin, method, params).catch(e => {
243+
if (typeof errorCallback === 'function') errorCallback(e.message);
244+
});
237245
}
238246
}
239247
listeners[listener_id].push(callback);
240248
return listeners[listener_id].length - 1
241249
};
242-
const unregister = function(plugin, event) {
250+
const unregister = function(plugin, event, errorCallback) {
243251
const listener_id = makeListenerId(plugin, event);
244252
delete listeners[listener_id];
245253
if (plugin !== 'ThunderJS') {
@@ -252,7 +260,9 @@ const unregister = function(plugin, event) {
252260
event,
253261
id: request_id,
254262
};
255-
this.api.request(plugin, method, params);
263+
this.api.request(plugin, method, params).catch(e => {
264+
if (typeof errorCallback === 'function') errorCallback(e.message);
265+
});
256266
}
257267
};
258268

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Metrological, Wouter <[email protected]>"
55
],
66
"name": "ThunderJS",
7-
"version": "1.2.2",
7+
"version": "1.2.3",
88
"license": "apache",
99
"browser": "dist/thunderJS.js",
1010
"main": "src/thunderJS.js",

readme.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ Thunder (WPEframework) broadcasts notifications when events ocur in the system.
239239

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

242-
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.
242+
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.
243243

244244
```js
245245
const listener = thunderJS.on('Controller', 'statechange', (notification) => {
@@ -289,6 +289,35 @@ const listener2 = thunderJS.Controller.on('statechange', (notification) => {
289289
})
290290
```
291291

292+
If the event does not exist (or there is another thunder issue) the error callback will be called.
293+
294+
```js
295+
const errorListener = thunderJS.Controller.on('thisdoesnotexist', () => {}, (error) => {
296+
console.log('This is an error callback', notification)
297+
}))
298+
```
299+
#### ThunderJS connection events
300+
301+
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.
302+
303+
For example:
304+
305+
```js
306+
thunderJS.on('connect', () => {
307+
console.log('Connect event!')
308+
})
309+
310+
thunderJS.on('disconnect', () => {
311+
console.log('Disconnect event!')
312+
})
313+
314+
thunderJS.on('error', () => {
315+
console.log('Error event!')
316+
})
317+
```
318+
319+
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.
320+
292321
> **Proposal / Work in progress!**
293322
294323
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.

src/api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export default options => {
4141
reject,
4242
}
4343

44-
execRequest(options, body).catch(m => {
45-
reject(m)
44+
execRequest(options, body).catch(e => {
45+
reject(e)
4646
})
4747
})
4848
},

src/api/connect.js

+8
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,16 @@ export default options => {
4646
if (socket === null) {
4747
socket = new WebSocket(makeWebsocketAddress(options), protocols)
4848
socket.addEventListener('message', message => {
49+
if (options.debug) {
50+
console.log(' ')
51+
console.log('API REPONSE:')
52+
console.log(JSON.stringify(message.data, null, 2))
53+
console.log(' ')
54+
}
55+
4956
requestQueueResolver(message.data)
5057
})
58+
5159
socket.addEventListener('message', message => {
5260
notificationListener(message.data)
5361
})

src/listener.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919

2020
import { listeners } from './store'
2121

22-
export default function(plugin, event, callback) {
22+
export default function(plugin, event, callback, errorCallback) {
2323
const thunder = this
2424

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

2828
return {
2929
dispose() {
3030
const listener_id = makeListenerId(plugin, event)
3131
listeners[listener_id].splice(index, 1)
3232

3333
if (listeners[listener_id].length === 0) {
34-
unregister.call(thunder, plugin, event)
34+
unregister.call(thunder, plugin, event, errorCallback)
3535
}
3636
},
3737
}
@@ -42,7 +42,7 @@ const makeListenerId = (plugin, event) => {
4242
return ['client', plugin, 'events', event].join('.')
4343
}
4444

45-
const register = function(plugin, event, callback) {
45+
const register = function(plugin, event, callback, errorCallback) {
4646
const listener_id = makeListenerId(plugin, event)
4747

4848
// no listener registered for this plugin/event yet
@@ -65,7 +65,10 @@ const register = function(plugin, event, callback) {
6565
event,
6666
id: request_id,
6767
}
68-
this.api.request(plugin, method, params)
68+
69+
this.api.request(plugin, method, params).catch(e => {
70+
if (typeof errorCallback === 'function') errorCallback(e.message)
71+
})
6972
}
7073
}
7174

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

79-
const unregister = function(plugin, event) {
82+
const unregister = function(plugin, event, errorCallback) {
8083
const listener_id = makeListenerId(plugin, event)
8184

8285
delete listeners[listener_id]
@@ -96,6 +99,8 @@ const unregister = function(plugin, event) {
9699
event,
97100
id: request_id,
98101
}
99-
this.api.request(plugin, method, params)
102+
this.api.request(plugin, method, params).catch(e => {
103+
if (typeof errorCallback === 'function') errorCallback(e.message)
104+
})
100105
}
101106
}

0 commit comments

Comments
 (0)