polen
Version:
A framework for delightful GraphQL developer portals
85 lines • 2.75 kB
JavaScript
import { debugPolen } from '#singletons/debug';
import { effect, isRef } from '@vue/reactivity';
const pluginDebug = debugPolen.sub(`vite-reactive-data`);
export const create = (options) => {
const codec = options.codec ?? JSON;
const moduleId = options.moduleId;
const name = options.name ?? `reactive-data`;
const debug = pluginDebug.sub(name);
debug(`constructor`, { moduleId });
let $server;
let $invalidationScheduled = false;
const tryInvalidate = () => {
$invalidationScheduled = false;
if (!$server)
throw new Error(`Server not available yet - this should be impossible`);
const moduleNode = $server.moduleGraph.getModuleById(moduleId);
if (moduleNode) {
debug(`invalidate`, { id: moduleNode.id });
$server.moduleGraph.invalidateModule(moduleNode);
}
else {
debug(`cannot invalidate`, {
reason: `notInModuleGraph`,
moduleId,
hint: `maybe it was not loaded yet`,
});
}
};
const scheduleInvalidate = () => {
if ($invalidationScheduled)
return; // already scheduled
$invalidationScheduled = true;
if (!$server)
return; // server will flush when ready
tryInvalidate();
};
// Helper to get the current data value
const getData = () => {
if (isRef(options.data)) {
return options.data.value;
}
else if (typeof options.data === `function`) {
return options.data();
}
else {
return options.data;
}
};
// Set up reactive effect immediately
effect(() => {
// Access data to track dependencies
const data = getData();
debug(`effect triggered`, { data });
scheduleInvalidate();
});
return {
name,
configureServer(_server) {
debug(`hook configureServer`);
$server = _server;
if ($invalidationScheduled) {
debug(`try invalidate scheduled before server was ready`);
tryInvalidate();
}
},
resolveId(id) {
if (id === moduleId) {
return moduleId;
}
},
// todo make use of Vite's builtin json plugin
// for example, call it here somehow
load(id) {
if (id !== moduleId)
return;
const data = getData();
debug(`hook load`, { data });
return {
code: codec.stringify(data),
map: null,
};
},
};
};
//# sourceMappingURL=vite-plugin-reactive-data.js.map