Skip to content

Commit d8038a8

Browse files
authored
Changed README
1 parent da5c26f commit d8038a8

1 file changed

Lines changed: 36 additions & 21 deletions

File tree

README.md

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,41 @@ Unlike traditional Web Workers which require separate entry files and manual mes
2424
npm install experimental-threads
2525
```
2626

27+
## Usage
28+
29+
```typescript
30+
import { spawn } from "experimental-threads";
31+
import { v4: uuiv4 } from "uuid";
32+
33+
const data = { hello: 'world' };
34+
35+
// We must use eval() to capture the local scope of 'data'
36+
const result = await eval(spawn(() => {
37+
// 'data' is available inside the Worker
38+
console.log(data.hello); // "world"
39+
40+
// Magic. No dynamic import necessary inside the closure
41+
return uuiv4();
42+
}));
43+
44+
console.log(result); // 'cca68b4c-7416-4b44-ba94-9cf6ca90bf59'
45+
```
46+
47+
## Why must `spawn()` be wrapped in `eval()`?
48+
49+
The usage of `eval()` is a deliberate architectural choice to enable **Runtime Scope Bridging**.
50+
51+
Standard JavaScript functions cannot reflectively access the local variables of their caller. While our AST analysis allows the library to identify *identifiers* (variable names) within your closure, it cannot access their *values*.
52+
53+
1. `spawn()` analyzes the source and finds that you used the variable `data`.
54+
2. `spawn()` returns a generated code string that acts as a bridge.
55+
3. `eval()` executes this string in the **current local scope**, allowing the library to resolve the runtime value of `data` and transfer it to the thread.
56+
57+
This eliminates the need for manual dependency injection, and preserves the ergonomics of standard JavaScript closures.
58+
2759
## Architecture
2860

29-
### 1. Runtime Scope Analysis & AST Traversal
61+
### Runtime Scope Analysis & AST Traversal
3062

3163
Standard JavaScript closures cannot be serialized to Workers because they are bound to the parent execution context. **experimental-threads** bypasses this limitation by performing **Just-In-Time (JIT) Static Analysis** on the calling code.
3264

@@ -38,7 +70,7 @@ When `spawn(fn)` is invoked:
3870
4. **Identifier Resolution:** It walks the scope chain of the function body, distinguishing between *bound identifiers* (parameters, local variables) and *free variables* (captured from the outer scope).
3971
5. **Transpilation:** It generates a standalone worker entry script that imports necessary modules and injects the captured free variables as structured-cloned properties.
4072

41-
### 2. Deterministic Memory Hydration (`Global<T>`)
73+
### Deterministic Memory Hydration (`Global<T>`)
4274

4375
In a multi-process or multi-isolate architecture (like Web Workers), module singletons are not shared. They are instantiated once per thread. This breaks the identity of shared locks or buffers.
4476

@@ -48,24 +80,7 @@ This library introduces **Location-Dependent Reference Integrity**.
4880
* **Memory Registry:** The main thread maintains a global memory registry, mapping these IDs to their underlying `SharedArrayBuffer` pointers.
4981
* **Hydration:** When a Worker boots and imports a module containing a `Global`, the constructor intercepts the instantiation. Instead of allocating new memory, it queries the registry transmitted during the handshake phase and **hydrates** the instance with the existing buffer from the main/parent thread.
5082

51-
This guarantees that `const lock = new Global(new Mutex())` refers to the exact same memory address (access cost) in every thread, enabling atomic synchronization.
52-
53-
## Usage
54-
55-
```typescript
56-
import { spawn } from "experimental-threads";
57-
58-
const data = { hello: 'world' };
59-
60-
const result = await eval(spawn(async () => {
61-
// Will be automatically captured
62-
console.log(data.hello); // "world"
63-
64-
return Math.random();
65-
}));
66-
67-
console.log(result); // 0.6378467071314606
68-
```
83+
This guarantees that `const lock = new Global(new Mutex())` refers to the exact same memory address in every thread, enabling atomic synchronization.
6984

7085
## Synchronization Primitives
7186

@@ -81,4 +96,4 @@ A signaling mechanism to control access to a common resource by multiple process
8196

8297
## License
8398

84-
MIT
99+
MIT

0 commit comments

Comments
 (0)