/** * HMR-Safe Singleton Helper * * Creates singletons that persist across Next.js Hot Module Replacement (HMR). * Uses globalThis to store state so it survives code reloads in development. * * @example Core initialization * ```typescript * import { createHmrSafeSingleton } from '@plyaz/core/utils'; * import { Core, backendConfig } from '@plyaz/core/backend'; * * export const getServerCore = createHmrSafeSingleton('core', async () => { * await Core.initialize(backendConfig); * return Core; * }); * * // Usage in API routes * export async function GET() { * const core = await getServerCore(); * // core.db, core.cache, etc. * } * ``` * * @example Database connection * ```typescript * import { createHmrSafeSingleton } from '@plyaz/core/utils'; * import { drizzle } from 'drizzle-orm/postgres-js'; * import postgres from 'postgres'; * * export const getDb = createHmrSafeSingleton('db', async () => { * const client = postgres(process.env.DATABASE_URL); * return drizzle(client); * }); * ``` * * @example Cache client * ```typescript * import { createHmrSafeSingleton } from '@plyaz/core/utils'; * import Redis from 'ioredis'; * * export const getRedis = createHmrSafeSingleton('redis', async () => { * return new Redis(process.env.REDIS_URL); * }); * ``` */ import type { HmrSingletonOptions, HmrSingletonAccessor } from '@plyaz/types/core'; export type { HmrSingletonOptions, HmrSingletonAccessor } from '@plyaz/types/core'; /** * Create an HMR-safe singleton accessor. * * The singleton will persist across Next.js Hot Module Replacement in development, * preventing re-initialization on every code change. * * @param key - Unique key for this singleton (e.g., 'core', 'db', 'redis') * @param initializer - Async function that creates the singleton instance * @param options - Optional callbacks for lifecycle events * @returns Accessor function and utilities * * @example * ```typescript * import { createHmrSafeSingleton } from '@plyaz/core/utils'; * * const getCore = createHmrSafeSingleton('core', async () => { * await Core.initialize(config); * return Core; * }); * * // In your API route or server component: * const core = await getCore(); * * // Check if initialized: * if (getCore.isInitialized()) { * // Already initialized * } * * // Reset (for testing): * await getCore.reset(); * ``` */ export declare function createHmrSafeSingleton(key: string, initializer: () => Promise, options?: HmrSingletonOptions): HmrSingletonAccessor; //# sourceMappingURL=hmr-singleton.d.ts.map