verifyUrlSequence()

in src/pro-src/rules.js [299:355]


  verifyUrlSequence(trigger) {
    let sequenceMatched = true;
    // We first check that there is enough urls in the history to match the sequence to
    if (this.history.path.length >= trigger.url.length) {
      // we use that next line to start from the end of the history.
      let historyPosition = this.history.path.length - 1;
      trigger.url.forEach((triggerUrl, index) => {
        if (sequenceMatched === false || historyPosition < 0) return;
        const historyUrl = this.history.path[historyPosition];
        if (
          !RulesHandler.compareUrls(historyUrl, triggerUrl.path, triggerUrl.partialMatch)
        ) {
          // If the very first url in the history is wrong, no point in testing the rest.
          if (index === 0) {
            sequenceMatched = false;
            return;
          }
          let matchedTrigger = false;
          // we check that we only compare urls that are the same than the previous one.
          let matchedFirstAndLast = true;
          let tries = 0;
          while (matchedFirstAndLast && !matchedTrigger && historyPosition >= 0) {
            historyPosition -= 1;
            // we only check up to 8 urls before failing. This is 100% arbitrary.
            // We use that so that we don't compare every urls in a history
            // which could be very long.
            if (tries > 8) {
              sequenceMatched = false;
              break;
            }
            tries += 1;
            matchedTrigger = RulesHandler.compareUrls(
              this.history.path[historyPosition],
              triggerUrl.path,
              trigger.partialMatch
            );
            // We only check that one if the trigger url did not match.
            if (!matchedTrigger) {
              matchedFirstAndLast = RulesHandler.compareUrls(
                this.history.path[historyPosition],
                historyUrl
              );
            }
          }
          // we continue the process if we matched that url, if not,
          // the start of the forEach will stop the process.
          sequenceMatched = matchedTrigger;
        } else {
          // This means it matched first try, then we go down in the history
          historyPosition -= 1;
        }
      });
    } else {
      sequenceMatched = false;
    }
    return sequenceMatched;
  }