1- import type {
1+ import {
2+ RealtimeListener as WebxdcRealtimeListener ,
23 ReceivedStatusUpdate ,
34 SendingStatusUpdate ,
45 Webxdc ,
@@ -10,6 +11,7 @@ type UpdateListenerMulti = (updates: ReceivedStatusUpdate<any>[]) => boolean;
1011
1112type ClearListener = ( ) => boolean ;
1213type DeleteListener = ( ) => boolean ;
14+ type RTListener = ( data : Uint8Array ) => boolean ;
1315
1416type Connect = (
1517 updateListener : UpdateListenerMulti ,
@@ -20,7 +22,9 @@ type Connect = (
2022
2123export type WebXdcMulti = {
2224 connect : Connect ;
25+ connectRealtime : ( listener : RTListener ) => void ;
2326 sendUpdate : Webxdc < any > [ "sendUpdate" ] ;
27+ sendRealtimeData : ( data : Uint8Array ) => void ;
2428} ;
2529
2630export type OnMessage = ( message : Message ) => void ;
@@ -33,6 +37,7 @@ export interface IProcessor {
3337
3438class Client implements WebXdcMulti {
3539 updateListener : UpdateListenerMulti | null = null ;
40+ realtimeListener : RTListener | null = null ;
3641 clearListener : ClearListener | null = null ;
3742 updateSerial : number | null = null ;
3843 deleteListener : DeleteListener | null = null ;
@@ -46,6 +51,35 @@ class Client implements WebXdcMulti {
4651 this . processor . distribute ( this . id , update ) ;
4752 }
4853
54+ sendRealtimeData ( data : Uint8Array ) {
55+ this . processor . distributeRealtime ( this . id , data ) ;
56+ }
57+
58+ connectRealtime ( listener : RTListener ) {
59+ this . processor . onMessage ( {
60+ type : "connect-realtime" ,
61+ instanceId : this . id ,
62+ instanceColor : getColorForId ( this . id ) ,
63+ timestamp : Date . now ( ) ,
64+ } ) ;
65+
66+ const realtimeListener = ( data : Uint8Array ) => {
67+ const hasReceived = listener ( data ) ;
68+ if ( hasReceived ) {
69+ this . processor . onMessage ( {
70+ type : "realtime-received" ,
71+ data,
72+ instanceId : this . id ,
73+ instanceColor : getColorForId ( this . id ) ,
74+ timestamp : Date . now ( ) ,
75+ } ) ;
76+ }
77+ return hasReceived ;
78+ } ;
79+
80+ this . realtimeListener = realtimeListener ;
81+ }
82+
4983 connect (
5084 listener : UpdateListenerMulti ,
5185 serial : number ,
@@ -108,6 +142,13 @@ class Client implements WebXdcMulti {
108142 this . updateListener ( [ update ] ) ;
109143 }
110144
145+ receiveRealtime ( data : Uint8Array ) {
146+ if ( this . realtimeListener == null ) {
147+ return ;
148+ }
149+ this . realtimeListener ( data ) ;
150+ }
151+
111152 clear ( ) {
112153 if (
113154 this . clearListener == null ||
@@ -148,6 +189,21 @@ class Processor implements IProcessor {
148189 this . clients . splice ( client_index , 1 ) ;
149190 }
150191
192+ distributeRealtime ( instanceId : string , data : Uint8Array ) {
193+ this . onMessage ( {
194+ type : "realtime-sent" ,
195+ instanceId : instanceId ,
196+ instanceColor : getColorForId ( instanceId ) ,
197+ data,
198+ timestamp : Date . now ( ) ,
199+ } ) ;
200+ for ( const client of this . clients ) {
201+ if ( client . id != instanceId ) {
202+ client . receiveRealtime ( data ) ;
203+ }
204+ }
205+ }
206+
151207 distribute ( instanceId : string , update : SendingStatusUpdate < any > ) {
152208 this . currentSerial ++ ;
153209 const receivedUpdate : ReceivedStatusUpdate < any > = {
0 commit comments