smartspawn/ts/smartipc.classes.thread.ts

34 lines
760 B
TypeScript
Raw Normal View History

2017-01-29 19:50:36 +00:00
import * as plugins from './smartipc.plugins'
import * as q from 'smartq'
2017-01-18 15:54:39 +00:00
2017-01-29 19:50:36 +00:00
export let setWorkerBasePath = (basePathArg: string) => {
plugins.threads.config.set({
basepath: {
node: basePathArg
}
})
2017-01-18 15:54:39 +00:00
}
export class Thread {
thread
2017-01-29 19:50:36 +00:00
constructor(filePathArg: string) {
this.thread = plugins.threads.spawn(filePathArg)
2017-01-18 15:54:39 +00:00
}
/**
* sends a message to the spawned process
*/
2017-01-29 19:50:36 +00:00
send<T>(message: any): Promise<T> {
let done = q.defer<T>()
this.thread.send(message).on('message', (message: T) => {
done.resolve(message)
}).on('error', err => {
done.reject(err)
})
return done.promise
2017-01-18 15:54:39 +00:00
}
2017-01-29 19:50:36 +00:00
kill() {
this.thread.kill()
}
2017-01-18 15:54:39 +00:00
}