Nerdy Poly Millionaires

    web-workers

    0.9.1 • Public • Published

    Web Workers Kit

    Run your code easily in web workers

    Features

    • 0️⃣ Zero Dependencies
    • ✌️ Promise based architecture
    • ☁️ Light Weight BundlePhobia Web Workes

    Install

    NPM -

    npm i web-workers

    or CDN -

    <script src="https://unpkg.com/web-workers@0.6.0/dist.js"></script>

    Usage

    const lunar = () => {
      async function get () {
        const res = await fetch('https://jsonplaceholder.typicode.com/todos/1')
        return res.json()
      }
    }
     
    const lunarMission = WebWorker(lunar)
     
    lunarMission.get().then(d => console.log(d))

    API Documentations

    WebWorker

    The WebWorker constructor is used invoke/spawn a web worker with the script you pass. And will return promises for all underlying methods of the script.

    const WebWorker = require('web-workers')
     
    const script = function () {
      console.log('Web Worker is Invocked')
     
      function add(a, b) {
        return a + b
      }
    }
     
    const worker = WebWorker(script)

    Params

    • script - script to run in the worker thread

    worker.call() and worker[method]

    The call() or [method] can be used to call underlying functions/methods which are also promise based

    worker.add(1, 2) // 3
     
    worker.call('add', 2, 3) // 5

    Params

    • ...args - arguments to pass to the method

    worker.message()

    The method is used to add a message to the worker ⚠️ but not post/send it. The same message you can use and append later in the stage and post.

    worker.message('Hello')
     
    worker.message({
      data: [],
      errors: []
    })

    To clear the message use message with no params 😉

    Params

    • message - message to set - can be of any type - string, object, array

    worker.append()

    This method is used to append the passed message to the earlier set message

    worker.message('Hello')
     
    worker.append(' World!')

    Params

    • message - message to append - can be of any type - string, object, array but should be matching the already set message

    worker.getMessage()

    getMessage() returns the current message with worker

    worker.message('Hello')
     
    worker.getMessage() // 'Hello'

    worker.kill()

    kill() will terminate your worker thread ⚠️ The kill method will kill the worker even if there is some execution happening

    worker.kill()

    worker.onerror(fn)

    onerror is an error event handler, i.e. if any error happens at the worker thread, you can listen to it right here

    worker.onerror((err) => {
      console.log(err)
    })

    worker.ondata(fn)

    Use this method to access the message posted in the worker thread, using postMessage()

    worker.ondata((data) => {
      console.log(data)
    })

    Keywords

    none

    Install

    npm i web-workers

    DownloadsWeekly Downloads

    9

    Version

    0.9.1

    License

    ISC

    Unpacked Size

    14 kB

    Total Files

    7

    Last publish

    Collaborators

    • maddhruv