1. artichokie
Mutual callable worker thread pool with fallback.
artichokie
Package: artichokie
Created by: sapphi-red
Last modified: Fri, 01 Dec 2023 10:27:38 GMT
Version: 0.2.0
License: MIT
Downloads: 2,211
Repository: https://github.com/sapphi-red/artichokie

Install

npm install artichokie
yarn add artichokie

artichokie

npm version CI MIT License

Okie dokie artichokie

Mutual callable worker thread pool with fallback. Based on okie.

Features

  • worker pool
  • calling functions in the main thread from the worker
  • falling back to run the code in the main thread

Examples

 const parent = async () => 1
const worker = new Worker(
  () => async () => {
    return (await parent()) + 1
  },
  {
    parentFunctions: { parent }
  }
)

const result = await worker.run()
console.log(result) // 2

worker.stop()
 const infSymbol = Symbol('inf')
const isInf = async (n: number | symbol) => n === infSymbol

const worker = new WorkerWithFallback(
  () => async (n: number | symbol) => {
    return await isInf(n) ? Infinity : 0
  },
  {
    parentFunctions: { isInf },
    shouldUseFake(n) {
      // symbol cannot be passed to a worker
      // fallback to run the code in main thread in that case
      return typeof n === 'symbol'
    }
  }
)

const results = await Promise.all([
  worker.run(1),
  worker.run(infSymbol)
])

console.log(results) // [0, Infinity]

worker.stop()

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code