-
Notifications
You must be signed in to change notification settings - Fork 16
feat: type in-page channel functions from protocol #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
antfu
merged 9 commits into
devframes:main
from
posva:feat/in-page-channel-protocol-functions
Sep 1, 2026
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
474a0fa
feat: type in-page channel functions from protocol
posva 23b3ced
refactor: require in-page channel function maps
posva 6598c40
refactor: simplify in-page channel option types
posva 4e1634b
test: cover in-page channel endpoint types
posva 91178a5
refactor: require in-page channel functions
posva 1c06428
test: organize in-page channel type coverage
posva 3947113
test: update in-page channel API snapshot
posva 9541c9a
fix: keep omitted channel sides strict
posva 28503b5
fix: declare empty a11y panel protocol
posva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,7 +45,7 @@ const DEFAULT_EVENT_BUFFER_LIMIT = 64 | |
| * the UI can key a fallback state off `status` / `whenConnected()`. | ||
| */ | ||
| export function connectPanelChannel<P extends InPageChannelProtocol>( | ||
| options: ConnectPanelChannelOptions, | ||
| options: ConnectPanelChannelOptions<P>, | ||
| ): PanelChannel<P> { | ||
| const { name } = options | ||
| const callTimeoutMs = options.callTimeoutMs ?? DEFAULT_CALL_TIMEOUT_MS | ||
|
|
@@ -62,8 +62,8 @@ export function connectPanelChannel<P extends InPageChannelProtocol>( | |
|
|
||
| const events = createEventEmitter<PanelChannelEvents>() | ||
| const registry = createLocalFunctionRegistry(codec) | ||
| for (const definition of options.functions ?? []) | ||
| registry.register(definition) | ||
| for (const [fnName, definition] of Object.entries(options.functions ?? {})) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as in page-script.ts |
||
| registry.register({ ...definition, name: fnName }) | ||
|
Comment on lines
64
to
+66
|
||
|
|
||
| let status: InPageChannelStatus = 'connecting' | ||
| let attached: AttachedChannelPort | undefined | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import type { InPageChannelProtocol } from './types' | ||
| import { describe, it } from 'vitest' | ||
| import { createPageScriptChannel } from './page-script' | ||
| import { connectPanelChannel } from './panel' | ||
|
|
||
| interface TestProtocol extends InPageChannelProtocol { | ||
| pageScript: { | ||
| echo: (value: string) => string | ||
| sum: (a: number, b: number) => number | ||
| save: (value: string) => Promise<void> | ||
| } | ||
| panel: { | ||
| notify: (message: string) => void | ||
| } | ||
| } | ||
|
|
||
| describe('in-page channel function definitions', () => { | ||
| it('infers page-script handlers from the protocol name', () => { | ||
| createPageScriptChannel<TestProtocol>({ | ||
| name: 'devframes:test', | ||
| functions: { | ||
| echo: { | ||
| handler: value => value.toUpperCase(), | ||
| }, | ||
| sum: { | ||
| handler: (a, b) => a + b, | ||
| }, | ||
| save: { | ||
| handler: () => {}, | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('infers panel handlers from the protocol name', () => { | ||
| const { port1 } = new MessageChannel() | ||
| const channel = connectPanelChannel<TestProtocol>({ | ||
| name: 'devframes:test', | ||
| window: false, | ||
| transport: port1, | ||
| functions: { | ||
| notify: { | ||
| handler: (message) => { | ||
| void message | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| channel.close() | ||
| }) | ||
|
|
||
| it('requires every function from the local protocol side', () => { | ||
| createPageScriptChannel<TestProtocol>({ | ||
| name: 'devframes:test', | ||
| // @ts-expect-error `sum` and `save` are required. | ||
| functions: { | ||
| echo: { handler: value => value }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('rejects keys from the remote side', () => { | ||
| createPageScriptChannel<TestProtocol>({ | ||
| name: 'devframes:test', | ||
| functions: { | ||
| echo: { handler: value => value }, | ||
| sum: { handler: (a, b) => a + b }, | ||
| save: { handler: () => {} }, | ||
| // @ts-expect-error `notify` is implemented by panels. | ||
| notify: { handler: (message: string) => void message }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('rejects handlers incompatible with the named protocol function', () => { | ||
| createPageScriptChannel<TestProtocol>({ | ||
| name: 'devframes:test', | ||
| functions: { | ||
| echo: { | ||
| // @ts-expect-error `echo` accepts and returns a string. | ||
| handler: (value: number) => value, | ||
| }, | ||
| sum: { handler: (a, b) => a + b }, | ||
| save: { handler: () => {} }, | ||
| }, | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we could use for in, it's a bit faster but goes through inherited properties, which in the case of functions, should be none unless someone pollutes the object prototype. I kept it this way because it shouldn't change much in the end