项目作者: nojsja

项目描述 :
Electron/Nodejs Candy
高级语言: JavaScript
项目地址: git://github.com/nojsja/electron-re.git
创建时间: 2020-11-17T11:49:21Z
项目社区:https://github.com/nojsja/electron-re

开源协议:MIT License

下载


GitHub license
GitHub issues
GitHub stars

electron-re


Test on electron@8.2.0 / 9.3.5

Contents

  1. ├── Contents (you are here!)
  2. ├── Architecture
  3. ├── * What can be used for?
  4. ├── In Electron Project
  5. └── In Nodejs/Electron Project
  6. ├── * Install
  7. ├── * Instruction1: ProcessManager
  8. ├── Require it in main.js(electron)
  9. └── Open process-manager window for your application
  10. ├── * Instruction2: Service
  11. ├── The arguments to create a service
  12. ├── Enable service auto reload after code changed
  13. └── The methods of a Service instance
  14. ├── * Instruction3: MessageChannel
  15. ├── The methods of MessageChannel
  16. └── A full usage
  17. ├── * Instruction4: ChildProcessPool
  18. ├── Create a childprocess pool
  19. ├── Send request to a process instance
  20. ├── Send request to all process instances
  21. ├── Destroy the child processes of the process pool
  22. └── Set the max instance limit of pool
  23. ├── * Instruction5: ProcessHost
  24. ├── Require it in a sub process
  25. ├── Registry a task with unique name
  26. ├── Working with ChildProcessPool
  27. └── Unregistry a task with unique name
  28. ├── * Instruction6: WorkerThreadPool
  29. ├── Create a static WorkerThreadPool pool
  30. ├── Create a static WorkerThreadPool excutor
  31. ├── Create a dynamic WorkerThreadPool pool
  32. └── Create a dynamic WorkerThreadPool excutor
  33. ├── Examples

Architecture


architecture

I. What can be used for?


  • BrowserServcie / MessageChannel
    • Using BrowserServcie to generate some service processes without UI and put your heavy tasks into them.
    • MessageChannel make it be possible to communicate with each other between main process,render process and service.
  • ProcessManager
    • ProcessManager provides a user interface for managing / monitoring processes, includes BrowserServcie / ChildProcess / renderer process / main process.
  • ChildProcessPool / ProcessHost
    • ChildProcessPool with load-balancing support may helps when you need to create and manage several processes on nodejs runtime of electron.
    • ProcessHost let us be focused on the core sub-process logic rather than various async event.
  • WorkerThreadPool
    • WorkerThreadPool is effective for creating and managing threads.

II. Install


  1. $: npm install electron-re --save
  2. # or
  3. $: yarn add electron-re --save

III. Instruction 1: ProcessManager


Used in Electron project, build for ChildProcessPool/BrowserService.

Compatible with native IpcRenderer/Main.

All functions:

  1. Show all alive processes in your Electron application: main process, renderer process, the service process (imported by electron-re), and the child process created by ChildProcessPool (imported by electron-re).

  2. The process list displays info: process ID, process type(mark), parent process ID, memory, CPU. All processes type include main (main process), service (service process), renderer (renderer process) , node (child process in process pool). click on table header to sort an item in increasing/decreasing order.

  3. You can kill a process, view process console data, check CPU/memory occupancy within 1 min.

  4. If a process marked as renderer, pressing the DevTools button then the built-in debugging tool will open as an undocked window. Besides the child-processes are created by ChildProcessPool with --inspect parameter, DevTools is not supported, just visit chrome://inspect in chrome for remote debugging.

  5. Try to use MessageChannel for sending/receiving ipc messages, there is a ui pannel area that show activities of it (logger).

Require it in main.js(electron)

  1. const {
  2. MessageChannel, // remember to require it in main.js even if you don't use it
  3. ProcessManager
  4. } = require('electron-re');

Open process-manager window

  1. ProcessManager.openWindow();
  1. Main

    The main ui

main

  1. Console

    Show console info of all processes

console

  1. DevTools

    Open devtools for electron renderer window

devtools

  1. Trends

    Show cpu/memory occupancy trends

trends

trends2

  1. Kill

    Kill process from one-click

kill

  1. Signals Pannel

    Activities logger for MessageChannel tool

signals

IV. Instruction 2: Service


Used in Electron project, working with MessageChannel, remember to check “Instruction 3”.

