1 Star 1 Fork 0

南宁橡芮科技有限公司 / ergate

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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

MIT License Copyright (c) 2020 myq1991 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Typescript子进程/线程类 展开 收起
TypeScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
TypeScript
1
https://gitee.com/nnxr/ergate.git
git@gitee.com:nnxr/ergate.git
nnxr
ergate
ergate
master

搜索帮助