one
Version:
One is a new React Framework that makes Vite serve both native and web.
135 lines (134 loc) • 5.09 kB
JavaScript
import * as React from "react";
var currentNotFoundState = null,
notFoundListeners = /* @__PURE__ */new Set();
function getNotFoundState() {
return currentNotFoundState;
}
function setNotFoundState(state) {
currentNotFoundState = state, notFoundListeners.forEach(function (listener) {
return listener();
});
}
function clearNotFoundState() {
currentNotFoundState !== null && (currentNotFoundState = null, notFoundListeners.forEach(function (listener) {
return listener();
}));
}
function useNotFoundState() {
var [, forceUpdate] = React.useReducer(function (x) {
return x + 1;
}, 0);
return React.useEffect(function () {
return notFoundListeners.add(forceUpdate), function () {
notFoundListeners.delete(forceUpdate);
};
}, []), currentNotFoundState;
}
function findNearestNotFoundRoute(pathname, rootNode) {
if (!rootNode) return null;
var pathParts = pathname.split("/").filter(Boolean);
function findNotFoundInNode(node) {
if (node.route === "+not-found") return node;
var _iteratorNormalCompletion = !0,
_didIteratorError = !1,
_iteratorError = void 0;
try {
for (var _iterator = (node.children || [])[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
var child = _step.value;
if (child.route === "+not-found") return child;
}
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
} finally {
try {
!_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
} finally {
if (_didIteratorError) throw _iteratorError;
}
}
return null;
}
function traverse(node, depth, notFoundStack) {
var notFoundAtLevel = findNotFoundInNode(node);
if (notFoundAtLevel && notFoundStack.push(notFoundAtLevel), depth >= pathParts.length) return notFoundStack.length > 0 ? notFoundStack[notFoundStack.length - 1] : null;
var segment = pathParts[depth],
_iteratorNormalCompletion = !0,
_didIteratorError = !1,
_iteratorError = void 0;
try {
for (var _iterator = (node.children || [])[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
var child = _step.value;
if (child.route !== "+not-found") {
var childRoute = child.route || "",
isDynamic = childRoute.startsWith("["),
isMatch = childRoute === segment || isDynamic;
if (isMatch) {
var result = traverse(child, depth + 1, [...notFoundStack]);
if (result) return result;
}
}
}
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
} finally {
try {
!_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
} finally {
if (_didIteratorError) throw _iteratorError;
}
}
return notFoundStack.length > 0 ? notFoundStack[notFoundStack.length - 1] : null;
}
return traverse(rootNode, 0, []);
}
function findRouteNodeByPath(notFoundPath, rootNode) {
if (!rootNode) return null;
var normalizedPath = notFoundPath.replace(/^(\.?\/)+|\/+$/g, "");
function searchNode(node) {
var nodeContextKey = node.contextKey || "",
contextKeyNormalized = nodeContextKey.replace(/^(\.?\/)+/, "").replace(/\.[^.]+$/, "");
if (contextKeyNormalized === normalizedPath) return node;
var _iteratorNormalCompletion2 = !0,
_didIteratorError2 = !1,
_iteratorError2 = void 0;
try {
for (var _iterator2 = (node.children || [])[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = !0) {
var child2 = _step2.value,
found2 = searchNode(child2);
if (found2) return found2;
}
} catch (err) {
_didIteratorError2 = !0, _iteratorError2 = err;
} finally {
try {
!_iteratorNormalCompletion2 && _iterator2.return != null && _iterator2.return();
} finally {
if (_didIteratorError2) throw _iteratorError2;
}
}
return null;
}
var found = searchNode(rootNode);
if (found) return found;
var _iteratorNormalCompletion = !0,
_didIteratorError = !1,
_iteratorError = void 0;
try {
for (var _iterator = (rootNode.children || [])[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
var child = _step.value,
found1 = searchNode(child);
if (found1) return found1;
}
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
} finally {
try {
!_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
} finally {
if (_didIteratorError) throw _iteratorError;
}
}
return null;
}
export { clearNotFoundState, findNearestNotFoundRoute, findRouteNodeByPath, getNotFoundState, setNotFoundState, useNotFoundState };
//# sourceMappingURL=notFoundState.native.js.map