forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
131 lines (119 loc) · 3.17 KB
/
client.ts
File metadata and controls
131 lines (119 loc) · 3.17 KB
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
import {
type ClientOptions,
CrossTabClient,
encryptActions,
status,
type StatusValue
} from '@logux/client'
import { type ServerConnection, TestPair, TestTime } from '@logux/core'
import { deleteUser, SUBPROTOCOL } from '@slowreader/api'
import { atom, effect, onMount } from 'nanostores'
import { getEnvironment, onEnvironment } from './environment.ts'
import { encryptionKey, hasPassword, syncServer, userId } from './settings.ts'
let testTime: TestTime | undefined
/**
* Logux uses complex time https://logux.org/guide/concepts/meta/#id-and-time
*
* Test time on every test run will return the same result
* (it is more like counter, than time).
*/
export function enableTestTime(): TestTime {
testTime = new TestTime()
return testTime
}
function getServer(): ClientOptions['server'] {
let server = getEnvironment().server
if (typeof server !== 'string') {
let pair = new TestPair()
// @ts-expect-error Dirty mocks for tests
pair.right.ws = {
_socket: {
remoteAddress: '127.0.0.1'
},
upgradeReq: {
headers: {}
}
}
server.addClient(pair.right as unknown as ServerConnection)
return pair.left
} else if (testTime) {
return new TestPair().right
} else {
let domain = syncServer.get() ?? server
let protocol = domain.startsWith('localhost') ? 'ws' : 'wss'
return `${protocol}://${domain}`
}
}
let prevClient: CrossTabClient | undefined
export const client = atom<CrossTabClient | undefined>()
onEnvironment(({ logStoreCreator }) => {
let unbindUser = effect(
[userId, hasPassword, encryptionKey],
(user, connect, key) => {
prevClient?.destroy()
if (user && key) {
let logux = new CrossTabClient({
prefix: 'slowreader',
server: getServer(),
store: logStoreCreator(),
subprotocol: SUBPROTOCOL,
time: testTime,
token: getEnvironment().getSession(),
userId: user
})
encryptActions(logux, key, {
ignore: [deleteUser.type]
})
logux.start(connect)
prevClient = logux
client.set(logux)
} else {
client.set(undefined)
}
}
)
return () => {
unbindUser()
prevClient?.destroy()
}
})
export function getClient(): CrossTabClient {
let logux = client.get()
/* node:coverage ignore next 3 */
if (!logux) {
throw new Error('No Slow Reader client')
}
return logux
}
export type SyncStatus =
| 'local'
| Exclude<StatusValue, 'denied' | 'protocolError' | 'syncError'>
export const syncStatus = atom<SyncStatus>('local')
onMount(syncStatus, () => {
let unbindState: (() => void) | undefined
let unbindClient = client.subscribe(logux => {
if (unbindState) {
unbindState()
unbindState = undefined
}
if (!logux) {
syncStatus.set('local')
} else {
unbindState = status(logux, value => {
if (
value === 'denied' ||
value === 'protocolError' ||
value === 'syncError'
) {
syncStatus.set('error')
} else {
syncStatus.set(value)
}
})
}
})
return () => {
unbindClient()
unbindState?.()
}
})