nrgy
Version:
The library for reactive programming using efficient computing and MVC/MVVM patterns
227 lines (224 loc) • 8.89 kB
TypeScript
import { S as Signal } from './types-CxOJPpKX.js';
import { a as Scope } from './types-jkBBq3Lp.js';
/**
* Base service type which is implemented by controllers
*/
type BaseService = Record<string, any> | ((...args: any[]) => any);
/**
* Effects and business logic controller.
*
* Implementation of the controller must provide `destroy()` method. It should
* be used for closing subscriptions and disposing resources.
*
* @example
* ```ts
* type LoggerController = Controller<{
* log: (message: string) => void;
* }>;
* ```
*/
type Controller<TService extends BaseService = BaseService> = TService & {
/** Dispose the controller and clean its resources */
destroy: () => void;
};
/**
* @internal
*/
type PartialController<TService extends BaseService> = TService & {
/** Dispose the controller and clean its resources */
destroy?: () => void;
};
/**
* Parameters for a controller
*/
type ControllerParams = Record<string, unknown>;
/**
* Base context for a controller
*/
type BaseControllerContext = {
scope: Scope;
params?: unknown;
};
/**
* Context with parameters for a controller
*/
type ControllerParamsContext<TParams extends ControllerParams> = BaseControllerContext & {
params: TParams;
};
/**
* An error thrown when a controller is failed to be created
*/
declare class ControllerConstructorError extends Error {
constructor(message: string);
}
/**
* Parameters for an extension
*/
type ExtensionParams = Record<string, any>;
/**
* Extension function for a controller
*
* It extends a controller context with additional data
*
* @param sourceContext The source context
* @param extensionParams Additional parameters for the extension
*
* @returns The extended context
*/
type ExtensionFn<TSourceContext extends BaseControllerContext, TContextExtension extends BaseControllerContext> = (sourceContext: TSourceContext, extensionParams?: ExtensionParams) => TSourceContext & TContextExtension;
/**
* Provider for arbitrary extension parameters
*
* It extends extension parameters with additional data and returns them.
* An extension can use this data during creation of a controller context.
*/
type ExtensionParamsProvider = (params: ExtensionParams) => ExtensionParams;
/**
* Base declaration for a controller
*/
type ControllerDeclaration<TContext extends BaseControllerContext, TService extends BaseService> = TContext extends ControllerParamsContext<infer TParams> ? {
/** @internal Keep the type for inference */
readonly __contextType?: TContext;
/** @internal Keep the type for inference */
readonly __paramsType?: TParams;
/** @internal Keep the type for inference */
readonly __serviceType?: TService;
/**
* Creates a new controller with the given parameters
*/
new (params: TParams): Controller<TService>;
/**
* Creates a new controller with the given providers
*/
new (providers: ReadonlyArray<ExtensionParamsProvider>): Controller<TService>;
} : {
/** @internal Keep the type for inference */
readonly __contextType?: TContext;
/** @internal Keep the type for inference */
readonly __serviceType?: TService;
/**
* Creates a new controller
*/
new (): Controller<TService>;
/**
* Creates a new controller with the given providers
*/
new (providers: ReadonlyArray<ExtensionParamsProvider>): Controller<TService>;
};
/**
* @internal
*
* Base class for controllers
*/
declare abstract class BaseController<TContext extends BaseControllerContext> {
protected readonly context: ControllerContext<TContext>;
protected readonly scope: Scope;
protected readonly params: TContext['params'];
protected readonly onCreateSignal: Signal<void>;
protected readonly onDestroySignal: Signal<void>;
protected constructor(paramsOrProviders?: TContext['params'] | ReadonlyArray<ExtensionParamsProvider>, extensions?: ReadonlyArray<ExtensionFn<any, any>>);
/**
* Called when the controller is created.
*
* This callback is called in a next microtask as soon as the controller is created.
*/
protected onCreated(): void;
/**
* Called when the controller is destroyed synchronously.
*/
protected onDestroy(): void;
/**
* Destroys the controller
*/
destroy(): void;
}
/**
* Base class declaration for controllers
*/
type ControllerClassDeclaration<TContext extends BaseControllerContext> = TContext extends ControllerParamsContext<infer TParams> ? {
/** @internal Keep the type for inference */
readonly __contextType?: TContext;
/** @internal Keep the type for inference */
readonly __paramsType?: TParams;
/**
* Creates a new controller with the given parameters
*/
new (params: TParams): BaseController<TContext>;
/**
* Creates a new controller with the given providers
*/
new (providers: ReadonlyArray<ExtensionParamsProvider>): BaseController<TContext>;
} : {
/** @internal Keep the type for inference */
readonly __contextType?: TContext;
/**
* Creates a new controller
*/
new (): BaseController<TContext>;
/**
* Creates a new controller with the given providers
*/
new (providers: ReadonlyArray<ExtensionParamsProvider>): BaseController<TContext>;
};
/**
* Utility type to infer the params type from a controller
*/
type InferContextParams<TContext extends BaseControllerContext, ElseType> = TContext extends ControllerParamsContext<infer InferredParams> ? InferredParams : ElseType;
type ControllerContext<TContext extends BaseControllerContext> = TContext & {
create<TContext extends BaseControllerContext, TService extends BaseService>(declaration: ControllerDeclaration<TContext, TService>): Controller<TService>;
create<TContext extends BaseControllerContext, TService extends BaseService, TParams extends InferContextParams<TContext, never>>(declaration: ControllerDeclaration<TContext, TService>, params: TParams): Controller<TService>;
};
/**
* Factory function for a controller
*/
type ControllerFactory<TContext extends BaseControllerContext, TService extends BaseService> = (context: ControllerContext<TContext>) => PartialController<TService> | undefined | void;
/**
* @internal
*
* Builder for controller declarations
*/
declare class ControllerDeclarationBuilder<TContext extends BaseControllerContext> {
/**
* Extensions that should be applied to the controller
*/
readonly extensions: Array<ExtensionFn<any, any>>;
/**
* Creates a new builder
*
* @param extensions Extensions that should be applied to the controller
*/
constructor(extensions?: Array<ExtensionFn<any, any>>);
/**
* Declares the controller with the given parameters
*/
params<TParams extends ControllerParams>(): ControllerDeclarationBuilder<TContext & ControllerParamsContext<TParams>>;
/**
* Declares the controller with the given extension
*/
extend<TResultContext extends TContext>(extension: ExtensionFn<TContext, TResultContext>): ControllerDeclarationBuilder<TResultContext>;
/**
* Creates the controller declaration using declared context and the given factory
*/
apply<TService extends BaseService>(factory: ControllerFactory<TContext, TService>): ControllerDeclaration<TContext, TService>;
/**
* Returns the base class for the controller using the declared context
*/
getBaseClass(): ControllerClassDeclaration<TContext>;
}
/**
* Returns a new controller declaration using the given factory
*/
declare function declareController<TService extends BaseService>(factory: ControllerFactory<BaseControllerContext, TService>): ControllerDeclaration<BaseControllerContext, TService>;
/**
* Returns the builder of controller declaration
*/
declare function declareController(): ControllerDeclarationBuilder<BaseControllerContext>;
/**
* Provides the controller parameters
*/
declare function provideControllerParams<TDeclaration extends ControllerDeclaration<any, any>, TContext extends TDeclaration extends ControllerDeclaration<infer InferredContext, any> ? InferredContext : never>(params: TContext['params']): ExtensionParamsProvider;
/**
* Provides arbitrary extension parameters
*/
declare function provideExtensionParams(params: ExtensionParams): ExtensionParamsProvider;
export { type BaseControllerContext as B, type Controller as C, type ExtensionFn as E, type BaseService as a, type ControllerContext as b, type ControllerDeclaration as c, type ControllerClassDeclaration as d, type ControllerFactory as e, type ControllerParams as f, type ControllerParamsContext as g, type ExtensionParams as h, type ExtensionParamsProvider as i, ControllerConstructorError as j, declareController as k, provideControllerParams as l, BaseController as m, provideExtensionParams as p };