-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocketio.js
46 lines (42 loc) · 1.42 KB
/
socketio.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
import { YSocketIO } from './y-socket-io/y-socket-io.js'
/**
* how to sync
* receive sync-step 1
* // @todo y-websocket should only accept updates after receiving sync-step 2
* redisId = ws.sub(conn)
* {doc,redisDocLastId} = api.getdoc()
* compute sync-step 2
* if (redisId > redisDocLastId) {
* subscriber.ensureId(redisDocLastId)
* }
*/
class YSocketIOServer {
/**
* @param {YSocketIO} app
* @param {import('./api.js').Api} client
* @param {import('./subscriber.js').Subscriber} subscriber
*/
constructor (app, client, subscriber) {
this.app = app
this.subscriber = subscriber
this.client = client
}
async destroy () {
this.subscriber.destroy()
await this.client.destroy()
}
}
/**
* @param {import('socket.io').Server} io
* @param {import('./storage.js').AbstractStorage} store
* @param {Object} conf
* @param {string} [conf.redisPrefix]
* @param {string} [conf.redisUrl]
* @param {import('./y-socket-io/y-socket-io.js').YSocketIOConfiguration['authenticate']} conf.authenticate
* @param {import('worker_threads').Worker=} [conf.persistWorker]
*/
export const registerYSocketIOServer = async (io, store, { authenticate, redisUrl, redisPrefix, persistWorker }) => {
const app = new YSocketIO(io, { authenticate })
const { client, subscriber } = await app.initialize(store, { redisUrl, redisPrefix, persistWorker })
return new YSocketIOServer(app, client, subscriber)
}