Skip to main content
Glama
useLoadDynamic.ts1.32 kB
import { signal } from '@angular/core'; /** * A "synchronous" loader for a dynamically‐imported JSON (or anything). * * - Immediately returns a reactive signal so that Angular can properly track changes. * - When the Promise resolves, it replaces the signal's value with the real data. */ export const useLoadDynamic = <T extends Record<string, any>>( key: string, promise: Promise<T> ): T => { // A module‐level cache of Promises, so we only import once per key. const cache: Map<string, Promise<T>> = (useLoadDynamic as any)._cache || ((useLoadDynamic as any)._cache = new Map()); // Hold the "current" value as a reactive signal // This starts as an empty object but Angular can track changes to it const container = signal({} as T); if (!cache.has(key)) { // Kick off the dynamic import & cache it const p = promise.then((real) => { // As soon as the import resolves, update the signal with the real data container.set(real); return real; }); cache.set(key, p); } else { // If it's already in flight (or done), hook into it so that the container still updates cache.get(key)?.then((real) => { container.set(real); }); } // Return the current signal value directly - Angular can track all changes to it return container(); };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/aymericzip/intlayer'

If you have feedback or need assistance with the MCP directory API, please join our Discord server