@@ -27,9 +27,9 @@ socket.addEventListener("message", ({ data }) => {
2727
2828// Function for sending messages over the websocket connection.
2929export const sendMessage = ( message : OutboundMessage ) => {
30- if ( socket . readyState == 0 ) {
30+ if ( socket . readyState == socket . CONNECTING ) {
3131 outboundQueue . push ( message ) ;
32- } else if ( socket . readyState == 1 ) {
32+ } else if ( socket . readyState == socket . OPEN ) {
3333 socket . send ( JSON . stringify ( message ) ) ;
3434 } else {
3535 throw new Error ( `Websocket readystate is ${ socket . readyState } ` ) ;
@@ -39,12 +39,14 @@ export const sendMessage = (message: OutboundMessage) => {
3939// Create a readable store that receives a specific message type.
4040// Svelte components can use the $ shorthand to auto-subscribe to the latest value.
4141export const createMessageReceiver = < T extends InboundMessage > (
42- action : string
43- ) : Readable < T | null > => ( {
44- subscribe : ( run , invalidate ) =>
45- messageStore . subscribe ( ( value ) => {
46- if ( ! value || value . action === action ) {
47- run ( value as T | null ) ;
42+ initialValue : T
43+ ) : Readable < T > => ( {
44+ subscribe : ( run , invalidate ) => {
45+ run ( initialValue ) ;
46+ return messageStore . subscribe ( ( value ) => {
47+ if ( value && value . action === initialValue . action ) {
48+ run ( value as T ) ;
4849 }
49- } , invalidate ) ,
50+ } , invalidate ) ;
51+ } ,
5052} ) ;
0 commit comments