Sha256: a66e35702228c46e49a290f0596261925392d86edfac16410649aa0e0cbb645f

Contents?: true

Size: 1.63 KB

Versions: 23

Compression:

Stored size: 1.63 KB

Contents

import { CocoComponent } from "@assets/js/coco/component";

export default CocoComponent("pollController", () => {
  return {
    polling: false,
    frame: null,
    pollTimer: null,

    get interval() {
      return parseInt(this.$root.dataset.interval || 0, 10);
    },

    init() {
      this.frame = document.getElementById("polling-controller-frame");
      this.observer = new MutationObserver(() => this.onUpdate());
      this.observer.observe(this.$el, { attributes: true });
    },

    startPolling() {
      if (this.interval > 0 && !this.polling) {
        this.polling = true;
        this.queuePollRequest(500);
      }
    },

    onUpdate() {
      this.$nextTick(() => {
        this.$dispatch("dom-updates:complete", { html: document }); // for compatability with legacy components
      });
      this.queuePollRequest();
    },

    queuePollRequest(wait) {
      if (this.pollTimer) clearTimeout(this.pollTimer);
      if (this.interval > 0 && this.polling) {
        this.pollTimer = setTimeout(
          () => this.reloadFrame(),
          wait || this.interval
        );
      }
    },

    reloadFrame() {
      this.frame.addEventListener(
        "turbo:before-fetch-request",
        addStreamHeaders,
        { once: true }
      );
      window.Turbo.visit(location.href, {
        frame: this.frame.id,
        action: "replace",
      });
    },

    destroy() {
      this.observer.disconnect();
      this.polling = false;
    },
  };
});

function addStreamHeaders(event) {
  const { headers } = event.detail.fetchOptions || {};
  if (headers) {
    headers.Accept = ["text/vnd.turbo-stream.html", headers.Accept].join(", ");
  }
}

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
coveragebook_components-0.19.9 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.19.8 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.19.7 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.19.6 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.19.5 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.19.4 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.19.3 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.19.2 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.19.1 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.19.0 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.18.8 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.18.7 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.18.0 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.17.7 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.17.6 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.17.5 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.17.4 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.17.3 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.17.2 app/components/coco/utilities/poll_controller/poll_controller.js
coveragebook_components-0.17.1 app/components/coco/utilities/poll_controller/poll_controller.js