Socket
Book a DemoInstallSign in
Socket

artichokie

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

artichokie

Mutual callable worker thread pool with fallback.

latest
Source
npmnpm
Version
0.4.0
Version published
Weekly downloads
2K
-0.1%
Maintainers
1
Weekly downloads
 
Created
Source

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

[!NOTE] The structured clone algorithm is used to pass the values between the worker thread and the main thread. Some object properties may be dropped because of that. If needed, you can serialize the vaules manually before returning on the worker thread side.

Examples

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

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

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()

Keywords

worker threads

FAQs

Package last updated on 29 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts