@@ -47,6 +47,60 @@ type ProtocolHandler<F> = F extends (...args: any[]) => any
4747 */
4848export type InPageFunctionType = 'action' | 'event' | 'query'
4949
50+ interface InPageFunctionDefinitionBase < NAME extends string > {
51+ name : NAME
52+ jsonSerializable ?: boolean
53+ }
54+
55+ interface InPageEventFunctionDefinition < NAME extends string , HANDLER > extends InPageFunctionDefinitionBase < NAME > {
56+ type : 'event'
57+ handler ?: HANDLER
58+ }
59+
60+ interface InPageQueryFunctionDefinition < NAME extends string , HANDLER > extends InPageFunctionDefinitionBase < NAME > {
61+ type ?: 'query'
62+ handler : HANDLER
63+ }
64+
65+ interface InPageActionFunctionDefinition < NAME extends string , HANDLER > extends InPageFunctionDefinitionBase < NAME > {
66+ type : 'action'
67+ handler : HANDLER
68+ }
69+
70+ type InPageFunctionDefinitionForType <
71+ NAME extends string ,
72+ TYPE extends InPageFunctionType ,
73+ HANDLER ,
74+ > = TYPE extends 'event'
75+ ? InPageEventFunctionDefinition < NAME , HANDLER >
76+ : TYPE extends 'action'
77+ ? InPageActionFunctionDefinition < NAME , HANDLER >
78+ : InPageQueryFunctionDefinition < NAME , HANDLER >
79+
80+ type InPageFunctionDefinitionSchemas <
81+ AS extends RpcArgsSchema | undefined ,
82+ RS extends RpcReturnSchema | undefined ,
83+ > = [ AS , RS ] extends [ undefined , undefined ]
84+ ? {
85+ args ?: AS
86+ returns ?: RS
87+ }
88+ : {
89+ /** Standard Schema array validating (and typing) the arguments. */
90+ args : AS
91+ /** Standard Schema typing the resolved return value. */
92+ returns : RS
93+ }
94+
95+ type InPageFunctionDefinitionHandler <
96+ ARGS extends any [ ] ,
97+ RETURN ,
98+ AS extends RpcArgsSchema | undefined ,
99+ RS extends RpcReturnSchema | undefined ,
100+ > = [ AS , RS ] extends [ undefined , undefined ]
101+ ? ( ...args : ARGS ) => RETURN
102+ : ( ...args : InferArgsType < AS > ) => Thenable < InferReturnType < RS > >
103+
50104/**
51105 * An in-page channel function definition: the `defineRpcFunction` authoring
52106 * shape (`name`, `type`, Standard-Schema `args`/`returns`,
@@ -65,27 +119,11 @@ export type InPageFunctionDefinition<
65119 AS extends RpcArgsSchema | undefined = undefined ,
66120 RS extends RpcReturnSchema | undefined = undefined ,
67121>
68- = [ AS , RS ] extends [ undefined , undefined ]
69- ? ( {
70- name : NAME
71- type ?: TYPE
72- args ?: AS
73- returns ?: RS
74- jsonSerializable ?: boolean
75- } & ( TYPE extends 'event'
76- ? { handler ?: ( ...args : ARGS ) => RETURN }
77- : { handler : ( ...args : ARGS ) => RETURN } ) )
78- : ( {
79- name : NAME
80- type ?: TYPE
81- /** Standard Schema array validating (and typing) the arguments. */
82- args : AS
83- /** Standard Schema typing the resolved return value. */
84- returns : RS
85- jsonSerializable ?: boolean
86- } & ( TYPE extends 'event'
87- ? { handler ?: ( ...args : InferArgsType < AS > ) => Thenable < InferReturnType < RS > > }
88- : { handler : ( ...args : InferArgsType < AS > ) => Thenable < InferReturnType < RS > > } ) )
122+ = InPageFunctionDefinitionForType <
123+ NAME ,
124+ TYPE ,
125+ InPageFunctionDefinitionHandler < ARGS , RETURN , AS , RS >
126+ > & InPageFunctionDefinitionSchemas < AS , RS >
89127
90128/**
91129 * Loosely-typed definition used by the internal function registry.
@@ -107,15 +145,25 @@ interface InPageFunctionOptionBase {
107145 jsonSerializable ?: boolean
108146}
109147
148+ interface InPageEventFunctionOption < F > extends InPageFunctionOptionBase {
149+ type : 'event'
150+ handler ?: ProtocolHandler < F >
151+ }
152+
153+ interface InPageQueryFunctionOption < F > extends InPageFunctionOptionBase {
154+ type ?: 'query'
155+ handler : ProtocolHandler < F >
156+ }
157+
158+ interface InPageActionFunctionOption < F > extends InPageFunctionOptionBase {
159+ type : 'action'
160+ handler : ProtocolHandler < F >
161+ }
162+
110163type InPageFunctionOption < F >
111- = | ( InPageFunctionOptionBase & {
112- type : 'event'
113- handler ?: ProtocolHandler < F >
114- } )
115- | ( InPageFunctionOptionBase & {
116- type ?: Exclude < InPageFunctionType , 'event' >
117- handler : ProtocolHandler < F >
118- } )
164+ = | InPageEventFunctionOption < F >
165+ | InPageQueryFunctionOption < F >
166+ | InPageActionFunctionOption < F >
119167
120168/**
121169 * Functions implemented by {@link createPageScriptChannel}.
0 commit comments