injector.d.ts 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { Dependency, DependencyOrInstance } from './dependencyCollection';
  2. import { DependencyIdentifier } from './dependencyIdentifier';
  3. import { LookUp, Quantity } from './types';
  4. export interface IAccessor {
  5. get: Injector['get'];
  6. }
  7. /**
  8. *
  9. */
  10. export declare class Injector {
  11. private readonly parent;
  12. private readonly dependencyCollection;
  13. private readonly resolvedDependencyCollection;
  14. private readonly children;
  15. private resolutionOngoing;
  16. private disposed;
  17. /**
  18. * Create a new `Injector` instance
  19. * @param dependencies Dependencies that should be resolved by this injector instance.
  20. * @param parent Optional parent injector.
  21. */
  22. constructor(dependencies?: Dependency[], parent?: Injector | null);
  23. /**
  24. * Create a child inject with a set of dependencies.
  25. * @param dependencies Dependencies that should be resolved by the newly created child injector.
  26. * @returns The child injector.
  27. */
  28. createChild(dependencies?: Dependency[]): Injector;
  29. /**
  30. * Dispose the injector and all dependencies held by this injector. Note that its child injectors will dispose first.
  31. */
  32. dispose(): void;
  33. private deleteSelfFromParent;
  34. /**
  35. * Add a dependency or its instance into injector. It would throw an error if the dependency
  36. * has already been instantiated.
  37. *
  38. * @param dependency The dependency or an instance that would be add in the injector.
  39. */
  40. add<T>(dependency: DependencyOrInstance<T>): void;
  41. /**
  42. * Replace an injection mapping for interface-based injection. It would throw an error if the dependency
  43. * has already been instantiated.
  44. *
  45. * @param dependency The dependency that will replace the already existed dependency.
  46. */
  47. replace<T>(dependency: Dependency<T>): void;
  48. /**
  49. * Delete a dependency from an injector. It would throw an error when the deleted dependency
  50. * has already been instantiated.
  51. *
  52. * @param identifier The identifier of the dependency that is supposed to be deleted.
  53. */
  54. delete<T>(identifier: DependencyIdentifier<T>): void;
  55. /**
  56. * Invoke a function with dependencies injected. The function could only get dependency from the injector
  57. * and other methods are not accessible for the function.
  58. *
  59. * @param cb the function to be executed
  60. * @param args arguments to be passed into the function
  61. * @returns the return value of the function
  62. */
  63. invoke<T, P extends any[] = []>(cb: (accessor: IAccessor, ...args: P) => T, ...args: P): T;
  64. /**
  65. * Check if the injector could initialize a dependency.
  66. *
  67. * @param id Identifier of the dependency
  68. */
  69. has<T>(id: DependencyIdentifier<T>): boolean;
  70. get<T>(id: DependencyIdentifier<T>, lookUp?: LookUp): T;
  71. get<T>(id: DependencyIdentifier<T>, quantity: Quantity.MANY, lookUp?: LookUp): T[];
  72. get<T>(id: DependencyIdentifier<T>, quantity: Quantity.OPTIONAL, lookUp?: LookUp): T | null;
  73. get<T>(id: DependencyIdentifier<T>, quantity: Quantity.REQUIRED, lookUp?: LookUp): T;
  74. get<T>(id: DependencyIdentifier<T>, quantity?: Quantity, lookUp?: LookUp): T[] | T | null;
  75. get<T>(id: DependencyIdentifier<T>, quantityOrLookup?: Quantity | LookUp, lookUp?: LookUp): T[] | T | null;
  76. private _get;
  77. /**
  78. * Get a dependency in the async way.
  79. */
  80. getAsync<T>(id: DependencyIdentifier<T>): Promise<T>;
  81. /**
  82. * Instantiate a class. The created instance would not be held by the injector.
  83. */
  84. createInstance<T extends unknown[], U extends unknown[], C>(ctor: new (...args: [...T, ...U]) => C, ...customArgs: T): C;
  85. private _resolveDependency;
  86. private _resolveExisting;
  87. private _resolveValueDependency;
  88. private _resolveClass;
  89. private _resolveClassImpl;
  90. private _resolveFactory;
  91. private _resolveAsync;
  92. private _resolveAsyncImpl;
  93. private getValue;
  94. private createDependency;
  95. private markNewResolution;
  96. private markResolutionCompleted;
  97. private _ensureInjectorNotDisposed;
  98. }