Skip to content

Commit 2ec7f68

Browse files
committed
Update code style and dependencies
1 parent df2dd77 commit 2ec7f68

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1552
-1406
lines changed

base-node/index.d.ts

Lines changed: 172 additions & 172 deletions
Large diffs are not rendered by default.

base-node/index.js

Lines changed: 139 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import { createNanoEvents } from 'nanoevents'
22

33
import {
4-
sendConnect,
5-
sendConnected,
4+
connectedMessage,
65
connectMessage,
7-
connectedMessage
6+
sendConnect,
7+
sendConnected
88
} from '../connect/index.js'
9+
import { debugMessage, sendDebug } from '../debug/index.js'
10+
import { errorMessage, sendError } from '../error/index.js'
11+
import { headersMessage, sendHeaders } from '../headers/index.js'
12+
import { LoguxError } from '../logux-error/index.js'
13+
import { pingMessage, pongMessage, sendPing } from '../ping/index.js'
914
import {
10-
syncedMessage,
11-
syncMessage,
15+
sendSync,
1216
sendSynced,
13-
sendSync
17+
syncedMessage,
18+
syncMessage
1419
} from '../sync/index.js'
15-
import { sendPing, pingMessage, pongMessage } from '../ping/index.js'
16-
import { sendHeaders, headersMessage } from '../headers/index.js'
17-
import { sendDebug, debugMessage } from '../debug/index.js'
18-
import { sendError, errorMessage } from '../error/index.js'
19-
import { LoguxError } from '../logux-error/index.js'
2020

2121
const NOT_TO_THROW = {
22-
'wrong-subprotocol': true,
22+
'timeout': true,
2323
'wrong-protocol': true,
24-
'timeout': true
24+
'wrong-subprotocol': true
2525
}
2626

2727
const BEFORE_AUTH = ['connect', 'connected', 'error', 'debug', 'headers']
@@ -112,10 +112,6 @@ export class BaseNode {
112112
this.remoteHeaders = {}
113113
}
114114

115-
on(event, listener) {
116-
return this.emitter.on(event, listener)
117-
}
118-
119115
catch(listener) {
120116
this.throwsError = false
121117
let unbind = this.on('error', listener)
@@ -125,18 +121,13 @@ export class BaseNode {
125121
}
126122
}
127123

