@@ -75,18 +75,31 @@ export default class MessageBus {
7575 * @template [T=unknown]
7676 * @param {string } channel
7777 * @param {string } topic
78- * @param {T } payload
78+ * @param {T } [ payload]
7979 * @returns {Event<T> } Returns the {@link Event} object passed to subscribers.
8080 */
8181 publish ( channel , topic , payload ) {
8282 const event = new Event ( channel , topic , payload ) ;
8383 this . ee . emit ( event . toKey ( ) , event ) ;
8484 this . sink . push ( event ) ;
8585 if ( this . bridge ) {
86+ /** @type {T | T[] } */
87+ let params = payload ;
88+
89+ if ( typeof payload !== 'undefined' ) {
90+ // JSON RPC 2.0 requires that params is either an object or an array. Wrap primitives in an an array.
91+ const isPrimitive =
92+ typeof params === 'string' ||
93+ typeof params === 'boolean' ||
94+ typeof params === 'number' ;
95+ if ( isPrimitive ) {
96+ params = [ payload ] ;
97+ }
98+ }
8699
87100 this . bridge . notification ( {
88101 method : `${ channel } /${ topic } ` ,
89- params : { "type" : typeof payload , payload } ,
102+ params,
90103 } ) ;
91104 }
92105 return event ;
@@ -124,10 +137,10 @@ export default class MessageBus {
124137 const bridgeListener = ( message ) => {
125138 const request =
126139 /** @type {import("@podium/bridge").RpcRequest<T> } */ (
127- message
128- ) ;
140+ message
141+ ) ;
129142
130- const event = new Event ( channel , topic , request . params . payload ) ;
143+ const event = new Event ( channel , topic , request . params ) ;
131144 this . sink . push ( event ) ;
132145 listener ( event ) ;
133146 } ;
0 commit comments