Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758import { QueryClient } from "@octanejs/tanstack-query";
const initial = { scope: null as string | null, epoch: 0, denied: false };
/** One mounted app's private cache. Socket generations are not cache identities. */export class CustomerQuerySession { readonly client = new QueryClient({ defaultOptions: { queries: { staleTime: 30_000, gcTime: 5 * 60_000, retry: false, refetchOnReconnect: false, }, mutations: { retry: false, networkMode: "always", gcTime: 0 }, }, }); private state = initial; private listeners = new Set<() => void>(); private lossListeners = new Set<() => void>(); getSnapshot = () => this.state; subscribe = (listener: () => void) => { this.listeners.add(listener); return () => { this.listeners.delete(listener); }; }; onLoss = (listener: () => void) => { this.lossListeners.add(listener); return () => { this.lossListeners.delete(listener); }; }; authenticate(epoch: number) { if (epoch !== this.state.epoch) throw new Error("Owner connection retired"); if (!this.state.scope) { this.state = { epoch, scope: crypto.randomUUID(), denied: false }; this.listeners.forEach((listener) => listener()); } } revoke(epoch: number) { if (epoch !== this.state.epoch) return; this.state = { epoch: epoch + 1, scope: null, denied: true }; // Disable/retire every native owner before notifying query observers. this.lossListeners.forEach((listener) => listener()); this.client.clear(); this.listeners.forEach((listener) => listener()); } key(...resource: string[]) { return [ "customer", typeof window === "undefined" ? "" : window.location.origin, this.state.scope, ...resource, ] as const; }}