knifecycle
Advanced tools
Comparing version 5.1.6 to 5.2.0
@@ -0,1 +1,15 @@ | ||
# [5.2.0](https://github.com/nfroidure/knifecycle/compare/v5.1.6...v5.2.0) (2019-02-09) | ||
### Bug Fixes | ||
* **$injector:** Fix services mapping in the $injector ([ee2a2bd](https://github.com/nfroidure/knifecycle/commit/ee2a2bd)) | ||
### Features | ||
* **$injector:** Allow new instantiations within $injector calls ([b66e10e](https://github.com/nfroidure/knifecycle/commit/b66e10e)) | ||
## [5.1.6](https://github.com/nfroidure/knifecycle/compare/v5.1.5...v5.1.6) (2019-02-02) | ||
@@ -2,0 +16,0 @@ |
@@ -161,3 +161,2 @@ "use strict"; | ||
const E_BAD_SERVICE_PROMISE = 'E_BAD_SERVICE_PROMISE'; | ||
const E_BAD_INJECTION = 'E_BAD_INJECTION'; | ||
const E_INSTANCE_DESTROYED = 'E_INSTANCE_DESTROYED'; | ||
@@ -220,9 +219,15 @@ const E_AUTOLOADER_DYNAMIC_DEPENDENCY = 'E_AUTOLOADER_DYNAMIC_DEPENDENCY'; | ||
type: 'provider', | ||
inject: [SILO_CONTEXT] | ||
inject: [SILO_CONTEXT], | ||
options: { | ||
// Despite its global definition, the injector | ||
// depends on the silo context and then needs | ||
// to be instanciated once per silo. | ||
singleton: false | ||
} | ||
}, async ({ | ||
$siloContext | ||
}) => ({ | ||
service: dependenciesDeclarations => this._initializeDependencies($siloContext, $siloContext.name, dependenciesDeclarations, { | ||
injectOnly: true | ||
}) | ||
service: async dependenciesDeclarations => _buildFinalHash((await this._initializeDependencies($siloContext, $siloContext.name, dependenciesDeclarations, { | ||
injectorContext: true | ||
})), dependenciesDeclarations) | ||
}))); | ||
@@ -569,3 +574,3 @@ this.register((0, _util.initializer)({ | ||
const servicesHash = await this._initializeDependencies(siloContext, siloContext.name, internalDependencies, { | ||
injectOnly: false, | ||
injectorContext: false, | ||
autoloading: false | ||
@@ -575,10 +580,3 @@ }); | ||
Promise.all(siloContext.errorsPromises).catch(siloContext.throwFatalError); | ||
return dependenciesDeclarations.reduce((finalHash, dependencyDeclaration) => { | ||
const { | ||
serviceName, | ||
mappedName | ||
} = (0, _util.parseDependencyDeclaration)(dependencyDeclaration); | ||
finalHash[serviceName] = servicesHash[mappedName]; | ||
return finalHash; | ||
}, {}); | ||
return _buildFinalHash(servicesHash, dependenciesDeclarations); | ||
} | ||
@@ -593,4 +591,4 @@ /** | ||
* Options for service retrieval | ||
* @param {Boolean} options.injectOnly | ||
* Flag indicating if existing services only should be used | ||
* @param {Boolean} options.injectorContext | ||
* Flag indicating the injection were initiated by the $injector | ||
* @param {Boolean} options.autoloading | ||
@@ -604,3 +602,3 @@ * Flag to indicating $autoload dependencies on the fly loading | ||
async _getServiceDescriptor(siloContext, serviceName, { | ||
injectOnly, | ||
injectorContext, | ||
autoloading | ||
@@ -616,3 +614,3 @@ }) { | ||
let initializer = await this._findInitializer(siloContext, serviceName, { | ||
injectOnly, | ||
injectorContext, | ||
autoloading | ||
@@ -630,9 +628,10 @@ }); | ||
return serviceDescriptorPromise; | ||
} // The inject service is intended to be used as a workaround for unavoidable | ||
// circular dependencies. It wouldn't make sense to instanciate new services | ||
// at this level so throwing an error | ||
} // The $injector service is mainly intended to be used as a workaround | ||
// for unavoidable circular dependencies. It rarely make sense to | ||
// instanciate new services at this level so printing a warning for | ||
// debug purposes | ||
if (injectOnly) { | ||
return Promise.reject(new _yerror.default(E_BAD_INJECTION, serviceName)); | ||
if (injectorContext) { | ||
debug(['Warning: Instantiating a new service via the $injector. It may' + ' mean that you no longer need it if your worked around a circular' + ' dependency.']); | ||
} | ||
@@ -642,3 +641,3 @@ | ||
autoloading: autoloading || AUTOLOAD === serviceName, | ||
injectOnly | ||
injectorContext | ||
}); | ||
@@ -669,3 +668,3 @@ | ||
async _findInitializer(siloContext, serviceName, { | ||
injectOnly, | ||
injectorContext, | ||
autoloading | ||
@@ -704,3 +703,3 @@ }) { | ||
const autoloadingDescriptor = await this._getServiceDescriptor(siloContext, AUTOLOAD, { | ||
injectOnly, | ||
injectorContext, | ||
autoloading: true | ||
@@ -756,4 +755,4 @@ }); | ||
* Options for service retrieval | ||
* @param {Boolean} options.injectOnly | ||
* Flag indicating if existing services only should be used | ||
* @param {Boolean} options.injectorContext | ||
* Flag indicating the injection were initiated by the $injector | ||
* @param {Boolean} options.autoloading | ||
@@ -767,3 +766,3 @@ * Flag to indicating $autoload dependendencies on the fly loading. | ||
autoloading, | ||
injectOnly | ||
injectorContext | ||
}) { | ||
@@ -785,3 +784,3 @@ let serviceDescriptor; | ||
const servicesHash = await this._initializeDependencies(siloContext, serviceName, initializer[_util.SPECIAL_PROPS.INJECT], { | ||
injectOnly, | ||
injectorContext, | ||
autoloading | ||
@@ -831,4 +830,4 @@ }); | ||
* Options for service retrieval | ||
* @param {Boolean} options.injectOnly | ||
* Flag indicating if existing services only should be used | ||
* @param {Boolean} options.injectorContext | ||
* Flag indicating the injection were initiated by the $injector | ||
* @param {Boolean} options.autoloading | ||
@@ -841,3 +840,3 @@ * Flag to indicating $autoload dependendencies on the fly loading. | ||
async _initializeDependencies(siloContext, serviceName, servicesDeclarations, { | ||
injectOnly = false, | ||
injectorContext = false, | ||
autoloading = false | ||
@@ -854,3 +853,3 @@ }) { | ||
const serviceDescriptor = await this._getServiceDescriptor(siloContext, mappedName, { | ||
injectOnly, | ||
injectorContext, | ||
autoloading | ||
@@ -963,2 +962,13 @@ }); | ||
})); | ||
} | ||
function _buildFinalHash(servicesHash, dependenciesDeclarations) { | ||
return dependenciesDeclarations.reduce((finalHash, dependencyDeclaration) => { | ||
const { | ||
serviceName, | ||
mappedName | ||
} = (0, _util.parseDependencyDeclaration)(dependencyDeclaration); | ||
finalHash[serviceName] = servicesHash[mappedName]; | ||
return finalHash; | ||
}, {}); | ||
} |
@@ -812,3 +812,3 @@ "use strict"; | ||
}); | ||
describe('inject', () => { | ||
describe('$injector', () => { | ||
it('should work with no dependencies', async () => { | ||
@@ -847,6 +847,25 @@ $.register((0, _index.constant)('ENV', ENV)); | ||
}); | ||
it('should fail with non instanciated dependencies', async () => { | ||
it('should work with name mapping', async () => { | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
const dependencies = await $.run(['time', 'hash', '$injector']); | ||
_assert.default.deepEqual(Object.keys(dependencies), ['time', 'hash', '$injector']); | ||
const injectDependencies = await dependencies.$injector(['aTime>time', 'aHash>hash']); | ||
_assert.default.deepEqual(Object.keys(injectDependencies), ['aTime', 'aHash']); | ||
_assert.default.deepEqual(injectDependencies, { | ||
aHash: { | ||
ENV | ||
}, | ||
aTime: time | ||
}); | ||
}); | ||
it('should work with non instanciated dependencies', async () => { | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
const dependencies = await $.run(['time', '$injector']); | ||
@@ -856,8 +875,12 @@ | ||
try { | ||
await dependencies.$injector(['time', 'hash']); | ||
throw new _yerror.default('E_UNEXPECTED_SUCCESS'); | ||
} catch (err) { | ||
_assert.default.equal(err.code, 'E_BAD_INJECTION'); | ||
} | ||
const injectDependencies = await dependencies.$injector(['time', 'hash']); | ||
_assert.default.deepEqual(Object.keys(injectDependencies), ['time', 'hash']); | ||
_assert.default.deepEqual(injectDependencies, { | ||
hash: { | ||
ENV | ||
}, | ||
time | ||
}); | ||
}); | ||
@@ -864,0 +887,0 @@ it('should create dependencies when not declared as singletons', async () => { |
{ | ||
"name": "knifecycle", | ||
"version": "5.1.6", | ||
"version": "5.2.0", | ||
"description": "Manage your NodeJS processes's lifecycle automatically with an unobtrusive dependency injection implementation.", | ||
@@ -101,3 +101,3 @@ "main": "dist/index.js", | ||
"karma-sauce-launcher": "^2.0.2", | ||
"metapak": "^3.1.1", | ||
"metapak": "^3.1.5", | ||
"metapak-nfroidure": "9.5.0", | ||
@@ -111,3 +111,3 @@ "mocha": "^5.2.0", | ||
"debug": "^4.1.1", | ||
"yerror": "^4.0.0" | ||
"yerror": "^4.0.1" | ||
}, | ||
@@ -114,0 +114,0 @@ "config": { |
@@ -591,3 +591,3 @@ [//]: # ( ) | ||
| options | <code>Object</code> | Options for service retrieval | | ||
| options.injectOnly | <code>Boolean</code> | Flag indicating if existing services only should be used | | ||
| options.injectorContext | <code>Boolean</code> | Flag indicating the injection were initiated by the $injector | | ||
| options.autoloading | <code>Boolean</code> | Flag to indicating $autoload dependencies on the fly loading | | ||
@@ -609,3 +609,3 @@ | serviceProvider | <code>String</code> | Service provider. | | ||
| options | <code>Object</code> | Options for service retrieval | | ||
| options.injectOnly | <code>Boolean</code> | Flag indicating if existing services only should be used | | ||
| options.injectorContext | <code>Boolean</code> | Flag indicating the injection were initiated by the $injector | | ||
| options.autoloading | <code>Boolean</code> | Flag to indicating $autoload dependendencies on the fly loading. | | ||
@@ -627,3 +627,3 @@ | ||
| options | <code>Object</code> | Options for service retrieval | | ||
| options.injectOnly | <code>Boolean</code> | Flag indicating if existing services only should be used | | ||
| options.injectorContext | <code>Boolean</code> | Flag indicating the injection were initiated by the $injector | | ||
| options.autoloading | <code>Boolean</code> | Flag to indicating $autoload dependendencies on the fly loading. | | ||
@@ -630,0 +630,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
211380
3763
Updatedyerror@^4.0.1