Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(core): fix cycle in DI

Wenzhao Hue7287e4abeb4
diff --git a/packages/core/src/DI/Decorators.ts b/packages/core/src/DI/Decorators.ts
index 465b461d8b..1465376321 100644
--- a/packages/core/src/DI/Decorators.ts
+++ b/packages/core/src/DI/Decorators.ts
@@ -1,16 +1,301 @@
-import { DependencyDescriptor } from './DependencyDescriptor';
import {
DependencyIdentifier,
IdentifierDecorator,
IdentifierDecoratorSymbol,
+ prettyPrintIdentifier,
} from './DependencyIdentifier';
-import { Ctor, prettyPrintIdentifier } from './DependencyItem';
-import { LookUp, Quantity } from './Types';
+import { Ctor, LookUp, Quantity } from './Types';
import { DIError } from './Error';
+export interface DependencyItemHooks<T> {
+ onInstantiation?: (instance: T) => void;
+}
+
+export interface ClassDependencyItem<T> extends DependencyItemHooks<T> {
+ useClass: Ctor<T>;
+ lazy?: boolean;
+}
+
+export function isClassDependencyItem<T>(
+ thing: unknown
+): thing is ClassDependencyItem<T> {
+ if (thing && typeof (thing as any).useClass !== 'undefined') {
+ return true;
+ }
+
+ return false;
+}
+
+export type FactoryDepModifier =
+ | typeof Self
+ | typeof SkipSelf
+ | typeof Optional
+ | typeof Many
+ | typeof WithNew;
+
+export type FactoryDep<T> =
+ | [...FactoryDepModifier[], DependencyIdentifier<T>]
+ | DependencyIdentifier<T>;
+
+export interface FactoryDependencyItem<T> extends DependencyItemHooks<T> {
+ useFactory: (...deps: any[]) => T;
+ dynamic?: true;
+ deps?: Array<FactoryDep<any>>;
+}
+
+export function isFactoryDependencyItem<T>(
+ thing: unknown
+): thing is FactoryDependencyItem<T> {
+ if (thing && typeof (thing as any).useFactory !== 'undefined') {
+ return true;
+ }
+
+ return false;
+}
+
+export interface ValueDependencyItem<T> extends DependencyItemHooks<T> {
+ useValue: T;
+}
+
+export function isValueDependencyItem<T>(
+ thing: unknown
+): thing is ValueDependencyItem<T> {
+ if (thing && typeof (thing as any).useValue !== 'undefined') {
+ return true;
+ }
+
+ return false;
+}
+
+export interface AsyncDependencyItem<T> extends DependencyItemHooks<T> {
+ useAsync: () => Promise<
+ T | Ctor<T> | [DependencyIdentifier<T>, SyncDependencyItem<T>]
+ >;
+}
+
+export function isAsyncDependencyItem<T>(
+ thing: unknown
+): thing is AsyncDependencyItem<T> {
+ if (thing && typeof (thing as any).useAsync !== 'undefined') {
+ return true;
+ }
+
+ return false;
+}
+
+export interface AsyncHook<T> {
+ whenReady(): Promise<T>;
+}
+
+export function isAsyncHook<T>(thing: unknown): thing is AsyncHook<T> {
+ if (thing && typeof (thing as any).whenReady !== 'undefined') {
+ return true;
+ }
+
+ return false;
+}
+
+export type SyncDependencyItem<T> =
+ | ClassDependencyItem<T>
+ | FactoryDependencyItem<T>
+ | ValueDependencyItem<T>;
+
+export type DependencyItem<T> = SyncDependencyItem<T> | AsyncDependencyItem<T>;
+
export const TARGET = Symbol('$$TARGET');
export const DEPENDENCIES = Symbol('$$DEPENDENCIES');
+export interface DependencyDescriptor<T> {
+ paramIndex: number;
+ identifier: DependencyIdentifier<T>;
+ quantity: Quantity;
+ lookUp?: LookUp;
+ withNew: boolean;
+}
+
+/**
+ * describes dependencies of a IDependencyItem
+ */
+export interface Dependencies {
+ dependencies: Array<DependencyDescriptor<any>>;
+}
+
+export function normalizeFactoryDeps(
+ deps?: Array<FactoryDep<any>>
+): Array<DependencyDescriptor<any>> {
+ if (!deps) {
+ return [];
+ }
+
+ return deps.map((dep, index) => {
+ if (!Array.isArray(dep)) {
+ return {
+ paramIndex: index,
+ identifier: dep,
+ quantity: Quantity.REQUIRED,
+ withNew: false,
+ };
+ }
+
+ const modifiers = dep.slice(0, dep.length - 1) as FactoryDepModifier[];
+ const identifier = dep[dep.length - 1] as DependencyIdentifier<any>;
+
+ let lookUp: LookUp | undefined;
+ let quantity = Quantity.REQUIRED;
+ let withNew = false;
+
+ (modifiers as FactoryDepModifier[]).forEach(
+ (modifier: FactoryDepModifier) => {
+ if (modifier instanceof Self) {
+ lookUp = LookUp.SELF;
+ } else if (modifier instanceof SkipSelf) {
+ lookUp = LookUp.SKIP_SELF;
+ } else if (modifier instanceof Optional) {
+ quantity = Quantity.OPTIONAL;
+ } else if (modifier instanceof Many) {
+ quantity = Quantity.MANY;
+ } else if (modifier instanceof WithNew) {
+ withNew = true;
+ } else {
+ throw new DIError(`unknown dep modifier ${modifier}.`);
+ }
+ }
+ );
+
+ return {
+ paramIndex: index,
+ identifier: identifier as DependencyIdentifier<any>,
+ quantity,
+ lookUp,
+ withNew,
+ };
+ });
+}
+
+function changeLookup(target: Ctor<any>, index: number, lookUp: LookUp) {
+ const descriptor = getDependencyByIndex(target, index);
+ descriptor.lookUp = lookUp;
+}
+
+function lookupDecoratorFactoryProducer(lookUp: LookUp) {
+ return function DecoratorFactory<T>(this: any) {
+ if (this instanceof DecoratorFactory) {
+ return this;
+ }
+
+ return function d(target: Ctor<T>, _key: string, index: number) {
+ changeLookup(target, index, lookUp);
+ };
+ } as any;
+}
+
+interface SkipSelfDecorator {
+ (): any;
+ // eslint-disable-next-line @typescript-eslint/no-misused-new
+ new (): SkipSelfDecorator;
+}
+/**
+ * when resolving this dependency, skip the current injector
+ */
+export const SkipSelf: SkipSelfDecorator = lookupDecoratorFactoryProducer(
+ LookUp.SKIP_SELF
+);
+
+interface SelfDecorator {
+ (): any;
+ // eslint-disable-next-line @typescript-eslint/no-misused-new
+ new (): SelfDecorator;
+}
+/**
+ * when resolving this dependency, only search the current injector
+ */
+export const Self: SelfDecorator = lookupDecoratorFactoryProducer(LookUp.SELF);
+
+class QuantityCheckError extends DIError {
+ constructor(id: DependencyIdentifier<any>, quantity: Quantity, actual: number) {
+ const msg = `Expect "${quantity}" dependency items for id "${prettyPrintIdentifier(
+ id
+ )}" but get ${actual}.`;
+
+ super(msg);
+ }
+}
+
+export function checkQuantity(
+ id: DependencyIdentifier<any>,
+ quantity: Quantity,
+ length: number
+): void {
+ if (
+ (quantity === Quantity.OPTIONAL && length > 1) ||
+ (quantity === Quantity.REQUIRED && length !== 1)
+ ) {
+ throw new QuantityCheckError(id, quantity, length);
+ }
+}
+
+export function retrieveQuantity<T>(quantity: Quantity, arr: T[]): T[] | T {
+ if (quantity === Quantity.MANY) {
+ return arr;
+ }
+ return arr[0];
+}
+
+function changeQuantity(target: Ctor<any>, index: number, quantity: Quantity) {
+ const descriptor = getDependencyByIndex(target, index);
+ descriptor.quantity = quantity;
+}
+
+function quantifyDecoratorFactoryProducer(quantity: Quantity) {
+ return function decoratorFactory<T>(
+ // typescript would remove `this` after transpilation
+ // this line just declare the type of `this`
+ this: any,
+ id?: DependencyIdentifier<T>
+ ) {
+ if (this instanceof decoratorFactory) {
+ return this;
+ }
+
+ return function d(registerTarget: Ctor<T>, _key: string, index: number) {
+ if (id) {
+ setDependency(registerTarget, id, index, quantity);
+ } else {
+ if (quantity === Quantity.REQUIRED) {
+ throw new IdentifierUndefinedError(registerTarget, index);
+ }
+
+ changeQuantity(registerTarget, index, quantity);
+ }
+ };
+ } as any;
+}
+
+interface ManyDecorator {
+ (id?: DependencyIdentifier<any>): any;
+ // eslint-disable-next-line @typescript-eslint/no-misused-new
+ new (): ManyDecorator;
+}
+export const Many: ManyDecorator = quantifyDecoratorFactoryProducer(Quantity.MANY);
+
+interface OptionalDecorator {
+ (id?: DependencyIdentifier<any>): any;
+ // eslint-disable-next-line @typescript-eslint/no-misused-new
+ new (): OptionalDecorator;
+}
+export const Optional: OptionalDecorator = quantifyDecoratorFactoryProducer(
+ Quantity.OPTIONAL
+);
+
+interface InjectDecorator {
+ (id: DependencyIdentifier<any>): any;
+ // eslint-disable-next-line @typescript-eslint/no-misused-new
+ new (): InjectDecorator;
+}
+export const Inject: InjectDecorator = quantifyDecoratorFactoryProducer(
+ Quantity.REQUIRED
+);
+
class DependencyDescriptorNotFoundError extends DIError {
constructor(index: number, target: Ctor<any>) {
const msg = `Could not find dependency registered on the ${
@@ -132,3 +417,31 @@ export function createIdentifier<T>(id: string): IdentifierDecorator<T> {
export function TEST_ONLY_clearKnownIdentifiers(): void {
knownIdentifiers.clear();
}
+
+function changeToSelf(target: Ctor<any>, index: number, withNew: boolean) {
+ const descriptor = getDependencyByIndex(target, index);
+ descriptor.withNew = withNew;
+}
+
+function withNewDecoratorFactoryProducer(withNew: boolean) {
+ return function DecoratorFactory<T>(this: any) {
+ if (this instanceof DecoratorFactory) {
+ return this;
+ }
+
+ return function d(target: Ctor<T>, _key: string, index: number) {
+ changeToSelf(target, index, withNew);
+ };
+ } as any;
+}
+
+interface ToSelfDecorator {
+ (): any;
+ // eslint-disable-next-line @typescript-eslint/no-misused-new
+ new (): ToSelfDecorator;
+}
+
+/**
+ * Always initialize a new instance of that dependency instead of getting the cached instance from the injector.
+ */
+export const WithNew: ToSelfDecorator = withNewDecoratorFactoryProducer(true);
diff --git a/packages/core/src/DI/DependencyCollection.ts b/packages/core/src/DI/DependencyCollection.ts
index 4f2cf20893..71a7fa9618 100644
--- a/packages/core/src/DI/DependencyCollection.ts
+++ b/packages/core/src/DI/DependencyCollection.ts
@@ -1,9 +1,8 @@
-import { DependencyIdentifier } from './DependencyIdentifier';
-import { Ctor, DependencyItem, prettyPrintIdentifier } from './DependencyItem';
-import { checkQuantity, retrieveQuantity } from './DependencyQuantity';
+import { DependencyIdentifier, prettyPrintIdentifier } from './DependencyIdentifier';
import { IDisposable, isIDisposable } from './Lifecycle';
-import { Quantity } from './Types';
+import { Ctor, Quantity } from './Types';
import { DIError } from './Error';
+import { DependencyItem, checkQuantity, retrieveQuantity } from './Decorators';
export type DependencyPair<T> = [DependencyIdentifier<T>, DependencyItem<T>];
export type DependencyClass<T> = [Ctor<T>];
diff --git a/packages/core/src/DI/DependencyDeclare.ts b/packages/core/src/DI/DependencyDeclare.ts
index 2e9da98f20..4a1173fbac 100644
--- a/packages/core/src/DI/DependencyDeclare.ts
+++ b/packages/core/src/DI/DependencyDeclare.ts
@@ -1,6 +1,5 @@
-import { setDependency } from './Decorators';
-import { normalizeFactoryDeps } from './DependencyDescriptor';
-import { Ctor, FactoryDep } from './DependencyItem';
+import { FactoryDep, normalizeFactoryDeps, setDependency } from './Decorators';
+import { Ctor } from './Types';
export function setDependencies<U>(
registerTarget: Ctor<U>,
diff --git a/packages/core/src/DI/DependencyDescriptor.ts b/packages/core/src/DI/DependencyDescriptor.ts
deleted file mode 100644
index dc2a8635a7..0000000000
--- a/packages/core/src/DI/DependencyDescriptor.ts
+++ /dev/null
@@ -1,74 +0,0 @@
-import { DependencyIdentifier } from './DependencyIdentifier';
-import { FactoryDep, FactoryDepModifier } from './DependencyItem';
-import { Self, SkipSelf } from './DependencyLookUp';
-import { Many, Optional } from './DependencyQuantity';
-import { WithNew } from './DependencyWithNew';
-import { DIError } from './Error';
-import { LookUp, Quantity } from './Types';
-
-export interface DependencyDescriptor<T> {
- paramIndex: number;
- identifier: DependencyIdentifier<T>;
- quantity: Quantity;
- lookUp?: LookUp;
- withNew: boolean;
-}
-
-/**
- * describes dependencies of a IDependencyItem
- */
-export interface Dependencies {
- dependencies: Array<DependencyDescriptor<any>>;
-}
-
-export function normalizeFactoryDeps(
- deps?: Array<FactoryDep<any>>
-): Array<DependencyDescriptor<any>> {
- if (!deps) {
- return [];
- }
-
- return deps.map((dep, index) => {
- if (!Array.isArray(dep)) {
- return {
- paramIndex: index,
- identifier: dep,
- quantity: Quantity.REQUIRED,
- withNew: false,
- };
- }
-
- const modifiers = dep.slice(0, dep.length - 1) as FactoryDepModifier[];
- const identifier = dep[dep.length - 1] as DependencyIdentifier<any>;
-
- let lookUp: LookUp | undefined;
- let quantity = Quantity.REQUIRED;
- let withNew = false;
-
- (modifiers as FactoryDepModifier[]).forEach(
- (modifier: FactoryDepModifier) => {
- if (modifier instanceof Self) {
- lookUp = LookUp.SELF;
- } else if (modifier instanceof SkipSelf) {
- lookUp = LookUp.SKIP_SELF;
- } else if (modifier instanceof Optional) {
- quantity = Quantity.OPTIONAL;
- } else if (modifier instanceof Many) {
- quantity = Quantity.MANY;
- } else if (modifier instanceof WithNew) {
- withNew = true;
- } else {
- throw new DIError(`unknown dep modifier ${modifier}.`);
- }
- }
- );
-
- return {
- paramIndex: index,
- identifier: identifier as DependencyIdentifier<any>,
- quantity,
- lookUp,
- withNew,
- };
- });
-}
diff --git a/packages/core/src/DI/DependencyForwardRef.ts b/packages/core/src/DI/DependencyForwardRef.ts
index 171c83cc0f..0d02e820ee 100644
--- a/packages/core/src/DI/DependencyForwardRef.ts
+++ b/packages/core/src/DI/DependencyForwardRef.ts
@@ -1,8 +1,4 @@
-import {
- DependencyIdentifier,
- NormalizedDependencyIdentifier,
-} from './DependencyIdentifier';
-import { Ctor } from './DependencyItem';
+import { Ctor } from './Types';
export interface ForwardRef<T> {
unwrap(): Ctor<T>;
@@ -17,13 +13,3 @@ export function forwardRef<T>(wrapper: () => Ctor<T>): ForwardRef<T> {
export function isForwardRef<T = any>(thing: unknown): thing is ForwardRef<T> {
return !!thing && typeof (thing as any).unwrap === 'function';
}
-
-export function normalizeForwardRef<T>(
- id: DependencyIdentifier<T>
-): NormalizedDependencyIdentifier<T> {
- if (isForwardRef(id)) {
- return id.unwrap();
- }
-
- return id;
-}
diff --git a/packages/core/src/DI/DependencyHook.ts b/packages/core/src/DI/DependencyHook.ts
deleted file mode 100644
index 9e296f12cd..0000000000
--- a/packages/core/src/DI/DependencyHook.ts
+++ /dev/null
@@ -1 +0,0 @@
-export function AfterInstantion(): void {}
diff --git a/packages/core/src/DI/DependencyIdentifier.ts b/packages/core/src/DI/DependencyIdentifier.ts
index 72528d273b..af7e337a54 100644
--- a/packages/core/src/DI/DependencyIdentifier.ts
+++ b/packages/core/src/DI/DependencyIdentifier.ts
@@ -1,7 +1,5 @@
-import { Ctor } from './DependencyItem';
import { ForwardRef } from './DependencyForwardRef';
-
-export const IdentifierDecoratorSymbol = Symbol('$$IDENTIFIER_DECORATOR');
+import { Ctor, isCtor } from './Types';
export type IdentifierDecorator<T> = {
// call signature of an decorator
@@ -11,9 +9,7 @@ export type IdentifierDecorator<T> = {
[IdentifierDecoratorSymbol]: true;
- /**
- * beautify console
- */
+ // beautify console
toString(): string;
};
@@ -27,3 +23,15 @@ export type NormalizedDependencyIdentifier<T> = Exclude<
DependencyIdentifier<T>,
ForwardRef<T>
>;
+
+export const IdentifierDecoratorSymbol = Symbol('$$IDENTIFIER_DECORATOR');
+
+export function prettyPrintIdentifier<T>(id: DependencyIdentifier<T>): string {
+ if (typeof id === 'undefined') {
+ return 'undefined';
+ }
+
+ return isCtor(id) && !(id as any)[IdentifierDecoratorSymbol]
+ ? id.name
+ : id.toString();
+}
diff --git a/packages/core/src/DI/DependencyItem.ts b/packages/core/src/DI/DependencyItem.ts
deleted file mode 100644
index c184d2212d..0000000000
--- a/packages/core/src/DI/DependencyItem.ts
+++ /dev/null
@@ -1,116 +0,0 @@
-import {
- DependencyIdentifier,
- IdentifierDecoratorSymbol,
-} from './DependencyIdentifier';
-import { Self, SkipSelf } from './DependencyLookUp';
-import { Many, Optional } from './DependencyQuantity';
-import { WithNew } from './DependencyWithNew';
-
-export interface Ctor<T> {
- name: string; // constructor function has a name
-
- new (...args: any[]): T;
-}
-export function isCtor<T>(thing: unknown): thing is Ctor<T> {
- return typeof thing === 'function';
-}
-
-export interface DependencyItemHooks<T> {
- onInstantiation?: (instance: T) => void;
-}
-
-export interface ClassDependencyItem<T> extends DependencyItemHooks<T> {
- useClass: Ctor<T>;
- lazy?: boolean;
-}
-export function isClassDependencyItem<T>(
- thing: unknown
-): thing is ClassDependencyItem<T> {
- if (thing && typeof (thing as any).useClass !== 'undefined') {
- return true;
- }
-
- return false;
-}
-
-export type FactoryDepModifier =
- | typeof Self
- | typeof SkipSelf
- | typeof Optional
- | typeof Many
- | typeof WithNew;
-
-export type FactoryDep<T> =
- | [...FactoryDepModifier[], DependencyIdentifier<T>]
- | DependencyIdentifier<T>;
-
-export interface FactoryDependencyItem<T> extends DependencyItemHooks<T> {
- useFactory: (...deps: any[]) => T;
- dynamic?: true;
- deps?: Array<FactoryDep<any>>;
-}
-export function isFactoryDependencyItem<T>(
- thing: unknown
-): thing is FactoryDependencyItem<T> {
- if (thing && typeof (thing as any).useFactory !== 'undefined') {
- return true;
- }
-
- return false;
-}
-
-export interface ValueDependencyItem<T> extends DependencyItemHooks<T> {
- useValue: T;
-}
-export function isValueDependencyItem<T>(
- thing: unknown
-): thing is ValueDependencyItem<T> {
- if (thing && typeof (thing as any).useValue !== 'undefined') {
- return true;
- }
-
- return false;
-}
-
-export interface AsyncDependencyItem<T> extends DependencyItemHooks<T> {
- useAsync: () => Promise<
- T | Ctor<T> | [DependencyIdentifier<T>, SyncDependencyItem<T>]
- >;
-}
-export function isAsyncDependencyItem<T>(
- thing: unknown
-): thing is AsyncDependencyItem<T> {
- if (thing && typeof (thing as any).useAsync !== 'undefined') {
- return true;
- }
-
- return false;
-}
-
-export interface AsyncHook<T> {
- whenReady(): Promise<T>;
-}
-export function isAsyncHook<T>(thing: unknown): thing is AsyncHook<T> {
- if (thing && typeof (thing as any).whenReady !== 'undefined') {
- return true;
- }
-
- return false;
-}
-
-export type SyncDependencyItem<T> =
- | ClassDependencyItem<T>
- | FactoryDependencyItem<T>
- | ValueDependencyItem<T>;
-
-export type DependencyItem<T> = SyncDependencyItem<T> | AsyncDependencyItem<T>;
-
-export function prettyPrintIdentifier<T>(id: DependencyIdentifier<T>): string {
- if (typeof id === 'undefined') {
- return 'undefined';
- }
-
- return isCtor(id) && !(id as any)[IdentifierDecoratorSymbol]
- ? id.name
- : id.toString();
-}
diff --git a/packages/core/src/DI/DependencyLookUp.ts b/packages/core/src/DI/DependencyLookUp.ts
deleted file mode 100644
index 82138c41c1..0000000000
--- a/packages/core/src/DI/DependencyLookUp.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-import { getDependencyByIndex } from './Decorators';
-import { Ctor } from './DependencyItem';
-import { LookUp } from './Types';
-
-function changeLookup(target: Ctor<any>, index: number, lookUp: LookUp) {
- const descriptor = getDependencyByIndex(target, index);
- descriptor.lookUp = lookUp;
-}
-
-function lookupDecoratorFactoryProducer(lookUp: LookUp) {
- return function DecoratorFactory<T>(this: any) {
- if (this instanceof DecoratorFactory) {
- return this;
- }
-
- return function d(target: Ctor<T>, _key: string, index: number) {
- changeLookup(target, index, lookUp);
- };
- } as any;
-}
-
-interface SkipSelfDecorator {
- (): any;
- // eslint-disable-next-line @typescript-eslint/no-misused-new
- new (): SkipSelfDecorator;
-}
-/**
- * when resolving this dependency, skip the current injector
- */
-export const SkipSelf: SkipSelfDecorator = lookupDecoratorFactoryProducer(
- LookUp.SKIP_SELF
-);
-
-interface SelfDecorator {
- (): any;
- // eslint-disable-next-line @typescript-eslint/no-misused-new
- new (): SelfDecorator;
-}
-/**
- * when resolving this dependency, only search the current injector
- */
-export const Self: SelfDecorator = lookupDecoratorFactoryProducer(LookUp.SELF);
diff --git a/packages/core/src/DI/DependencyQuantity.ts b/packages/core/src/DI/DependencyQuantity.ts
deleted file mode 100644
index 32d45be616..0000000000
--- a/packages/core/src/DI/DependencyQuantity.ts
+++ /dev/null
@@ -1,94 +0,0 @@
-import {
- getDependencyByIndex,
- IdentifierUndefinedError,
- setDependency,
-} from './Decorators';
-import { DependencyIdentifier } from './DependencyIdentifier';
-import { Ctor, prettyPrintIdentifier } from './DependencyItem';
-import { DIError } from './Error';
-import { Quantity } from './Types';
-
-class QuantityCheckError extends DIError {
- constructor(id: DependencyIdentifier<any>, quantity: Quantity, actual: number) {
- const msg = `Expect "${quantity}" dependency items for id "${prettyPrintIdentifier(
- id
- )}" but get ${actual}.`;
-
- super(msg);
- }
-}
-
-export function checkQuantity(
- id: DependencyIdentifier<any>,
- quantity: Quantity,
- length: number
-): void {
- if (
- (quantity === Quantity.OPTIONAL && length > 1) ||
- (quantity === Quantity.REQUIRED && length !== 1)
- ) {
- throw new QuantityCheckError(id, quantity, length);
- }
-}
-
-export function retrieveQuantity<T>(quantity: Quantity, arr: T[]): T[] | T {
- if (quantity === Quantity.MANY) {
- return arr;
- }
- return arr[0];
-}
-
-function changeQuantity(target: Ctor<any>, index: number, quantity: Quantity) {
- const descriptor = getDependencyByIndex(target, index);
- descriptor.quantity = quantity;
-}
-
-function quantifyDecoratorFactoryProducer(quantity: Quantity) {
- return function decoratorFactory<T>(
- // typescript would remove `this` after transpilation
- // this line just declare the type of `this`
- this: any,
- id?: DependencyIdentifier<T>
- ) {
- if (this instanceof decoratorFactory) {
- return this;
- }
-
- return function d(registerTarget: Ctor<T>, _key: string, index: number) {
- if (id) {
- setDependency(registerTarget, id, index, quantity);
- } else {
- if (quantity === Quantity.REQUIRED) {
- throw new IdentifierUndefinedError(registerTarget, index);
- }
-
- changeQuantity(registerTarget, index, quantity);
- }
- };
- } as any;
-}
-
-interface ManyDecorator {
- (id?: DependencyIdentifier<any>): any;
- // eslint-disable-next-line @typescript-eslint/no-misused-new
- new (): ManyDecorator;
-}
-export const Many: ManyDecorator = quantifyDecoratorFactoryProducer(Quantity.MANY);
-
-interface OptionalDecorator {
- (id?: DependencyIdentifier<any>): any;
- // eslint-disable-next-line @typescript-eslint/no-misused-new
- new (): OptionalDecorator;
-}
-export const Optional: OptionalDecorator = quantifyDecoratorFactoryProducer(
- Quantity.OPTIONAL
-);
-
-interface InjectDecorator {
- (id: DependencyIdentifier<any>): any;
- // eslint-disable-next-line @typescript-eslint/no-misused-new
- new (): InjectDecorator;
-}
-export const Inject: InjectDecorator = quantifyDecoratorFactoryProducer(
- Quantity.REQUIRED
-);
diff --git a/packages/core/src/DI/DependencySingletons.ts b/packages/core/src/DI/DependencySingletons.ts
index d0eb57927e..4f350c712f 100644
--- a/packages/core/src/DI/DependencySingletons.ts
+++ b/packages/core/src/DI/DependencySingletons.ts
@@ -1,6 +1,6 @@
import { DIError } from './Error';
-import { DependencyIdentifier } from './DependencyIdentifier';
-import { DependencyItem, prettyPrintIdentifier } from './DependencyItem';
+import { DependencyIdentifier, prettyPrintIdentifier } from './DependencyIdentifier';
+import { DependencyItem } from './Decorators';
let singletonFetchedLock = false;
diff --git a/packages/core/src/DI/DependencyWithNew.ts b/packages/core/src/DI/DependencyWithNew.ts
deleted file mode 100644
index 9aef03efe6..0000000000
--- a/packages/core/src/DI/DependencyWithNew.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-import { getDependencyByIndex } from './Decorators';
-import { Ctor } from './DependencyItem';
-
-function changeToSelf(target: Ctor<any>, index: number, withNew: boolean) {
- const descriptor = getDependencyByIndex(target, index);
- descriptor.withNew = withNew;
-}
-
-function withNewDecoratorFactoryProducer(withNew: boolean) {
- return function DecoratorFactory<T>(this: any) {
- if (this instanceof DecoratorFactory) {
- return this;
- }
-
- return function d(target: Ctor<T>, _key: string, index: number) {
- changeToSelf(target, index, withNew);
- };
- } as any;
-}
-
-interface ToSelfDecorator {
- (): any;
- // eslint-disable-next-line @typescript-eslint/no-misused-new
- new (): ToSelfDecorator;
-}
-
-/**
- * Always initialize a new instance of that dependency instead of getting the cached instance from the injector.
- */
-export const WithNew: ToSelfDecorator = withNewDecoratorFactoryProducer(true);
diff --git a/packages/core/src/DI/Injector.ts b/packages/core/src/DI/Injector.ts
index f65b632824..e6368145b2 100644
--- a/packages/core/src/DI/Injector.ts
+++ b/packages/core/src/DI/Injector.ts
@@ -1,15 +1,5 @@
-import { getDependencies } from './Decorators';
import {
- Dependency,
- DependencyCollection,
- DependencyNotFoundError,
- ResolvedDependencyCollection,
- DependencyPair,
-} from './DependencyCollection';
-import { normalizeFactoryDeps } from './DependencyDescriptor';
-import { DependencyIdentifier } from './DependencyIdentifier';
-import {
- Ctor,
+ getDependencies,
DependencyItem,
AsyncDependencyItem,
isClassDependencyItem,
@@ -20,16 +10,27 @@ import {
FactoryDependencyItem,
AsyncHook,
isAsyncHook,
- isCtor,
ValueDependencyItem,
+ normalizeFactoryDeps,
+} from './Decorators';
+import {
+ Dependency,
+ DependencyCollection,
+ DependencyNotFoundError,
+ ResolvedDependencyCollection,
+ DependencyPair,
+} from './DependencyCollection';
+import {
+ DependencyIdentifier,
+ NormalizedDependencyIdentifier,
prettyPrintIdentifier,
-} from './DependencyItem';
-import { normalizeForwardRef } from './DependencyForwardRef';
+} from './DependencyIdentifier';
import { IdleValue } from './IdleValue';
import { getSingletonDependencies } from './DependencySingletons';
import { DIError } from './Error';
-import { Quantity, LookUp } from './Types';
+import { Quantity, LookUp, Ctor, isCtor } from './Types';
import { isICreatable } from './Lifecycle';
+import { isForwardRef } from './DependencyForwardRef';
const MAX_RESOLUTIONS_QUEUED = 300;
@@ -65,6 +66,16 @@ class GetAsyncItemFromSyncApiError<T> extends DIError {
}
}
+function normalizeForwardRef<T>(
+ id: DependencyIdentifier<T>
+): NormalizedDependencyIdentifier<T> {
+ if (isForwardRef(id)) {
+ return id.unwrap();
+ }
+
+ return id;
+}
+
export class Injector {
private readonly dependencyCollection: DependencyCollection;
diff --git a/packages/core/src/DI/Types.ts b/packages/core/src/DI/Types.ts
index aebc1a8f6e..d793293b2d 100644
--- a/packages/core/src/DI/Types.ts
+++ b/packages/core/src/DI/Types.ts
@@ -8,3 +8,12 @@ export enum LookUp {
SELF = 'self',
SKIP_SELF = 'skipSelf',
}
+
+export interface Ctor<T> {
+ name: string; // constructor function has a name
+
+ new (...args: any[]): T;
+}
+export function isCtor<T>(thing: unknown): thing is Ctor<T> {
+ return typeof thing === 'function';
+}
diff --git a/packages/core/src/DI/index.ts b/packages/core/src/DI/index.ts
index 464bb04af3..b4f5793858 100644
--- a/packages/core/src/DI/index.ts
+++ b/packages/core/src/DI/index.ts
@@ -1,31 +1,31 @@
-export { createIdentifier } from './Decorators';
-export { Quantity, LookUp } from './Types';
-export { Many, Optional, Inject } from './DependencyQuantity';
-export { forwardRef } from './DependencyForwardRef';
-export { Injector } from './Injector';
-export { SkipSelf, Self } from './DependencyLookUp';
-export { DependencyPair, Dependency } from './DependencyCollection';
-export { DependencyIdentifier, IdentifierDecorator } from './DependencyIdentifier';
-export { IDisposable, ICreatable } from './Lifecycle';
-export { setDependencies } from './DependencyDeclare';
-export { registerSingleton } from './DependencySingletons';
-export { WithNew } from './DependencyWithNew';
export {
AsyncDependencyItem,
AsyncHook,
ClassDependencyItem,
- Ctor,
+ createIdentifier,
DependencyItem,
FactoryDependencyItem,
+ Inject,
isAsyncDependencyItem,
isAsyncHook,
isClassDependencyItem,
- isCtor,
isFactoryDependencyItem,
isValueDependencyItem,
+ Many,
+ Optional,
+ Self,
+ SkipSelf,
SyncDependencyItem,
ValueDependencyItem,
-} from './DependencyItem';
+} from './Decorators';
+export { Quantity, LookUp, Ctor, isCtor } from './Types';
+export { forwardRef } from './DependencyForwardRef';
+export { Injector } from './Injector';
+export { DependencyPair, Dependency } from './DependencyCollection';
+export { DependencyIdentifier, IdentifierDecorator } from './DependencyIdentifier';
+export { IDisposable, ICreatable } from './Lifecycle';
+export { setDependencies } from './DependencyDeclare';
+export { registerSingleton } from './DependencySingletons';
export { DIError } from './Error';
const globalObject: any =