Jerko Steiner 8c732ba91e Install eslint and fix errors
- argparse
- scripts
- tasq
- validator
2019-09-15 15:57:22 +07:00

26 lines
789 B
TypeScript

import {Executor} from './Executor'
import { Request, Response, ErrorMessage } from './Task'
export class Messenger<T, R> {
constructor(readonly executor: Executor<T, R>) {
if (!process.send) {
throw new Error('Messenger can only be used from a forked subprocess')
}
process.on('message', async (request: Request<T>) => {
try {
const result: R = await this.executor.execute(request)
const response: Response<R> = {id: request.id, result, type: 'success'}
process.send!('response_' + request.id, response)
} catch (error) {
const response: ErrorMessage = {id: request.id, error, type: 'error'}
process.send!('response_' + request.id, response)
}
})
}
exit(code: number) {
process.exit(code)
}
}