Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for named constant when auto resolving params. #37

Merged
merged 2 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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