2022-10-01 08:57:34 +00:00
|
|
|
import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'
|
|
|
|
import { Awareness } from 'y-protocols/awareness'
|
|
|
|
import { WebrtcProvider } from 'y-webrtc'
|
2022-10-08 05:24:09 +00:00
|
|
|
import { Doc, XmlFragment } from 'yjs'
|
2022-10-07 19:35:53 +00:00
|
|
|
import type { Reaction } from '../../../graphql/types.gen'
|
|
|
|
import { setReactions } from '../../../stores/editor'
|
2022-10-01 08:57:34 +00:00
|
|
|
|
2022-10-08 05:24:09 +00:00
|
|
|
export const roomConnect = (room, username = '', keyname = 'collab'): [XmlFragment, WebrtcProvider] => {
|
|
|
|
const ydoc = new Doc()
|
2022-10-07 19:35:53 +00:00
|
|
|
const yarr = ydoc.getArray(keyname + '-reactions')
|
|
|
|
const yXmlFragment = ydoc.getXmlFragment(keyname)
|
2022-10-01 08:57:34 +00:00
|
|
|
const webrtcOptions = {
|
|
|
|
awareness: new Awareness(ydoc),
|
|
|
|
filterBcConns: true,
|
|
|
|
maxConns: 33,
|
|
|
|
signaling: [
|
2022-10-07 19:35:53 +00:00
|
|
|
// 'wss://signaling.discours.io',
|
2022-10-01 08:57:34 +00:00
|
|
|
// 'wss://stun.l.google.com:19302',
|
|
|
|
'wss://y-webrtc-signaling-eu.herokuapp.com',
|
|
|
|
'wss://signaling.yjs.dev'
|
|
|
|
],
|
|
|
|
peerOpts: {},
|
|
|
|
password: ''
|
|
|
|
}
|
|
|
|
const provider = new WebrtcProvider(room, ydoc, webrtcOptions)
|
2022-10-04 10:45:10 +00:00
|
|
|
let name = username
|
2022-10-01 08:57:34 +00:00
|
|
|
|
2022-10-07 19:35:53 +00:00
|
|
|
yarr.observeDeep(() => {
|
|
|
|
console.debug('yarray updated:', yarr.toArray())
|
|
|
|
setReactions(yarr.toArray() as Reaction[])
|
|
|
|
})
|
|
|
|
|
2022-10-04 10:45:10 +00:00
|
|
|
if (Boolean(name) === false) {
|
|
|
|
name = uniqueNamesGenerator({
|
|
|
|
dictionaries: [adjectives, animals],
|
|
|
|
style: 'capital',
|
|
|
|
separator: ' ',
|
|
|
|
length: 2
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
provider.awareness.setLocalStateField('user', { name })
|
2022-10-07 19:35:53 +00:00
|
|
|
return [yXmlFragment, provider]
|
2022-10-01 08:57:34 +00:00
|
|
|
}
|