one
Version:
One is a new React Framework that makes Vite serve both native and web.
254 lines (252 loc) • 8.2 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var createMemoryHistory_exports = {};
__export(createMemoryHistory_exports, {
createMemoryHistory: () => createMemoryHistory
});
module.exports = __toCommonJS(createMemoryHistory_exports);
var import_non_secure = require("nanoid/non-secure");
function createMemoryHistory() {
var index = 0;
var items = [];
var pending = [];
var interrupt = function () {
pending.forEach(function (it) {
var cb = it.cb;
it.cb = function () {
return cb(true);
};
});
};
var history = {
get index() {
var _window_history_state;
var id = (_window_history_state = window.history.state) === null || _window_history_state === void 0 ? void 0 : _window_history_state.id;
if (id) {
var index1 = items.findIndex(function (item) {
return item.id === id;
});
return index1 > -1 ? index1 : 0;
}
return 0;
},
get(index2) {
return items[index2];
},
backIndex(param) {
var {
path
} = param;
for (var i = index - 1; i >= 0; i--) {
var item = items[i];
if (item.path === path) {
return i;
}
}
return -1;
},
// @modified - added displayPath and unmaskOnReload parameters for route masking
push(param) {
var {
path,
state,
displayPath,
unmaskOnReload
} = param;
interrupt();
var id = (0, import_non_secure.nanoid)();
items = items.slice(0, index + 1);
items.push({
path,
state,
id,
displayPath
});
index = items.length - 1;
var urlPath = displayPath !== null && displayPath !== void 0 ? displayPath : path;
if (process.env.ONE_DEBUG_ROUTER) {
console.info(`[one] \u{1F4DC} history.push path=${path}${displayPath ? ` displayPath=${displayPath}` : ""}`);
}
var historyState = {
id
};
if (displayPath && displayPath !== path) {
historyState.__tempLocation = {
pathname: path,
search: "",
hash: ""
};
if (unmaskOnReload) {
historyState.__tempKey = id;
}
}
window.history.pushState(historyState, "", urlPath);
},
// @modified - added displayPath and unmaskOnReload parameters for route masking
replace(param) {
var {
path,
state,
displayPath,
unmaskOnReload
} = param;
var _ref;
var _window_history_state;
interrupt();
var id = (_ref = (_window_history_state = window.history.state) === null || _window_history_state === void 0 ? void 0 : _window_history_state.id) !== null && _ref !== void 0 ? _ref : (0, import_non_secure.nanoid)();
var pathWithHash = path;
var hash = pathWithHash.includes("#") ? "" : location.hash;
if (!items.length || items.findIndex(function (item) {
return item.id === id;
}) < 0) {
pathWithHash = pathWithHash + hash;
items = [{
path: pathWithHash,
state,
id,
displayPath
}];
index = 0;
} else {
if (items[index].path === path) {
pathWithHash = pathWithHash + hash;
}
items[index] = {
path,
state,
id,
displayPath
};
}
var urlPath = displayPath ? displayPath + hash : pathWithHash;
if (process.env.ONE_DEBUG_ROUTER) {
console.info(`[one] \u{1F4DC} history.replace path=${pathWithHash}${displayPath ? ` displayPath=${displayPath}` : ""}`);
}
var historyState = {
id
};
if (displayPath && displayPath !== path) {
historyState.__tempLocation = {
pathname: path,
search: "",
hash: ""
};
if (unmaskOnReload) {
historyState.__tempKey = id;
}
}
window.history.replaceState(historyState, "", urlPath);
},
// `history.go(n)` is asynchronous, there are couple of things to keep in mind:
// - it won't do anything if we can't go `n` steps, the `popstate` event won't fire.
// - each `history.go(n)` call will trigger a separate `popstate` event with correct location.
// - the `popstate` event fires before the next frame after calling `history.go(n)`.
// This method differs from `history.go(n)` in the sense that it'll go back as many steps it can.
go(n) {
interrupt();
var nextIndex = index + n;
var lastItemIndex = items.length - 1;
if (n < 0 && !items[nextIndex]) {
n = -index;
index = 0;
} else if (n > 0 && nextIndex > lastItemIndex) {
n = lastItemIndex - index;
index = lastItemIndex;
} else {
index = nextIndex;
}
if (n === 0) {
return;
}
return new Promise(function (resolve, reject) {
var done = function (interrupted) {
clearTimeout(timer);
if (interrupted) {
reject(new Error("History was changed during navigation."));
return;
}
var {
title
} = window.document;
window.document.title = "";
window.document.title = title;
resolve();
};
pending.push({
ref: done,
cb: done
});
var timer = setTimeout(function () {
var index2 = pending.findIndex(function (it) {
return it.ref === done;
});
if (index2 > -1) {
pending[index2].cb();
pending.splice(index2, 1);
}
}, 100);
var onPopState = function () {
var _window_history_state;
var id = (_window_history_state = window.history.state) === null || _window_history_state === void 0 ? void 0 : _window_history_state.id;
var currentIndex = items.findIndex(function (item) {
return item.id === id;
});
index = Math.max(currentIndex, 0);
var last = pending.pop();
window.removeEventListener("popstate", onPopState);
last === null || last === void 0 ? void 0 : last.cb();
};
window.addEventListener("popstate", onPopState);
window.history.go(n);
});
},
// The `popstate` event is triggered when history changes, except `pushState` and `replaceState`
// If we call `history.go(n)` ourselves, we don't want it to trigger the listener
// Here we normalize it so that only external changes (e.g. user pressing back/forward) trigger the listener
listen(listener) {
var onPopState = function () {
var _window_history_state;
if (pending.length) {
return;
}
var popId = (_window_history_state = window.history.state) === null || _window_history_state === void 0 ? void 0 : _window_history_state.id;
if (popId) {
var foundIndex = items.findIndex(function (item) {
return item.id === popId;
});
if (foundIndex > -1) {
index = foundIndex;
}
}
listener();
};
window.addEventListener("popstate", onPopState);
return function () {
return window.removeEventListener("popstate", onPopState);
};
}
};
return history;
}
//# sourceMappingURL=createMemoryHistory.native.js.map