-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsymbol.ts
More file actions
52 lines (47 loc) · 1.66 KB
/
symbol.ts
File metadata and controls
52 lines (47 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { wrap as comlink_wrap } from "comlink";
// Re-export commonly used Comlink utilities for convenience
export {
proxy,
proxyMarker,
finalizer,
releaseProxy,
createEndpoint
} from 'comlink'
/**
* Symbol used to access the underlying Worker/SharedWorker instance
* from a Comlink-wrapped worker proxy.
*
* Usage:
* ```ts
* const worker = new ComlinkWorker<typeof import('./worker')>(
* new URL('./worker', import.meta.url)
* );
* const nativeWorker = worker[endpointSymbol]; // Access underlying Worker
* ```
*/
export const endpointSymbol = Symbol("getEndpoint");
/**
* Enhanced wrap function that extends Comlink's wrap with endpoint access.
*
* This function wraps a Worker/SharedWorker endpoint with Comlink's proxy,
* but also adds the ability to access the original endpoint via a symbol.
* This allows users to access native Worker methods and properties when needed.
*
* @param ep - The endpoint (Worker, SharedWorker.port, MessagePort, etc.) to wrap
* @returns Comlink proxy with additional endpoint access via endpointSymbol
*
* @internal This is used internally by the plugin transformation
*/
export const wrap: typeof comlink_wrap = (ep) => {
// Create the standard Comlink proxy
const wrapped = comlink_wrap(ep);
// Enhance the proxy to expose the underlying endpoint via symbol
return new Proxy(wrapped, {
get(target, prop, receiver) {
// If accessing the endpoint symbol, return the original endpoint
if (prop === endpointSymbol) return ep;
// Otherwise, delegate to the wrapped Comlink proxy
return Reflect.get(target, prop, receiver);
}
}) as any;
}