11import { promiseHandles , newCrossOriginWorker , isCrossOrigin } from '../utils' ;
2- import { EventMessage , Message , Response , SyncRequest , WebSocketCloseMessage , WebSocketMessage , WebSocketOpenMessage } from './message' ;
2+ import { EventMessage , Message , PostMessageWorkerMessage , Response , SyncRequest , WebSocketCloseMessage , WebSocketMessage , WebSocketOpenMessage , WorkerErrorMessage , WorkerMessage , WorkerMessageErrorMessage } from './message' ;
33import { Endpoint } from './task-common' ;
44import { syncResponse } from './task-main' ;
55import { ChannelMain , ChannelWorker } from './channel' ;
@@ -145,6 +145,25 @@ export class SharedBufferChannelMain extends ChannelMain {
145145 await syncResponse ( worker , reqData , { type : 'eval-response' , data } ) ;
146146 break ;
147147 }
148+ case 'post-message-worker' : {
149+ const message = payload . data as PostMessageWorkerMessage [ 'data' ] ;
150+ message . handles = promiseHandles ( ) ;
151+ this . systemQueue . put ( { type : 'postMessageWorker' , data : message } ) ;
152+
153+ if ( message . async ) {
154+ await syncResponse ( worker , reqData , { type : 'post-message-response' } ) ;
155+ } else {
156+ message . handles . promise . then (
157+ ( value ) => {
158+ void syncResponse ( worker , reqData , { type : 'post-message-response' , data : { result : value } } ) ;
159+ } ,
160+ ( error ) => {
161+ void syncResponse ( worker , reqData , { type : 'post-message-response' , data : { error : String ( error ) } } ) ;
162+ }
163+ ) ;
164+ }
165+ break ;
166+ }
148167 default :
149168 throw new WebRChannelError ( `Unsupported request type '${ payload . type } '.` ) ;
150169 }
@@ -162,11 +181,14 @@ export class SharedBufferChannelMain extends ChannelMain {
162181
163182import { setEventBuffer , setEventsHandler , SyncTask } from './task-worker' ;
164183import { Module } from '../emscripten' ;
165- import { WebSocketProxy , WebSocketProxyFactory } from './websocket' ;
184+ import { WebSocketProxy , WebSocketProxyFactory } from './proxy-websocket' ;
185+ import { WorkerProxy , WorkerProxyFactory } from './proxy-worker' ;
166186
167187export class SharedBufferChannelWorker implements ChannelWorker {
168188 WebSocketProxy : typeof WebSocket ;
169- proxies : Map < string , WebSocketProxy > ;
189+ WorkerProxy : typeof Worker ;
190+ ws : Map < string , WebSocketProxy > ;
191+ workers : Map < string , WorkerProxy > ;
170192 #ep: Endpoint ;
171193 #dispatch: ( msg : Message ) => void = ( ) => 0 ;
172194 #eventBuffer = new Int32Array ( new SharedArrayBuffer ( 4 ) ) ;
@@ -178,23 +200,26 @@ export class SharedBufferChannelWorker implements ChannelWorker {
178200 setEventBuffer ( this . #eventBuffer. buffer ) ;
179201 setEventsHandler ( ( ) => this . handleEvents ( ) ) ;
180202
203+ // Event functionality to be handled via proxy to main thread
181204 this . WebSocketProxy = WebSocketProxyFactory . proxy ( this ) ;
182- this . proxies = new Map ( ) ;
205+ this . ws = new Map ( ) ;
206+ this . WorkerProxy = WorkerProxyFactory . proxy ( this ) ;
207+ this . workers = new Map ( ) ;
183208 }
184209
185210 resolve ( ) {
186211 this . write ( { type : 'resolve' , data : this . #eventBuffer. buffer } ) ;
187212 }
188213
189- write ( msg : Message , transfer ?: [ Transferable ] ) {
214+ write ( msg : Message , transfer ?: Transferable [ ] ) {
190215 this . #ep. postMessage ( msg , transfer ) ;
191216 }
192217
193- writeSystem ( msg : Message , transfer ?: [ Transferable ] ) {
218+ writeSystem ( msg : Message , transfer ?: Transferable [ ] ) {
194219 this . #ep. postMessage ( { type : 'system' , data : msg } , transfer ) ;
195220 }
196221
197- syncRequest ( msg : Message , transfer ?: [ Transferable ] ) : Message {
222+ syncRequest ( msg : Message , transfer ?: Transferable [ ] ) : Message {
198223 const task = new SyncTask ( this . #ep, msg , transfer ) ;
199224 return task . syncify ( ) as Message ;
200225 }
@@ -240,22 +265,37 @@ export class SharedBufferChannelWorker implements ChannelWorker {
240265 break ;
241266 case 'websocket-open' : {
242267 const message = response . data . msg as WebSocketOpenMessage ;
243- this . proxies . get ( message . data . uuid ) ?. _accept ( ) ;
268+ this . ws . get ( message . data . uuid ) ?. _accept ( ) ;
244269 break ;
245270 }
246271 case 'websocket-message' : {
247272 const message = response . data . msg as WebSocketMessage ;
248- this . proxies . get ( message . data . uuid ) ?. _recieve ( message . data . data ) ;
273+ this . ws . get ( message . data . uuid ) ?. _recieve ( message . data . data ) ;
249274 break ;
250275 }
251276 case 'websocket-close' : {
252277 const message = response . data . msg as WebSocketCloseMessage ;
253- this . proxies . get ( message . data . uuid ) ?. _close ( message . data . code , message . data . reason ) ;
278+ this . ws . get ( message . data . uuid ) ?. _close ( message . data . code , message . data . reason ) ;
254279 break ;
255280 }
256281 case 'websocket-error' : {
257282 const message = response . data . msg as WebSocketMessage ;
258- this . proxies . get ( message . data . uuid ) ?. _error ( ) ;
283+ this . ws . get ( message . data . uuid ) ?. _error ( ) ;
284+ break ;
285+ }
286+ case 'worker-message' : {
287+ const message = response . data . msg as WorkerMessage ;
288+ this . workers . get ( message . data . uuid ) ?. _message ( message . data . data ) ;
289+ break ;
290+ }
291+ case 'worker-messageerror' : {
292+ const message = response . data . msg as WorkerMessageErrorMessage ;
293+ this . workers . get ( message . data . uuid ) ?. _messageerror ( message . data . data ) ;
294+ break ;
295+ }
296+ case 'worker-error' : {
297+ const message = response . data . msg as WorkerErrorMessage ;
298+ this . workers . get ( message . data . uuid ) ?. _error ( ) ;
259299 break ;
260300 }
261301 default :
0 commit comments