add basic functionality
This commit is contained in:
@ -1,2 +1,3 @@
|
||||
export * from './smartipc.classes.thread'
|
||||
export * from './smartipc.classes.threadfunction'
|
||||
export * from './smartipc.classes.pool'
|
||||
|
11
ts/smartipc.classes.pool.ts
Normal file
11
ts/smartipc.classes.pool.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import * as plugins from './smartipc.plugins'
|
||||
|
||||
export class Pool {
|
||||
pool
|
||||
constructor() {
|
||||
this.pool = new plugins.threads.Pool()
|
||||
}
|
||||
run(workerPathArg: string) {
|
||||
return this.pool.run(workerPathArg)
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
import * as plugins from './smartipc.plugins'
|
||||
import * as q from 'smartq'
|
||||
|
||||
import { Pool } from './smartipc.classes.pool'
|
||||
|
||||
export let setWorkerBasePath = (basePathArg: string) => {
|
||||
plugins.threads.config.set({
|
||||
basepath: {
|
||||
@ -11,8 +13,11 @@ export let setWorkerBasePath = (basePathArg: string) => {
|
||||
|
||||
export class Thread {
|
||||
thread
|
||||
workerPath: string
|
||||
running: boolean = false
|
||||
assignedPool: Pool = null
|
||||
constructor(filePathArg: string) {
|
||||
this.thread = plugins.threads.spawn(filePathArg)
|
||||
this.workerPath = filePathArg
|
||||
}
|
||||
|
||||
/**
|
||||
@ -20,9 +25,15 @@ export class Thread {
|
||||
*/
|
||||
send<T>(message: any): Promise<T> {
|
||||
let done = q.defer<T>()
|
||||
this.thread.send(message).on('message', (message: T) => {
|
||||
this.checkSpawn()
|
||||
this.thread.send(message)
|
||||
this.thread.on('message', (message: T) => {
|
||||
done.resolve(message)
|
||||
}).on('error', err => {
|
||||
})
|
||||
this.thread.on('done', (job, message: T) => {
|
||||
done.resolve(message)
|
||||
})
|
||||
this.thread.on('error', err => {
|
||||
done.reject(err)
|
||||
})
|
||||
return done.promise
|
||||
@ -30,5 +41,20 @@ export class Thread {
|
||||
|
||||
kill() {
|
||||
this.thread.kill()
|
||||
this.running = false
|
||||
}
|
||||
}
|
||||
|
||||
assignToPool(poolArg: Pool) {
|
||||
this.assignedPool = poolArg
|
||||
}
|
||||
|
||||
private checkSpawn() {
|
||||
if (!this.running && !this.assignedPool) {
|
||||
this.running = true
|
||||
this.thread = plugins.threads.spawn(this.workerPath)
|
||||
} else if (!this.running && this.assignedPool) {
|
||||
this.running = true
|
||||
this.thread = this.assignedPool.run(this.workerPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user