Skip to content

Commit

Permalink
chore: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev committed Feb 28, 2024
1 parent 195c751 commit e1a8f2f
Show file tree
Hide file tree
Showing 20 changed files with 556 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ root = true
end_of_line = lf
indent_style = tab
trim_trailing_whitespace = true
indent_size = 4
indent_size = 2

# The indent size used in the `package.json` file cannot be changed
# https://github.com/npm/npm/pull/3180#issuecomment-16336516
Expand Down
8 changes: 4 additions & 4 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"singleQuote": true,
"semi": false,
"tabWidth": 4,
"printWidth": 120
"singleQuote": true,
"semi": false,
"tabWidth": 2,
"printWidth": 80
}
27 changes: 20 additions & 7 deletions src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { DependencyDescriptor } from './dependencyDescriptor'
import { DependencyIdentifier, IdentifierDecorator, IdentifierDecoratorSymbol } from './dependencyIdentifier'
import {
DependencyIdentifier,
IdentifierDecorator,
IdentifierDecoratorSymbol,
} from './dependencyIdentifier'
import { Ctor, prettyPrintIdentifier } from './dependencyItem'
import { LookUp, Quantity } from './types'
import { RediError } from './error'
Expand Down Expand Up @@ -32,17 +36,24 @@ export class IdentifierUndefinedError extends RediError {
/**
* @internal
*/
export function getDependencies<T>(registerTarget: Ctor<T>): DependencyDescriptor<any>[] {
export function getDependencies<T>(
registerTarget: Ctor<T>
): DependencyDescriptor<any>[] {
const target = registerTarget as any
return target[DEPENDENCIES] || []
}

/**
* @internal
*/
export function getDependencyByIndex<T>(registerTarget: Ctor<T>, index: number): DependencyDescriptor<any> {
export function getDependencyByIndex<T>(
registerTarget: Ctor<T>,
index: number
): DependencyDescriptor<any> {
const allDependencies = getDependencies(registerTarget)
const dep = allDependencies.find((descriptor) => descriptor.paramIndex === index)
const dep = allDependencies.find(
(descriptor) => descriptor.paramIndex === index
)

if (!dep) {
throw new DependencyDescriptorNotFoundError(index, registerTarget)
Expand Down Expand Up @@ -100,9 +111,11 @@ export function createIdentifier<T>(id: string): IdentifierDecorator<T> {
knownIdentifiers.add(id)
}

const decorator = (<any>function (registerTarget: Ctor<T>, _key: string, index: number): void {
setDependency(registerTarget, decorator, index)
}) as IdentifierDecorator<T> // decorator as an identifier
const decorator = (<any>(
function (registerTarget: Ctor<T>, _key: string, index: number): void {
setDependency(registerTarget, decorator, index)
}
)) as IdentifierDecorator<T> // decorator as an identifier

// TODO: @wzhudev should assign a name to the function so it would be easy to debug in inspect tools
// decorator.name = `[redi]: ${id}`;
Expand Down
95 changes: 74 additions & 21 deletions src/dependencyCollection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { DependencyIdentifier, isIdentifierDecorator } from './dependencyIdentifier'
import {
DependencyIdentifier,
isIdentifierDecorator,
} from './dependencyIdentifier'
import { Ctor, DependencyItem, prettyPrintIdentifier } from './dependencyItem'
import { checkQuantity, retrieveQuantity } from './dependencyQuantity'
import { IDisposable, isDisposable } from './dispose'
Expand All @@ -8,17 +11,32 @@ import { RediError } from './error'
export type DependencyPair<T> = [DependencyIdentifier<T>, DependencyItem<T>]
export type DependencyClass<T> = [Ctor<T>]
export type Dependency<T = any> = DependencyPair<T> | DependencyClass<T>
export type DependencyWithInstance<T = any> = [Ctor<T> | DependencyIdentifier<T>, T]
export type DependencyOrInstance<T = any> = Dependency<T> | DependencyWithInstance<T>

export function isBareClassDependency<T>(thing: Dependency<T>): thing is DependencyClass<T> {
export type DependencyWithInstance<T = any> = [
Ctor<T> | DependencyIdentifier<T>,
T
]
export type DependencyOrInstance<T = any> =
| Dependency<T>
| DependencyWithInstance<T>

export function isBareClassDependency<T>(
thing: Dependency<T>
): thing is DependencyClass<T> {
return thing.length === 1
}

export class DependencyNotFoundForModuleError extends RediError {
constructor(toInstantiate: Ctor<any> | DependencyIdentifier<any>, id: DependencyIdentifier<any>, index: number) {
const msg = `Cannot find "${prettyPrintIdentifier(id)}" registered by any injector. It is the ${index}th param of "${
isIdentifierDecorator(toInstantiate) ? prettyPrintIdentifier(toInstantiate): (toInstantiate as Ctor<any>).name
constructor(
toInstantiate: Ctor<any> | DependencyIdentifier<any>,
id: DependencyIdentifier<any>,
index: number
) {
const msg = `Cannot find "${prettyPrintIdentifier(
id
)}" registered by any injector. It is the ${index}th param of "${
isIdentifierDecorator(toInstantiate)
? prettyPrintIdentifier(toInstantiate)
: (toInstantiate as Ctor<any>).name
}".`

super(msg)
Expand All @@ -27,7 +45,9 @@ export class DependencyNotFoundForModuleError extends RediError {

export class DependencyNotFoundError extends RediError {
constructor(id: DependencyIdentifier<any>) {
const msg = `Cannot find "${prettyPrintIdentifier(id)}" registered by any injector.`
const msg = `Cannot find "${prettyPrintIdentifier(
id
)}" registered by any injector.`

super(msg)
}
Expand All @@ -39,15 +59,23 @@ export class DependencyNotFoundError extends RediError {
* @internal
*/
export class DependencyCollection implements IDisposable {
private readonly dependencyMap = new Map<DependencyIdentifier<any>, DependencyItem<any>[]>()
private readonly dependencyMap = new Map<
DependencyIdentifier<any>,
DependencyItem<any>[]
>()

constructor(dependencies: Dependency[]) {
this.normalizeDependencies(dependencies).map((pair) => this.add(pair[0], pair[1]))
this.normalizeDependencies(dependencies).map((pair) =>
this.add(pair[0], pair[1])
)
}

public add<T>(ctor: Ctor<T>): void
public add<T>(id: DependencyIdentifier<T>, val: DependencyItem<T>): void
public add<T>(ctorOrId: Ctor<T> | DependencyIdentifier<T>, val?: DependencyItem<T>): void {
public add<T>(
ctorOrId: Ctor<T> | DependencyIdentifier<T>,
val?: DependencyItem<T>
): void {
if (typeof val === 'undefined') {
val = { useClass: ctorOrId as Ctor<T>, lazy: false }
}
Expand All @@ -65,10 +93,22 @@ export class DependencyCollection implements IDisposable {
}

public get<T>(id: DependencyIdentifier<T>): DependencyItem<T>
public get<T>(id: DependencyIdentifier<T>, quantity: Quantity.REQUIRED): DependencyItem<T>
public get<T>(id: DependencyIdentifier<T>, quantity: Quantity.MANY): DependencyItem<T>[]
public get<T>(id: DependencyIdentifier<T>, quantity: Quantity.OPTIONAL): DependencyItem<T> | null
public get<T>(id: DependencyIdentifier<T>, quantity: Quantity): DependencyItem<T> | DependencyItem<T>[] | null
public get<T>(
id: DependencyIdentifier<T>,
quantity: Quantity.REQUIRED
): DependencyItem<T>
public get<T>(
id: DependencyIdentifier<T>,
quantity: Quantity.MANY
): DependencyItem<T>[]
public get<T>(
id: DependencyIdentifier<T>,
quantity: Quantity.OPTIONAL
): DependencyItem<T> | null
public get<T>(
id: DependencyIdentifier<T>,
quantity: Quantity
): DependencyItem<T> | DependencyItem<T>[] | null
public get<T>(
id: DependencyIdentifier<T>,
quantity: Quantity = Quantity.REQUIRED
Expand All @@ -85,7 +125,9 @@ export class DependencyCollection implements IDisposable {
}

public append(dependencies: Dependency<any>[]): void {
this.normalizeDependencies(dependencies).forEach((pair) => this.add(pair[0], pair[1]))
this.normalizeDependencies(dependencies).forEach((pair) =>
this.add(pair[0], pair[1])
)
}

public dispose(): void {
Expand All @@ -95,7 +137,9 @@ export class DependencyCollection implements IDisposable {
/**
* normalize dependencies to `DependencyItem`
*/
private normalizeDependencies(dependencies: Dependency[]): DependencyPair<any>[] {
private normalizeDependencies(
dependencies: Dependency[]
): DependencyPair<any>[] {
return dependencies.map((dependency) => {
const id = dependency[0]
let val: DependencyItem<any>
Expand All @@ -119,7 +163,10 @@ export class DependencyCollection implements IDisposable {
* @internal
*/
export class ResolvedDependencyCollection implements IDisposable {
private readonly resolvedDependencies = new Map<DependencyIdentifier<any>, any[]>()
private readonly resolvedDependencies = new Map<
DependencyIdentifier<any>,
any[]
>()

public add<T>(id: DependencyIdentifier<T>, val: T | null): void {
let arr = this.resolvedDependencies.get(id)
Expand All @@ -144,11 +191,17 @@ export class ResolvedDependencyCollection implements IDisposable {
}

public get<T>(id: DependencyIdentifier<T>): T
public get<T>(id: DependencyIdentifier<T>, quantity: Quantity.OPTIONAL): T | null
public get<T>(
id: DependencyIdentifier<T>,
quantity: Quantity.OPTIONAL
): T | null
public get<T>(id: DependencyIdentifier<T>, quantity: Quantity.REQUIRED): T
public get<T>(id: DependencyIdentifier<T>, quantity: Quantity.MANY): T[]
public get<T>(id: DependencyIdentifier<T>, quantity: Quantity): T[] | T | null
public get<T>(id: DependencyIdentifier<T>, quantity: Quantity = Quantity.REQUIRED): T | T[] | null {
public get<T>(
id: DependencyIdentifier<T>,
quantity: Quantity = Quantity.REQUIRED
): T | T[] | null {
const ret = this.resolvedDependencies.get(id)

if (!ret) {
Expand Down
5 changes: 4 additions & 1 deletion src/dependencyDeclare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { Ctor, FactoryDep } from './dependencyItem'
* @param registerTarget The target constructor
* @param deps Dependencies
*/
export function setDependencies<U>(registerTarget: Ctor<U>, deps: FactoryDep<any>[]): void {
export function setDependencies<U>(
registerTarget: Ctor<U>,
deps: FactoryDep<any>[]
): void {
const normalizedDescriptors = normalizeFactoryDeps(deps)
normalizedDescriptors.forEach((descriptor) => {
setDependency(
Expand Down
34 changes: 19 additions & 15 deletions src/dependencyDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export interface Dependencies {
dependencies: DependencyDescriptor<any>[]
}

export function normalizeFactoryDeps(deps?: FactoryDep<any>[]): DependencyDescriptor<any>[] {
export function normalizeFactoryDeps(
deps?: FactoryDep<any>[]
): DependencyDescriptor<any>[] {
if (!deps) {
return []
}
Expand All @@ -43,21 +45,23 @@ export function normalizeFactoryDeps(deps?: FactoryDep<any>[]): DependencyDescri
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 RediError(`unknown dep modifier ${modifier}.`)
;(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 RediError(`unknown dep modifier ${modifier}.`)
}
}
})
)

return {
paramIndex: index,
Expand Down
9 changes: 7 additions & 2 deletions src/dependencyForwardRef.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { DependencyIdentifier, NormalizedDependencyIdentifier } from './dependencyIdentifier'
import {
DependencyIdentifier,
NormalizedDependencyIdentifier,
} from './dependencyIdentifier'
import { Ctor } from './dependencyItem'

export interface ForwardRef<T> {
Expand All @@ -15,7 +18,9 @@ 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> {
export function normalizeForwardRef<T>(
id: DependencyIdentifier<T>
): NormalizedDependencyIdentifier<T> {
if (isForwardRef(id)) {
return id.unwrap()
}
Expand Down
15 changes: 12 additions & 3 deletions src/dependencyIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ export type IdentifierDecorator<T> = {
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function isIdentifierDecorator<T>(thing: any): thing is IdentifierDecorator<T> {
export function isIdentifierDecorator<T>(
thing: any
): thing is IdentifierDecorator<T> {
return thing && thing[IdentifierDecoratorSymbol] === true
}

export type DependencyIdentifier<T> = string | Ctor<T> | ForwardRef<T> | IdentifierDecorator<T>
export type DependencyIdentifier<T> =
| string
| Ctor<T>
| ForwardRef<T>
| IdentifierDecorator<T>

export type NormalizedDependencyIdentifier<T> = Exclude<DependencyIdentifier<T>, ForwardRef<T>>
export type NormalizedDependencyIdentifier<T> = Exclude<
DependencyIdentifier<T>,
ForwardRef<T>
>
Loading

0 comments on commit e1a8f2f

Please sign in to comment.