Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/deebloo/worker-swarm
A small library to help distribute work across a pool of workers
https://github.com/deebloo/worker-swarm
Last synced: 5 days ago
JSON representation
A small library to help distribute work across a pool of workers
- Host: GitHub
- URL: https://github.com/deebloo/worker-swarm
- Owner: deebloo
- Created: 2020-04-08T15:18:45.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-10-24T15:46:17.000Z (20 days ago)
- Last Synced: 2024-11-01T11:51:34.214Z (12 days ago)
- Language: TypeScript
- Homepage:
- Size: 12.6 MB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Worker Swarm
A small library to help distribute work across a pool of workers
```
npm i worker-swarm
```### worker.js
```TS
self.onmessage = (e) => {
self.postMessage({
jobId: e.data.jobId, // send the job id back to complete a task
message: 'RESPONSE FROM ANOTHER THREAD'
})
}
```### UI Thread
```TS
import { WorkerSwarm } from 'worker-swarm';// Create 3 instances of the worker
const swarm = new WorkerSwarm(() => new Worker('./worker.js'), 3);// Will go to first worker
swarm.post({}).then((res) => {
console.log(res)
});// Will go to the second worker
swarm.post({}).then((res) => {
console.log(res)
});// Will go to the third worker
swarm.post({}).then((res) => {
console.log(res)
});
```