Skip to content

Commit 3eb32b9

Browse files
committed
docs: clarify in-page event direction
1 parent c5337a8 commit 3eb32b9

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

docs/content/1.guide/12.in-page-channel.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ Channel names are namespaced with the devframe id, like RPC ids. Function names
5454

5555
## The page script endpoint
5656

57-
The required `functions` object declares every function on that endpoint's protocol side, preserving a compile-time completeness check. Request/response declarations require a `handler`; an event declaration uses `type: 'event'` and may receive events through either its optional `handler` or runtime `channel.on()` listeners. Functions use the same Standard-Schema `args`/`returns` and `jsonSerializable` metadata as `defineRpcFunction`, narrowed to the browser. Each handler is contextually typed from its key and the corresponding protocol function. `defineChannelFunction` retains the named definition shape for lower-level authoring. Define each side's functions in that side's source files; the shared protocol file carries only types.
57+
The required `functions` object declares every function on that endpoint's protocol side, preserving a compile-time completeness check. Request/response declarations require a `handler`; an event declaration uses `type: 'event'`, and the receiving endpoint may provide an optional `handler` or subscribe at runtime with `on()`. Functions use the same Standard-Schema `args`/`returns` and `jsonSerializable` metadata as `defineRpcFunction`, narrowed to the browser. Each handler is contextually typed from its key and the corresponding protocol function. `defineChannelFunction` retains the named definition shape for lower-level authoring. Define each side's functions in that side's source files; the shared protocol file carries only types.
5858

5959
```ts
6060
import type { MyChannelProtocol } from '../shared/protocol'
6161
// inject/index.ts: runs in the user app's page
6262
import { createPageScriptChannel } from 'devframe/in-page-channel'
6363
import { MY_CHANNEL } from '../shared/protocol'
6464

65-
const channel = createPageScriptChannel<MyChannelProtocol>({
65+
const pageChannel = createPageScriptChannel<MyChannelProtocol>({
6666
name: MY_CHANNEL,
6767
functions: {
6868
highlight: {
@@ -79,12 +79,12 @@ const channel = createPageScriptChannel<MyChannelProtocol>({
7979
},
8080
})
8181

82-
channel.emit('flash', 'scanning…') // fans out to every connected panel
83-
channel.events.on('panel:connected', panel => console.log(panel.id))
84-
channel.events.on('panel:disconnected', () => pauseWorkIfNobodyWatches())
82+
pageChannel.emit('flash', 'scanning…') // received by each panel endpoint
83+
pageChannel.events.on('panel:connected', panel => console.log(panel.id))
84+
pageChannel.events.on('panel:disconnected', () => pauseWorkIfNobodyWatches())
8585
```
8686

87-
`emit` on the page script is 1:N: it fans out to every connected panel. Request/response *to* a panel goes through an explicit peer handle: `channel.panels[0].call('flash', '…')`.
87+
`emit` on the page-script endpoint is 1:N: it fans out to every connected panel endpoint. Request/response *to* a panel goes through an explicit peer handle: `pageChannel.panels[0].call('flash', '…')`.
8888

8989
## The panel endpoint
9090

@@ -94,20 +94,22 @@ import type { MyChannelProtocol } from '../shared/protocol'
9494
import { connectPanelChannel } from 'devframe/in-page-channel'
9595
import { MY_CHANNEL } from '../shared/protocol'
9696

97-
const channel = connectPanelChannel<MyChannelProtocol>({
97+
const panelChannel = connectPanelChannel<MyChannelProtocol>({
9898
name: MY_CHANNEL,
9999
functions: {
100100
flash: { type: 'event' },
101101
},
102102
})
103103

104-
const offFlash = channel.on('flash', message => showFlash(message))
105-
channel.emit('highlight', '.hero') // buffered until connected
106-
const size = await channel.call('measure', '.hero')
104+
const offFlash = panelChannel.on('flash', message => showFlash(message))
105+
panelChannel.emit('highlight', '.hero') // received by the page-script endpoint
106+
const size = await panelChannel.call('measure', '.hero')
107107

108108
offFlash() // stop listening
109109
```
110110

111+
The snippets form one channel pair: `pageChannel.emit('flash', …)` invokes `panelChannel.on('flash', …)`. In the other direction, `panelChannel.emit('highlight', …)` invokes the page-script endpoint's `highlight` handler and any matching `pageChannel.on()` listeners. An endpoint never receives its own emission.
112+
111113
## Shared state
112114

113115
The channel's shared-state layer mirrors [`rpc.sharedState`](/guide/shared-state) (same `SharedState<T>` handle, same accessor), with the page script playing the server's role as rendezvous and authority. Its first `get` of a key must provide the initial value; panels are seeded automatically on connect (including late joiners and re-connects) and converge through syncId-deduplicated patches.

docs/content/8.references/5.browser-api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ The values of `rpc.status`: [Handling connection and auth errors](/guide/client#
4747

4848
## In-page channel endpoints
4949

50-
The browser-only endpoint methods of the [in-page channel](/guide/in-page-channel).
50+
The browser-only endpoint methods of the [in-page channel](/guide/in-page-channel). `emit()` sends to the opposite endpoint; `on()` handles events arriving from that endpoint.
5151

52-
| Method or property | Page script | Panel |
52+
| Method or property | Page-script endpoint | Panel endpoint |
5353
|--------------------|-------------|-------|
5454
| `emit(name, ...args)` | Fans an event out to every connected panel. | Sends an event to the page script, buffering while connecting. |
5555
| `on(name, listener)` | Subscribes to events emitted by a panel. | Subscribes to events emitted by the page script. Returns an unsubscribe function. |

0 commit comments

Comments
 (0)