/** * Type representing a constructor for a context. * * It defines a constructor signature for a context with a given set of constructor properties. * @typeParam ConstructorProps - The type of the constructor properties, defaulting to a string-indexed object. */ export type ContextConstructor = new (contextManager: ContextManager, props: ConstructorProps) => Context; /** * Type representing the keys of an object that are required. * * This type extracts the keys from an object type where the corresponding properties are not optional. * @typeParam T - The object type to analyze for required keys. */ type RequiredKeys = { [K in keyof T]-?: {} extends Pick ? never : K; }[keyof T]; /** * Type representing the constructor properties of a given type. * * It extracts the constructor properties from a type that has a constructor signature. * @typeParam T - The type with a constructor signature to extract the properties from. */ type ConstructorProps = T extends { new (contextManager: ContextManager, props: infer U): any; } ? U : never; /** * Type representing a constructor where all properties are optional. * * This type filters for constructors where all the constructor properties are optional. * @typeParam T - The constructor type to filter. */ type ConstructorWithOnlyOptional = RequiredKeys> extends never ? T : never; /** * Type representing a constructor with its associated properties. * * This type creates a tuple consisting of a constructor type and its corresponding properties. * @typeParam T - The type of the context constructor. */ type ConstructorWithProps = [T, ConstructorParameters[1]]; /** * Type representing instances of a list of constructors with properties. * * It maps an array of constructors with properties to their corresponding instance types. * @typeParam T - An array type of `ConstructorWithProps`. */ type InstancesOfConstructorsWithProps> = { [K in keyof T]: InstanceType; }; /** * The base class for all contexts. * * Contexts are used to manage and maintain state and functionality that can be shared across different components and behaviors. * @typeParam ConstructorPropsType - The type of the constructor properties, defaulting to an empty object. */ export declare class Context { contextManager: ContextManager; constructorProps: ConstructorPropsType; disposed: boolean; /** * Creates an instance of Context. * @param contextManager The current ContextManager * @param constructorProps - The constructor properties. */ constructor(contextManager: ContextManager, constructorProps: ConstructorPropsType); dispose(): never; private static callSuperDispose; } /** * Class for managing contexts. * * This class provides functionality to create, retrieve, and manage contexts. * * The `ContextManager` is also used to create components, behaviors, and other objects that require a context. */ export declare class ContextManager { private _byKey; private _root; /** * Creates a new instance of `ContextManager`. * * @typeParam T - The type of the context to initialize. * @param ctx - The constructor of the context to initialize. * @param props - Optional properties to initialize the context. * @returns A new instance of `ContextManager`. */ static create any>(ctx: ConstructorWithOnlyOptional): ContextManager; static create any>(ctx: T, props: ConstructorParameters[1]): ContextManager; /** * Retrieves an existing context instance or creates a new one if it does not exist. * * @typeParam T - The type of the context to retrieve or create. * @param ctx - The constructor of the context to retrieve or create. * @param props - Optional properties to initialize the context if it needs to be created. * @returns The retrieved or newly created context instance. */ get any>(ctx: ConstructorWithOnlyOptional): InstanceType; get any>(ctx: T, props: ConstructorParameters[1]): InstanceType; /** * Retrieves an existing context instance, or returns undefined if it does not exist. * * @typeParam T - The type of the context to retrieve. * @param ctx - The constructor of the context to retrieve. * @returns The retrieved context instance or undefined. */ getExisting any>(ctx: T): InstanceType | undefined; /** * Retrieves an existing context or throws an error if it does not exist. * * @typeParam T - The type of the context to retrieve. * @param ctx - The constructor of the context to retrieve. * @returns The retrieved context instance. * @throws Error if the required context is not present. */ getOrThrow any>(ctx: T): InstanceType; /** * Adds a context to the `ContextManager`. * * @typeParam T - The type of the context to add. * @param ctx - The constructor of the context to add. * @param props - Properties to initialize the context. * @returns The newly added context instance. */ add any>(ctx: ConstructorWithOnlyOptional): InstanceType; add any>(ctx: T, props: ConstructorParameters[1]): InstanceType; /** * Creates a fork of the `ContextManager` with a specific context added. * * @typeParam T - The type of the context to fork. * @param ctx - The constructor of the context to add. * @param props - Optional properties to initialize the context. * @returns A tuple containing the forked `ContextManager` and the newly added context instance. */ fork any>(ctx: ConstructorWithOnlyOptional): [ContextManager, InstanceType]; fork any>(ctx: T, props: ConstructorParameters[1]): [ContextManager, InstanceType]; /** * Factory function for creating new instances of a given context type, along with associated properties. * * @typeParam T - An array type representing a combination of a constructor and its corresponding properties. * @returns An array where each element is a tuple, with the first element being the context manager and the second being the instance created by the constructor. */ forkMultiple(...ctx: T): [ContextManager, InstancesOfConstructorsWithProps]; /** * Creates a new forked `ContextManager` instance with a shallow copy of the current context. * * @returns A new `ContextManager` instance forked from the current context. */ emptyFork(): ContextManager; /** * Deletes a context from the manager. * * @param ctx - The constructor of the context to delete. */ delete(ctx: ContextConstructor): void; private _keyForContext; } export {};