-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.ts
76 lines (66 loc) · 2.16 KB
/
dev.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// bun add elysia @elysiajs/static @types/bun
import { watch } from 'node:fs/promises'
import { mkdir } from 'node:fs/promises'
import { staticPlugin } from '@elysiajs/static'
import { Elysia } from 'elysia'
const port = process.env.PORT || 3000
const app = new Elysia({
websocket: {
idleTimeout: 960,
},
})
// .get('/', ({ redirect }) => {
// return redirect('/index.xml')
// })
.use(
staticPlugin({
assets: 'output',
prefix: '',
}),
)
.ws('/live', {
async open(ws) {
ws.subscribe('update')
},
async close(ws) {
ws.unsubscribe('update')
},
})
app.listen(port, async ({ hostname, port }) => {
console.log(`Serving: http://${hostname}:${port}/`)
// console.log(app.server?.publish)
await mkdir('build/live/', { recursive: true })
const watcher = watch('build/live/')
let lastSent = Date.now()
let lastSentFile: string
for await (const event of watcher) {
// // debounce
// if (Date.now() - lastSent < 2000) {
// continue
// }
// console.log('event:', event)
if (event.eventType === 'change' && event.filename === 'trigger.txt') {
const updated_file = Bun.file('build/live/updated_file.txt')
if (await updated_file.exists()) {
const updated_file_name = await updated_file.text()
// same file debounce
// if (updated_file_name == lastSentFile && Date.now() - lastSent < 3000) {
// continue
// }
// postpone to debounce
setTimeout(() => {
console.log('🔥live reload:', updated_file_name.trim())
app.server?.publish(
'update',
JSON.stringify({
type: 'update',
data: updated_file_name.trim(),
}),
)
lastSent = Date.now()
lastSentFile = updated_file_name
}, 10)
}
}
}
})