/ *
Helpers for dealing with the ` socket/ ` handlers , similar to the
code in ` client/api.js ` . WebSockets already work well with JSON
data , so this mostly handles create a single connection , and
reconnecting when you need to .
* /
import { inject _remote } from "$/client/components/Source.svelte" ;
let raw _socket ;
/ *
Load the ` /js/socket.io.min.js ` file using ` client/components/Sourse.svelte:inject_remote ` .
This makes it so if you don 't use the socket.io stuff then you won' t download the code .
* /
export const configure _socket = async ( ) => {
await inject _remote ( document , "/js/socket.io.min.js" ) ;
}
/ *
Connect to the server _ _once _ _ . This should maintain the connection
in the SPA for as long as possible and return only one .
+ ` reconnection boolean ` -- The reconnection policy that ' s passed to socket . io ` io() ` .
* /
export const connect _socket = ( reconnection = false ) => {
if ( raw _socket === undefined ) {
raw _socket = io ( { reconnection } ) ;
}
return raw _socket ;
}
/ *
This is actually the way you should connect a socket in most pages .
This will detect if a connection already exists , and if it does it
simply disconnects , then reconnects .
If there isn ' t a connection yet then it will call ` connect_socket() `
to establish the first one .
* /
export const reconnect _socket = ( ) => {
if ( raw _socket !== undefined ) {
raw _socket . disconnect ( ) ;
raw _socket . connect ( ) ;
} else {
connect _socket ( ) ;
}
return raw _socket ;
}