-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue.js
26 lines (24 loc) · 1.03 KB
/
vue.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
import { Client } from './index.js'
import { inject, onBeforeUnmount, reactive } from 'vue'
export function createSocket(url, { autoConnect = true, ...options } = {}) {
const socket = reactive(new Client(url, { autoConnect: false, ...options }))
// The reason automatic connection is handled differently here,
// is because calling `this.connect()` in the constructor preempts
// the reactivity of the object and makes users unable to watch the `connected`
// attribute; so instead we run `connect()` on the reactive client
if (autoConnect) socket.connect()
return socket
}
export function listen(actions, { name = 'ws', subscribe = false } = {}) {
const client = inject(`$${name}`)
for (const action in actions) {
client.on(action, actions[action])
if (subscribe) client.send('subscribe', action)
}
onBeforeUnmount(() => {
for (const action in actions) {
if (subscribe) client.send('unsubscribe', action)
client.off(action, actions[action])
}
})
}