refactor: reuse vite's websocket
This commit is contained in:
@ -1,14 +1,14 @@
|
||||
import { createBirpc } from 'birpc'
|
||||
import { parse, stringify } from 'flatted'
|
||||
import { createHotContext } from 'vite-hot-client'
|
||||
import type { ClientFunctions, ServerFunctions } from '../../src/types'
|
||||
import { PATH_ENTRY } from '../../src/constants'
|
||||
|
||||
const RECONNECT_INTERVAL = 2000
|
||||
import { WS_EVENT_NAME } from '../../src/constants'
|
||||
|
||||
export const wsConnecting = ref(true)
|
||||
export const wsError = ref<any>()
|
||||
export const wsConnectingDebounced = useDebounce(wsConnecting, 2000)
|
||||
|
||||
let connectPromise = connectWS()
|
||||
const connectPromise = connectVite()
|
||||
let onMessage: Function = () => {}
|
||||
|
||||
export const clientFunctions = {
|
||||
@ -19,9 +19,11 @@ export const extendedRpcMap = new Map<string, any>()
|
||||
|
||||
export const rpc = createBirpc<ServerFunctions>(clientFunctions, {
|
||||
post: async (d) => {
|
||||
(await connectPromise).send(d)
|
||||
(await connectPromise).send(WS_EVENT_NAME, d)
|
||||
},
|
||||
on: (fn) => {
|
||||
onMessage = fn
|
||||
},
|
||||
on: (fn) => { onMessage = fn },
|
||||
serialize: stringify,
|
||||
deserialize: parse,
|
||||
resolver(name, fn) {
|
||||
@ -35,35 +37,22 @@ export const rpc = createBirpc<ServerFunctions>(clientFunctions, {
|
||||
onError(error, name) {
|
||||
console.error(`[nuxt-devtools] RPC error on executing "${name}":`, error)
|
||||
},
|
||||
timeout: 120_000,
|
||||
})
|
||||
|
||||
async function connectWS() {
|
||||
const wsUrl = new URL(`ws://host${PATH_ENTRY}`)
|
||||
wsUrl.protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||
wsUrl.host = 'localhost:3000'
|
||||
async function connectVite() {
|
||||
const hot = await createHotContext()
|
||||
|
||||
const ws = new WebSocket(wsUrl.toString())
|
||||
ws.addEventListener('message', e => onMessage(String(e.data)))
|
||||
ws.addEventListener('error', (e) => {
|
||||
console.error(e)
|
||||
wsError.value = e
|
||||
if (!hot)
|
||||
throw new Error('Unable to connect to devtools')
|
||||
|
||||
hot.on(WS_EVENT_NAME, (data) => {
|
||||
onMessage(data)
|
||||
})
|
||||
ws.addEventListener('close', () => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('[nuxt-devtools] WebSocket closed, reconnecting...')
|
||||
wsConnecting.value = true
|
||||
setTimeout(async () => {
|
||||
connectPromise = connectWS()
|
||||
}, RECONNECT_INTERVAL)
|
||||
})
|
||||
wsConnecting.value = true
|
||||
if (ws.readyState !== WebSocket.OPEN)
|
||||
await new Promise(resolve => ws.addEventListener('open', resolve))
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('[nuxt-devtools] WebSocket connected.')
|
||||
wsConnecting.value = false
|
||||
wsError.value = null
|
||||
// TODO:
|
||||
// hot.on('vite:connect', (data) => {})
|
||||
// hot.on('vite:disconnect', (data) => {})
|
||||
|
||||
return ws
|
||||
return hot
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user