Coder Social home page Coder Social logo

mkgaru / esthread Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 29 KB

modern worker threading library . inspired by deep-rain/thread

Home Page: https://jsfiddle.net/MKGaru/btqovea0/

License: MIT License

TypeScript 100.00%
javascript thread webworker typescript promise publicdomain

esthread's Introduction

ESThread

modern worker threading library

npm version

Usage

browser

<script src="https://unpkg.com/esthread"></script>
<script>
// any task.
// const thread = new Thread(function(a,b){return a+b})
</script>

or esmodule

<script type="module">
import Thread from 'https://unpkg.com/esthread/dist/thread.mjs'
// any task.
// const thread = new Thread(function(a,b){return a+b})
</script>

or npm module

npm install esthread --save

and

import Thread from 'esthread'
// const thread = new Thread(function(a,b){return a+b})

[Example1] Simple

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.

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.

// 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.

// 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

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]

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

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] ( nodejs > v10.5.0 )

node --experimental-worker examples/nodejs/example3.js
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))

esthread's People

Contributors

mkgaru avatar

Watchers

 avatar  avatar

esthread's Issues

Require packages in a thread

Hi,

First of all, I really like your library! It's simple and hides lots of things.

I am new to threads in Typescript, so this might be very simple. Is there a way to require local packages inside the thread? I see you have example 6 with a link to a library but I am looking for local packages in the node_modules. Example: I use MongoDB to manage collections of big files. I would like to access to the DB from inside the thread requiring the mongodb package (or any other package we use in our application) with a const MongoDB = require('mongodb').

Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.