1. The arguments to create a service

The service process is a customized render process that works in the background, receiving path, options as arguments:

  • path [string] * — The absolute path to a js file
  • options [object] — The same as new BrowserWindow() options.
  1. /* --- main.js --- */
  2. const { BrowserService } = require('electron-re');
  3. const myService = new BrowserService('app', 'path/to/app.service.js', options);app.service.js'));

2. Enable service auto reload after code changed

The auto-reload feature is based on nodejs - fs.watch api. When webSecurity closed and in dev mode, service will reload when service code changed.

1.Set dev mode in new BrowserService() options
2.Get webSecurity closed

  1. /* --- main.js --- */
  2. const myService = new BrowserService('app', 'path/to/app.service.js', {
  3. ...options,
  4. // set dev mode with webSecurity closed
  5. dev: true,
  6. webPreferences: { webSecurity: false }
  7. });

3. The methods of a Service instance

The service instance is a customized BrowserWindow instance too, initialized by a file worked with commonJs module, so you can use require('name') and can’t use import some from 'name' syntax. It has two extension methods:

  • connected() - return a resolved Promise when service is ready.
  • openDevTools - open an undocked window for debugging.

suggest to put some business-related code into a service.

  1. /* --- main.js --- */
  2. const {
  3. BrowserService,
  4. MessageChannel // must required in main.js even if you don't use it
  5. } = require('electron-re');
  6. ...
  7. app.whenReady().then(async() => {
  8. // after app is ready in main process
  9. const myService = new BrowserService('app', 'path/to/app.service.js');
  10. // async
  11. await myService.connected();
  12. mhyService.openDevTools();
  13. /* work with webContents method, also you can use MessageChannel instead */
  14. mhyService.webContents.send('channel1', { value: 'test1' });
  15. });
  16. ...
  1. /* --- app.service.js --- */
  2. const { ipcRenderer } = require('electron');
  3. /* working with ipc method, also you can use MessageChannel instead */
  4. ipcRenderer.on('channel1', (event, result) => {
  5. // works
  6. ...
  7. });

V. Instruction 3: MessageChannel


Used in Electron project, working with Service.

When sending data from main/other process to a service you need to use MesssageChannel, such as: MessageChannel.send('service-name', 'channel', 'params'), And also it can be used to replace other build-in ipc methods, more flexible.

The methods of MessageChannel

1.Public methods,used in Main-Pocess / Renderer-Process / Service

  1. /* send data to a service - like the build-in ipcMain.send */
  2. MessageChannel.send('service-name', channel, params);
  3. /* send data to a service and return a Promise - extension method */
  4. MessageChannel.invoke('service-name', channel, params);
  5. /*
  6. send data to a renderer/servcie which id is same as the given windowId/webContentsId,
  7. same as ipcRenderer.sendTo,
  8. recommend to use it when you want to send data from main/service to a renderer window
  9. */
  10. MessageChannel.sendTo('windowId/webContentsId', channel, params);
  11. /* listen a channel and return unlisten function, probably same as ipcMain.on/ipcRenderer.on */
  12. const removeOnListener = MessageChannel.on(channel, func);
  13. /* listen a channel once and return unlisten function, probably same as ipcMain.once/ipcRenderer.once */
  14. const removeOnceListener = MessageChannel.once(channel, func);

2.Only used in Renderer-process / Service

  1. /* send data to main process - like the build-in ipcRender.send */
  2. MessageChannel.send('main', channel, params);
  3. /* send data to main process and return a Promise - extension method */
  4. MessageChannel.invoke('main', channel, params);

3.Only used in Main-process / Service

  1. /*
  2. handle a channel signal, extension method,
  3. and you can return data directly or return a Promise instance
  4. */
  5. MessageChannel.handle(channel, processorFunc);

