@@ -41,6 +41,7 @@ export type TransportConnectCallback = () => void;
4141export type Transport = {
4242 send ( data : any ) : void ;
4343 onMessage ( callback : TransportMessageCallback ) : void ;
44+ hasMessageListener ( ) : boolean ;
4445 onConnect ( callback : TransportConnectCallback ) : void ; // Socket connection cb
4546 clear ( ) : void ;
4647 address ( ) : string ;
@@ -53,13 +54,13 @@ type Log = (...args: any[]) => void;
5354
5455export class RealtimeListener implements WebxdcRealtimeListener {
5556 private trashed = false ;
56- private listener : ( data : Uint8Array ) => void = ( ) => { } ;
57+ private listener : ( data : Uint8Array ) => void = ( ) => { } ;
5758
5859 constructor (
59- public sendHook : ( data : Uint8Array ) => void = ( ) => { } ,
60- public setListenerHook : ( ) => void = ( ) => { } ,
61- private leaveHook : ( ) => void = ( ) => { } ,
62- ) { }
60+ public sendHook : ( data : Uint8Array ) => void = ( ) => { } ,
61+ public setListenerHook : ( ) => void = ( ) => { } ,
62+ private leaveHook : ( ) => void = ( ) => { } ,
63+ ) { }
6364
6465 is_trashed ( ) : boolean {
6566 return this . trashed ;
@@ -94,11 +95,11 @@ export class RealtimeListener implements WebxdcRealtimeListener {
9495
9596export function createWebXdc (
9697 transport : Transport ,
97- log : Log = ( ) => { } ,
98+ log : Log = ( ) => { } ,
9899) : Webxdc < any > {
99100 let resolveUpdateListenerPromise : ( ( ) => void ) | null = null ;
100101 let realtime : RealtimeListener | null = null ;
101-
102+
102103 const webXdc : Webxdc < any > = {
103104 sendUpdate : ( update ) => {
104105 transport . send ( { type : "sendUpdate" , update } ) ;
@@ -116,10 +117,11 @@ export function createWebXdc(
116117 resolveUpdateListenerPromise = null ;
117118 }
118119 } else if ( isRealtimeMessage ( message ) ) {
119- // TODO: move this out of setUpdateListener because otherwise
120- // You have to set an update listener such that realtime works
121120 // Conversion to any because the actual data is a dict representation of Uint8Array
122121 // This is due to JSON.stringify conversion.
122+ if ( realtime === null ) {
123+ return
124+ }
123125 realtime ! . receive ( new Uint8Array ( Object . values ( message . data as any ) ) ) ;
124126 } else if ( isClearMessage ( message ) ) {
125127 log ( "clear" ) ;
@@ -199,13 +201,11 @@ export function createWebXdc(
199201 ) ;
200202 }
201203 }
202- const msg = `The app would now close and the user would select a chat to send this message:\nText: ${
203- content . text ? `"${ content . text } "` : "No Text"
204- } \nFile: ${
205- content . file
204+ const msg = `The app would now close and the user would select a chat to send this message:\nText: ${ content . text ? `"${ content . text } "` : "No Text"
205+ } \nFile: ${ content . file
206206 ? `${ content . file . name } - ${ base64Content . length } bytes`
207207 : "No File"
208- } `;
208+ } `;
209209 if ( content . file ) {
210210 const confirmed = confirm (
211211 msg + "\n\nDownload the file in the browser instead?" ,
@@ -249,8 +249,22 @@ export function createWebXdc(
249249 } ,
250250
251251 joinRealtimeChannel : ( ) => {
252+ if ( ! transport . hasMessageListener ( ) ) {
253+ // we can only have one message listener with the current implementation,
254+ // so we need to set it here to receive realtime data. When `setUpdateListener`
255+ // is called, the callback is overwritten but the new value also looks for
256+ // realtime data.
257+ transport . onMessage ( ( message ) => {
258+ if ( isRealtimeMessage ( message ) ) {
259+ if ( realtime === null ) {
260+ return
261+ }
262+ realtime ! . receive ( new Uint8Array ( Object . values ( message . data as any ) ) ) ;
263+ }
264+ } )
265+ }
252266 realtime = new RealtimeListener (
253- ( ) => { } ,
267+ ( ) => { } ,
254268 ( ) => {
255269 transport . send ( { type : "setRealtimeListener" } ) ;
256270 } ,
0 commit comments