Skip to content
Snippets Groups Projects
concurrent-load-detector.js 1.26 KiB
Newer Older
// Detects if another instance of ConcurrentLoadDetector is start()'ed by in the same local storage
// context with the same instance key. Once a duplicate run is detected this will not fire any additional
// events.

const LOCAL_STORE_KEY = "___concurrent_load_detector";
Brian Peiris's avatar
Brian Peiris committed
import { EventTarget } from "event-target-shim";

export default class ConcurrentLoadDetector extends EventTarget {
  constructor(instanceKey) {
    super();

    this.interval = null;
    this.startedAt = null;
    this.instanceKey = instanceKey || "global";
  }

  start = () => {
    this.startedAt = new Date();
    localStorage.setItem(this.localStorageKey(), JSON.stringify({ started_at: this.startedAt }));

    // Check for concurrent load every second
    this.interval = setInterval(this._step, 1000);
Brian Peiris's avatar
Brian Peiris committed
  };

  stop = () => {
    if (this.interval) {
      clearInterval(this.interval);
    }
Brian Peiris's avatar
Brian Peiris committed
  };

  localStorageKey = () => {
    return `${LOCAL_STORE_KEY}_${this.instanceKey}`;
Brian Peiris's avatar
Brian Peiris committed
  };

  _step = () => {
    const currentState = JSON.parse(localStorage.getItem(this.localStorageKey()));
    const maxStartedAt = new Date(currentState.started_at);

    if (maxStartedAt.getTime() !== this.startedAt.getTime()) {
      this.dispatchEvent(new CustomEvent("concurrentload"));
      this.stop();
    }
Brian Peiris's avatar
Brian Peiris committed
  };