Skip to content

Commit

Permalink
Update per review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
fnimick committed Dec 30, 2023
1 parent 8cbdaa0 commit ced24e7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,6 @@ in the root container, and would always have the `currentUser` from the first
request. Modules should generally not have a longer lifetime than their
dependencies, as this can cause issues of stale data.

If you want a mismatched configuration like the above to error, set
`errorOnShorterLivedDependencies` in the container options.

```js
const makePrintTime = ({ time }) => () => {
console.log('Time:', time)
Expand All @@ -324,6 +321,11 @@ container.resolve('printTime')()
container.resolve('printTime')()
```

If you want a mismatched configuration like this to error, set
`errorOnShorterLivedDependencies` in the container options. This will trigger
the following error at runtime when the singleton `printTime` is resolved:
`AwilixResolutionError: Could not resolve 'time'. Dependency 'time' has a shorter lifetime than its ancestor: 'printTime'`

Read the documentation for [`container.createScope()`](#containercreatescope)
for more examples.

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ describe('container', () => {
const err = throws(() => container.resolve('first'))
expect(err.message).toContain('first -> second')
expect(err.message).toContain(
"Dependency 'second' has a shorter lifetime than its parent: 'first'",
"Dependency 'second' has a shorter lifetime than its ancestor: 'first'",
)
})

Expand Down Expand Up @@ -516,7 +516,7 @@ describe('container', () => {
const err = throws(() => container.resolve('first'))
expect(err.message).toContain('first -> second')
expect(err.message).toContain(
"Dependency 'second' has a shorter lifetime than its parent: 'first'",
"Dependency 'second' has a shorter lifetime than its ancestor: 'first'",
)
})

Expand Down
2 changes: 1 addition & 1 deletion src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ export function createContainer<T extends object = any, U extends object = any>(
throw new AwilixResolutionError(
name,
resolutionStack,
`Dependency '${name.toString()}' has a shorter lifetime than its parent: '${resolutionStack[
`Dependency '${name.toString()}' has a shorter lifetime than its ancestor: '${resolutionStack[
maybeLongerLifetimeParentIndex
].name.toString()}'`,
)
Expand Down
3 changes: 3 additions & 0 deletions src/lifetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export const Lifetime: Record<LifetimeType, LifetimeType> = {
SCOPED: 'SCOPED',
}

/**
* Returns true if and only if the first lifetime is strictly longer than the second.
*/
export function isLifetimeLonger(a: LifetimeType, b: LifetimeType): boolean {
return (
(a === Lifetime.SINGLETON && b !== Lifetime.SINGLETON) ||
Expand Down
3 changes: 2 additions & 1 deletion src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export type Constructor<T> = { new (...args: any[]): T }
export function asValue<T>(value: T, opts?: ResolverOptions<T>): Resolver<T> {
return {
resolve: () => value,
...{ lifetime: Lifetime.SCOPED, ...opts },
lifetime: Lifetime.SCOPED,
...opts,
}
}

Expand Down

0 comments on commit ced24e7

Please sign in to comment.