Skip to content

Commit b730c27

Browse files
Allow accessing un-wrapped worker instance (#138)
* Allow accessing un-wrapped worker instance Fixes #111 * Use symbol and wrap proxy in another proxy * Fix typo * add example * rebuild package-lock --------- Co-authored-by: mathe42 <[email protected]>
1 parent 6e701eb commit b730c27

19 files changed

+2160
-1391
lines changed

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
src
2+
example
23
.github
34
renovate.json
45
tsconfig.json

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,15 @@ const result = await instance.add(2, 3)
102102

103103
result === 5
104104
```
105+
### Get Worker Instance
106+
You can get to the worker instance like this:
105107

108+
```ts
109+
import { endpointSymbol } from 'vite-plugin-comlink/symbol'
110+
111+
const api = new ComlinkWorker<typeof import('./worker')>(new URL('./worker', import.meta.url), {/* normal Worker options*/})
112+
const worker = api[endpointSymbol];
113+
```
106114

107115
## Module Worker
108116
Not all Browsers support module Workers (see https://caniuse.com/mdn-api_worker_worker_ecmascript_modules).

client.d.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import type { Remote } from 'comlink'
2+
import { endpointSymbol as sym} from "./symbol";
23

34
declare global {
5+
declare const endpointSymbol: sym;
6+
47
var ComlinkWorker: {
5-
new<T = any>(scriptURL: URL, options?: ComlinkWorkerOptions): Remote<T>;
8+
new<T = any>(scriptURL: URL, options?: ComlinkWorkerOptions): {readonly [sym]: Worker} & Remote<T>;
69
};
710

811
var ComlinkSharedWorker: {
9-
new<T = any>(scriptURL: URL, options?: ComlinkWorkerOptions): Remote<T>;
12+
new<T = any>(scriptURL: URL, options?: ComlinkWorkerOptions): {readonly [sym]: SharedWorker} & Remote<T>;
1013
};
1114

1215
interface ComlinkWorkerOptions extends WorkerOptions {

example/.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

example/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + TS</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/src/main.ts"></script>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)