rsnext/packages/next/client/dev/error-overlay/websocket.ts
Tim Neutkens 4cd8b23032
Enable @typescript-eslint/no-use-before-define for functions (#39602)
Follow-up to the earlier enabling of classes/variables etc.

Bug

 Related issues linked using fixes #number
 Integration tests added
 Errors have helpful link attached, see contributing.md

Feature

 Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
 Related issues linked using fixes #number
 Integration tests added
 Documentation added
 Telemetry added. In case of a feature if it's used or not.
 Errors have helpful link attached, see contributing.md

Documentation / Examples

 Make sure the linting passes by running pnpm lint
 The examples guidelines are followed from our contributing doc

Co-authored-by: Steven <steven@ceriously.com>
2022-08-15 10:29:51 -04:00

82 lines
1.9 KiB
TypeScript

let source: WebSocket
const eventCallbacks: ((event: any) => void)[] = []
let lastActivity = Date.now()
function getSocketProtocol(assetPrefix: string): string {
let protocol = location.protocol
try {
// assetPrefix is a url
protocol = new URL(assetPrefix).protocol
} catch (_) {}
return protocol === 'http:' ? 'ws' : 'wss'
}
export function addMessageListener(cb: (event: any) => void) {
eventCallbacks.push(cb)
}
export function sendMessage(data: any) {
if (!source || source.readyState !== source.OPEN) return
return source.send(data)
}
export function connectHMR(options: {
path: string
assetPrefix: string
timeout: number
log?: boolean
}) {
if (!options.timeout) {
options.timeout = 5 * 1000
}
function init() {
if (source) source.close()
function handleOnline() {
if (options.log) console.log('[HMR] connected')
lastActivity = Date.now()
}
function handleMessage(event: any) {
lastActivity = Date.now()
eventCallbacks.forEach((cb) => {
cb(event)
})
}
let timer: NodeJS.Timeout
function handleDisconnect() {
clearInterval(timer)
source.close()
setTimeout(init, options.timeout)
}
timer = setInterval(function () {
if (Date.now() - lastActivity > options.timeout) {
handleDisconnect()
}
}, options.timeout / 2)
const { hostname, port } = location
const protocol = getSocketProtocol(options.assetPrefix || '')
const assetPrefix = options.assetPrefix.replace(/^\/+/, '')
let url = `${protocol}://${hostname}:${port}${
assetPrefix ? `/${assetPrefix}` : ''
}`
if (assetPrefix.startsWith('http')) {
url = `${protocol}://${assetPrefix.split('://')[1]}`
}
source = new window.WebSocket(`${url}${options.path}`)
source.onopen = handleOnline
source.onerror = handleDisconnect
source.onmessage = handleMessage
}
init()
}