@@ -300,7 +300,7 @@ export const applyEvent = async (event, socket) => {
300300 if ( socket ) {
301301 socket . emit (
302302 "event" ,
303- JSON . stringify ( event , ( k , v ) => ( v === undefined ? null : v ) )
303+ event ,
304304 ) ;
305305 return true ;
306306 }
@@ -407,6 +407,8 @@ export const connect = async (
407407 transports : transports ,
408408 autoUnref : false ,
409409 } ) ;
410+ // Ensure undefined fields in events are sent as null instead of removed
411+ socket . current . io . encoder . replacer = ( k , v ) => ( v === undefined ? null : v )
410412
411413 function checkVisibility ( ) {
412414 if ( document . visibilityState === "visible" ) {
@@ -443,8 +445,7 @@ export const connect = async (
443445 } ) ;
444446
445447 // On each received message, queue the updates and events.
446- socket . current . on ( "event" , async ( message ) => {
447- const update = JSON5 . parse ( message ) ;
448+ socket . current . on ( "event" , async ( update ) => {
448449 for ( const substate in update . delta ) {
449450 dispatch [ substate ] ( update . delta [ substate ] ) ;
450451 }
@@ -456,7 +457,7 @@ export const connect = async (
456457 } ) ;
457458 socket . current . on ( "reload" , async ( event ) => {
458459 event_processing = false ;
459- queueEvents ( [ ...initialEvents ( ) , JSON5 . parse ( event ) ] , socket ) ;
460+ queueEvents ( [ ...initialEvents ( ) , event ] , socket ) ;
460461 } ) ;
461462
462463 document . addEventListener ( "visibilitychange" , checkVisibility ) ;
@@ -497,23 +498,31 @@ export const uploadFiles = async (
497498 // Whenever called, responseText will contain the entire response so far.
498499 const chunks = progressEvent . event . target . responseText . trim ( ) . split ( "\n" ) ;
499500 // So only process _new_ chunks beyond resp_idx.
500- chunks . slice ( resp_idx ) . map ( ( chunk ) => {
501- event_callbacks . map ( ( f , ix ) => {
502- f ( chunk )
503- . then ( ( ) => {
504- if ( ix === event_callbacks . length - 1 ) {
505- // Mark this chunk as processed.
506- resp_idx += 1 ;
507- }
508- } )
509- . catch ( ( e ) => {
510- if ( progressEvent . progress === 1 ) {
511- // Chunk may be incomplete, so only report errors when full response is available.
512- console . log ( "Error parsing chunk" , chunk , e ) ;
513- }
514- return ;
515- } ) ;
516- } ) ;
501+ chunks . slice ( resp_idx ) . map ( ( chunk_json ) => {
502+ try {
503+ const chunk = JSON5 . parse ( chunk_json ) ;
504+ event_callbacks . map ( ( f , ix ) => {
505+ f ( chunk )
506+ . then ( ( ) => {
507+ if ( ix === event_callbacks . length - 1 ) {
508+ // Mark this chunk as processed.
509+ resp_idx += 1 ;
510+ }
511+ } )
512+ . catch ( ( e ) => {
513+ if ( progressEvent . progress === 1 ) {
514+ // Chunk may be incomplete, so only report errors when full response is available.
515+ console . log ( "Error processing chunk" , chunk , e ) ;
516+ }
517+ return ;
518+ } ) ;
519+ } ) ;
520+ } catch ( e ) {
521+ if ( progressEvent . progress === 1 ) {
522+ console . log ( "Error parsing chunk" , chunk_json , e ) ;
523+ }
524+ return ;
525+ }
517526 } ) ;
518527 } ;
519528
@@ -799,7 +808,7 @@ export const useEventLoop = (
799808 connect (
800809 socket ,
801810 dispatch ,
802- [ "websocket" , "polling" ] ,
811+ [ "websocket" ] ,
803812 setConnectErrors ,
804813 client_storage
805814 ) ;
0 commit comments