Skip to content

Commit d00bf1b

Browse files
committed
refactor: simlpify
1 parent 003dbac commit d00bf1b

3 files changed

Lines changed: 15 additions & 72 deletions

File tree

packages/devframe/src/in-page-channel/in-page-channel.test.ts

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -146,64 +146,38 @@ describe('in-page channel over bring-your-own ports', () => {
146146
await expect(panel.call('hang')).rejects.toMatchObject({ code: 'timeout' })
147147
})
148148

149-
it('keeps same-named functions and events independent in both directions', async ({ onTestFinished }) => {
149+
it('keeps same-named functions and events independent', async ({ onTestFinished }) => {
150150
interface Protocol {
151-
functions: { pageScript: { save: () => void }, panel: { save: () => void } }
152-
events: { pageScript: { save: (value: string) => void }, panel: { save: (value: string) => void } }
151+
functions: { pageScript: { save: () => void } }
152+
events: { pageScript: { save: (value: string) => void } }
153153
}
154154
const pageAction = vi.fn()
155-
const panelAction = vi.fn()
156-
const pageEvent = vi.fn()
157-
const panelEvent = vi.fn()
158155
const pageListener = vi.fn()
159-
const panelListener = vi.fn()
160156
const pageScript = createPageScriptChannel<Protocol>({
161157
name: 'test',
162158
...noHandshake,
163159
functions: { save: { type: 'action', handler: pageAction } },
164-
events: { save: { handler: pageEvent } },
165160
})
166161
const { port1, port2 } = new MessageChannel()
167-
const peer = pageScript.addPanelPort(port1)
162+
pageScript.addPanelPort(port1)
168163
const panel = connectPanelChannel<Protocol>({
169164
name: 'test',
170165
...noHandshake,
171166
transport: port2,
172-
functions: { save: { type: 'action', handler: panelAction } },
173-
events: { save: { handler: panelEvent } },
167+
functions: {},
174168
})
175169
onTestFinished(() => {
176170
panel.close()
177171
pageScript.close()
178172
})
179173
const offPage = pageScript.on('save', pageListener)
180-
const offPanel = panel.on('save', panelListener)
181174
await panel.call('save')
182-
await peer.call('save')
183-
expect(pageEvent).not.toHaveBeenCalled()
184-
expect(panelEvent).not.toHaveBeenCalled()
185175
expect(pageListener).not.toHaveBeenCalled()
186-
expect(panelListener).not.toHaveBeenCalled()
187176
panel.emit('save', 'draft')
188-
pageScript.callEvent('save', 'saved')
189-
await until(() => pageListener.mock.calls.length === 1 && panelListener.mock.calls.length === 1)
190-
expect(pageEvent).toHaveBeenCalledWith('draft')
191-
expect(panelEvent).toHaveBeenCalledWith('saved')
177+
await until(() => pageListener.mock.calls.length === 1)
178+
expect(pageListener).toHaveBeenCalledWith('draft')
192179
offPage()
193-
offPanel()
194-
panel.callEvent('save', 'again')
195-
pageScript.emit('save', 'again')
196-
await until(() => pageEvent.mock.calls.length === 2 && panelEvent.mock.calls.length === 2)
197-
expect(pageListener).toHaveBeenCalledOnce()
198-
expect(panelListener).toHaveBeenCalledOnce()
199180
expect(pageAction).toHaveBeenCalledOnce()
200-
expect(panelAction).toHaveBeenCalledOnce()
201-
})
202-
203-
it('keeps untyped runtime subscriptions isolated from functions', ({ onTestFinished }) => {
204-
const { pageScript, dispose } = createLinkedPair()
205-
onTestFinished(dispose)
206-
expect(() => pageScript.on('boom' as any, () => {})).not.toThrow()
207181
})
208182

209183
it('round-trips calls, arguments, and results', async () => {
@@ -280,7 +254,6 @@ describe('in-page channel over bring-your-own ports', () => {
280254
const { s } = await import('devframe/utils/simple-schema')
281255
const { port1, port2 } = new MessageChannel()
282256
const pageScript = createPageScriptChannel<TestProtocol>({
283-
events: { note: {} },
284257
name: 'devframes:test',
285258
...noHandshake,
286259
functions: {
@@ -294,7 +267,6 @@ describe('in-page channel over bring-your-own ports', () => {
294267
})
295268
pageScript.addPanelPort(port1)
296269
const panel = connectPanelChannel<TestProtocol>({
297-
events: { notify: {} },
298270
name: 'devframes:test',
299271
...noHandshake,
300272
transport: port2,
@@ -315,7 +287,6 @@ describe('in-page channel over bring-your-own ports', () => {
315287
const a = new MessageChannel()
316288
const b = new MessageChannel()
317289
const pageScript = createPageScriptChannel<TestProtocol>({
318-
events: { note: {} },
319290
name: 'devframes:test',
320291
...noHandshake,
321292
functions: defaultPageScriptFunctions,
@@ -324,7 +295,6 @@ describe('in-page channel over bring-your-own ports', () => {
324295
pageScript.addPanelPort(b.port1)
325296
const received: string[] = []
326297
const panelA = connectPanelChannel<TestProtocol>({
327-
events: { notify: {} },
328298
name: 'devframes:test',
329299
...noHandshake,
330300
transport: a.port2,
@@ -361,14 +331,12 @@ describe('in-page channel over bring-your-own ports', () => {
361331
it('lets the page script call one panel through its peer handle', async () => {
362332
const { port1, port2 } = new MessageChannel()
363333
const pageScript = createPageScriptChannel<TestProtocol>({
364-
events: { note: {} },
365334
name: 'devframes:test',
366335
...noHandshake,
367336
functions: defaultPageScriptFunctions,
368337
})
369338
pageScript.addPanelPort(port1)
370339
const panel = connectPanelChannel<TestProtocol>({
371-
events: { notify: {} },
372340
name: 'devframes:test',
373341
...noHandshake,
374342
transport: port2,
@@ -387,14 +355,12 @@ describe('in-page channel over bring-your-own ports', () => {
387355
it('applies serialize/deserialize hooks to arguments and results', async () => {
388356
const { port1, port2 } = new MessageChannel()
389357
const pageScript = createPageScriptChannel<TestProtocol>({
390-
events: { note: {} },
391358
name: 'devframes:test',
392359
...noHandshake,
393360
functions: defaultPageScriptFunctions,
394361
})
395362
pageScript.addPanelPort(port1)
396363
const panel = connectPanelChannel<TestProtocol>({
397-
events: { notify: {} },
398364
name: 'devframes:test',
399365
...noHandshake,
400366
transport: port2,
@@ -417,7 +383,6 @@ describe('in-page channel over bring-your-own ports', () => {
417383
it('notifies the page script of panel lifecycle', async () => {
418384
const { port1, port2 } = new MessageChannel()
419385
const pageScript = createPageScriptChannel<TestProtocol>({
420-
events: { note: {} },
421386
name: 'devframes:test',
422387
...noHandshake,
423388
functions: defaultPageScriptFunctions,
@@ -428,7 +393,6 @@ describe('in-page channel over bring-your-own ports', () => {
428393
pageScript.events.on('panel:disconnected', peer => disconnected.push(peer.id))
429394
pageScript.addPanelPort(port1)
430395
const panel = connectPanelChannel<TestProtocol>({
431-
events: { notify: {} },
432396
name: 'devframes:test',
433397
...noHandshake,
434398
transport: port2,
@@ -510,15 +474,14 @@ describe('in-page channel shared state', () => {
510474
const a = new MessageChannel()
511475
const b = new MessageChannel()
512476
const pageScript = createPageScriptChannel<TestProtocol>({
513-
events: { note: {} },
514477
name: 'devframes:test',
515478
...noHandshake,
516479
functions: defaultPageScriptFunctions,
517480
})
518481
pageScript.addPanelPort(a.port1)
519482
pageScript.addPanelPort(b.port1)
520-
const panelA = connectPanelChannel<TestProtocol>({ events: { notify: {} }, name: 'devframes:test', ...noHandshake, transport: a.port2, functions: defaultPanelFunctions })
521-
const panelB = connectPanelChannel<TestProtocol>({ events: { notify: {} }, name: 'devframes:test', ...noHandshake, transport: b.port2, functions: defaultPanelFunctions })
483+
const panelA = connectPanelChannel<TestProtocol>({ name: 'devframes:test', ...noHandshake, transport: a.port2, functions: defaultPanelFunctions })
484+
const panelB = connectPanelChannel<TestProtocol>({ name: 'devframes:test', ...noHandshake, transport: b.port2, functions: defaultPanelFunctions })
522485
try {
523486
const authority = await pageScript.sharedState.get('doc', { initialValue: { count: 0 } })
524487
const mirrorA = await panelA.sharedState.get('doc')
@@ -539,7 +502,7 @@ describe('in-page channel shared state', () => {
539502

540503
it('seeds a late-joining panel with the current value', async () => {
541504
const { port1, port2 } = new MessageChannel()
542-
const pageScript = createPageScriptChannel<TestProtocol>({ events: { note: {} }, name: 'devframes:test', ...noHandshake, functions: defaultPageScriptFunctions })
505+
const pageScript = createPageScriptChannel<TestProtocol>({ name: 'devframes:test', ...noHandshake, functions: defaultPageScriptFunctions })
543506
const authority = await pageScript.sharedState.get('doc', { initialValue: { count: 0 } })
544507
authority.mutate((draft) => {
545508
draft.count = 41
@@ -549,7 +512,7 @@ describe('in-page channel shared state', () => {
549512
})
550513

551514
pageScript.addPanelPort(port1)
552-
const panel = connectPanelChannel<TestProtocol>({ events: { notify: {} }, name: 'devframes:test', ...noHandshake, transport: port2, functions: defaultPanelFunctions })
515+
const panel = connectPanelChannel<TestProtocol>({ name: 'devframes:test', ...noHandshake, transport: port2, functions: defaultPanelFunctions })
553516
try {
554517
const mirror = await panel.sharedState.get('doc')
555518
expect(mirror.value()).toEqual({ count: 42 })
@@ -643,14 +606,12 @@ describe('in-page channel handshake', () => {
643606
it('connects a panel to the page script and survives page-script restarts', async () => {
644607
const { hostWin, panelWin } = createWindowPair()
645608
const pageScript = createPageScriptChannel<TestProtocol>({
646-
events: { note: {} },
647609
name: 'devframes:test',
648610
window: asWindow(hostWin),
649611
heartbeat: false,
650612
functions: defaultPageScriptFunctions,
651613
})
652614
const panel = connectPanelChannel<TestProtocol>({
653-
events: { notify: {} },
654615
name: 'devframes:test',
655616
window: asWindow(panelWin),
656617
targets: [asWindow(hostWin)],
@@ -668,7 +629,6 @@ describe('in-page channel handshake', () => {
668629

669630
// … and a fresh one boots in the same window: the panel re-handshakes.
670631
const revived = createPageScriptChannel<TestProtocol>({
671-
events: { note: {} },
672632
name: 'devframes:test',
673633
window: asWindow(hostWin),
674634
heartbeat: false,
@@ -695,7 +655,6 @@ describe('in-page channel handshake', () => {
695655
const { hostWin, panelWin } = createWindowPair()
696656
const noted: string[] = []
697657
const panel = connectPanelChannel<TestProtocol>({
698-
events: { notify: {} },
699658
name: 'devframes:test',
700659
window: asWindow(panelWin),
701660
targets: [asWindow(hostWin)],
@@ -706,7 +665,6 @@ describe('in-page channel handshake', () => {
706665
panel.emit('note', 'buffered')
707666

708667
const pageScript = createPageScriptChannel<TestProtocol>({
709-
events: { note: {} },
710668
name: 'devframes:test',
711669
window: asWindow(hostWin),
712670
heartbeat: false,
@@ -728,7 +686,6 @@ describe('in-page channel handshake', () => {
728686
const { hostWin, panelWin } = createWindowPair()
729687
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
730688
const pageScript = createPageScriptChannel<TestProtocol>({
731-
events: { note: {} },
732689
name: 'devframes:test-origin',
733690
window: asWindow(hostWin),
734691
heartbeat: false,
@@ -760,7 +717,6 @@ describe('in-page channel handshake', () => {
760717
const { hostWin, panelWin } = createWindowPair()
761718
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
762719
const pageScript = createPageScriptChannel<TestProtocol>({
763-
events: { note: {} },
764720
name: 'devframes:test-version',
765721
window: asWindow(hostWin),
766722
heartbeat: false,
@@ -791,14 +747,12 @@ describe('in-page channel handshake', () => {
791747
it('honors an instance pin', async () => {
792748
const { hostWin, panelWin } = createWindowPair()
793749
const pageScript = createPageScriptChannel<TestProtocol>({
794-
events: { note: {} },
795750
name: 'devframes:test',
796751
window: asWindow(hostWin),
797752
heartbeat: false,
798753
functions: defaultPageScriptFunctions,
799754
})
800755
const pinnedElsewhere = connectPanelChannel<TestProtocol>({
801-
events: { notify: {} },
802756
name: 'devframes:test',
803757
window: asWindow(panelWin),
804758
targets: [asWindow(hostWin)],
@@ -810,7 +764,6 @@ describe('in-page channel handshake', () => {
810764
await expect(pinnedElsewhere.whenConnected(100)).rejects.toMatchObject({ code: 'timeout' })
811765

812766
const pinnedHere = connectPanelChannel<TestProtocol>({
813-
events: { notify: {} },
814767
name: 'devframes:test',
815768
window: asWindow(panelWin),
816769
targets: [asWindow(hostWin)],
@@ -834,7 +787,6 @@ describe('in-page channel handshake', () => {
834787
it('stays connecting and warns when the panel has nowhere to handshake', async () => {
835788
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
836789
const lonely = connectPanelChannel<TestProtocol>({
837-
events: { notify: {} },
838790
name: `devframes:test-lonely-${Math.random()}`,
839791
window: false,
840792
heartbeat: false,
@@ -853,7 +805,6 @@ describe('in-page channel handshake', () => {
853805

854806
it('rejects buffered calls with a status-aware timeout', async () => {
855807
const lonely = connectPanelChannel<TestProtocol>({
856-
events: { notify: {} },
857808
name: `devframes:test-lonely-${Math.random()}`,
858809
window: false,
859810
heartbeat: false,
@@ -873,7 +824,6 @@ describe('in-page channel handshake', () => {
873824

874825
it('rejects pending work when the channel closes', async () => {
875826
const lonely = connectPanelChannel<TestProtocol>({
876-
events: { notify: {} },
877827
name: `devframes:test-lonely-${Math.random()}`,
878828
window: false,
879829
heartbeat: false,

packages/devframe/src/in-page-channel/internal.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { StandardSchemaV1 } from '@standard-schema/spec'
22
import type { BirpcReturn } from 'birpc'
33
import type { RpcArgsSchema } from '../rpc/types'
44
import type { InPageChannelControlFrame } from './protocol'
5-
import type { InPageFunctionDefinitionAny } from './types'
5+
import type { InPageFunctionDefinitionAny, InPageFunctionType } from './types'
66
import { createBirpc } from 'birpc'
77
import { diagnostics } from './diagnostics'
88
import { isControlFrame } from './protocol'
@@ -158,8 +158,9 @@ export function deserializeResult(codec: InPageChannelSerialization, result: unk
158158
return codec.deserialize && result !== undefined ? codec.deserialize(result) : result
159159
}
160160

161-
export function channelMethod(kind: 'function' | 'event', name: string): string {
161+
export function channelMethod(type: InPageFunctionType | 'function' | undefined, name: string): string {
162162
// Keep user functions, user events, and internal methods in separate wire namespaces.
163+
const kind = type === 'event' ? 'event' : 'function'
163164
return `devframe:in-page:${kind}:${name}`
164165
}
165166

@@ -180,7 +181,7 @@ export function createLocalFunctionRegistry(codec: InPageChannelSerialization):
180181
const listeners = new Map<string, Set<(...args: unknown[]) => void>>()
181182
return {
182183
register(definition) {
183-
definitions.set(channelMethod(definition.type === 'event' ? 'event' : 'function', definition.name), definition)
184+
definitions.set(channelMethod(definition.type, definition.name), definition)
184185
},
185186
on(name, listener) {
186187
const key = channelMethod('event', name)

packages/devframe/src/in-page-channel/types.test-d.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ describe('In-page script channel', () => {
252252

253253
describe('Panel channel', () => {
254254
const channel = connectPanelChannel<TestProtocol>({
255-
events: { notify: {} },
256255
name: 'devframes:test',
257256
functions: {
258257
notify: { handler: () => { } },
@@ -263,7 +262,6 @@ describe('Panel channel', () => {
263262
it('infers handlers from the protocol', () => {
264263
const { port1 } = new MessageChannel()
265264
const inferredChannel = connectPanelChannel<TestProtocol>({
266-
events: { notify: {} },
267265
name: 'devframes:test',
268266
window: false,
269267
transport: port1,
@@ -283,7 +281,6 @@ describe('Panel channel', () => {
283281
connectPanelChannel<TestProtocol>({ name: 'devframes:test' })
284282

285283
connectPanelChannel<TestProtocol>({
286-
events: { notify: {} },
287284
name: 'devframes:test',
288285
// @ts-expect-error `notify` must be declared.
289286
functions: {},
@@ -292,15 +289,13 @@ describe('Panel channel', () => {
292289

293290
it('allows event declarations to omit their handler', () => {
294291
connectPanelChannel<TestProtocol>({
295-
events: { notify: {} },
296292
name: 'devframes:test',
297293
functions: {
298294
notify: { handler: () => {} },
299295
},
300296
})
301297

302298
connectPanelChannel<TestProtocol>({
303-
events: { notify: {} },
304299
name: 'devframes:test',
305300
functions: {
306301
// @ts-expect-error Request/response functions require a handler.
@@ -311,7 +306,6 @@ describe('Panel channel', () => {
311306

312307
it('rejects in-page script functions', () => {
313308
connectPanelChannel<TestProtocol>({
314-
events: { notify: {} },
315309
name: 'devframes:test',
316310
functions: {
317311
notify: { handler: () => { } },
@@ -323,7 +317,6 @@ describe('Panel channel', () => {
323317

324318
it('rejects incompatible handlers', () => {
325319
connectPanelChannel<TestProtocol>({
326-
events: { notify: {} },
327320
name: 'devframes:test',
328321
functions: {
329322
notify: {
@@ -405,7 +398,6 @@ describe('Panel channel', () => {
405398

406399
it('rejects runtime subscriptions to panel queries', () => {
407400
const mixedChannel = connectPanelChannel<MixedPanelProtocol>({
408-
events: { notify: {} },
409401
name: 'devframes:mixed-panel',
410402
functions: {
411403
confirm: { handler: () => true },

0 commit comments

Comments
 (0)