Commit 2910c8c
Add examples (#10)
## Summary
- Adds `worker-basic` example: HTTP batch + WebSocket transport toggle
with interactive playground
- Adds `worker-bidirectional` example: server-to-client RPC with Durable
Object audit log broadcast
- Returns `RpcSession` from WebSocket helpers instead of a proxy, giving
callers access to `session.onClose()` for connection lifecycle
management
- Adds `onClose(handler)` and `Symbol.dispose` to `RpcSession`
## API changes
`newWorkersWebSocketRpcSession` now returns `{ response, session }`
instead of `{ response, remote }`:
```ts
const { response, session } = newWorkersWebSocketRpcSession<ClientApi, typeof service>(
request,
service,
);
session.remote.onEvent(event); // call methods on the client
session.onClose(() => cleanup()); // clean up when connection drops
```
`newWebSocketRpcSession` now returns an `RpcSession` instead of a proxy:
```ts
const session = newWebSocketRpcSession<ServerApi>(wsUrl);
await session.remote.add(1, 2); // was: await proxy.add(1, 2)
session.close();
```
## Test plan
- [x] All 163 existing tests pass
- [x] New tests for `onClose` (transport close, session close,
already-closed, multiple handlers)
- [x] New test for `Symbol.dispose` on `RpcSession`
- [ ] Manual: run `worker-bidirectional` example, open two tabs, verify
audit events broadcast, close a tab and verify subscription cleanup via
`onClose`
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent 5a8749a commit 2910c8c
34 files changed
Lines changed: 7149 additions & 52 deletions
File tree
- examples
- worker-basic
- src
- web
- src
- worker-bidirectional
- src
- web
- src
- src
- __tests__
- fixtures
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments