Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: API guide using jsdoc comments #9

Merged
merged 2 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 77 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,58 +32,106 @@
npm install nodejs-threads
```

## Usage
## API Guide

### Define a worker job that needs to be run in a separate thread.
<!-- API Docs start here -->

Use the ```defineWorker``` function to define a worker job in ```worker.js``` file (can be named anything). This function takes an async function as argument. It passes the payload received from the main thread to the argument function of the worker thread.
## Modules

```javascript
/**
* This code will execute in a separate thread
*/
const { defineWorker } = require('nodejs-threads');
// OR
import { defineWorker } from 'nodejs-threads';
<dl>
<dt><a href="#module_nodejs-threads">nodejs-threads</a></dt>
<dd></dd>
</dl>

### Functions

defineWorker(async (payload) => {
console.log(payload); // Payload from the Primary thread is availbale here
<dl>
<dt><a href="#createWorker">createWorker(workerPath, [payload])</a> ⇒</dt>
<dd><p>Function to create a new worker thread. Calling this function will instantiate a new worker thread
and return a promise which resolves to the reference to the worker thread. The worker thread will
be executed in a separate process.</p>
</dd>
<dt><a href="#defineWorker">defineWorker(job)</a> ⇒</dt>
<dd><p>Function to define what should be executed inside the worker thread.
The async function will be executed as soon as the worker thread is created.</p>
</dd>
</dl>

/*
* Do any CPU intensive task here.
* The event loop in primary thread won't be blocked .
*/
let result = 0;
for (let i = 0; i < payload.range; i++) {
result += i;
}
console.log('COMPLETED');
return result;
});
```
<a name="module_nodejs-threads"></a>

### nodejs-threads
<a name="createWorker"></a>

### createWorker(workerPath, [payload]) ⇒
Function to create a new worker thread. Calling this function will instantiate a new worker thread
and return a promise which resolves to the reference to the worker thread. The worker thread will
be executed in a separate process.

### Create a worker thread
**Kind**: global function
**Returns**: Promise which resolves to the reference of the worker
**Throws**:

You can create a new worker thread by simply calling ```createWorker``` function. The first argument is the path of the ```worker.js``` file and you can pass any payload as the second argument. The payload passed will be provided as an argument in the worker callback function.
- <code>Error</code> If workerPath is not a string

```javascript
**Created**: 27-DEC-2021
**Author**: Karan Raina <karanraina1996@gmail.com>

| Param | Type | Description |
| --- | --- | --- |
| workerPath | <code>string</code> | Path to the worker file |
| [payload] | <code>object</code> | Payload to be sent to the worker |

**Example**
```js
const { createWorker } = require('nodejs-threads');
// OR
import { createWorker } from 'nodejs-threads';

// Inside any async function
const worker = await createWorker('./worker.js', {
range: 50000000,
});

// Attach a listener if you expect any return value from the worker funcion
worker.on('message', (result) => {
console.log(result);
});
```
<a name="defineWorker"></a>

### defineWorker(job) ⇒
Function to define what should be executed inside the worker thread.
The async function will be executed as soon as the worker thread is created.

**Kind**: global function
**Returns**: Returns a promise which resolves to the result of the job
**Created**: 27-DEC-2021
**Author**: Karan Raina <karanraina1996@gmail.com>

| Param | Type | Description |
| --- | --- | --- |
| job | <code>function</code> | Async function to be executed in the worker |

**Example**
```js
// This code will execute in a separate thread
const { defineWorker } = require('nodejs-threads');
// OR
import { defineWorker } from 'nodejs-threads';
defineWorker(async (payload) => {
console.log(payload); // Payload from the Primary thread is availbale here

// Do any CPU intensive task here.
// The event loop in primary thread won't be blocked .

let result = 0;
for (let i = 0; i < payload.range; i++) {
result += i;
}
console.log('COMPLETED');
return result;
});
```

<!-- API Docs end here -->

## Run tests

Expand Down
Loading