Sha256: d16c766a2b5ef793a3a3ec5608c4da6463339c122e96c186f2a8ca7385b996ae

Contents?: true

Size: 1.26 KB

Versions: 18

Compression:

Stored size: 1.26 KB

Contents

import { connectStreamSource, disconnectStreamSource } from "@hotwired/turbo"
import { subscribeTo } from "./cable"
import snakeize from "./snakeize"

class TurboCableStreamSourceElement extends HTMLElement {
  async connectedCallback() {
    connectStreamSource(this)
    this.subscription = await subscribeTo(this.channel, {
      received: this.dispatchMessageEvent.bind(this),
      connected: this.subscriptionConnected.bind(this),
      disconnected: this.subscriptionDisconnected.bind(this)
    })
  }

  disconnectedCallback() {
    disconnectStreamSource(this)
    if (this.subscription) this.subscription.unsubscribe()
  }

  dispatchMessageEvent(data) {
    const event = new MessageEvent("message", { data })
    return this.dispatchEvent(event)
  }

  subscriptionConnected() {
    this.setAttribute("connected", "")
  }

  subscriptionDisconnected() {
    this.removeAttribute("connected")
  }

  get channel() {
    const channel = this.getAttribute("channel")
    const signed_stream_name = this.getAttribute("signed-stream-name")
    return { channel, signed_stream_name, ...snakeize({ ...this.dataset }) }
  }
}


if (customElements.get("turbo-cable-stream-source") === undefined) {
  customElements.define("turbo-cable-stream-source", TurboCableStreamSourceElement)
}

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
turbo-rails-2.0.7 app/javascript/turbo/cable_stream_source_element.js
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/turbo-rails-2.0.5/app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.6 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.5 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.4 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.3 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.2 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.1 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.0 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.0.pre.rc.3 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.0.pre.rc.2 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.0.pre.rc.1 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.0.pre.beta.4 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.0.pre.beta.3 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.0.pre.beta.2 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-2.0.0.pre.beta.1 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-1.5.0 app/javascript/turbo/cable_stream_source_element.js
turbo-rails-1.4.0 app/javascript/turbo/cable_stream_source_element.js