Full Usage

  • 1)In main process
    ```js
    const {
    BrowserService,
    MessageChannel // must required in main.js even if you don’t use it
    } = require(‘electron-re’);
    const isInDev = process.env.NODE_ENV === ‘dev’;

/ use MessageChannel instead of build-in method /
app.whenReady().then(() => {
const myService = new BrowserService(‘app’, ‘path/to/app.service.js’);
myService.connected().then(() => {
// open devtools in dev mode for debugging
if (isInDev) myService.openDevTools();
MessageChannel.send(‘app’, ‘channel1’, { value: ‘test1’ });
MessageChannel.invoke(‘app’, ‘channel2’, { value: ‘test2’ }).then((response) => {
console.log(response);
});

  1. MessageChannel.on('channel3', (event, response) => {
  2. console.log(response);
  3. });
  4. MessageChannel.handle('channel4', (event, response) => {
  5. console.log(response);
  6. return { res: 'channel4-res' };
  7. });

})
});

  1. - 2Send or Receive data in a __service__ named app
  2. ```js
  3. const { ipcRenderer } = require('electron');
  4. const { MessageChannel } = require('electron-re');
  5. MessageChannel.on('channel1', (event, result) => {
  6. console.log(result);
  7. });
  8. MessageChannel.handle('channel2', (event, result) => {
  9. console.log(result);
  10. return { response: 'channel2-response' }
  11. });
  12. MessageChannel.invoke('app2', 'channel3', { value: 'channel3' }).then((event, result) => {
  13. console.log(result);
  14. });
  15. MessageChannel.send('app2', 'channel4', { value: 'channel4' });
  • 3)Send or receive data in a service named app2
    ```js
    MessageChannel.handle(‘channel3’, (event, result) => {
    console.log(result);
    return { response: ‘channel3-response’ }
    });

MessageChannel.once(‘channel4’, (event, result) => {
console.log(result);
});

MessageChannel.send(‘main’, ‘channel3’, { value: ‘channel3’ });
MessageChannel.send(‘main’, ‘channel3’, { value: ‘channel3’ });
MessageChannel.invoke(‘main’, ‘channel4’, { value: ‘channel4’ });

  1. - 3Send or receive data in a __renderer__ window
  2. ```js
  3. const { ipcRenderer } = require('electron');
  4. const { MessageChannel } = require('electron-re');
  5. MessageChannel.send('app', 'channel1', { value: 'test1'});
  6. MessageChannel.invoke('app2', 'channel3', { value: 'test2' });
  7. MessageChannel.send('main', 'channel3', { value: 'test3' });
  8. MessageChannel.invoke('main', 'channel4', { value: 'test4' });

VI. Instruction 4: ChildProcessPool


Used in Nodejs/Electron project, working with ProcessHost, remember to check “Instruction 5”.

Multi-process helps to make full use of multi-core CPU, let’s see some differences between multi-process and multi-thread:

  1. It is difficult to share data between different processes, but threads can share memory.
  2. Processes consume more computer resources than threads.
  3. The processes will not affect each other, a thread hanging up will cause the whole process to hang up.

Attention of Electron Bugs !!

DO NOT USE require('electron') in child_process js exec file, this will cause fatal error in the production environment!

Besides that, In order to use ChildProcessPool, you need to place your child_process exec js file in an external directory such as ~/.config/. Otherwise, when you packaged your app, Node.js can not find that exec file.

The another way to solve this problem is to set asar to false in the electron-builder.json, this is not recommended but works.

  1. {
  2. ...
  3. "asar": false,
  4. ...
  5. }

1. Create a childprocess pool

  • path [string] * - the absolute path to a js file.
  • max [number] * - the max count of instance created by pool.
  • env [object] - env variable object.
  • strategy [enum] - load balancing strategy, default is POLLING.
    • POLLING: pick process one by one.
    • WEIGHTS: pick process by process weight.
    • RANDOM: pick by random.
    • WEIGHTS_POLLING: pick process one by one, Affected by WEIGHTS.
    • WEIGHTS_RANDOM: pick process by random, Affected by WEIGHTS.
    • MINIMUM_CONNECTION (not recommend, DO NOT USE): pick process by minimum connection count of per process.
    • WEIGHTS_MINIMUM_CONNECTION (not recommend, DO NOT USE): pick process by minimum connection count of per process, Affected by WEIGHTS.
  • weight [array] - the weight of each process, default is [1…].
  1. const { ChildProcessPool, LoadBalancer } = require('electron-re');
  2. global.ipcUploadProcess = new ChildProcessPool({
  3. path: path.join(app.getAppPath(), 'app/services/child/upload.js'),
  4. max: 3,
  5. env: { lang: global.lang, NODE_ENV: nodeEnv },
  6. strategy: LoadBalancer.ALGORITHM.WEIGHTS_POLLING, // loadbalance strategy
  7. weights: [1, 2, 3],
  8. });

