smartspawn/ts/smartipc.classes.threadsimple.ts

30 lines
890 B
TypeScript
Raw Permalink Normal View History

2017-03-04 10:40:32 +00:00
import * as plugins from './smartipc.plugins'
import * as smartq from 'smartq'
2017-03-04 10:40:32 +00:00
import * as childProcess from 'child_process'
import { workerBasePath } from './smartipc.classes.thread'
export class ThreadSimple {
workerPath: string
threadChildProcess: childProcess.ChildProcess
2017-03-04 11:24:32 +00:00
forkOptions: childProcess.ForkOptions
constructor (filePathArg: string, forkOptionsArg: childProcess.ForkOptions = {}) {
2017-03-04 10:40:32 +00:00
this.workerPath = filePathArg
2017-03-04 11:24:32 +00:00
this.forkOptions = forkOptionsArg
2017-03-04 10:40:32 +00:00
}
run () {
2017-03-04 11:05:53 +00:00
let done = smartq.defer<childProcess.ChildProcess>()
2017-03-04 10:40:32 +00:00
let forkPath = (() => {
if (workerBasePath) {
return plugins.path.join(workerBasePath, this.workerPath)
} else {
return this.workerPath
}
})()
2017-03-04 11:24:32 +00:00
this.threadChildProcess = childProcess.fork(forkPath, [], this.forkOptions)
done.resolve(this.threadChildProcess)
return done.promise
2017-03-04 10:40:32 +00:00
}
}