servido
Version:
Versatile services for React ⚗️
61 lines (60 loc) • 6.31 kB
TypeScript
import { Service, ServiceClass, ServiceQuery, ServiceSource, ServiceType } from "./service";
import { ServiceConfig } from "./service-config";
import { ServiceContext } from "./service-context";
import { ServiceDependent } from "./service-dependent";
import { ServiceExecution } from "./service-execution";
import { ModuleThunk } from "./service-fns";
import { Class, ServiceIdentifier } from "./service-types";
/** Check if any of the passed services are currently constructing. */
export declare function isConstructing(...services: (Service | undefined)[]): boolean;
/** Check if the passed service is deconstructed and not in use. */
export declare function isDeconstructed(service: Service): boolean;
/** Returns the execution of a service or service dependent (or defines and returns it if undefined). */
export declare function executionOf(dependent: ServiceDependent): ServiceExecution;
/** Returns the params for the service context. */
export declare function useParams(): servido.Params;
/** Returns the params for the service context of the specified dependent. */
export declare function paramsOf(dependent: ServiceDependent): servido.Params;
export declare function useDependent(name?: string, deps?: readonly any[]): ServiceDependent;
/** Create a query for a service. Useful for typings. */
export declare function serviceQuery<S extends Service>(service: S | ServiceType<S, []>): ServiceQuery<S, [], []>;
export declare function serviceQuery<S extends Service, A extends any[]>(service: S | ServiceType<S, A>, ...args: A): ServiceQuery<S, [], A>;
export declare function serviceQuery<S extends Service, A extends any[], QA extends any[]>(query: ServiceQuery<S, A, QA>, ...args: A): ServiceQuery<S, QA, A>;
/** Create a async query for a service. Useful for typings. */
export declare function asyncServiceQuery<S extends Service, A extends any[]>(service: AsyncServiceQuery<S, A, []>, ...args: A): Promise<ServiceQuery<S, [], A>>;
export declare function asyncServiceQuery<S extends Service>(service: AsyncServiceQuery<S, [], []>): Promise<ServiceQuery<S, [], []>>;
export declare function asyncServiceQuery<S extends Service, A extends any[], QA extends any[]>(query: AsyncServiceQuery<S, A, QA>, ...args: A): Promise<ServiceQuery<S, QA, A>>;
export declare type AsyncServiceQuery<S extends Service, A extends any[] = any[], QA extends any[] = any[]> = ServiceQuery<S, A, QA> | Promise<ModuleThunk<ServiceQuery<S, A, QA>>> | Promise<ModuleThunk<ServiceType<S, A>>> | Promise<ModuleThunk<Class<S, A>>> | Promise<ModuleThunk<ServiceSource<S, A>>> | [Promise<ModuleThunk<ServiceType<S, QA>>>, ...QA] | [Promise<ModuleThunk<Class<S, QA>>>, ...QA] | [Promise<ModuleThunk<ServiceSource<S, QA>>>, ...QA];
/** Returns a function returning a promise that resolves once all of the passed services have been resolved (using `Service.resolve`). */
export declare function useHandler<A extends any[], S extends Service, T>(service: AsyncServiceQuery<S, []> | ((...args: A | []) => AsyncServiceQuery<S, A>), handler: (service: S, ...args: A) => T | Promise<T>): (...args: A) => Promise<T>;
/** Returns a function returning a promise that resolves once all of the passed services have been resolved (using `Service.resolve`). */
export declare function useResolver<A extends any[]>(...services: (AsyncServiceQuery<Service, []> | ((...args: A | []) => AsyncServiceQuery<Service>))[]): (...args: A) => Promise<void>;
/** Returns a function returning a promise that resolves once all of the passed services have been resolved (using `Service.resolve`). */
export declare function useExecutionResolver<A extends any[]>(execution: Pick<ServiceExecution, "done" | "onDone"> | ((...args: A | []) => Pick<ServiceExecution, "done" | "onDone">), ...services: (AsyncServiceQuery<Service, []> | ((...args: A | []) => AsyncServiceQuery<Service>))[]): (...args: A) => Promise<void>;
export declare function parseQuery<S extends Service, A extends any[]>(service: ServiceQuery<S, A>, args?: A): {
class: ServiceClass<S, A>;
service?: S;
args?: A;
};
/** Returns whether the `query` matches the `matchesQuery`. */
export declare function doesQueryMatch(serviceOrContext: Service | ServiceContext, query: ServiceQuery<Service>, matchesQuery: ServiceQuery<Service>): boolean;
export declare function identifierOf<S extends Service>(service: S): ServiceIdentifier | undefined;
export declare function identifierFor<A extends any[]>(context: ServiceContext, query: ServiceQuery<Service, A>, args?: A): ServiceIdentifier | undefined;
export declare function identifierFor<A extends any[]>(contextOf: Service, query: ServiceQuery<Service, A>, args?: A): ServiceIdentifier | undefined;
export declare function contextOf<S extends ServiceDependent>(service: S): ServiceContext;
export declare function configOf<S extends Service>(service: S): ServiceConfig<DataOf<S>> | undefined;
export declare function configOf<DT>(service: Service): ServiceConfig<DT> | undefined;
/** Returns whether the context of the specified service contains an active instance of the specified service query. */
export declare function hasInstance<A extends any[]>(context: ServiceContext, query: ServiceQuery<Service, A>, args?: A): boolean;
export declare function hasInstance<A extends any[]>(contextOf: ServiceDependent, query: ServiceQuery<Service, A>, args?: A): boolean;
export declare function configure<DT = any>(service: Service, config: ServiceConfig<DT>): void;
/** Requires services until the handler has finished. */
export declare function handle<RT, RS extends Service>(dependent: ServiceDependent | ServiceContext, requirement: ServiceQuery<RS>, handler: (service: RS, execution: ServiceExecution) => RT | PromiseLike<RT>, execution?: ServiceExecution): Promise<RT>;
export declare function handleSync<RT, RS extends Service>(dependent: ServiceDependent | ServiceContext, requirement: ServiceQuery<RS>, handler: (service: RS) => RT, execution?: ServiceExecution): RT;
export declare function isQuery<T extends Service, A extends any[]>(query: any): query is ServiceQuery<T, A>;
declare type DataOf<S extends Service> = S extends {
getServiceConfig(): ServiceConfig<infer T>;
} ? T : S extends {
serviceConfig: ServiceConfig<infer T>;
} ? T : any;
export {};