thirteen-tien-len/app/javascript/subscriptions/video_room_subscription.js
2023-12-03 16:02:27 +00:00

38 lines
778 B
JavaScript

import { cable } from '@hotwired/turbo-rails'
export default class VideoRoomSubscription {
constructor ({ delegate, id, clientId }) {
this.delegate = delegate
this.id = id
this.clientId = clientId
}
async start () {
const self = this
this.subscription = await cable.subscribeTo({
channel: 'VideoRoomChannel',
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.videoRoomPinged(data)
}
})
this.started = true
}
greet (data) {
if (!this.started) return
this.subscription.greet(data)
}
}