Skip to content

Commit

Permalink
Merge fad287b into 5f0b5ae
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidhanson90 authored Mar 4, 2021
2 parents 5f0b5ae + fad287b commit 8b4b117
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
5 changes: 5 additions & 0 deletions main/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ export const UNDEFINED_VALUE = { type: 'UNDEFINED_VALUE' };
* Represents a type not being found inside of the injector or external injector
*/
export const TYPE_NOT_FOUND = { type: 'TYPE_NOT_FOUND' };

/**
* This constant can be used in conjunction with AutoFactory
*/
export const AUTO_RESOLVE = undefined;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.3.7",
"version": "0.3.8",
"description": "A small & lightweight dependency injection container for use in multiple contexts like Angular, React & node.",
"name": "@morgan-stanley/needle",
"license": "Apache-2.0",
Expand Down
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,13 @@ const carWithSuperPowerfulEngine = factory.create(new SuperPowerfulEngine());

```

If you would **not** like the injector to auto resolve the value for engine and you wanted to actually return `null` or `undefined` you can use well known injector values to achieve this.
If you prefer not to pass undefined to the factory, there is also a named constant `AUTO_RESOLVE` which can be used instead.

```typescript
factory.create(AUTO_RESOLVE, 4);
```

If you would **not** like the injector to auto resolve the value for engine and you wanted to actually return `null` or `undefined` you can use well known injector values (`UNDEFINED_VALUE`, `NULL_VALUE` ) to achieve this.

```typescript

Expand Down
21 changes: 20 additions & 1 deletion spec/injection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ import {
Optional,
Strategy,
} from '../main';
import { DI_ROOT_INJECTOR_KEY, NULL_VALUE, TYPE_NOT_FOUND, UNDEFINED_VALUE } from '../main/constants/constants';
import {
DI_ROOT_INJECTOR_KEY,
NULL_VALUE,
TYPE_NOT_FOUND,
UNDEFINED_VALUE,
AUTO_RESOLVE,
} from '../main/constants/constants';
import { InstanceCache } from '../main/core/cache';
import { isInjectorLike } from '../main/core/guards';
import { InjectionTokensCache } from '../main/core/tokens';
Expand Down Expand Up @@ -1029,6 +1035,19 @@ describe('Injector', () => {
expect(car.engine).toBeDefined();
});

it('should auto resolve parameters for a factory when supplied using named constant', () => {
const instance = getInstance();

instance.register(Car).register(Engine);

const carFactory = instance.getFactory(Car);
const car = carFactory.create(AUTO_RESOLVE);

expect(carFactory).toBeDefined();
expect(car).toBeDefined();
expect(car.engine).toBeDefined();
});

it('should resolve a undefined if explicitly passed', () => {
const instance = getInstance();

Expand Down

0 comments on commit 8b4b117

Please sign in to comment.