128-
waitFor(state) {
129-
if (this.state === state) {
130-
return Promise.resolve()
131-
}
132-
return new Promise(resolve => {
133-
let unbind = this.on('state', () => {
134-
if (this.state === state) {
135-
unbind()
136-
resolve()
137-
}
138-
})
139-
})
124+
delayPing() {
125+
if (!this.options.ping) return
126+
if (this.pingTimeout) clearTimeout(this.pingTimeout)
127+
128+
this.pingTimeout = setTimeout(() => {
129+
if (this.connected && this.authenticated) this.sendPing()
130+
}, this.options.ping)
140131
}
141132

142133
destroy() {
@@ -150,32 +141,78 @@ export class BaseNode {
150141
this.endTimeout()
151142
}
152143

153-
setLocalHeaders(headers) {
154-
this.localHeaders = headers
155-
if (this.connected) {
156-
this.sendHeaders(headers)
144+
duilianMessage(line) {
145+
if (DUILIANS[line]) {
146+
this.send(['duilian', DUILIANS[line]])
157147
}
158148
}
159149

160-
send(msg) {
161-
if (!this.connected) return
162-
this.delayPing()
163-
try {
164-
this.connection.send(msg)
165-
} catch (e) {
166-
this.error(e)
150+
endTimeout() {
151+
if (this.timeouts.length > 0) {
152+
clearTimeout(this.timeouts.shift())
167153
}
168154
}
169155

170-
onConnecting() {
171-
this.setState('connecting')
156+
error(err) {
157+
this.emitter.emit('error', err)
158+
this.connection.disconnect('error')
159+
if (this.throwsError) {
160+
throw err
161+
}
162+
}
163+
164+
async initialize() {
165+
let [synced, added] = await Promise.all([
166+
this.log.store.getLastSynced(),
167+
this.log.store.getLastAdded()
168+
])
169+
this.initialized = true
170+
this.lastSent = synced.sent
171+
this.lastReceived = synced.received
172+
this.lastAddedCache = added
173+
if (this.connection.connected) this.onConnect()
174+
}
175+
176+
now() {
177+
return Date.now()
178+
}
179+
180+
on(event, listener) {
181+
return this.emitter.on(event, listener)
182+
}
183+
184+
async onAdd(action, meta) {
185+
if (!this.authenticated) return
186+
if (this.lastAddedCache < meta.added) {
187+
this.lastAddedCache = meta.added
188+
}
189+
190+
if (this.received && this.received[meta.id]) {
191+
delete this.received[meta.id]
192+
return
193+
}
194+
195+
if (this.options.outFilter) {
196+
try {
197+
let result = await this.options.outFilter(action, meta)
198+
if (result) syncMappedEvent(this, action, meta)
199+
} catch (e) {
200+
this.error(e)
201+
}
202+
} else {
203+
syncMappedEvent(this, action, meta)
204+
}
172205
}
173206

174207
onConnect() {
175208
this.delayPing()
176209
this.connected = true
177210
}
178211

212+
onConnecting() {
213+
this.setState('connecting')
214+
}
215+
179216
onDisconnect() {
180217
while (this.timeouts.length > 0) {
181218
this.endTimeout()
@@ -199,42 +236,36 @@ export class BaseNode {
199236
this[name + 'Message'](...msg.slice(1))
200237
}
201238

202-
async onAdd(action, meta) {
203-
if (!this.authenticated) return
204-
if (this.lastAddedCache < meta.added) {
205-
this.lastAddedCache = meta.added
239+
send(msg) {
240+
if (!this.connected) return
241+
this.delayPing()
242+
try {
243+
this.connection.send(msg)
244+
} catch (e) {
245+
this.error(e)
206246
}
247+
}
207248

208-
if (this.received && this.received[meta.id]) {
209-
delete this.received[meta.id]
210-
return
211-
}
249+
sendDuilian() {
250+
this.send(['duilian', Object.keys(DUILIANS)[0]])
251+
}
212252

213-
if (this.options.outFilter) {
214-
try {
215-
let result = await this.options.outFilter(action, meta)
216-
if (result) syncMappedEvent(this, action, meta)
217-
} catch (e) {
218-
this.error(e)
219-
}
220-
} else {
221-
syncMappedEvent(this, action, meta)
222-
}
253+
setLastReceived(value) {
254+
if (this.lastReceived < value) this.lastReceived = value
255+
this.log.store.setLastSynced({ received: value })
223256
}
224257

225-
syncError(type, options, received) {
226-
let err = new LoguxError(type, options, received)
227-
this.emitter.emit('error', err)
228-
if (!NOT_TO_THROW[type] && this.throwsError) {
229-
throw err
258+
setLastSent(value) {
259+
if (this.lastSent < value) {
260+
this.lastSent = value
261+
this.log.store.setLastSynced({ sent: value })
230262
}
231263
}
232264

233-
error(err) {
234-
this.emitter.emit('error', err)
235-
this.connection.disconnect('error')
236-
if (this.throwsError) {
237-
throw err
265+
setLocalHeaders(headers) {
266+
this.localHeaders = headers
267+
if (this.connected) {
268+
this.sendHeaders(headers)
238269
}
239270
}
240271

@@ -257,19 +288,36 @@ export class BaseNode {
257288
this.timeouts.push(timeout)
258289
}
259290

260-
endTimeout() {
261-
if (this.timeouts.length > 0) {
262-
clearTimeout(this.timeouts.shift())
291+
syncError(type, options, received) {
292+
let err = new LoguxError(type, options, received)
293+
this.emitter.emit('error', err)
294+
if (!NOT_TO_THROW[type] && this.throwsError) {
295+
throw err
263296
}
264297
}
265298

266-
delayPing() {
267-
if (!this.options.ping) return
268-
if (this.pingTimeout) clearTimeout(this.pingTimeout)
269-
270-
this.pingTimeout = setTimeout(() => {
271-
if (this.connected && this.authenticated) this.sendPing()
272-
}, this.options.ping)
299+
async syncSince(lastSynced) {
300+
let data = await this.syncSinceQuery(lastSynced)
301+
if (!this.connected) return
302+
if (data.entries.length > 0) {
303+
if (this.options.outMap) {
304+
Promise.all(
305+
data.entries.map(i => {
306+
return this.options.outMap(i[0], i[1])
307+
})
308+
)
309+
.then(changed => {
310+
this.sendSync(data.added, changed)
311+
})
312+
.catch(e => {
313+
this.error(e)
314+
})
315+
} else {
316+
this.sendSync(data.added, data.entries)
317+
}
318+
} else {
319+
this.setState('synchronized')
320+
}
273321
}
274322

275323
async syncSinceQuery(lastSynced) {
@@ -309,66 +357,18 @@ export class BaseNode {
309357
return data
310358
}
311359

312-
async syncSince(lastSynced) {
313-
let data = await this.syncSinceQuery(lastSynced)
314-
if (!this.connected) return
315-
if (data.entries.length > 0) {
316-
if (this.options.outMap) {
317-
Promise.all(
318-
data.entries.map(i => {
319-
return this.options.outMap(i[0], i[1])
320-
})
321-
)
322-
.then(changed => {
323-
this.sendSync(data.added, changed)
324-
})
325-
.catch(e => {
326-
this.error(e)
327-
})
328-
} else {
329-
this.sendSync(data.added, data.entries)
330-
}
331-
} else {
332-
this.setState('synchronized')
333-
}
334-
}
335-
336-
setLastSent(value) {
337-
if (this.lastSent < value) {
338-
this.lastSent = value
339-
this.log.store.setLastSynced({ sent: value })
340-
}
341-
}
342-
343-
setLastReceived(value) {
344-
if (this.lastReceived < value) this.lastReceived = value
345-
this.log.store.setLastSynced({ received: value })
346-
}
347-
348-
now() {
349-
return Date.now()
350-
}
351-
352-
async initialize() {
353-
let [synced, added] = await Promise.all([
354-
this.log.store.getLastSynced(),
355-
this.log.store.getLastAdded()
356-
])
357-
this.initialized = true
358-
this.lastSent = synced.sent
359-
this.lastReceived = synced.received
360-
this.lastAddedCache = added
361-
if (this.connection.connected) this.onConnect()
362-
}
363-
364-
sendDuilian() {
365-
this.send(['duilian', Object.keys(DUILIANS)[0]])
366-
}
367-
368-
duilianMessage(line) {
369-
if (DUILIANS[line]) {
370-
this.send(['duilian', DUILIANS[line]])
360+
waitFor(state) {
361+
if (this.state === state) {
362+
return Promise.resolve()
371363
}
364+
return new Promise(resolve => {
365+
let unbind = this.on('state', () => {
366+
if (this.state === state) {
367+
unbind()
368+
resolve()
369+
}
370+
})
371+
})
372372
}
373373
}
374374

base-node/index.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { equal, is, not, ok, throws, type } from 'uvu/assert'
21
import { delay } from 'nanodelay'
32
import { spyOn } from 'nanospy'
43
import { test } from 'uvu'
4+
import { equal, is, not, ok, throws, type } from 'uvu/assert'
55

66
import {
77
BaseNode,
8-
TestTime,
8+
type NodeOptions,
9+
type NodeState,
10+
type TestLog,
911
TestPair,
10-
NodeOptions,
11-
NodeState
12+
TestTime
1213
} from '../index.js'
13-
import { TestLog } from '../test-log/index.js'
1414

1515
function createNode(
1616
opts?: NodeOptions,
@@ -183,7 +183,7 @@ test('loads lastSent, lastReceived and lastAdded from store', async () => {
183183
let pair = new TestPair()
184184
let node
185185

186-
log.store.setLastSynced({ sent: 1, received: 2 })
186+
log.store.setLastSynced({ received: 2, sent: 1 })
187187
await log.add({ type: 'a' }, { reasons: ['test'] })
188188
node = new BaseNode('client', log, pair.left)
189189
await node.initializing

client-node/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Log, Meta } from '../log/index.js'
21
import { BaseNode } from '../base-node/index.js'
2+
import type { Log, Meta } from '../log/index.js'
33

44
/**
55
* Client node in synchronization pair.

0 commit comments

Comments
 (0)