respond-framework
Version:
create as fast you think
63 lines (62 loc) • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.replace = exports.push = exports.default = void 0;
var _browserState = require("./browserState.js");
var _createTrap = require("./createTrap.js");
const changePath0 = e => {
if (e.changePath === false) return;
if (_browserState.default.pop) {
_browserState.default.queuedNavigation = e; // trap will dequeue
} else {
const useReplace = e.event.respond.topState.replayTools?.playing;
const change = () => changePath(e, useReplace);
debounce(change);
}
};
var _default = exports.default = changePath0;
const changePath = (e, useReplace) => {
const {
fromEvent,
mem
} = e.event.respond;
const {
url
} = fromEvent(e);
change(url, useReplace || mem.changedPath);
mem.changedPath = true;
(0, _createTrap.createTrap)();
};
const change = (url, useReplace) => {
const index = history.state?.index;
if (index === undefined) replace(url); // first visit
else if (useReplace) replace(url); // subsequent redirects in single dispatch pipeline must both be treated as a replace
else if (!_browserState.default.hasTrap) replace(url); // return visit in same session when not cached by browser (index will be defined, but trap not yet setup)
else push(url); // new links (only one per user-triggered dispatch pipeline)
};
const replace = async url => {
const index = history.state?.index ?? 0;
_browserState.default.prevIndex = index;
history.replaceState({
index
}, '', url);
window.state.respond.prevUrl = url;
};
exports.replace = replace;
const push = url => {
const index = (history.state?.index ?? 0) + 1;
_browserState.default.prevIndex = index;
_browserState.default.maxIndex = index;
_browserState.default.linkedOut = false;
history.pushState({
index
}, '', url);
window.state.respond.prevUrl = url;
};
exports.push = push;
let timer;
const debounce = func => {
clearTimeout(timer);
timer = setTimeout(func, 50);
};