UNPKG

get-svelte

Version:

A simple, powerful state management library for Svelte applications, inspired by GetX for Flutter

82 lines (81 loc) 3.59 kB
import GetxController from "./GetxController"; import GetxControllerID from "./GetxControllerID"; /** * Interface for optional parameters when registering a controller with the Get system */ export interface GetxControllerIDParams { tag: string; } /** * Main service class for controller dependency injection and management. * Uses request-scoped storage in SSR environments to prevent cross-request contamination. */ export default class Get { /** Registry of all controllers currently managed by the Get system */ static controllerIDS: GetxControllerID[]; /** * Generates a unique random ID for a controller * @returns {string} A randomly generated unique identifier */ static generateRandomControllerID(): string; /** * Checks if a controller of the given type and optional tag is already registered * @template T - Type extending GetxController * @param {new (...args: any[]) => T} ControllerClass - Constructor of the controller class to check * @param {{tag?: string}} params - Optional params containing a tag * @returns {boolean} True if a matching controller is registered, false otherwise */ static isRegistered<T extends GetxController>(ControllerClass: new (...args: any[]) => T, { tag }?: { tag?: string; }): boolean; /** * Registers a controller instance with the Get system * If a controller of the same type and tag already exists, returns the existing instance * @template T - Type extending GetxController * @param {T} controller - The controller instance to register * @param {GetxControllerIDParams} [params] - Optional parameters including tag * @returns {T} The registered controller instance */ static put<T extends GetxController>(controller: T, params?: GetxControllerIDParams): T; /** * Finds and returns a registered controller of the specified type and optional tag * @template T - Type extending GetxController * @param {new (...args: any[]) => T} ControllerClass - Constructor of the controller class to find * @param {{tag?: string}} params - Optional params containing a tag * @returns {T} The found controller instance * @throws {Error} If no matching controller is found */ static find<T extends GetxController>(ControllerClass: new (...args: any[]) => T, { tag }?: { tag?: string; }): T; /** * Removes a controller of the specified type and optional tag from the registry * @template T - Type extending GetxController * @param {new (...args: any[]) => T} ControllerClass - Constructor of the controller class to delete * @param {{tag?: string}} params - Optional params containing a tag */ static delete<T extends GetxController>(ControllerClass: new (...args: any[]) => T, { tag }?: { tag?: string; }): void; /** * Removes a controller with the specified ID from the registry * @param {string} id - The unique ID of the controller to delete * @private */ static _deleteById(id: string): void; /** * Removes all controllers from the registry */ static deleteAll(): void; /** * For SSR: Call this method at the beginning of each request handler * to clear controllers from previous requests and prevent cross-user contamination * * Example usage in SvelteKit hooks.server.ts: * export async function handle({ event, resolve }) { * Get.clearForSSR(); * return await resolve(event); * } */ static clearForSSR(): void; }