Files
once-campfire/app/javascript/controllers/rooms_list_controller.js
T
2026-10-07 16:18:39 +02:00

73 lines
1.8 KiB
JavaScript

import { Controller } from "@hotwired/stimulus"
import { cable } from "@hotwired/turbo-rails"
import { ignoringBriefDisconnects } from "helpers/dom_helpers"
export default class extends Controller {
static targets = [ "room" ]
static classes = [ "unread" ]
#disconnected = true
#connection = 0
async connect() {
this.channel ??= await cable.subscribeTo({ channel: "UnreadRoomsChannel" }, {
connected: this.#channelConnected.bind(this),
disconnected: this.#channelDisconnected.bind(this),
received: this.#unread.bind(this)
})
}
disconnect() {
ignoringBriefDisconnects(this.element, () => {
this.#channelDisconnected()
this.channel?.unsubscribe()
this.channel = null
})
}
loaded() {
this.read({ detail: { roomId: Current.room.id } })
}
read({ detail: { roomId } }) {
const room = this.#findRoomTarget(roomId)
if (room) {
room.classList.remove(this.unreadClass)
this.dispatch("read", { detail: { targetId: roomId } })
}
}
async #channelConnected() {
if (this.#disconnected) {
this.#disconnected = false
const connection = ++this.#connection
// Reloading an unfinished frame aborts its response body reader.
await this.element.loaded
if (this.element.isConnected && !this.#disconnected && connection === this.#connection) {
this.element.reload()
}
}
}
#channelDisconnected() {
this.#disconnected = true
}
#unread({ roomId }) {
const unreadRoom = this.#findRoomTarget(roomId)
if (unreadRoom) {
if (Current.room.id != roomId) {
unreadRoom.classList.add(this.unreadClass)
}
this.dispatch("unread", { detail: { targetId: unreadRoom.id } })
}
}
#findRoomTarget(roomId) {
return this.roomTargets.find(roomTarget => roomTarget.dataset.roomId == roomId)
}
}