From dd548f40cea2cea15234bdab28ea41d1d9a271a8 Mon Sep 17 00:00:00 2001 From: David Hanson <1564426+Davidhanson90@users.noreply.github.com> Date: Thu, 4 Mar 2021 09:40:18 +0000 Subject: [PATCH 1/2] Add support for named constant when auto resolving params. --- main/constants/constants.ts | 5 +++++ package-lock.json | 2 +- package.json | 2 +- readme.md | 8 +++++++- spec/injection.spec.ts | 21 ++++++++++++++++++++- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/main/constants/constants.ts b/main/constants/constants.ts index 9052e97..b1a0e52 100644 --- a/main/constants/constants.ts +++ b/main/constants/constants.ts @@ -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; diff --git a/package-lock.json b/package-lock.json index aacf2e5..d52a7b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@morgan-stanley/needle", - "version": "0.3.7", + "version": "0.3.8", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 57834c6..5721775 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/readme.md b/readme.md index 14dbc6b..1fa0fbb 100644 --- a/readme.md +++ b/readme.md @@ -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 convenience named constant `AUTO_RESOLVE` which 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 diff --git a/spec/injection.spec.ts b/spec/injection.spec.ts index 58e3ee7..70b6e41 100644 --- a/spec/injection.spec.ts +++ b/spec/injection.spec.ts @@ -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'; @@ -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(); From fad287b65eb71ec358abef6d3096a3c1920c7d15 Mon Sep 17 00:00:00 2001 From: David Hanson <1564426+Davidhanson90@users.noreply.github.com> Date: Thu, 4 Mar 2021 09:42:52 +0000 Subject: [PATCH 2/2] fix typo --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 1fa0fbb..d390ef4 100644 --- a/readme.md +++ b/readme.md @@ -481,7 +481,7 @@ const carWithSuperPowerfulEngine = factory.create(new SuperPowerfulEngine()); ``` -If you prefer not to pass undefined to the factory, there is also a convenience named constant `AUTO_RESOLVE` which be used instead. +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);