Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mkgaru/esthread
modern worker threading library . inspired by deep-rain/thread
https://github.com/mkgaru/esthread
javascript promise publicdomain thread typescript webworker
Last synced: about 2 months ago
JSON representation
modern worker threading library . inspired by deep-rain/thread
- Host: GitHub
- URL: https://github.com/mkgaru/esthread
- Owner: MKGaru
- License: mit
- Created: 2018-03-10T03:21:27.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-28T18:05:02.000Z (almost 6 years ago)
- Last Synced: 2024-10-07T12:09:55.238Z (3 months ago)
- Topics: javascript, promise, publicdomain, thread, typescript, webworker
- Language: TypeScript
- Homepage: https://jsfiddle.net/MKGaru/btqovea0/
- Size: 28.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
ESThread
==================
modern worker threading library[![npm version](https://badge.fury.io/js/esthread.svg)](https://badge.fury.io/js/esthread)
Usage
--------------------browser
```html// any task.
// const thread = new Thread(function(a,b){return a+b})```
or esmodule
```html
import Thread from 'https://unpkg.com/esthread/dist/thread.mjs'
// any task.
// const thread = new Thread(function(a,b){return a+b})```
or npm module
```bash
npm install esthread --save
```and
```javascript
import Thread from 'esthread'
// const thread = new Thread(function(a,b){return a+b})
```### [Example1] Simple
```javascript
const thread = new Thread((a,b)=>a+b)
thread.execute(2,3).then(result=>console.log(result))
.then(()=>thread.terminate())
.catch(()=>thread.terminate())
```### [Example2-a] with progress.
```javascript
const thread2a = new Thread((async function(v){
let sum = 0
for(let i=v;i>=0;i--){
await new Promise(res=>setTimeout(res,1000))
this.emit('count',i) // <-------------
sum+=i
}
return sum
}))
thread2a.on('count',(n)=>console.info(n))
thread2a.execute(10).then(n=>{
console.log(n)
thread2a.terminate()
})
```### [Example2-b] with progress. if use arrow function task , can't modify this args. should use scoped emit function.
```javascript
// if use typescript: declare function emit(type:string,data?:any):void
const thread2b = new Thread((async (v)=>{
let sum = 0
for(let i=v;i>=0;i--){
await new Promise(res=>setTimeout(res,1000))
emit('count',i) // <------------
sum+=i
}
return sum
}))
thread2b.on('count',(n)=>console.info(n))
thread2b.execute(10).then(n=>{
console.log(n)
thread2a.terminate()
})
```### [Example3] cloned thread.
```javascript
// blocking slow function.
function fibonacci(n){
return n<2 ? n : ( fibonacci(n-1) + fibonacci(n-2) )
}
const [thread3a,thread3b,thread3c] = new Thread(fibonacci).clone(3)
thread3a.once(40).then(result=>console.log(result))
thread3b.once(42).then(result=>console.log(result))
thread3c.once(44).then(result=>console.log(result))
```### [Example4] transferable ArrayBuffer. like webworker.postMessage
```javascript
const thread4 = new Thread(buffer=>{
buffer.set([192,168,10,3])
return buffer
})
const input = new Uint32Array(256)
thread4.execute(input,[input.buffer]).then(output=>{
// input was transfered. ( can not access from sender )
console.log(output)
})
```### [Example5] transferable OffscreenCanvas [[experimental technology](https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas)]
```javascript
const canvas = document.createElement('canvas')
document.body.appendChild(canvas)
const renderer = new Thread((canvas)=>{
const ctx = canvas.getContext('2d')
ctx.fillStyle = 'rgb(200, 0, 0)'
ctx.fillRect(20, 30, 60, 40)
ctx.commit()
})
const offscreen = canvas.transferControlToOffscreen() // NOTE: OffscreenCanvas required explicitly enable this feature 2018.3.
renderer.execute( offscreen , [offscreen] )
```### [Example6] with other libraries
```javascript
async function learn(){
// foo bar
}
const thread6 = new Thread(learn,['https://cdn.jsdelivr.net/npm/[email protected]/setImmediate.min.js'])
```### [Example7] NodeJS Worker [[experimental technology](https://nodejs.org/api/worker_threads.html)] ( nodejs > v10.5.0 )
```bash
node --experimental-worker examples/nodejs/example3.js
``````javascript
const { Thread } = require('esthread')function fibonacci(n){
// if need other library. you can use require('some library') here.
return n<2 ? n : ( fibonacci(n-1) + fibonacci(n-2) )
}
const [thread3a,thread3b,thread3c] = new Thread(fibonacci).clone(3)
thread3a.once(37).then(result=>console.log(result))
thread3b.once(39).then(result=>console.log(result))
thread3c.once(41).then(result=>console.log(result))
```