constructor()

in src/pro-src/rules.js [14:66]


  constructor(rules, sendMethod, triggerEventListenerUpdateRate = 500) {
    this.url = window.location.host + window.location.pathname;
    this.numberOfVisits = 0;
    this.rules = rules;
    this.sendMethod = sendMethod;
    this.timeoutIds = [];
    this.eventListeners = [];
    this.resetListenersTimeouts = [];
    this.protocol = window.location.protocol;
    this.triggerEventListenerUpdateRate = triggerEventListenerUpdateRate;
    this.lastLocationChange = Date.now();

    // Here we supersede those events to all redirect them to our custom event
    window.history.pushState = (f =>
      function pushState() {
        const ret = f.apply(this, arguments);
        window.dispatchEvent(new Event('pushstate'));
        window.dispatchEvent(new Event('locationchange'));
        return ret;
      })(window.history.pushState);

    window.history.replaceState = (f =>
      function replaceState() {
        const ret = f.apply(this, arguments);
        window.dispatchEvent(new Event('replacestate'));
        window.dispatchEvent(new Event('locationchange'));
        return ret;
      })(window.history.replaceState);

    this.popstateCallback = () => {
      window.dispatchEvent(new Event('locationchange'));
    };

    window.addEventListener('popstate', this.popstateCallback);

    this.locationChangeCallback = () => {
      if ((Date.now() - window[RULES_HANDLER_SINGLETON].lastLocationChange) < 150) {
        return;
      }
      window[RULES_HANDLER_SINGLETON].lastLocationChange = Date.now();
      // We use the window object that was set in the react component
      // So that we have an up to date version
      if (window[RULES_HANDLER_SINGLETON]) {
        window[RULES_HANDLER_SINGLETON].url =
                    window.location.host + window.location.pathname;
        // We clean up the timeouts so that we don't have race conditions and unwanted triggers
        window[RULES_HANDLER_SINGLETON].cleanUp();
        window[RULES_HANDLER_SINGLETON].initHandler();
      }
    };

    window.addEventListener('locationchange', this.locationChangeCallback);
  }