dependencyCollection.d.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { DependencyIdentifier } from './dependencyIdentifier';
  2. import { Ctor, DependencyItem } from './dependencyItem';
  3. import { IDisposable } from './dispose';
  4. import { Quantity } from './types';
  5. import { RediError } from './error';
  6. export type DependencyPair<T> = [DependencyIdentifier<T>, DependencyItem<T>];
  7. export type DependencyClass<T> = [Ctor<T>];
  8. export type Dependency<T = any> = DependencyPair<T> | DependencyClass<T>;
  9. export type DependencyWithInstance<T = any> = [
  10. Ctor<T> | DependencyIdentifier<T>,
  11. T
  12. ];
  13. export type DependencyOrInstance<T = any> = Dependency<T> | DependencyWithInstance<T>;
  14. export declare function isBareClassDependency<T>(thing: Dependency<T>): thing is DependencyClass<T>;
  15. export declare function pushResolvingStack(id: DependencyIdentifier<unknown>): void;
  16. export declare function popupResolvingStack(): void;
  17. export declare function clearResolvingStack(): void;
  18. export declare class DependencyNotFoundForModuleError extends RediError {
  19. constructor(toInstantiate: Ctor<any> | DependencyIdentifier<any>, id: DependencyIdentifier<any>, index: number);
  20. }
  21. export declare class DependencyNotFoundError extends RediError {
  22. constructor(id: DependencyIdentifier<any>);
  23. }
  24. /**
  25. * Store unresolved dependencies in an injector.
  26. *
  27. * @internal
  28. */
  29. export declare class DependencyCollection implements IDisposable {
  30. private readonly dependencyMap;
  31. constructor(dependencies: Dependency[]);
  32. add<T>(ctor: Ctor<T>): void;
  33. add<T>(id: DependencyIdentifier<T>, val: DependencyItem<T>): void;
  34. delete<T>(id: DependencyIdentifier<T>): void;
  35. get<T>(id: DependencyIdentifier<T>): DependencyItem<T>;
  36. get<T>(id: DependencyIdentifier<T>, quantity: Quantity.REQUIRED): DependencyItem<T>;
  37. get<T>(id: DependencyIdentifier<T>, quantity: Quantity.MANY): DependencyItem<T>[];
  38. get<T>(id: DependencyIdentifier<T>, quantity: Quantity.OPTIONAL): DependencyItem<T> | null;
  39. get<T>(id: DependencyIdentifier<T>, quantity: Quantity): DependencyItem<T> | DependencyItem<T>[] | null;
  40. has<T>(id: DependencyIdentifier<T>): boolean;
  41. append(dependencies: Dependency<any>[]): void;
  42. dispose(): void;
  43. /**
  44. * normalize dependencies to `DependencyItem`
  45. */
  46. private normalizeDependencies;
  47. }
  48. /**
  49. * Store resolved dependencies.
  50. *
  51. * @internal
  52. */
  53. export declare class ResolvedDependencyCollection implements IDisposable {
  54. private readonly resolvedDependencies;
  55. add<T>(id: DependencyIdentifier<T>, val: T | null): void;
  56. has<T>(id: DependencyIdentifier<T>): boolean;
  57. delete<T>(id: DependencyIdentifier<T>): void;
  58. get<T>(id: DependencyIdentifier<T>): T;
  59. get<T>(id: DependencyIdentifier<T>, quantity: Quantity.OPTIONAL): T | null;
  60. get<T>(id: DependencyIdentifier<T>, quantity: Quantity.REQUIRED): T;
  61. get<T>(id: DependencyIdentifier<T>, quantity: Quantity.MANY): T[];
  62. get<T>(id: DependencyIdentifier<T>, quantity: Quantity): T[] | T | null;
  63. dispose(): void;
  64. }