Skip to content

Commit 3fd5e51

Browse files
committed
docs: add Limitations section for inline functions
1 parent 08e73aa commit 3fd5e51

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,67 @@ if (result.status === 'fulfilled') console.log(result.value)
294294

295295
---
296296

297+
## Limitations (Inline Functions)
298+
299+
When using `bee()`, `beeThreads.run()`, or `turbo()` (without `worker()`), data is transferred via [Structured Clone Algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm).
300+
301+
### ✅ What CAN be passed as parameters
302+
303+
| Type | Works |
304+
|------|-------|
305+
| Primitives (`string`, `number`, `boolean`, `null`, `undefined`, `BigInt`) ||
306+
| Arrays, Objects (POJOs) ||
307+
| `Date`, `RegExp`, `Map`, `Set` ||
308+
| `ArrayBuffer`, TypedArrays (`Uint8Array`, `Float64Array`, etc.) ||
309+
| `Error` (with custom properties) ||
310+
| Nested objects ||
311+
312+
### ❌ What CANNOT be passed as parameters
313+
314+
| Type | Why |
315+
|------|-----|
316+
| **Functions** | Not cloneable (use `setContext` instead) |
317+
| **Symbols** | Not cloneable |
318+
| **Class instances** | Lose prototype and methods |
319+
| **WeakMap, WeakSet** | Not cloneable |
320+
| **Circular references** | Not supported |
321+
| **Streams** | Not cloneable |
322+
323+
### ⚠️ Closures Must Be Explicit
324+
325+
Functions lose access to external variables when sent to workers:
326+
327+
```ts
328+
// ❌ FAILS - x doesn't exist in worker
329+
const x = 10
330+
await bee((a) => a + x)(5) // ReferenceError: x is not defined
331+
332+
// ✅ WORKS - pass x explicitly
333+
const x = 10
334+
await bee((a) => a + x)(5, { beeClosures: { x } }) // 15
335+
```
336+
337+
```ts
338+
// ❌ FAILS - helper loses access to multiplier
339+
const multiplier = 2
340+
const helper = (n) => n * multiplier
341+
342+
await beeThreads.run((x) => helper(x))
343+
.setContext({ helper }) // helper is stringified, loses closure!
344+
.usingParams(5)
345+
.execute()
346+
347+
// ✅ WORKS - pass all dependencies
348+
await beeThreads.run((x) => helper(x))
349+
.setContext({ helper, multiplier })
350+
.usingParams(5)
351+
.execute() // 10
352+
```
353+
354+
> **Need `require()`, database, or npm modules?** Use [`beeThreads.worker()`](#beethreadsworker---file-workers) instead.
355+
356+
---
357+
297358
## Why bee-threads?
298359

299360
- **Zero dependencies** - Lightweight and secure

0 commit comments

Comments
 (0)