2. Send request to a process instance

  • 1)taskName [string] * - a task registried with ProcessHost.
  • 2)data [any] * - the data passed to process.
  • 3)id [any] - the unique id bound to a process instance.
    • The unique id bound to a process instance(id will be automatically bound after call send()).
    • Sometime you send request to a process with special data, then expect to get callback data from that process. You can provide an unique id in send function, each time pool will send a request to the process bound with this id.
    • If you give an empty/undefined/null id, pool will select a process by load-balancing strategy.
  1. global.ipcUploadProcess.send(
  2. 'init-works',
  3. {
  4. name: 'fileName',
  5. type: 'fileType',
  6. size: 'fileSize',
  7. },
  8. 'id-number' // optional and it's given by you
  9. )
  10. .then((rsp) => {
  11. console.log(rsp);
  12. });

3. Send request to all process instances

All sub processes will receive a request, and you can get a response data array from all sub processes.

  • 1)taskName [string] * - a task registried with ProcessHost(check usage below).
  • 2)data [any] - the data passed to process.
  1. global.ipcUploadProcess.sendToAll(
  2. 'task-get-all',
  3. { key: 'test' }
  4. )
  5. .then((rsp) => {
  6. console.log(rsp);
  7. });

4. Destroy the child processes of the process pool

  • If do not specify id, all child processes will be destroyed. Specifying the id parameter can separately destroy a child process bound to this id.

  • After the destruction, using the process pool to send a new request, a new child process will be created automatically.

  • It should be noted that the id binding operation is automatically performed after the processPool.send('task-name', params, id) method is called.

  1. // destroy a process with id value
  2. global.ipcUploadProcess.disconnect(id);
  3. // destroy all processes
  4. global.ipcUploadProcess.disconnect();

5. Set the max instance limitation of pool

In addition to using the max parameter to specify the maximum number of child process instances created by the process pool, you can also call this method to dynamically set the number of child process instances that need to be created.

  1. global.ipcUploadProcess.setMaxInstanceLimit(number);

VII. Instruction 5: ProcessHost


Used in Nodejs/Electron project, working with ChildProcessPool.

In Instruction 4, We already know how to create a sub-process pool and send request using it. Now let’s figure out how to registry a task and handle process messages in a sub process(created by ChildProcessPool constructor with param - path).

Using ProcessHost we will no longer pay attention to the message sending/receiving between main process and sub processes. Just declaring a task with a unique service-name and put your processing code into a function. And remember that if the code is async, return a Promise instance instead.

1. Require it in a sub process

  1. const { ProcessHost } = require('electron-re');

2. Registry a task with unique name

Support chain call

  1. ProcessHost
  2. .registry('init-works', (params) => {
  3. return initWorks(params);
  4. })
  5. .registry('async-works', (params) => {
  6. return asyncWorks(params);
  7. });
  8. function initWorks(params) {
  9. console.log(params);
  10. return params;
  11. }
  12. function asyncWorks(params) {
  13. console.log(params);
  14. return fetch(url);
  15. }

3. Working with ChildProcessPool

  1. /* 1. send a request in main process */
  2. global.ipcUploadProcess.send(
  3. 'init-works',
  4. {
  5. name: 'fileName',
  6. type: 'fileType',
  7. size: 'fileSize',
  8. }
  9. );
  10. ...
  11. /* 2. handle this request in sub process */
  12. ...

4. Unregistry a task with unique name(if necessary)

Support chain call

  1. ProcessHost
  2. .unregistry('init-works')
  3. .unregistry('async-works')
  4. ...

VIII. WorkerThreadPool


Multi Processes help to make full use of cpu, Multi Threads improve task parallelism ability of Node.js.

In Node.js, there is only one main process which has single main thread, the main thread run event loops and executes macro/micro tasks. In theory, macro/micro task should be short and quick, if we use main thread for some cpu-sensitive heavy tasks, this will block event loop on main thread.

So, try to put your heavy tasks into worker threads will be better in Node.js. The worker thread pool is effective for creating and managing threads, besides, it provides a task queue. When pool has no idle thread, more coming tasks are placed in queue and be taken out from queue after while to be excuted by new idle thread.

