Skip to content

Commit

Permalink
fix: fix optional looking up (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev authored Dec 13, 2024
1 parent 7d5dddb commit 84e3b79
Show file tree
Hide file tree
Showing 6 changed files with 857 additions and 674 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/wzhudev/squirrel/master/src/schema/package.schema.json",
"schema": "https://raw.githubusercontent.com/wzhudev/squirrel/master/src/schema/package.schema.json",
"name": "@wendellhu/redi",
"version": "0.16.1",
"description": "A dependency library for TypeScript and JavaScript, along with a binding for React.",
Expand Down
1,473 changes: 813 additions & 660 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
packages:
- ./
- doc/
- docs/
16 changes: 12 additions & 4 deletions src/dependencyQuantity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ import { Ctor, prettyPrintIdentifier } from './dependencyItem'
import { RediError } from './error'
import { Quantity } from './types'

class QuantityCheckError extends RediError {
export class QuantityCheckError extends RediError {
constructor(
id: DependencyIdentifier<any>,
quantity: Quantity,
actual: number
public readonly quantity: Quantity,
public readonly actual: number
) {
const msg = `Expect "${quantity}" dependency items for id "${prettyPrintIdentifier(
let msg = `Expect "${quantity}" dependency items for id "${prettyPrintIdentifier(
id
)}" but get ${actual}.`

if (actual == 0) {
msg += ' Did you forget to register it?'
}

if (actual > 1) {
msg += ' You register it more than once.'
}

super(msg)
}
}
Expand Down
22 changes: 15 additions & 7 deletions src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
isExistingDependencyItem,
ExistingDependencyItem,
} from './dependencyItem'
import { checkQuantity, QuantityCheckError } from './dependencyQuantity'
import { RediError } from './error'
import { IdleValue } from './idleValue'
import { LookUp, Quantity } from './types'
Expand Down Expand Up @@ -525,7 +526,7 @@ export class Injector {
)
resolvedArgs.push(thing)
} catch (error: unknown) {
if (error instanceof DependencyNotFoundError) {
if (error instanceof DependencyNotFoundError || (error instanceof QuantityCheckError && error.actual === 0)) {
throw new DependencyNotFoundForModuleError(
ctor,
dep.identifier,
Expand Down Expand Up @@ -586,7 +587,7 @@ export class Injector {
)
resolvedArgs.push(thing)
} catch (error: unknown) {
if (error instanceof DependencyNotFoundError) {
if (error instanceof DependencyNotFoundError || (error instanceof QuantityCheckError && error.actual === 0)) {
throw new DependencyNotFoundForModuleError(
id,
dep.identifier,
Expand Down Expand Up @@ -677,14 +678,25 @@ export class Injector {
if (this.parent) {
return this.parent.getValue(id, quantity)
} else {
return NotInstantiatedSymbol
// If the parent injector is missing, we should check quantity with 0 values.
checkQuantity(id, quantity, 0);

if (quantity === Quantity.MANY) {
return []
} else {
return null
}
}
}

if (lookUp === LookUp.SKIP_SELF) {
return onParent()
}

if (id === Injector) {
return this as unknown as T;
}

if (lookUp === LookUp.SELF) {
return onSelf()
}
Expand Down Expand Up @@ -740,10 +752,6 @@ export class Injector {
return onParent()
}

if ((id as any as Ctor<Injector>) === Injector) {
return this as any as T
}

if (this.dependencyCollection.has(id)) {
return onSelf()
}
Expand Down
16 changes: 15 additions & 1 deletion test/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
setDependencies,
SkipSelf,
WithNew,
Quantity,
LookUp,
} from '@wendellhu/redi'

import { TEST_ONLY_clearKnownIdentifiers } from '../src/decorators'
Expand Down Expand Up @@ -705,6 +707,7 @@ describe('core', () => {
const j = new Injector([[aI, { useValue: a }]])

// totally verbose for real use case, but to show this works
expect(j.get(Injector)).toBe(j)
expect(j.get(Injector).get(aI).key).toBe('a')
})
})
Expand Down Expand Up @@ -964,7 +967,7 @@ describe('core', () => {

expectToThrow(
() => j.get(A),
`[redi]: Cannot find "A" registered by any injector.`
`[redi]: Expect "required" dependency items for id "A" but get 0. Did you forget to register it?`
)
})
})
Expand Down Expand Up @@ -1245,6 +1248,17 @@ describe('core', () => {
})
})

describe('Lookup', () => {
it('should support optional lookup even if parent does not exist', () => {
class A { }

const parentInjector = new Injector()
const childInjector = parentInjector.createChild([[A]])

expect(childInjector.get(A, Quantity.OPTIONAL, LookUp.SKIP_SELF)).toBeNull()
});
})

it('should throw error when identifier has been declared before', () => {
createIdentifier('a')

Expand Down

1 comment on commit 84e3b79

@vercel
Copy link

@vercel vercel bot commented on 84e3b79 Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.