Socket
Socket
Sign inDemoInstall

artichokie

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    artichokie

Mutual callable worker thread pool with fallback.


Version published
Weekly downloads
399
decreased by-38.99%
Maintainers
1
Install size
10.9 kB
Created
Weekly downloads
 

Readme

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

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

Keywords

FAQs

Last updated on 01 Dec 2023

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc