-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.js
49 lines (43 loc) · 1.1 KB
/
index.js
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
function block(e) {
e.returnValue = 'unsynced'
return 'unsynced'
}
export function confirm(client) {
let disconnected = client.state === 'disconnected'
let wait = false
let update = () => {
if (client.state === 'disconnected') {
disconnected = true
} else if (client.state === 'synchronized') {
disconnected = false
wait = false
}
if (typeof window !== 'undefined' && window.addEventListener) {
if (client.role !== 'follower' && wait && disconnected) {
window.addEventListener('beforeunload', block)
} else {
window.removeEventListener('beforeunload', block)
}
}
}
let unbind = []
unbind.push(client.on('role', update))
unbind.push(client.on('state', update))
update()
unbind.push(
client.on('add', (action, meta) => {
if (action.type === 'logux/subscribe') {
return
} else if (action.type === 'logux/unsubscribe') {
return
}
if (disconnected && meta.sync && meta.added) {
wait = true
update()
}
})
)
return () => {
for (let i of unbind) i()
}
}