All files / public/details historyState.js

100% Statements 12/12
100% Branches 4/4
100% Functions 5/5
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31    12x 12x   12x 13x 4x           14x 9x 9x 7x     5x   14x       3x          
class HistoryState {
  constructor() {
    this.path = window.location.hash.substr(1).split("/");
    this.popListeners = [];
 
    window.addEventListener("popstate", (e) => {
      this.popListeners.forEach((fn) => {
        fn(e.state);
      });
    });
  }
 
  updatePath(part, path) {
    if (part === 0) {
      this.path = path;
      if (!Array.isArray(this.path)) {
        this.path = [this.path];
      }
    } else {
      this.path[part] = path;
    }
    history.pushState(this.path, null, `#${this.path.join("/")}`);
  }
 
  addPopListener(fn) {
    this.popListeners.push(fn);
  }
}
 
export { HistoryState };