@tanstack/ai-mcp
Version:
Host-side Model Context Protocol client for TanStack AI: discover and run MCP server tools, resources, and prompts in any adapter's chat() loop, with generated end-to-end types.
61 lines (60 loc) • 1.76 kB
JavaScript
//#region src/events/memory-store.ts
/**
* In-process event subscription store. For development and tests only.
* Callers receive structured clones, so they cannot mutate stored state.
*
* @experimental
*
* @example
* ```ts
* import { inMemoryMCPEventStore } from '@tanstack/ai-mcp/events'
*
* const store = inMemoryMCPEventStore()
* ```
*/
function inMemoryMCPEventStore() {
const byKey = /* @__PURE__ */ new Map();
const byId = /* @__PURE__ */ new Map();
function index(record) {
if (typeof record.id === "string") byId.set(record.id, record.key);
}
function unindex(record) {
if (record?.id !== void 0) byId.delete(record.id);
}
return {
async get(key) {
const stored = byKey.get(key);
return stored === void 0 ? void 0 : structuredClone(stored);
},
async getById(id) {
const key = byId.get(id);
if (key === void 0) return void 0;
const stored = byKey.get(key);
return stored === void 0 ? void 0 : structuredClone(stored);
},
async set(subscription) {
const previous = byKey.get(subscription.key);
if (previous?.id !== void 0 && previous.id !== subscription.id) byId.delete(previous.id);
const stored = structuredClone(subscription);
byKey.set(stored.key, stored);
index(stored);
},
async update(key, patch) {
const stored = byKey.get(key);
if (stored === void 0) return;
if (patch.id !== void 0 && stored.id !== void 0 && patch.id !== stored.id) byId.delete(stored.id);
Object.assign(stored, patch);
index(stored);
},
async delete(key) {
unindex(byKey.get(key));
byKey.delete(key);
},
async *list() {
for (const stored of byKey.values()) yield structuredClone(stored);
}
};
}
//#endregion
export { inMemoryMCPEventStore };
//# sourceMappingURL=memory-store.js.map