Create a static WorkerThreadPool pool

  1. Options of StaticThreadPool:
  • constructor(options, threadOptions)
  • @param {Object} opitons: the options to create a static thread pool:
    • One of follow params is required and unique:
      • execPath {String}: path to an executable commonjs module file.
      • execString {String}: executable code string.
      • execFunction {Function}: js function.
    • lazyLoad {Boolean}: if diabled, all threads will be created when init pool.
    • maxThreads {Number}: max thread count of pool.
    • maxTasks {Number}: max task count of pool.
    • taskRetry {Number}: number of task retries.
    • taskLoopTime {Number}: time of task loop.
    • taskTimeout {Number}: timeout time.
  • @param {Object} threadOpitions: Some origin options for node.js worker_threads.
    • transferList {Array}: A list of ArrayBuffer, MessagePort and FileHandle objects. After transferring, they will not be usable on the sending side.
  1. const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]);
  2. const staticPool = new StaticThreadPool(
  3. {
  4. // execPath: 'path/to/executable.js',
  5. execString: 'module.exports = (payload) => `res:${payload}`',
  6. // execFunction: (payload) => payload,
  7. lazyLoad: true,
  8. maxThreads: 24,
  9. maxTasks: 48,
  10. taskRetry: 1,
  11. taskLoopTime: 1e3,
  12. taskTimeout: 5e3,
  13. },
  14. {
  15. transferList: [uint8Array.buffer]
  16. }
  17. );
  1. Attributes of a StaticThreadPool instance
  • isFull {Boolean}: whether the pool is full of threads, related to this param - maxThreads.
  • threadLength {Number}: current thread count of pool.
  • taskLength {Number}: current task count of pool.
  1. Methods of a StaticThreadPool instance
  • fillPoolWithIdleThreads(): fill pool with idle threads, this is effective when pool is not full.
  • queue(payload, options): Save a task request to task queue, will throw an error when the task queue is full.
    • @param {Any} payload *: The request payload data.
    • @param {Object} options: Options to create a task:
      • @param {Number} taskTimeout:The task timeout in milliseconds
      • @param {Number} taskRetry:Number of task retries.
      • @param {Array} transferList: A list of ArrayBuffer, MessagePort and FileHandle objects. After transferring, they will not be usable on the sending side.
  • exec(payload, options): Send a task request to pool, will throw an error when there is no idle thread and the task queue is full.
    • @param {Any} payload *: The request payload data.
    • @param {Object} options: Options to create a task:
      • @param {Number} taskTimeout:The task timeout in milliseconds
      • @param {Number} taskRetry:Number of task retries.
      • @param {Array} transferList: A list of ArrayBuffer, MessagePort and FileHandle objects. After transferring, they will not be usable on the sending side.
  • createExecutor(options={}): Create an static executor to execute tasks.
    • @param {Object} options: Options to create a executor:
      • @param {Number} taskTimeout:The task timeout in milliseconds
      • @param {Number} taskRetry:Number of task retries.
      • @param {Array} transferList: A list of ArrayBuffer, MessagePort and FileHandle objects. After transferring, they will not be usable on the sending side.
  • wipeTaskQueue(): Wipe all tasks in queue.
  • wipeThreadPool(): Wipe all threads in pool.
  • setMaxThreads(maxThreads): Set max thread count of pool.
    • @param {Number} maxThreads:max thread count.
  • setMaxTasks(maxTasks): Set max task count of pool.
    • @param {Number} maxTasks:max task count.
  • setTaskLoopTime(taskLoopTime): Set time of task loop.
    • @param {Number} taskLoopTime:task loop time.
  • setTaskRetry(taskRetry): Set count of task retries.
    • @param {Number} taskRetry:Number of task retries.
  • setTransferList(transferList): Set transfer-list data of task.
    • @param {Array} transferList:transfer-list data.
  1. staticPool
  2. .setTaskRetry(1)
  3. .exec('payload-data', {
  4. taskTimeout: 5e3,
  5. taskRetry: 1,
  6. })
  7. .then((rsp) => {
  8. console.log(rsp);
  9. });

