ergate
v2.0.11
Published
ergate
Downloads
26
Maintainers
Readme
Eragte
Ergate is a npm package for better use ChildProcess and ThreadWorker in typescript. By using Ergate, you don't need to create childProcess or worker in trandition way:
fork("xxxx/xxxx/xxxx.js")
new Worker("xxxx/xxxx/xxxx.js")
The simple example usage like this:
const proc = await Process(TestProcessClass)
const sum: number = await proc.testFunction(111, 222, 333)
console.log('testFunction sum', sum)
proc.testCallbackFunction(5, 6, (sum, timestamp) => {
console.log('testCallbackFunction sum and timestamp', sum, timestamp)
})
proc.on('property-changed', (property, value) => {
console.log('Child process class property changed', property, value)
console.log('Current property value', proc.testProperty)
//Close the child process
proc.terminate().then(() => {
console.log('Child process closed')
})
})
proc.testProperty = "this is assigned property"
The TestProcessClass is a typescript class which exteneds ProcessClass
export class TestProcessClass extends ProcessClass {
public testProperty: string = "this is test property string"
public testCallbackFunction(a: number, b: number, callback: (sum: number, timestamp: number) => void) {
callback(a + b, Date.now())
}
public async testFunction(a: number, b: number, c: number) {
return a + b + c
}
protected async setup(): Promise<void> {
console.log('This is log from child process')
}
}
Then run this example, you will get the following retults:
This is log from child process
testFunction sum 666
testCallbackFunction sum and timestamp 11 1602350870867
Child process class property changed testProperty this is assigned property
Current property value this is assigned property
Child process closed
Both ChildProcess and WorkerThreads are created in similar way, the difference between create childProcess and workerThread is the base class your custom class have extended are differnet.
The Base process class example
export class YourProcessClass extends ProcessClass {
protected async setup(): Promise<void> {
//the code will be executed while the childProcess initialized
}
}
The Base thread class example
export class YourThreadClass extends ThreadClass {
protected async setup(): Promise<void> {
//the code will be executed while the thread worker initialized
}
}
Create a process
const yourProcessClassInstance = await Process(YourProcessClass)
now you can use "yourProcessClassInstance" just like a local class instance
Create a thread
const yourThreadClassInstance = await Thread(YourThreadClass)
now you can use "yourThreadClassInstance" just like a local class instance
API
ProcessClass and ThreadClass are both extended ErgateBaseClass, therefore, you can invoke terminate() function for terminate process/worker
Some common function will injected into your own class:
async setup(): Promise
async terminate(): Promise
EVENT
Following event like methods are designed for process/worker communication, so DO NOT abuse them
on(event: string, handler: (...args: any[]) => void): void
- error
error event will be emitted while the process/worker error occurred
- property-changed
property changed event will be emitted while the in process/worker class property changed
once(event: string, handler: (...args: any[]) => void): void
emit(event: string, ...args: any[]): void
off(event: string): void
Contributing
Just feel free to post issues or suggestions, this package is under MIT lisence