UNPKG

react-service-container

Version:

A simple, robust, idiomatic service locator library for React applications

35 lines (34 loc) 1.45 kB
import React from "react"; export declare type ServiceFor<T> = T extends new (...args: any[]) => infer R ? R : any; export interface UseValueProvider<T> { provide: T; useValue: ServiceFor<T>; } export interface UseClassProvider<T> { provide: T; useClass: new (...args: any) => ServiceFor<T>; } export interface UseFactoryProvider<T> { provide: T; useFactory(): ServiceFor<T>; } export interface UseExistingProvider<T, R = T> { provide: T; useExisting: R; } export declare type Provider<T> = (new (...args: any[]) => any) | UseValueProvider<T> | UseClassProvider<T> | UseFactoryProvider<T> | UseExistingProvider<T>; export declare type Providers = Array<Provider<any>>; export declare class ServiceContainerRegistry { private readonly parent; private providers; constructor(parent: ServiceContainerRegistryReadonlyProxy | null); get<T, R = ServiceFor<T>>(serviceToken: T): R; add<T>(provider: Provider<T>): void; } export declare type ServiceContainerRegistryReadonlyProxy = Pick<ServiceContainerRegistry, "get">; export declare const ServiceContainerContext: React.Context<Pick<ServiceContainerRegistry, "get"> | null>; export declare type ServiceContainerProps = React.PropsWithChildren<{ providers: Providers; }>; export declare function ServiceContainer({ providers, children, }: ServiceContainerProps): JSX.Element; export declare function useService<T, R = ServiceFor<T>>(serviceToken: T): R;