2023-10-23 06:58:18 +00:00

38 lines
763 B
JavaScript

import { cable } from '@hotwired/turbo-rails'
export default class RoomSubscription {
constructor ({ delegate, id, clientId }) {
this.delegate = delegate
this.id = id
this.clientId = clientId
}
async start () {
const self = this
this.subscription = await cable.subscribeTo({
channel: 'RoomChannel',
id: this.id,
client_id: this.clientId
}, {
greet (data) {
this.perform('greet', data)
},
received (data) {
// Ignore self-sent data
if (data.from === self.clientId) return
if (data.type === 'ping') self.delegate.roomPinged(data)
}
})
this.started = true
}
greet (data) {
if (!this.started) return
this.subscription.greet(data)
}
}