https://github.com/sapphi-red/artichokie
Mutual callable worker thread pool with fallback. Based on okie.
https://github.com/sapphi-red/artichokie
Last synced: about 1 month ago
JSON representation
Mutual callable worker thread pool with fallback. Based on okie.
- Host: GitHub
- URL: https://github.com/sapphi-red/artichokie
- Owner: sapphi-red
- License: mit
- Created: 2023-11-06T08:23:34.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-02T01:21:30.000Z (about 2 months ago)
- Last Synced: 2025-04-02T02:27:27.036Z (about 2 months ago)
- Language: TypeScript
- Homepage:
- Size: 525 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# artichokie
[](https://badge.fury.io/js/artichokie)  [](LICENSE)
> Okie dokie artichokie
Mutual callable worker thread pool with fallback. Based on [okie](https://github.com/yyx990803/okie).
## Features
- worker pool
- calling functions in the main thread from the worker
- falling back to run the code in the main thread## Examples
```js
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) // 4worker.stop()
``````js
const infSymbol = Symbol('inf')
const isInf = async (n: number | symbol) => n === infSymbolconst 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()
```