Create a static WorkerThreadPool excutor

  1. Options of StaticThreadPool Executor
  • @params {Object} options
    • taskRetry {Number}: number of task retries.
    • taskTimeout {Number}: timeout time.
    • transferList {Array}: A list of ArrayBuffer, MessagePort and FileHandle objects. After transferring, they will not be usable on the sending side.
  1. const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]);
  2. const staticExecutor = staticPool.createExecutor({
  3. taskRetry: 2,
  4. taskTimeout: 2e3,
  5. transferList: [unit8Array.buffer]
  6. });
  1. Methods of a StaticThreadPool Executor
  • queue(payload, options): Save a task request to task queue, will throw an error when the task queue is full.
    • @param {Any} payload *: The request payload data.
  • exec(payload): Send a task request to pool from excutor.
    • @param {Any} payload *: The request payload data.
  • setTaskRetry(taskRetry): Set count of task retries.
    • @param {Number} taskRetry:Number of task retries.
  • setTransferList(transferList): Set transfer-list data of task.
    • @param {Array} transferList:transfer-list data.
  • setTaskTimeout(taskTimeout): Set timeout time of task.
    • @param {Number} taskTimeout:timeout time.
  1. staticExecutor
  2. .setTaskRetry(2)
  3. .exec('test')
  4. .then((rsp) => {
  5. console.log(rsp);
  6. });

Create a dynamic WorkerThreadPool pool

  1. Options of DynamicThreadPool:
  • constructor(options, threadOptions)
  • @param {Object} opitions: the options to create a static thread pool:
    • maxThreads {Number}: max thread count of pool.
    • maxTasks {Number}: max task count of pool.
    • taskRetry {Number}: number of task retries.
    • taskLoopTime {Number}: time of task loop.
    • taskTimeout {Number}: timeout time.
  • @param {Object} threadOptions: Some origin options for node.js worker_threads.
    • transferList {Array}: A list of ArrayBuffer, MessagePort and FileHandle objects. After transferring, they will not be usable on the sending side.
  1. const dynamicPool = new DynamicThreadPool({
  2. maxThreads: 24,
  3. maxTasks: 48,
  4. taskRetry: 1,
  5. taskLoopTime: 1e3,
  6. taskTimeout: 5e3,
  7. });
  1. Attributes of a DynamicThreadPool instance
  • isFull {Boolean}: whether the pool is full of threads, related to this param - maxThreads.
  • threadLength {Number}: current thread count of pool.
  • taskLength {Number}: current task count of pool.
  1. Methods of a DynamicThreadPool instance
  • queue(payload, options): Save a task request to task queue, will throw an error when the task queue is full.
    • @param {Any} payload *: The request payload data.
    • @param {Object} options: Options to create a task:
      • One of follow params is optional and unique:
        • execPath {String}: path to an executable commonjs module file.
        • execString {String}: executable code string.
        • execFunction {Function}: js function.
      • @param {Number} taskTimeout:The task timeout in milliseconds
      • @param {Number} taskRetry:Number of task retries.
      • @param {Array} transferList: A list of ArrayBuffer, MessagePort and FileHandle objects. After transferring, they will not be usable on the sending side.
  • exec(payload, options): Send a task request to pool, will throw an error when there is no idle thread and the task queue is full.
    • @param {Any} payload *: The request payload data.
    • @param {Object} options: Options to create a task:
      • One of follow params is optional and unique:
        • execPath {String}: path to an executable commonjs module file.
        • execString {String}: executable code string.
        • execFunction {Function}: js function.
      • taskTimeout {Number}:The task timeout in milliseconds
      • taskRetry {Number}:Number of task retries.
      • transferList {Array}: A list of ArrayBuffer, MessagePort and FileHandle objects. After transferring, they will not be usable on the sending side.
  • createExecutor(options={}): Create a dynamic executor to execute tasks.
    • @param {Object} options: Options to create a executor:
      • One of follow params is optional and unique:
        • execPath {String}: path to an executable commonjs module file.
        • execString {String}: executable code string.
        • execFunction {Function}: js function.
      • taskTimeout {Number}:The task timeout in milliseconds
      • taskRetry {Number}:Number of task retries.
      • transferList {Array}: A list of ArrayBuffer, MessagePort and FileHandle objects. After transferring, they will not be usable on the sending side.
  • wipeTaskQueue(): Wipe all tasks in queue.
  • wipeThreadPool(): Wipe all threads in pool.
  • setMaxThreads(maxThreads): Set max thread count of pool.
    • @param {Number} maxThreads:max thread count.
  • setMaxTasks(maxTasks): Set max task count of pool.
    • @param {Number} maxTasks:max task count.
  • setTaskLoopTime(taskLoopTime): Set time of task loop.
    • @param {Number} taskLoopTime:task loop time.
  • setTaskRetry(taskRetry): Set count of task retries.
    • @param {Number} taskRetry:Number of task retries.
  • setTransferList(transferList): Set transfer-list data of task.
    • @param {Array} transferList:transfer-list data.
  • setExecPath(execPath): Set path of an executable commonjs module file.
    • @param {String} execPath:path to an executable commonjs module file.
  • setExecString(execString): Set executable code string.
    • @param {String} execString:executable code string.
  • setExecFunction(execFunction): Set js function.
    • @param {Function} execFunction:js function.
  1. dynamicPool
  2. .setExecString(`module.exports = (payload) => console.log(payload);`)
  3. .setTaskRetry(1)
  4. .exec('payload-data', {
  5. taskTimeout: 5e3,
  6. taskRetry: 1,
  7. })
  8. .then((rsp) => {
  9. console.log(rsp);
  10. });

Create a dynamic WorkerThreadPool excutor

  1. Options of DynamicThreadPool Executor
  • @params {Object} options
    • One of follow params is optional and unique:
      • execPath {String}: path to an executable commonjs module file.
      • execString {String}: executable code string.
      • execFunction {Function}: js function.
    • taskRetry {Number}: number of task retries.
    • taskTimeout {Number}: timeout time.
    • transferList {Array}: A list of ArrayBuffer, MessagePort and FileHandle objects. After transferring, they will not be usable on the sending side.
  1. const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]);
  2. const dynamicExecutor = dynamicPool.createExecutor({
  3. execString: `module.exports = (payload) => payload`,
  4. // execFunction: (payload) => payload,
  5. // execPath: 'path/to/executable.js',
  6. taskRetry: 2,
  7. taskTimeout: 2e3,
  8. transferList: [unit8Array.buffer]
  9. });
  1. Methods of a DynamicThreadPool Executor
  • setExecPath(execPath): Set path of an executable commonjs module file.
    • @param {String} execPath:path to an executable commonjs module file.
  • setExecString(execString): Set executable code string.
    • @param {String} execString:executable code string.
  • setExecFunction(execFunction): Set js function.
    • @param {Function} execFunction:js function.
  • queue(payload): Save a task request to task queue, will throw an error when the task queue is full.
    • @param {Any} payload *: The request payload data.
  • exec(payload): Send a task request to pool from excutor.
    • @param {Any} payload *: The request payload data.
  • setTaskRetry(taskRetry): Set count of task retries.
    • @param {Number} taskRetry:Number of task retries.
  • setTransferList(transferList): Set transfer-list data of task.
    • @param {Array} transferList:transfer-list data.
  • setExecPath(execPath): Set path of an executable commonjs module file.

IX. Examples


  1. electronux - A project of mine that uses BroserService and MessageChannel of electron-re.

  2. file-slice-upload - A demo about parallel upload of multiple files, it uses ChildProcessPool and ProcessHost of electron-re, based on Electron@9.3.5.

  3. Also you can check the index.dev.js and test dir in root, there are some useful cases.

