-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlive_client.mjs
135 lines (107 loc) · 3.16 KB
/
live_client.mjs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* eslint-env browser */
/*
Client for "live reloading" in browsers.
Served and fed by the broadcaster in `live_deno.mjs`.
Limitations:
* Currently doesn't support `<base>`.
*/
export function main() {
class Client {
constructor() {
this.url = new URL(import.meta.url)
this.delay = 1024
this.req = undefined
this.timer = undefined
}
reinit() {
this.deinit()
const req = this.req = new EventSource(new URL(`events`, this.url))
req.onmessage = this.onNetMsg.bind(this)
req.onerror = this.onNetErr.bind(this)
}
deinit() {
if (this.timer) {
clearTimeout(this.timer)
this.timer = undefined
}
if (this.req) {
this.req.close()
this.req = undefined
}
}
onNetErr() {
this.deinit()
this.timer = setTimeout(this.reinit.bind(this), this.delay)
}
onNetMsg(event) {
this.onMsg(JSON.parse(event.data || `null`))
}
get key() {return this.url.searchParams.get(`key`)}
isMsgAllowed(msg) {return !!(msg && eq(msg.key, this.key))}
onMsg(msg) {
if (!this.isMsgAllowed(msg)) return
const {type} = msg
if (type === `deinit`) {this.deinit(); return}
if (this.isChangeType(type)) {this.onChange(msg); return}
}
isChangeType(val) {
return (
val === `change` || // Used by Node.
val === `rename` || // Used by Node.
val === `modify` // Used by Deno.
)
}
onChange(msg) {
const ext = extName(msg.path)
if (ext === `.map`) return
if (ext === `.css`) {this.onCssChange(msg); return}
window.location.reload()
}
/*
This may be triggered multiple times concurrently. We must handle the case
where multiple links reload the same stylesheet at once, racing each other.
*/
onCssChange({path}) {
const next = document.createElement(`link`)
next.rel = `stylesheet`
next.href = path
next.href = salted(next.href)
const prev = similarStylesheets(next)
if (!prev.length || prev.some(isPending)) return
next.onload = linkOnLoad
next.onerror = next.remove
last(prev).insertAdjacentElement(`afterend`, next)
}
}
new Client().reinit()
function similarStylesheets(next) {
const path = new URL(next.href).pathname
return filter(
document.head.querySelectorAll(`link`),
prev => new URL(prev.href).pathname === path,
)
}
function linkOnLoad() {
this.onload = null
this.onerror = null
for (const node of similarStylesheets(this)) {
if (node !== this) node.remove()
}
}
function isPending(val) {return !!val.onload}
function salted(val) {
val = new URL(val)
val.search = val.search || salt()
return val
}
function salt() {
return String(Math.random()).replace(/\d*\./, ``).slice(0, 6)
}
function extName(path = ``) {
const match = path.match(/.([.][^.]+)$/)
return !match ? `` : match[1]
}
function eq(a, b) {return Object.is(a ?? undefined, b ?? undefined)}
function last(list) {return list[list.length - 1]}
function filter(list, fun) {return Array.prototype.filter.call(list ?? [], fun)}
}