filo_caspar/api/io/helper.js

22 lines
440 B
JavaScript
Raw Normal View History

2016-04-10 08:37:05 +00:00
import _ from 'lodash'
2016-04-14 04:01:51 +00:00
2016-04-10 08:37:05 +00:00
export function register(ctx, name, method) {
if (_.isPlainObject(method)) {
Object.keys(method).forEach(key => {
register(ctx, [name, key].join('.'), method[key])
})
return
}
ctx.socket.on(name, async (data) => {
2016-04-14 04:01:51 +00:00
ctx.log.info('Got event', name)
2016-04-10 08:37:05 +00:00
try {
await method(ctx, data)
}
catch (error) {
ctx.log.error(error, `Error processing ${name}`)
}
})
}