X. Test Coverage

  1. ------------------------------------|---------|----------|---------|---------|---------------------------------------------------------------------------------------------
  2. File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
  3. ------------------------------------|---------|----------|---------|---------|---------------------------------------------------------------------------------------------
  4. All files | 75.27 | 55.73 | 68.15 | 79.29 |
  5. lib | 96.42 | 66.66 | 100 | 96.42 |
  6. index.js | 96.42 | 66.66 | 100 | 96.42 | 23
  7. lib/libs | 66.83 | 44.61 | 59.12 | 72.59 |
  8. BrowserService.class.js | 76.99 | 55.26 | 55.17 | 77.22 | 61-64,69-92,106-108,172,184-185,191,209-210,231,235
  9. EventCenter.class.js | 80 | 58.33 | 83.33 | 100 | 15-35
  10. FileWatcher.class.js | 60 | 41.66 | 42.85 | 66.66 | 29-49
  11. MessageChannel.class.js | 52.11 | 32.35 | 46.8 | 58.49 | 60-61,75-256,292-297,312-313,394-409,540-547
  12. ProcessHost.class.js | 70.73 | 35.71 | 62.5 | 76.31 | 27,51-59,80,92-108
  13. ProcessLifeCycle.class.js | 88.46 | 67.64 | 94.11 | 98.36 | 95
  14. consts.js | 100 | 100 | 100 | 100 |
  15. utils.js | 65.16 | 45.83 | 60 | 68.67 | 75-93,107-114,120-131,180,188
  16. lib/libs/ChildProcessPool | 84.84 | 62 | 80 | 89.91 |
  17. ForkedProcess.js | 78.33 | 50 | 72.72 | 82.45 | 22,64-75,97,103-105
  18. index.js | 86.76 | 65.78 | 82.35 | 92.39 | 119,141,219-223,231-233,287,306-315
  19. lib/libs/LoadBalancer | 80.29 | 50 | 84.37 | 80.91 |
  20. consts.js | 100 | 100 | 100 | 100 |
  21. index.js | 77.58 | 50 | 82.14 | 78.18 | 63-69,83,97,103,113,126,153,158-162,178-193,203
  22. scheduler.js | 95 | 50 | 100 | 95 | 28
  23. lib/libs/LoadBalancer/algorithm | 94.79 | 67.3 | 100 | 100 |
  24. MINIMUM_CONNECTION.js | 92.3 | 64.28 | 100 | 100 | 5-6,19
  25. POLLING.js | 85.71 | 50 | 100 | 100 | 5-9
  26. RANDOM.js | 100 | 50 | 100 | 100 | 7
  27. SPECIFY.js | 100 | 75 | 100 | 100 | 14
  28. WEIGHTS.js | 92.85 | 66.66 | 100 | 100 | 5-11
  29. WEIGHTS_MINIMUM_CONNECTION.js | 94.11 | 80 | 100 | 100 | 5,15
  30. WEIGHTS_POLLING.js | 92.3 | 66.66 | 100 | 100 | 5-10
  31. WEIGHTS_RANDOM.js | 100 | 66.66 | 100 | 100 | 9,17
  32. index.js | 100 | 100 | 100 | 100 |
  33. lib/libs/ProcessManager | 51.08 | 25 | 38.46 | 51.77 |
  34. index.js | 57.21 | 31.39 | 48 | 58.6 | 66,71,76,81,110,123,132,143,150-169,175-224,228-230,236-245,285-287,295-298,303-306,311-336
  35. ui.js | 32.35 | 0 | 6.66 | 32.83 | 34-122,130-137
  36. lib/libs/WorkerThreadPool | 81.52 | 57.5 | 78 | 84.72 |
  37. Task.js | 85.36 | 83.33 | 61.53 | 85.36 | 44-45,77-92
  38. TaskQueue.js | 66.19 | 39.28 | 86.66 | 69.23 | 78-79,98-139
  39. Thread.js | 85 | 64.7 | 80 | 91.78 | 61-63,150-156,165
  40. consts.js | 100 | 100 | 100 | 100 |
  41. index.js | 100 | 100 | 100 | 100 |
  42. utils.js | 91.66 | 50 | 100 | 91.66 | 23
  43. lib/libs/WorkerThreadPool/Executor | 81.53 | 67.5 | 80.76 | 89.79 |
  44. DynamicExecutor.js | 76.66 | 71.42 | 75 | 84.09 | 50-53,66-67,97
  45. Executor.js | 93.75 | 66.66 | 100 | 93.75 | 70,74
  46. StaticExecutor.js | 78.94 | 60 | 75 | 95.45 | 39
  47. lib/libs/WorkerThreadPool/Pool | 83.37 | 72.41 | 81.01 | 87.95 |
  48. DynamicThreadPool.js | 82.5 | 66.12 | 76.47 | 90.56 | 87-89,129,141
  49. StaticThreadPool.js | 87.69 | 70.83 | 92.85 | 97.95 | 144
  50. ThreadPool.js | 82.57 | 75.49 | 79.16 | 85.21 | 116-118,144-145,165,213-214,224,256-258,356,494-533,564,568,572,576,587
  51. lib/libs/WorkerThreadPool/Worker | 77.35 | 60 | 62.5 | 81.01 |
  52. index.js | 84.28 | 67.85 | 76.47 | 95.34 | 49,73
  53. worker-runner.js | 63.88 | 41.66 | 28.57 | 63.88 | 19-22,32,36-42,54-59,65,76
  54. lib/tasks | 83.33 | 50 | 100 | 88.23 |
  55. app.init.js | 83.33 | 50 | 100 | 88.23 | 33-39
  56. ------------------------------------|---------|----------|---------|---------|---------------------------------------------------------------------------------------------