one
Version:
One is a new React Framework that makes Vite serve both native and web.
77 lines • 2.64 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 useMatches_exports = {};
__export(useMatches_exports, {
getClientMatchesSnapshot: () => getClientMatchesSnapshot,
setClientMatches: () => setClientMatches,
subscribeToClientMatches: () => subscribeToClientMatches,
updateMatchLoaderData: () => updateMatchLoaderData,
useMatch: () => useMatch,
useMatches: () => useMatches,
usePageMatch: () => usePageMatch
});
module.exports = __toCommonJS(useMatches_exports);
var import_react = require("react");
var import_one_server_only = require("./vite/one-server-only.cjs");
let clientMatches = [];
const clientMatchesListeners = /* @__PURE__ */new Set();
function subscribeToClientMatches(callback) {
clientMatchesListeners.add(callback);
return () => clientMatchesListeners.delete(callback);
}
function getClientMatchesSnapshot() {
return clientMatches;
}
function setClientMatches(matches) {
clientMatches = matches;
for (const listener of clientMatchesListeners) {
listener();
}
}
function updateMatchLoaderData(routeId, loaderData) {
clientMatches = clientMatches.map(m => m.routeId === routeId ? {
...m,
loaderData
} : m);
for (const listener of clientMatchesListeners) {
listener();
}
}
function useMatches() {
const serverContext = (0, import_one_server_only.useServerContext)();
if (process.env.VITE_ENVIRONMENT === "ssr") {
return serverContext?.matches ?? [];
}
const clientStoreMatches = (0, import_react.useSyncExternalStore)(subscribeToClientMatches, getClientMatchesSnapshot,
// server snapshot for hydration - must match what SSR rendered
() => serverContext?.matches ?? []);
return clientStoreMatches;
}
function useMatch(routeId) {
const matches = useMatches();
return matches.find(m => m.routeId === routeId);
}
function usePageMatch() {
const matches = useMatches();
return matches[matches.length - 1];
}