Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

feat(skills): implement createCatalogTtl memo with injected clock

Abdurrahman Fakhrulc36dbc8d6171
diff --git a/lib/skills/catalog-ttl.ts b/lib/skills/catalog-ttl.ts
new file mode 100644
index 00000000..fffb18bd
--- /dev/null
+++ b/lib/skills/catalog-ttl.ts
@@ -0,0 +1,70 @@
+import { catalogSkillsDetailed, type SkillCatalog } from "./catalog";
+
+export const DEFAULT_CATALOG_TTL_MS = 15_000;
+
+export type CatalogTtlOptions<T = SkillCatalog> = {
+ load?: () => Promise<T>;
+ now?: () => number;
+ ttlMs?: number;
+};
+
+export type CatalogTtl<T = SkillCatalog> = {
+ get: (opts?: { refresh?: boolean }) => Promise<T>;
+ invalidate: () => void;
+};
+
+export function createCatalogTtl<T = SkillCatalog>(
+ options: CatalogTtlOptions<T> = {}
+): CatalogTtl<T> {
+ const load = options.load ?? (catalogSkillsDetailed as unknown as () => Promise<T>);
+ const now = options.now ?? Date.now;
+ const ttlMs = options.ttlMs ?? DEFAULT_CATALOG_TTL_MS;
+
+ let cached: { data: T; expiresAt: number } | null = null;
+ let inFlight: Promise<T> | null = null;
+ let currentRequestId = 0;
+
+ const fetchFresh = (): Promise<T> => {
+ const reqId = ++currentRequestId;
+ const p = Promise.resolve()
+ .then(() => load())
+ .then((data) => {
+ if (reqId === currentRequestId) {
+ cached = { data, expiresAt: now() + ttlMs };
+ }
+ return data;
+ })
+ .finally(() => {
+ if (reqId === currentRequestId) {
+ inFlight = null;
+ }
+ });
+ inFlight = p;
+ return p;
+ };
+
+ return {
+ async get(opts?: { refresh?: boolean }): Promise<T> {
+ if (opts?.refresh) {
+ cached = null;
+ inFlight = null;
+ return fetchFresh();
+ }
+ const currentTime = now();
+ if (cached && currentTime < cached.expiresAt) {
+ return cached.data;
+ }
+ if (inFlight) {
+ return inFlight;
+ }
+ return fetchFresh();
+ },
+ invalidate(): void {
+ cached = null;
+ inFlight = null;
+ currentRequestId++;
+ },
+ };
+}
+
+export const catalogTtl = createCatalogTtl();