# node-threading

> The brand-new solution for easy threading in node.

Latest version **1.0.4** (published 2021-09-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install node-threading
pnpm add node-threading
yarn add node-threading
bun add node-threading
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2021-09-15 |
| First published | 2021-09-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | hyperdev-dc |

## Links

- npm: https://www.npmjs.com/package/node-threading
- npm.io page: https://npm.io/package/node-threading

## Recent versions

- 1.0.4 (latest) — 2021-09-15
- 1.0.3 — 2021-09-15
- 1.0.2 — 2021-09-14
- 1.0.1 — 2021-09-14
- 1.0.0 — 2021-09-14

## README

# Node-threading
The brand-new solution for easy threading in node.

Threading can sometimes be a time-consuming thing to do, and rather confusing thing aswell (Especially for beginners)
That changes today, instead of messing around with ``isMainThread`` and other files we now allow you to thread a function itself!
The threads are as simple, if not simplet than python threading. **node-threading** is an upcoming library to reïnvent how threading is done in nodejs.
We are planning to see if we can sync variables across thread/parent in the near future. for now every thread is a seperate instance of node, like the included sollution in node. We currently do not have a git repo but when it is at a stable point we will create a git repo for this library. **PLEASE NOTE! This libary is not ment for production products yet, you can use it in production products. But we strongly advise you not to.** With that out of the way we hope you will enjoy the simplicity of **node-threading** 

Discord: <https://discord.gg/zMtJQafwhh>

Examples:
```js
let { Thread } = require("node-threading");

function foo() {
    console.log("Bar")    
}

new Thread(foo).start();
```

```js
let { Thread } = require(`node-threading`);

function foo() {
    setInterval(() => {
        console.log("bar") //Send bar
    }, 90) //Enough delay to send bar 5x in 500ms
}

for (let i = 0; i < 2; i++) {
    let t = new Thread(foo);
    t.start(); //Start thread
    setTimeout(() => {
        t.terminate(); //Kill thread
    }, 500) //Allow 2 threads to send 5x Bar totaling in 10x bar
}
```
```js
function foo() {
    let { Parent } = require(`./index.js`);
    let parent = new Parent();

    parent.on("test", (data) => {
        console.log(data); //logs "works"
    })
}

for (let i = 0; i < 10; i++) {
    let t = new Thread(foo);
    t.start();
    t.send("test", "works")
}
```

## Class - Thread

Create constructor ``let t = new Thread(function, { cwd: dir, modules: dir});`` (Options are optional)
```
cwd = Run directory
modules = node modules dir
```

Start the thread ``t.start();``

stop the thread ``t.terminate();``

send message to thread ``t.send(channel, data)``

recieve message from thread ``t.on(channel, (data) => {})``

## Class - Parent

```js
//this code must be in the thread
let { Parent } = require(`node-threading`); //Require parent from node-threading, make sure to import it from the thread as variables as of now are not shared
let parent = new Parent(); //Create new instance
```
get message from parent ``parent.on(channel, (data) => {})``

send message to the parent ``parent.send(channel, data)``

## Changelog
1.0.4
```
* Update readme
```

1.0.3
```
+ Add parent class for IPC and getting data in the thread
+ Add IPC
+ Add thread.send and parent.send
+ Extend events to thread and parent classes
```

1.0.2
```
+ Fix require statement
+ Add thread.start() and thread.terminate()
```

1.0.1
```
+ Thread class
+ Fix Filesystem lib
```

1.0.0
```
+ Add Readme
```

---
_Source: https://npm.io/package/node-threading · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
