Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 3f7be7f

Browse files
committed
Improve server stable
1 parent d3c563d commit 3f7be7f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

server/server.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ export class Server {
3737
if (pathname === '/_hmr') {
3838
const { socket, response } = Deno.upgradeWebSocket(req)
3939
const watcher = aleph.createFSWatcher()
40+
const send = (message: object) => {
41+
try {
42+
socket.send(JSON.stringify(message))
43+
} catch (err) {
44+
log.warn('socket.send:', err.message)
45+
}
46+
}
4047
socket.addEventListener('open', () => {
4148
watcher.on('add', (mod: any) => socket.send(JSON.stringify({ ...mod, type: 'add' })))
4249
watcher.on('remove', (specifier: string) => {
4350
watcher.removeAllListeners('modify-' + specifier)
44-
socket.send(JSON.stringify({ type: 'remove', specifier }))
51+
send({ type: 'remove', specifier })
4552
})
4653
log.debug('hmr connected')
4754
})
@@ -57,19 +64,18 @@ export class Server {
5764
const mod = aleph.getModule(data.specifier)
5865
if (mod) {
5966
watcher.on(`modify-${mod.specifier}`, (data) => {
60-
socket.send(JSON.stringify({
67+
send({
6168
...data,
6269
type: 'update',
6370
specifier: mod.specifier,
6471
updateUrl: util.cleanPath(`${basePath}/_aleph/${trimBuiltinModuleExts(mod.specifier)}.js`),
65-
}))
72+
})
6673
})
6774
}
6875
}
6976
} catch (e) { }
7077
}
7178
})
72-
7379
try {
7480
await respondWith(response)
7581
} catch (err) {

0 commit comments

Comments
 (0)