knifecycle
Advanced tools
Comparing version 2.6.2 to 2.7.0
@@ -0,1 +1,12 @@ | ||
<a name="2.7.0"></a> | ||
# [2.7.0](https://github.com/nfroidure/knifecycle/compare/v2.6.2...v2.7.0) (2018-10-08) | ||
### Features | ||
* **Decorators:** Add a `service` decorator ([3216abf](https://github.com/nfroidure/knifecycle/commit/3216abf)) | ||
* **Decorators:** Add the `constant` decorator ([911898e](https://github.com/nfroidure/knifecycle/commit/911898e)), closes [#60](https://github.com/nfroidure/knifecycle/issues/60) | ||
<a name="2.6.2"></a> | ||
@@ -2,0 +13,0 @@ ## [2.6.2](https://github.com/nfroidure/knifecycle/compare/v2.6.1...v2.6.2) (2018-09-30) |
@@ -6,8 +6,20 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "initializer", { | ||
Object.defineProperty(exports, "SPECIAL_PROPS", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.initializer; | ||
return _util.SPECIAL_PROPS; | ||
} | ||
}); | ||
Object.defineProperty(exports, "DECLARATION_SEPARATOR", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.DECLARATION_SEPARATOR; | ||
} | ||
}); | ||
Object.defineProperty(exports, "OPTIONAL_FLAG", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.OPTIONAL_FLAG; | ||
} | ||
}); | ||
Object.defineProperty(exports, "name", { | ||
@@ -37,2 +49,50 @@ enumerable: true, | ||
}); | ||
Object.defineProperty(exports, "extra", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.extra; | ||
} | ||
}); | ||
Object.defineProperty(exports, "reuseSpecialProps", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.reuseSpecialProps; | ||
} | ||
}); | ||
Object.defineProperty(exports, "initializer", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.initializer; | ||
} | ||
}); | ||
Object.defineProperty(exports, "constant", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.constant; | ||
} | ||
}); | ||
Object.defineProperty(exports, "service", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.service; | ||
} | ||
}); | ||
Object.defineProperty(exports, "provider", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.provider; | ||
} | ||
}); | ||
Object.defineProperty(exports, "handler", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.handler; | ||
} | ||
}); | ||
Object.defineProperty(exports, "wrapInitializer", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.wrapInitializer; | ||
} | ||
}); | ||
exports.Knifecycle = exports.getInstance = exports.default = void 0; | ||
@@ -59,3 +119,2 @@ | ||
const FATAL_ERROR = '$fatalError'; | ||
const ALLOWED_INITIALIZER_TYPES = ['provider', 'service']; | ||
const E_BAD_INITIALIZER_TYPE = 'E_BAD_INITIALIZER_TYPE'; | ||
@@ -72,6 +131,8 @@ const E_BAD_AUTOLOADED_INITIALIZER = 'E_BAD_AUTOLOADED_INITIALIZER'; | ||
const E_BAD_INJECTION = 'E_BAD_INJECTION'; | ||
const E_CONSTANT_INJECTION = 'E_CONSTANT_INJECTION'; | ||
const E_INSTANCE_DESTROYED = 'E_INSTANCE_DESTROYED'; | ||
const E_AUTOLOADER_DYNAMIC_DEPENDENCY = 'E_AUTOLOADER_DYNAMIC_DEPENDENCY'; | ||
const E_BAD_CLASS = 'E_BAD_CLASS'; // Constants that should use Symbol whenever possible | ||
const E_BAD_CLASS = 'E_BAD_CLASS'; | ||
const E_UNDEFINED_CONSTANT_INITIALIZER = 'E_UNDEFINED_CONSTANT_INITIALIZER'; | ||
const E_NON_SINGLETON_CONSTANT_INITIALIZER = 'E_NON_SINGLETON_CONSTANT_INITIALIZER'; | ||
const E_BAD_VALUED_NON_CONSTANT_INITIALIZER = 'E_BAD_VALUED_NON_CONSTANT_INITIALIZER'; // Constants that should use Symbol whenever possible | ||
@@ -148,2 +209,5 @@ const INSTANCE = '__instance'; | ||
* The created/saved instance | ||
* @deprecated | ||
* Simply use `new Knifecycle()` and eventually recreate | ||
* this singleton by your side. | ||
* @example | ||
@@ -187,3 +251,3 @@ * | ||
/** | ||
* Register a constant service | ||
* Register a constant initializer | ||
* @param {String} constantName | ||
@@ -195,2 +259,4 @@ * The name of the service | ||
* The Knifecycle instance (for chaining) | ||
* @deprecated | ||
* Use the `constant` decorator with the `Knifecycle.register` method | ||
* @example | ||
@@ -210,19 +276,3 @@ * | ||
constant(constantName, constantValue) { | ||
const contantLooksLikeAnInitializer = constantValue instanceof Function && constantValue[_util.SPECIAL_PROPS.INJECT]; | ||
if (contantLooksLikeAnInitializer) { | ||
throw new _yerror.default(E_CONSTANT_INJECTION, constantValue[_util.SPECIAL_PROPS.INJECT]); | ||
} | ||
this.register((0, _util.initializer)({ | ||
name: constantName, | ||
options: { | ||
singleton: true | ||
} | ||
}, Promise.resolve.bind(Promise, { | ||
service: constantValue, | ||
dispose: Promise.resolve.bind(Promise) | ||
}))); | ||
debug('Registered a new constant:', constantName); | ||
return this; | ||
return this.register((0, _util.constant)(constantName, constantValue)); | ||
} | ||
@@ -233,4 +283,4 @@ /** | ||
* Service name | ||
* @param {Function} initializer | ||
* An initializer returning the service promise | ||
* @param {Function} serviceBuilder | ||
* An asynchronous function returning the actual service | ||
* @param {Object} options | ||
@@ -240,2 +290,4 @@ * Options attached to the initializer | ||
* The Knifecycle instance (for chaining) | ||
* @deprecated | ||
* Use the `service` decorator with the `Knifecycle.register` method | ||
* @example | ||
@@ -268,10 +320,4 @@ * | ||
service(serviceName, initializer, options) { | ||
this.register((0, _util.reuseSpecialProps)(initializer, initializer, { | ||
[_util.SPECIAL_PROPS.NAME]: serviceName, | ||
[_util.SPECIAL_PROPS.OPTIONS]: options, | ||
[_util.SPECIAL_PROPS.TYPE]: 'service' | ||
}), options); | ||
debug('Registered a new service initializer:', serviceName); | ||
return this; | ||
service(serviceName, serviceBuilder, options) { | ||
return this.register((0, _util.service)(serviceName, serviceBuilder, options)); | ||
} | ||
@@ -288,2 +334,4 @@ /** | ||
* The Knifecycle instance (for chaining) | ||
* @deprecated | ||
* Use the `initializer` decorator with the `Knifecycle.register` method | ||
* @example | ||
@@ -296,3 +344,3 @@ * | ||
* | ||
* $.provider('config', function configProvider() { | ||
* $.register(provider('config', function configProvider() { | ||
* return new Promise((resolve, reject) { | ||
@@ -314,3 +362,3 @@ * fs.readFile('config.js', function(err, data) { | ||
* }); | ||
* }); | ||
* })); | ||
*/ | ||
@@ -324,6 +372,13 @@ | ||
})); | ||
debug('Registered a new service provider:', serviceName); | ||
return this; | ||
} | ||
/** | ||
* Register an initializer | ||
* @param {Function} initializer | ||
* An initializer | ||
* @return {Knifecycle} | ||
* The Knifecycle instance (for chaining) | ||
*/ | ||
register(initializer) { | ||
@@ -336,3 +391,3 @@ if (typeof initializer !== 'function') { | ||
initializer[_util.SPECIAL_PROPS.OPTIONS] = initializer[_util.SPECIAL_PROPS.OPTIONS] || {}; | ||
initializer[_util.SPECIAL_PROPS.TYPE] = initializer[_util.SPECIAL_PROPS.TYPE] || ALLOWED_INITIALIZER_TYPES[0]; | ||
initializer[_util.SPECIAL_PROPS.TYPE] = initializer[_util.SPECIAL_PROPS.TYPE] || _util.ALLOWED_INITIALIZER_TYPES[0]; | ||
@@ -347,9 +402,25 @@ if (!initializer[_util.SPECIAL_PROPS.NAME]) { | ||
if (!ALLOWED_INITIALIZER_TYPES.includes(initializer[_util.SPECIAL_PROPS.TYPE])) { | ||
throw new _yerror.default(E_BAD_INITIALIZER_TYPE, initializer[_util.SPECIAL_PROPS.TYPE], ALLOWED_INITIALIZER_TYPES); | ||
if (!_util.ALLOWED_INITIALIZER_TYPES.includes(initializer[_util.SPECIAL_PROPS.TYPE])) { | ||
throw new _yerror.default(E_BAD_INITIALIZER_TYPE, initializer[_util.SPECIAL_PROPS.NAME], initializer[_util.SPECIAL_PROPS.TYPE], _util.ALLOWED_INITIALIZER_TYPES); | ||
} | ||
if ('service' === initializer[_util.SPECIAL_PROPS.TYPE]) { | ||
if (initializer[_util.SPECIAL_PROPS.TYPE] === 'constant') { | ||
if ('undefined' === typeof initializer[_util.SPECIAL_PROPS.VALUE]) { | ||
throw new _yerror.default(E_UNDEFINED_CONSTANT_INITIALIZER, initializer[_util.SPECIAL_PROPS.NAME]); | ||
} | ||
if (!initializer[_util.SPECIAL_PROPS.OPTIONS].singleton) { | ||
throw new _yerror.default(E_NON_SINGLETON_CONSTANT_INITIALIZER, initializer[_util.SPECIAL_PROPS.NAME]); | ||
} | ||
} else if ('undefined' !== typeof initializer[_util.SPECIAL_PROPS.VALUE]) { | ||
throw new _yerror.default(E_BAD_VALUED_NON_CONSTANT_INITIALIZER, initializer[_util.SPECIAL_PROPS.NAME]); | ||
} // Temporary cast service/constant initializers into | ||
// providers. Best would be to threat each differently | ||
// at dependencies initialization level to boost performances | ||
if ('service' === initializer[_util.SPECIAL_PROPS.TYPE] || 'constant' === initializer[_util.SPECIAL_PROPS.TYPE]) { | ||
initializer = (0, _util.reuseSpecialProps)(initializer, serviceAdapter.bind(null, initializer[_util.SPECIAL_PROPS.NAME], initializer)); | ||
initializer[_util.SPECIAL_PROPS.TYPE] = 'provider'; | ||
initializer[_util.SPECIAL_PROPS.VALUE] = {}.undef; | ||
} | ||
@@ -404,3 +475,3 @@ | ||
* | ||
* import { Knifecycle, inject } from 'knifecycle'; | ||
* import { Knifecycle, inject, constant, service } from 'knifecycle'; | ||
* import appInitializer from './app'; | ||
@@ -410,5 +481,5 @@ * | ||
* | ||
* $.constant('ENV', process.env); | ||
* $.constant('OS', require('os')); | ||
* $.service('app', inject(['ENV', 'OS'], appInitializer)); | ||
* $.register(constant('ENV', process.env)); | ||
* $.register(constant('OS', require('os'))); | ||
* $.register(service('app', inject(['ENV', 'OS'], appInitializer))); | ||
* $.toMermaidGraph(); | ||
@@ -478,3 +549,3 @@ * | ||
* | ||
* $.constant('ENV', process.env); | ||
* $.register(constant('ENV', process.env)); | ||
* $.run(['ENV']) | ||
@@ -481,0 +552,0 @@ * .then(({ ENV }) => { |
@@ -54,11 +54,6 @@ "use strict"; | ||
}); | ||
it('should fail with dependencies since it makes no sense', () => { | ||
_assert.default.throws(() => { | ||
$.constant('time', (0, _index.inject)(['hash3'], time)); | ||
}, /E_CONSTANT_INJECTION/); | ||
}); | ||
}); | ||
describe('service', () => { | ||
it('should register service', () => { | ||
$.service('time', timeService); | ||
$.register((0, _index.service)('time', timeService)); | ||
}); | ||
@@ -93,4 +88,4 @@ }); | ||
fn[_util.SPECIAL_PROPS.NAME] = 'test'; | ||
fn[_util.SPECIAL_PROPS.TYPE] = 'not_allowed_type'; | ||
fn[_index.SPECIAL_PROPS.NAME] = 'test'; | ||
fn[_index.SPECIAL_PROPS.TYPE] = 'not_allowed_type'; | ||
$.register(fn); | ||
@@ -100,3 +95,3 @@ }, err => { | ||
_assert.default.deepEqual(err.params, ['not_allowed_type', ['provider', 'service']]); | ||
_assert.default.deepEqual(err.params, ['test', 'not_allowed_type', _util.ALLOWED_INITIALIZER_TYPES]); | ||
@@ -106,5 +101,59 @@ return true; | ||
}); | ||
it('should fail with an undefined constant', () => { | ||
_assert.default.throws(() => { | ||
const fn = () => {}; | ||
fn[_index.SPECIAL_PROPS.NAME] = 'THE_NUMBER'; | ||
fn[_index.SPECIAL_PROPS.TYPE] = 'constant'; | ||
fn[_index.SPECIAL_PROPS.VALUE] = {}.undef; | ||
fn[_index.SPECIAL_PROPS.OPTIONS] = { | ||
singleton: true | ||
}; | ||
$.register(fn); | ||
}, err => { | ||
_assert.default.deepEqual(err.code, 'E_UNDEFINED_CONSTANT_INITIALIZER'); | ||
_assert.default.deepEqual(err.params, ['THE_NUMBER']); | ||
return true; | ||
}); | ||
}); | ||
it('should fail with a constant that is not a singleton', () => { | ||
_assert.default.throws(() => { | ||
const fn = () => {}; | ||
fn[_index.SPECIAL_PROPS.NAME] = 'THE_NUMBER'; | ||
fn[_index.SPECIAL_PROPS.TYPE] = 'constant'; | ||
fn[_index.SPECIAL_PROPS.VALUE] = NaN; | ||
fn[_index.SPECIAL_PROPS.OPTIONS] = { | ||
singleton: false | ||
}; | ||
$.register(fn); | ||
}, err => { | ||
_assert.default.deepEqual(err.code, 'E_NON_SINGLETON_CONSTANT_INITIALIZER'); | ||
_assert.default.deepEqual(err.params, ['THE_NUMBER']); | ||
return true; | ||
}); | ||
}); | ||
it('should fail with a non constant that has a value', () => { | ||
_assert.default.throws(() => { | ||
const fn = () => {}; | ||
fn[_index.SPECIAL_PROPS.NAME] = 'myService'; | ||
fn[_index.SPECIAL_PROPS.TYPE] = 'service'; | ||
fn[_index.SPECIAL_PROPS.VALUE] = 42; | ||
$.register(fn); | ||
}, err => { | ||
_assert.default.deepEqual(err.code, 'E_BAD_VALUED_NON_CONSTANT_INITIALIZER'); | ||
_assert.default.deepEqual(err.params, ['myService']); | ||
return true; | ||
}); | ||
}); | ||
it('should fail with special autoload intitializer that is not a singleton', () => { | ||
_assert.default.throws(() => { | ||
$.register((0, _util.initializer)({ | ||
$.register((0, _index.initializer)({ | ||
name: '$autoload', | ||
@@ -202,4 +251,4 @@ type: 'provider' | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
const dependencies = yield $.run(['time', 'ENV']); | ||
@@ -217,8 +266,8 @@ | ||
_asyncToGenerator(function* () { | ||
$.service('sample', (0, _index.inject)(['time'], function sampleService({ | ||
$.register((0, _index.service)('sample', (0, _index.inject)(['time'], function sampleService({ | ||
time | ||
}) { | ||
return Promise.resolve(typeof time); | ||
})); | ||
$.constant('time', time); | ||
}))); | ||
$.register((0, _index.constant)('time', time)); | ||
const dependencies = yield $.run(['sample']); | ||
@@ -235,5 +284,5 @@ | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
const dependencies = yield $.run(['time', 'hash']); | ||
@@ -253,6 +302,6 @@ | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('DEBUG', {}); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV', '?DEBUG'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('DEBUG', {})); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV', '?DEBUG'], hashProvider))); | ||
const dependencies = yield $.run(['time', 'hash']); | ||
@@ -273,5 +322,5 @@ | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV', '?DEBUG'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV', '?DEBUG'], hashProvider))); | ||
const dependencies = yield $.run(['time', 'hash']); | ||
@@ -292,10 +341,10 @@ | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('hash1', (0, _index.inject)(['hash'], hashProvider)); | ||
$.provider('hash2', (0, _index.inject)(['hash1'], hashProvider)); | ||
$.provider('hash3', (0, _index.inject)(['hash2'], hashProvider)); | ||
$.provider('hash4', (0, _index.inject)(['hash3'], hashProvider)); | ||
$.provider('hash5', (0, _index.inject)(['hash4'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash4', (0, _index.inject)(['hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash5', (0, _index.inject)(['hash4'], hashProvider))); | ||
const dependencies = yield $.run(['hash5', 'time']); | ||
@@ -310,7 +359,7 @@ | ||
$.constant('ENV', ENV); | ||
$.service('time', timeServiceStub); | ||
$.provider('hash', (0, _index.inject)(['ENV', 'time'], hashProvider)); | ||
$.provider('hash2', (0, _index.inject)(['ENV', 'time'], hashProvider)); | ||
$.provider('hash3', (0, _index.inject)(['ENV', 'time'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.service)('time', timeServiceStub)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV', 'time'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['ENV', 'time'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['ENV', 'time'], hashProvider))); | ||
const dependencies = yield $.run(['hash', 'hash2', 'hash3', 'time']); | ||
@@ -333,4 +382,4 @@ | ||
$.provider('mappedStub', (0, _index.inject)(['stub2>mappedStub2'], providerStub)); | ||
$.provider('mappedStub2', providerStub2); | ||
$.register((0, _index.provider)('mappedStub', (0, _index.inject)(['stub2>mappedStub2'], providerStub))); | ||
$.register((0, _index.provider)('mappedStub2', providerStub2)); | ||
const dependencies = yield $.run(['stub>mappedStub']); | ||
@@ -351,7 +400,7 @@ | ||
$.constant('ENV', ENV); | ||
$.service('aTime', timeServiceStub); | ||
$.provider('aHash', (0, _index.inject)(['ENV', 'time>aTime'], hashProvider)); | ||
$.provider('aHash2', (0, _index.inject)(['ENV', 'hash>aHash'], hashProvider)); | ||
$.provider('aHash3', (0, _index.inject)(['ENV', 'hash>aHash'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.service)('aTime', timeServiceStub)); | ||
$.register((0, _index.provider)('aHash', (0, _index.inject)(['ENV', 'time>aTime'], hashProvider))); | ||
$.register((0, _index.provider)('aHash2', (0, _index.inject)(['ENV', 'hash>aHash'], hashProvider))); | ||
$.register((0, _index.provider)('aHash3', (0, _index.inject)(['ENV', 'hash>aHash'], hashProvider))); | ||
const dependencies = yield $.run(['hash2>aHash2', 'hash3>aHash3', 'time>aTime']); | ||
@@ -366,3 +415,3 @@ | ||
_asyncToGenerator(function* () { | ||
$.service('lol', () => {}); | ||
$.register((0, _index.service)('lol', () => {})); | ||
@@ -381,3 +430,3 @@ try { | ||
_asyncToGenerator(function* () { | ||
$.provider('lol', () => {}); | ||
$.register((0, _index.provider)('lol', () => {})); | ||
@@ -396,3 +445,3 @@ try { | ||
_asyncToGenerator(function* () { | ||
$.provider('lol', () => Promise.resolve()); | ||
$.register((0, _index.provider)('lol', () => Promise.resolve())); | ||
@@ -423,6 +472,6 @@ try { | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV', 'hash2'], hashProvider)); | ||
$.provider('hash2', (0, _index.inject)(['ENV', 'lol'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV', 'hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['ENV', 'lol'], hashProvider))); | ||
@@ -441,7 +490,7 @@ try { | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('db', (0, _index.inject)(['ENV'], dbProvider)); | ||
$.provider('process', (0, _index.inject)(['$fatalError'], processProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('db', (0, _index.inject)(['ENV'], dbProvider))); | ||
$.register((0, _index.provider)('process', (0, _index.inject)(['$fatalError'], processProvider))); | ||
@@ -500,3 +549,3 @@ function processProvider({ | ||
_asyncToGenerator(function* () { | ||
$.register((0, _util.initializer)({ | ||
$.register((0, _index.initializer)({ | ||
type: 'service', | ||
@@ -517,3 +566,3 @@ name: '$autoload', | ||
path: '/path/of/debug', | ||
initializer: (0, _util.initializer)({ | ||
initializer: (0, _index.initializer)({ | ||
type: 'service', | ||
@@ -536,5 +585,5 @@ name: 'DEBUG', | ||
}))); | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV', '?DEBUG'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV', '?DEBUG'], hashProvider))); | ||
const dependencies = yield $.run(['time', 'hash']); | ||
@@ -555,3 +604,3 @@ | ||
_asyncToGenerator(function* () { | ||
$.register((0, _util.initializer)({ | ||
$.register((0, _index.initializer)({ | ||
name: '$autoload', | ||
@@ -571,3 +620,3 @@ type: 'service', | ||
path: `/path/to/${serviceName}`, | ||
initializer: (0, _util.initializer)({ | ||
initializer: (0, _index.initializer)({ | ||
type: 'provider', | ||
@@ -586,8 +635,8 @@ name: serviceName, | ||
}))); | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('hash1', (0, _index.inject)(['hash'], hashProvider)); | ||
$.provider('hash3', (0, _index.inject)(['hash2'], hashProvider)); | ||
$.provider('hash5', (0, _index.inject)(['hash4'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash5', (0, _index.inject)(['hash4'], hashProvider))); | ||
const dependencies = yield $.run(['hash5', 'time']); | ||
@@ -600,6 +649,6 @@ | ||
_asyncToGenerator(function* () { | ||
$.provider('hash', (0, _index.inject)(['hash2'], hashProvider)); | ||
$.provider('hash3', (0, _index.inject)(['?ENV'], hashProvider)); | ||
$.constant('DEBUG', 1); | ||
$.register((0, _util.initializer)({ | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['?ENV'], hashProvider))); | ||
$.register((0, _index.constant)('DEBUG', 1)); | ||
$.register((0, _index.initializer)({ | ||
type: 'service', | ||
@@ -620,3 +669,3 @@ name: '$autoload', | ||
path: '/path/of/debug', | ||
initializer: (0, _util.initializer)({ | ||
initializer: (0, _index.initializer)({ | ||
type: 'service', | ||
@@ -646,3 +695,3 @@ name: 'hash2', | ||
_asyncToGenerator(function* () { | ||
$.register((0, _util.initializer)({ | ||
$.register((0, _index.initializer)({ | ||
name: '$autoload', | ||
@@ -662,3 +711,3 @@ type: 'service', | ||
path: `/path/to/${serviceName}`, | ||
initializer: (0, _util.initializer)({ | ||
initializer: (0, _index.initializer)({ | ||
type: 'provider', | ||
@@ -680,6 +729,6 @@ name: serviceName, | ||
$.constant('ENV', ENV); | ||
$.service('time', timeServiceStub); | ||
$.provider('hash', (0, _index.inject)(['hash1', 'hash2', 'hash3'], hashProvider)); | ||
$.provider('hash_', (0, _index.inject)(['hash1', 'hash2', 'hash3'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.service)('time', timeServiceStub)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['hash1', 'hash2', 'hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash_', (0, _index.inject)(['hash1', 'hash2', 'hash3'], hashProvider))); | ||
const dependencies = yield $.run(['hash', 'hash_', 'hash3']); | ||
@@ -704,3 +753,3 @@ | ||
_asyncToGenerator(function* () { | ||
$.register((0, _util.initializer)({ | ||
$.register((0, _index.initializer)({ | ||
type: 'service', | ||
@@ -741,3 +790,3 @@ name: '$autoload', | ||
_asyncToGenerator(function* () { | ||
$.register((0, _util.initializer)({ | ||
$.register((0, _index.initializer)({ | ||
type: 'service', | ||
@@ -772,3 +821,3 @@ name: '$autoload', | ||
_asyncToGenerator(function* () { | ||
$.register((0, _util.initializer)({ | ||
$.register((0, _index.initializer)({ | ||
type: 'service', | ||
@@ -789,3 +838,3 @@ name: '$autoload', | ||
path: '/path/of/debug', | ||
initializer: (0, _util.initializer)({ | ||
initializer: (0, _index.initializer)({ | ||
type: 'service', | ||
@@ -821,3 +870,3 @@ name: 'not-' + serviceName, | ||
_asyncToGenerator(function* () { | ||
$.register((0, _util.initializer)({ | ||
$.register((0, _index.initializer)({ | ||
type: 'service', | ||
@@ -838,3 +887,3 @@ name: '$autoload', | ||
path: '/path/of/debug', | ||
initializer: (0, _util.initializer)({ | ||
initializer: (0, _index.initializer)({ | ||
type: 'service', | ||
@@ -872,5 +921,5 @@ name: 'DEBUG', | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
const dependencies = yield $.run(['time', 'hash', '$injector']); | ||
@@ -889,5 +938,5 @@ | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
const dependencies = yield $.run(['time', 'hash', '$injector']); | ||
@@ -911,5 +960,5 @@ | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
const dependencies = yield $.run(['time', '$injector']); | ||
@@ -929,4 +978,4 @@ | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
const [{ | ||
@@ -949,9 +998,9 @@ hash | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider), { | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider), { | ||
singleton: true | ||
}); | ||
$.provider('hash2', (0, _index.inject)(['ENV'], (0, _index.options)({ | ||
})); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['ENV'], (0, _index.options)({ | ||
singleton: true | ||
}, hashProvider))); | ||
}, hashProvider)))); | ||
const [{ | ||
@@ -989,9 +1038,9 @@ hash, | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider), { | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider), { | ||
singleton: true | ||
}); | ||
$.provider('hash1', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('hash2', (0, _index.inject)(['ENV'], hashProvider)); | ||
})); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['ENV'], hashProvider))); | ||
const [dependencies] = yield Promise.all([$.run(['$destroy']), $.run(['ENV', 'hash', 'hash1', 'time']), $.run(['ENV', 'hash', 'hash2'])]); | ||
@@ -1006,7 +1055,7 @@ | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('hash1', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('hash2', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['ENV'], hashProvider))); | ||
const dependenciesBuckets = yield Promise.all([$.run(['$destroy']), $.run(['$destroy', 'ENV', 'hash', 'hash1', 'time']), $.run(['$destroy', 'ENV', 'hash', 'hash2'])]); | ||
@@ -1018,7 +1067,7 @@ yield Promise.all(dependenciesBuckets.map(dependencies => dependencies.$destroy())); | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('hash1', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('hash2', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['ENV'], hashProvider))); | ||
const [dependencies1, dependencies2] = yield Promise.all([$.run(['$destroy']), $.run(['$dispose', 'ENV', 'hash', 'hash1', 'time']), $.run(['ENV', 'hash', 'hash2'])]); | ||
@@ -1030,6 +1079,6 @@ yield Promise.all([dependencies2.$dispose(), dependencies1.$destroy()]); | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('hash1', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['ENV'], hashProvider))); | ||
const dependencies = yield $.run(['$destroy']); | ||
@@ -1062,4 +1111,4 @@ | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
const dependencies = yield $.run(['time', 'ENV', '$dispose']); | ||
@@ -1074,5 +1123,5 @@ | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
const dependencies = yield $.run(['time', 'hash', '$dispose']); | ||
@@ -1100,11 +1149,11 @@ | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('hash1', (0, _index.inject)(['hash'], hashProvider)); | ||
$.provider('hash2', (0, _index.inject)(['hash1'], hashProvider)); | ||
$.provider('hash3', (0, _index.inject)(['hash2'], hashProvider)); | ||
$.provider('hash4', (0, _index.inject)(['hash3'], hashProvider)); | ||
$.provider('hash5', (0, _index.inject)(['hash4'], hashProvider)); | ||
$.provider('shutdownChecker', (0, _index.inject)(['hash4'], () => Promise.resolve({ | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash4', (0, _index.inject)(['hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash5', (0, _index.inject)(['hash4'], hashProvider))); | ||
$.register((0, _index.provider)('shutdownChecker', (0, _index.inject)(['hash4'], () => Promise.resolve({ | ||
service: { | ||
@@ -1115,3 +1164,3 @@ shutdownStub, | ||
dispose: shutdownStub | ||
}))); | ||
})))); | ||
const dependencies = yield $.run(['hash5', 'time', '$dispose', 'shutdownChecker']); | ||
@@ -1145,5 +1194,5 @@ | ||
$.constant('ENV', ENV); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('shutdownChecker', (0, _index.inject)(['hash'], () => Promise.resolve({ | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('shutdownChecker', (0, _index.inject)(['hash'], () => Promise.resolve({ | ||
service: { | ||
@@ -1154,5 +1203,5 @@ shutdownStub, | ||
dispose: shutdownStub | ||
}))); | ||
$.provider('hash1', (0, _index.inject)(['shutdownChecker'], hashProvider)); | ||
$.provider('hash2', (0, _index.inject)(['shutdownChecker'], hashProvider)); | ||
})))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['shutdownChecker'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['shutdownChecker'], hashProvider))); | ||
const dependencies = yield $.run(['hash1', 'hash2', '$dispose', 'shutdownChecker']); | ||
@@ -1175,14 +1224,14 @@ | ||
$.provider('hash', () => Promise.resolve({ | ||
$.register((0, _index.provider)('hash', () => Promise.resolve({ | ||
service: {}, | ||
dispose: servicesShutdownCalls.bind(null, 'hash') | ||
})); | ||
$.provider('hash1', (0, _index.inject)(['hash'], () => Promise.resolve({ | ||
}))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], () => Promise.resolve({ | ||
service: {}, | ||
dispose: servicesShutdownCalls.bind(null, 'hash1') | ||
}))); | ||
$.provider('hash2', (0, _index.inject)(['hash1', 'hash'], () => Promise.resolve({ | ||
})))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1', 'hash'], () => Promise.resolve({ | ||
service: {}, | ||
dispose: servicesShutdownCalls.bind(null, 'hash2') | ||
}))); | ||
})))); | ||
const dependencies = yield $.run(['hash2', '$dispose']); | ||
@@ -1199,7 +1248,7 @@ | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider), { | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider), { | ||
singleton: true | ||
}); | ||
})); | ||
const { | ||
@@ -1220,7 +1269,7 @@ hash | ||
_asyncToGenerator(function* () { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider), { | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider), { | ||
singleton: true | ||
}); | ||
})); | ||
const { | ||
@@ -1238,4 +1287,4 @@ hash, | ||
it('should print nothing when no dependency', () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
@@ -1245,10 +1294,10 @@ _assert.default.equal($.toMermaidGraph(), ''); | ||
it('should print a dependency graph', () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('hash1', (0, _index.inject)(['hash'], hashProvider)); | ||
$.provider('hash2', (0, _index.inject)(['hash1'], hashProvider)); | ||
$.provider('hash3', (0, _index.inject)(['hash2'], hashProvider)); | ||
$.provider('hash4', (0, _index.inject)(['hash3'], hashProvider)); | ||
$.provider('hash5', (0, _index.inject)(['hash4'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash4', (0, _index.inject)(['hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash5', (0, _index.inject)(['hash4'], hashProvider))); | ||
@@ -1258,10 +1307,10 @@ _assert.default.equal($.toMermaidGraph(), 'graph TD\n' + ' hash-->ENV\n' + ' hash1-->hash\n' + ' hash2-->hash1\n' + ' hash3-->hash2\n' + ' hash4-->hash3\n' + ' hash5-->hash4'); | ||
it('should allow custom shapes', () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('hash1', (0, _index.inject)(['hash'], hashProvider)); | ||
$.provider('hash2', (0, _index.inject)(['hash1'], hashProvider)); | ||
$.provider('hash3', (0, _index.inject)(['hash2'], hashProvider)); | ||
$.provider('hash4', (0, _index.inject)(['hash3'], hashProvider)); | ||
$.provider('hash5', (0, _index.inject)(['hash4'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash4', (0, _index.inject)(['hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash5', (0, _index.inject)(['hash4'], hashProvider))); | ||
@@ -1282,10 +1331,10 @@ _assert.default.equal($.toMermaidGraph({ | ||
it('should allow custom styles', () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', (0, _index.inject)(['ENV'], hashProvider)); | ||
$.provider('hash1', (0, _index.inject)(['hash'], hashProvider)); | ||
$.provider('hash2', (0, _index.inject)(['hash1'], hashProvider)); | ||
$.provider('hash3', (0, _index.inject)(['hash2'], hashProvider)); | ||
$.provider('hash4', (0, _index.inject)(['hash3'], hashProvider)); | ||
$.provider('hash5', (0, _index.inject)(['hash4'], hashProvider)); | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash4', (0, _index.inject)(['hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash5', (0, _index.inject)(['hash4'], hashProvider))); | ||
@@ -1292,0 +1341,0 @@ _assert.default.equal($.toMermaidGraph({ |
166
dist/util.js
@@ -14,5 +14,8 @@ "use strict"; | ||
exports.initializer = initializer; | ||
exports.constant = constant; | ||
exports.service = service; | ||
exports.provider = provider; | ||
exports.handler = handler; | ||
exports.parseDependencyDeclaration = parseDependencyDeclaration; | ||
exports.OPTIONAL_FLAG = exports.DECLARATION_SEPARATOR = exports.ALLOWED_SPECIAL_PROPS = exports.SPECIAL_PROPS = exports.SPECIAL_PROPS_PREFIX = void 0; | ||
exports.ALLOWED_INITIALIZER_TYPES = exports.OPTIONAL_FLAG = exports.DECLARATION_SEPARATOR = exports.ALLOWED_SPECIAL_PROPS = exports.SPECIAL_PROPS = exports.SPECIAL_PROPS_PREFIX = void 0; | ||
@@ -37,3 +40,4 @@ var _yerror = _interopRequireDefault(require("yerror")); | ||
TYPE: `${SPECIAL_PROPS_PREFIX}type`, | ||
EXTRA: `${SPECIAL_PROPS_PREFIX}extra` | ||
EXTRA: `${SPECIAL_PROPS_PREFIX}extra`, | ||
VALUE: `${SPECIAL_PROPS_PREFIX}value` | ||
}; | ||
@@ -46,2 +50,7 @@ exports.SPECIAL_PROPS = SPECIAL_PROPS; | ||
const OPTIONAL_FLAG = '?'; | ||
exports.OPTIONAL_FLAG = OPTIONAL_FLAG; | ||
const ALLOWED_INITIALIZER_TYPES = ['provider', 'service', 'constant']; | ||
exports.ALLOWED_INITIALIZER_TYPES = ALLOWED_INITIALIZER_TYPES; | ||
const E_BAD_INJECT_IN_CONSTANT = 'E_BAD_INJECT_IN_CONSTANT'; | ||
const E_CONSTANT_INJECTION = 'E_CONSTANT_INJECTION'; | ||
/** | ||
@@ -55,4 +64,2 @@ * Apply special props to the given function from another one | ||
exports.OPTIONAL_FLAG = OPTIONAL_FLAG; | ||
function reuseSpecialProps(from, to, amend = {}) { | ||
@@ -74,3 +81,3 @@ return [...new Set(Object.keys(from).concat(Object.keys(amend)))].filter(prop => prop.startsWith(SPECIAL_PROPS_PREFIX)).reduce((fn, prop) => { | ||
/** | ||
* Allows to wrap an initializer to add extra | ||
* Allows to wrap an initializer to add extra initialization steps | ||
* @param {Function} wrapper | ||
@@ -127,2 +134,6 @@ * A function taking dependencies and the base | ||
function inject(dependenciesDeclarations, initializer, merge = false) { | ||
if ('constant' === initializer[SPECIAL_PROPS.TYPE]) { | ||
throw new _yerror.default(E_BAD_INJECT_IN_CONSTANT, initializer[SPECIAL_PROPS.NAME], dependenciesDeclarations); | ||
} | ||
const uniqueInitializer = reuseSpecialProps(initializer, initializer, { | ||
@@ -296,2 +307,140 @@ [SPECIAL_PROPS.INJECT]: merge ? (initializer[SPECIAL_PROPS.INJECT] || []).concat(dependenciesDeclarations) : dependenciesDeclarations | ||
/** | ||
* Decorator that creates an initializer for a constant value | ||
* @param {String} name | ||
* The constant's name. | ||
* @param {any} initializer | ||
* The constant's value | ||
* @return {Function} | ||
* Returns a new initializer | ||
* @example | ||
* import { Knifecycle, constant, service } from 'knifecycle'; | ||
* | ||
* const { printAnswer } = new Knifecycle() | ||
* .register(constant('THE_NUMBER', value)) | ||
* .register(constant('log', console.log.bind(console))) | ||
* .register(service( | ||
* 'printAnswer', | ||
* async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
* { | ||
* inject: ['THE_NUMBER', 'log'], | ||
* } | ||
* .run(['printAnswer']); | ||
* | ||
* printAnswer(); // 42 | ||
*/ | ||
function constant(name, value) { | ||
const contantLooksLikeAnInitializer = value instanceof Function && value[SPECIAL_PROPS.INJECT]; | ||
if (contantLooksLikeAnInitializer) { | ||
throw new _yerror.default(E_CONSTANT_INJECTION, value[SPECIAL_PROPS.INJECT]); | ||
} | ||
const uniqueInitializer = initializer({ | ||
name: name, | ||
type: 'constant', | ||
options: { | ||
singleton: true | ||
}, | ||
inject: [], | ||
value: value | ||
}, deliverConstantValue.bind(null, value)); | ||
debug(`Created an initializer from a constant: ${name}.`); | ||
return uniqueInitializer; | ||
} | ||
/** | ||
* Decorator that creates an initializer for a service | ||
* @param {String} name | ||
* The service's name. | ||
* @param {Function} initializer | ||
* An initializer returning the service promise | ||
* @param {Object} options | ||
* Options attached to the initializer | ||
* @return {Function} | ||
* Returns a new initializer | ||
* @example | ||
* import { Knifecycle, constant, service } from 'knifecycle'; | ||
* | ||
* const { printAnswer } = new Knifecycle() | ||
* .register(constant('THE_NUMBER', value)) | ||
* .register(constant('log', console.log.bind(console))) | ||
* .register(service( | ||
* 'printAnswer', | ||
* async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
* { | ||
* inject: ['THE_NUMBER', 'log'], | ||
* } | ||
* .run(['printAnswer']); | ||
* | ||
* printAnswer(); // 42 | ||
*/ | ||
function service(serviceName, serviceBuilder, options = {}) { | ||
const uniqueInitializer = reuseSpecialProps(serviceBuilder, serviceBuilder, { | ||
[SPECIAL_PROPS.NAME]: serviceName, | ||
[SPECIAL_PROPS.TYPE]: 'service', | ||
[SPECIAL_PROPS.OPTIONS]: options | ||
}); | ||
debug(`Created an initializer from a service builder: ${name}.`); | ||
return uniqueInitializer; | ||
} | ||
/** | ||
* Decorator that creates an initializer for a provider | ||
* @param {String} name | ||
* The provider's name. | ||
* @param {Function} provider | ||
* A provider returning the service builder promise | ||
* @param {Object} options | ||
* Options attached to the initializer | ||
* @return {Function} | ||
* Returns a new initializer | ||
* @example | ||
* | ||
* import Knifecycle from 'knifecycle' | ||
* import fs from 'fs'; | ||
* | ||
* const $ = new Knifecycle(); | ||
* | ||
* $.register(provider('config', async function configProvider() { | ||
* return new Promise((resolve, reject) { | ||
* fs.readFile('config.js', function(err, data) { | ||
* let config; | ||
* | ||
* if(err) { | ||
* reject(err); | ||
* return; | ||
* } | ||
* | ||
* try { | ||
* config = JSON.parse(data.toString); | ||
* } catch (err) { | ||
* reject(err); | ||
* return; | ||
* } | ||
* | ||
* resolve({ | ||
* service: config, | ||
* }); | ||
* }); | ||
* }); | ||
* })); | ||
*/ | ||
function provider(providerName, provider, options = {}) { | ||
const uniqueInitializer = reuseSpecialProps(provider, provider, { | ||
[SPECIAL_PROPS.NAME]: providerName, | ||
[SPECIAL_PROPS.TYPE]: 'provider', | ||
[SPECIAL_PROPS.OPTIONS]: options | ||
}); | ||
debug(`Created an initializer from a provider builder: ${name}.`); | ||
return uniqueInitializer; | ||
} | ||
function deliverConstantValue(_x2) { | ||
return _deliverConstantValue.apply(this, arguments); | ||
} | ||
/** | ||
* Shortcut to create an initializer with a simple handler | ||
@@ -321,2 +470,9 @@ * @param {Function} handlerFunction | ||
function _deliverConstantValue() { | ||
_deliverConstantValue = _asyncToGenerator(function* (value) { | ||
return value; | ||
}); | ||
return _deliverConstantValue.apply(this, arguments); | ||
} | ||
function handler(handlerFunction, dependencies = [], extra) { | ||
@@ -323,0 +479,0 @@ if (!handlerFunction.name) { |
@@ -252,2 +252,71 @@ "use strict"; | ||
}); | ||
describe('constant', () => { | ||
it('should allow to create an initializer from a constant', | ||
/*#__PURE__*/ | ||
_asyncToGenerator(function* () { | ||
const baseValue = 'THE_VALUE'; | ||
const baseName = 42; | ||
const newInitializer = (0, _util.constant)(baseName, baseValue); | ||
_assert.default.notEqual(newInitializer, aProvider); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], []); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], { | ||
singleton: true | ||
}); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.NAME], baseName); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.TYPE], 'constant'); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.VALUE], baseValue); | ||
_assert.default.equal((yield newInitializer()), baseValue); | ||
})); | ||
it('should fail with dependencies since it makes no sense', () => { | ||
_assert.default.throws(() => { | ||
(0, _util.constant)('time', (0, _util.inject)(['hash3'], | ||
/*#__PURE__*/ | ||
_asyncToGenerator(function* () {}))); | ||
}, /E_CONSTANT_INJECTION/); | ||
}); | ||
}); | ||
describe('service', () => { | ||
it('should allow to create an initializer from a service builder', | ||
/*#__PURE__*/ | ||
_asyncToGenerator(function* () { | ||
const aServiceBuilder = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref5 = _asyncToGenerator(function* () {}); | ||
return function aServiceBuilder() { | ||
return _ref5.apply(this, arguments); | ||
}; | ||
}(); | ||
const dependencies = ['ANOTHER_ENV>ENV']; | ||
const baseOptions = { | ||
singleton: true | ||
}; | ||
const baseName = 'hash'; | ||
const baseType = 'service'; | ||
const newInitializer = (0, _util.service)(baseName, (0, _util.inject)(dependencies, aServiceBuilder), baseOptions); | ||
_assert.default.notEqual(newInitializer, aProvider); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], dependencies); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], dependencies); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], baseOptions); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], baseOptions); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.NAME], baseName); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.TYPE], baseType); | ||
})); | ||
}); | ||
describe('handler', () => { | ||
@@ -254,0 +323,0 @@ it('should work', |
{ | ||
"name": "knifecycle", | ||
"version": "2.6.2", | ||
"version": "2.7.0", | ||
"description": "Manage your NodeJS processes's lifecycle.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
174
README.md
@@ -374,3 +374,3 @@ [//]: # ( ) | ||
<dt><a href="#wrapInitializer">wrapInitializer(wrapper, baseInitializer)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Allows to wrap an initializer to add extra</p> | ||
<dd><p>Allows to wrap an initializer to add extra initialization steps</p> | ||
</dd> | ||
@@ -400,2 +400,11 @@ <dt><a href="#inject">inject(dependenciesDeclarations, initializer, [merge])</a> ⇒ <code>function</code></dt> | ||
</dd> | ||
<dt><a href="#constant">constant(name, initializer)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Decorator that creates an initializer for a constant value</p> | ||
</dd> | ||
<dt><a href="#service">service(name, initializer, options)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Decorator that creates an initializer for a service</p> | ||
</dd> | ||
<dt><a href="#provider">provider(name, provider, options)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Decorator that creates an initializer for a provider</p> | ||
</dd> | ||
<dt><a href="#handler">handler(handlerFunction, [dependencies], [extra])</a> ⇒ <code>function</code></dt> | ||
@@ -417,5 +426,6 @@ <dd><p>Shortcut to create an initializer with a simple handler</p> | ||
* _instance_ | ||
* [.constant(constantName, constantValue)](#Knifecycle+constant) ⇒ [<code>Knifecycle</code>](#Knifecycle) | ||
* [.service(serviceName, initializer, options)](#Knifecycle+service) ⇒ [<code>Knifecycle</code>](#Knifecycle) | ||
* [.provider(serviceName, initializer, options)](#Knifecycle+provider) ⇒ [<code>Knifecycle</code>](#Knifecycle) | ||
* ~~[.constant(constantName, constantValue)](#Knifecycle+constant) ⇒ [<code>Knifecycle</code>](#Knifecycle)~~ | ||
* ~~[.service(serviceName, serviceBuilder, options)](#Knifecycle+service) ⇒ [<code>Knifecycle</code>](#Knifecycle)~~ | ||
* ~~[.provider(serviceName, initializer, options)](#Knifecycle+provider) ⇒ [<code>Knifecycle</code>](#Knifecycle)~~ | ||
* [.register(initializer)](#Knifecycle+register) ⇒ [<code>Knifecycle</code>](#Knifecycle) | ||
* [.toMermaidGraph(options)](#Knifecycle+toMermaidGraph) ⇒ <code>String</code> | ||
@@ -427,3 +437,3 @@ * [.run(dependenciesDeclarations)](#Knifecycle+run) ⇒ <code>Promise</code> | ||
* _static_ | ||
* [.getInstance()](#Knifecycle.getInstance) ⇒ [<code>Knifecycle</code>](#Knifecycle) | ||
* ~~[.getInstance()](#Knifecycle.getInstance) ⇒ [<code>Knifecycle</code>](#Knifecycle)~~ | ||
@@ -444,5 +454,7 @@ <a name="new_Knifecycle_new"></a> | ||
### knifecycle.constant(constantName, constantValue) ⇒ [<code>Knifecycle</code>](#Knifecycle) | ||
Register a constant service | ||
### ~~knifecycle.constant(constantName, constantValue) ⇒ [<code>Knifecycle</code>](#Knifecycle)~~ | ||
***Deprecated*** | ||
Register a constant initializer | ||
**Kind**: instance method of [<code>Knifecycle</code>](#Knifecycle) | ||
@@ -469,3 +481,5 @@ **Returns**: [<code>Knifecycle</code>](#Knifecycle) - The Knifecycle instance (for chaining) | ||
### knifecycle.service(serviceName, initializer, options) ⇒ [<code>Knifecycle</code>](#Knifecycle) | ||
### ~~knifecycle.service(serviceName, serviceBuilder, options) ⇒ [<code>Knifecycle</code>](#Knifecycle)~~ | ||
***Deprecated*** | ||
Register a service initializer | ||
@@ -479,3 +493,3 @@ | ||
| serviceName | <code>String</code> | Service name | | ||
| initializer | <code>function</code> | An initializer returning the service promise | | ||
| serviceBuilder | <code>function</code> | An asynchronous function returning the actual service | | ||
| options | <code>Object</code> | Options attached to the initializer | | ||
@@ -510,3 +524,5 @@ | ||
### knifecycle.provider(serviceName, initializer, options) ⇒ [<code>Knifecycle</code>](#Knifecycle) | ||
### ~~knifecycle.provider(serviceName, initializer, options) ⇒ [<code>Knifecycle</code>](#Knifecycle)~~ | ||
***Deprecated*** | ||
Register a provider initializer | ||
@@ -530,3 +546,3 @@ | ||
$.provider('config', function configProvider() { | ||
$.register(provider('config', function configProvider() { | ||
return new Promise((resolve, reject) { | ||
@@ -548,4 +564,16 @@ fs.readFile('config.js', function(err, data) { | ||
}); | ||
}); | ||
})); | ||
``` | ||
<a name="Knifecycle+register"></a> | ||
### knifecycle.register(initializer) ⇒ [<code>Knifecycle</code>](#Knifecycle) | ||
Register an initializer | ||
**Kind**: instance method of [<code>Knifecycle</code>](#Knifecycle) | ||
**Returns**: [<code>Knifecycle</code>](#Knifecycle) - The Knifecycle instance (for chaining) | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| initializer | <code>function</code> | An initializer | | ||
<a name="Knifecycle+toMermaidGraph"></a> | ||
@@ -569,3 +597,3 @@ | ||
```js | ||
import { Knifecycle, inject } from 'knifecycle'; | ||
import { Knifecycle, inject, constant, service } from 'knifecycle'; | ||
import appInitializer from './app'; | ||
@@ -575,5 +603,5 @@ | ||
$.constant('ENV', process.env); | ||
$.constant('OS', require('os')); | ||
$.service('app', inject(['ENV', 'OS'], appInitializer)); | ||
$.register(constant('ENV', process.env)); | ||
$.register(constant('OS', require('os'))); | ||
$.register(service('app', inject(['ENV', 'OS'], appInitializer))); | ||
$.toMermaidGraph(); | ||
@@ -604,3 +632,3 @@ | ||
$.constant('ENV', process.env); | ||
$.register(constant('ENV', process.env)); | ||
$.run(['ENV']) | ||
@@ -663,3 +691,5 @@ .then(({ ENV }) => { | ||
### Knifecycle.getInstance() ⇒ [<code>Knifecycle</code>](#Knifecycle) | ||
### ~~Knifecycle.getInstance() ⇒ [<code>Knifecycle</code>](#Knifecycle)~~ | ||
***Deprecated*** | ||
Returns a Knifecycle instance (always the same) | ||
@@ -714,3 +744,3 @@ | ||
## wrapInitializer(wrapper, baseInitializer) ⇒ <code>function</code> | ||
Allows to wrap an initializer to add extra | ||
Allows to wrap an initializer to add extra initialization steps | ||
@@ -879,2 +909,108 @@ **Kind**: global function | ||
``` | ||
<a name="constant"></a> | ||
## constant(name, initializer) ⇒ <code>function</code> | ||
Decorator that creates an initializer for a constant value | ||
**Kind**: global function | ||
**Returns**: <code>function</code> - Returns a new initializer | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| name | <code>String</code> | The constant's name. | | ||
| initializer | <code>any</code> | The constant's value | | ||
**Example** | ||
```js | ||
import { Knifecycle, constant, service } from 'knifecycle'; | ||
const { printAnswer } = new Knifecycle() | ||
.register(constant('THE_NUMBER', value)) | ||
.register(constant('log', console.log.bind(console))) | ||
.register(service( | ||
'printAnswer', | ||
async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
{ | ||
inject: ['THE_NUMBER', 'log'], | ||
} | ||
.run(['printAnswer']); | ||
printAnswer(); // 42 | ||
``` | ||
<a name="service"></a> | ||
## service(name, initializer, options) ⇒ <code>function</code> | ||
Decorator that creates an initializer for a service | ||
**Kind**: global function | ||
**Returns**: <code>function</code> - Returns a new initializer | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| name | <code>String</code> | The service's name. | | ||
| initializer | <code>function</code> | An initializer returning the service promise | | ||
| options | <code>Object</code> | Options attached to the initializer | | ||
**Example** | ||
```js | ||
import { Knifecycle, constant, service } from 'knifecycle'; | ||
const { printAnswer } = new Knifecycle() | ||
.register(constant('THE_NUMBER', value)) | ||
.register(constant('log', console.log.bind(console))) | ||
.register(service( | ||
'printAnswer', | ||
async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
{ | ||
inject: ['THE_NUMBER', 'log'], | ||
} | ||
.run(['printAnswer']); | ||
printAnswer(); // 42 | ||
``` | ||
<a name="provider"></a> | ||
## provider(name, provider, options) ⇒ <code>function</code> | ||
Decorator that creates an initializer for a provider | ||
**Kind**: global function | ||
**Returns**: <code>function</code> - Returns a new initializer | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| name | <code>String</code> | The provider's name. | | ||
| provider | <code>function</code> | A provider returning the service builder promise | | ||
| options | <code>Object</code> | Options attached to the initializer | | ||
**Example** | ||
```js | ||
import Knifecycle from 'knifecycle' | ||
import fs from 'fs'; | ||
const $ = new Knifecycle(); | ||
$.register(provider('config', async function configProvider() { | ||
return new Promise((resolve, reject) { | ||
fs.readFile('config.js', function(err, data) { | ||
let config; | ||
if(err) { | ||
reject(err); | ||
return; | ||
} | ||
try { | ||
config = JSON.parse(data.toString); | ||
} catch (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve({ | ||
service: config, | ||
}); | ||
}); | ||
}); | ||
})); | ||
``` | ||
<a name="handler"></a> | ||
@@ -881,0 +1017,0 @@ |
146
src/index.js
/* eslint max-len: ["warn", { "ignoreComments": true }] */ | ||
import { | ||
SPECIAL_PROPS, | ||
reuseSpecialProps, | ||
initializer, | ||
ALLOWED_INITIALIZER_TYPES, | ||
DECLARATION_SEPARATOR, | ||
OPTIONAL_FLAG, | ||
name, | ||
@@ -10,2 +11,10 @@ inject, | ||
options, | ||
extra, | ||
reuseSpecialProps, | ||
initializer, | ||
constant, | ||
service, | ||
provider, | ||
handler, | ||
wrapInitializer, | ||
parseDependencyDeclaration, | ||
@@ -25,4 +34,2 @@ } from './util'; | ||
const ALLOWED_INITIALIZER_TYPES = ['provider', 'service']; | ||
const E_BAD_INITIALIZER_TYPE = 'E_BAD_INITIALIZER_TYPE'; | ||
@@ -39,6 +46,10 @@ const E_BAD_AUTOLOADED_INITIALIZER = 'E_BAD_AUTOLOADED_INITIALIZER'; | ||
const E_BAD_INJECTION = 'E_BAD_INJECTION'; | ||
const E_CONSTANT_INJECTION = 'E_CONSTANT_INJECTION'; | ||
const E_INSTANCE_DESTROYED = 'E_INSTANCE_DESTROYED'; | ||
const E_AUTOLOADER_DYNAMIC_DEPENDENCY = 'E_AUTOLOADER_DYNAMIC_DEPENDENCY'; | ||
const E_BAD_CLASS = 'E_BAD_CLASS'; | ||
const E_UNDEFINED_CONSTANT_INITIALIZER = 'E_UNDEFINED_CONSTANT_INITIALIZER'; | ||
const E_NON_SINGLETON_CONSTANT_INITIALIZER = | ||
'E_NON_SINGLETON_CONSTANT_INITIALIZER'; | ||
const E_BAD_VALUED_NON_CONSTANT_INITIALIZER = | ||
'E_BAD_VALUED_NON_CONSTANT_INITIALIZER'; | ||
@@ -137,2 +148,5 @@ // Constants that should use Symbol whenever possible | ||
* The created/saved instance | ||
* @deprecated | ||
* Simply use `new Knifecycle()` and eventually recreate | ||
* this singleton by your side. | ||
* @example | ||
@@ -178,3 +192,3 @@ * | ||
/** | ||
* Register a constant service | ||
* Register a constant initializer | ||
* @param {String} constantName | ||
@@ -186,2 +200,4 @@ * The name of the service | ||
* The Knifecycle instance (for chaining) | ||
* @deprecated | ||
* Use the `constant` decorator with the `Knifecycle.register` method | ||
* @example | ||
@@ -199,28 +215,3 @@ * | ||
constant(constantName, constantValue) { | ||
const contantLooksLikeAnInitializer = | ||
constantValue instanceof Function && constantValue[SPECIAL_PROPS.INJECT]; | ||
if (contantLooksLikeAnInitializer) { | ||
throw new YError( | ||
E_CONSTANT_INJECTION, | ||
constantValue[SPECIAL_PROPS.INJECT], | ||
); | ||
} | ||
this.register( | ||
initializer( | ||
{ | ||
name: constantName, | ||
options: { singleton: true }, | ||
}, | ||
Promise.resolve.bind(Promise, { | ||
service: constantValue, | ||
dispose: Promise.resolve.bind(Promise), | ||
}), | ||
), | ||
); | ||
debug('Registered a new constant:', constantName); | ||
return this; | ||
return this.register(constant(constantName, constantValue)); | ||
} | ||
@@ -232,4 +223,4 @@ | ||
* Service name | ||
* @param {Function} initializer | ||
* An initializer returning the service promise | ||
* @param {Function} serviceBuilder | ||
* An asynchronous function returning the actual service | ||
* @param {Object} options | ||
@@ -239,2 +230,4 @@ * Options attached to the initializer | ||
* The Knifecycle instance (for chaining) | ||
* @deprecated | ||
* Use the `service` decorator with the `Knifecycle.register` method | ||
* @example | ||
@@ -265,13 +258,4 @@ * | ||
*/ | ||
service(serviceName, initializer, options) { | ||
this.register( | ||
reuseSpecialProps(initializer, initializer, { | ||
[SPECIAL_PROPS.NAME]: serviceName, | ||
[SPECIAL_PROPS.OPTIONS]: options, | ||
[SPECIAL_PROPS.TYPE]: 'service', | ||
}), | ||
options, | ||
); | ||
debug('Registered a new service initializer:', serviceName); | ||
return this; | ||
service(serviceName, serviceBuilder, options) { | ||
return this.register(service(serviceName, serviceBuilder, options)); | ||
} | ||
@@ -289,2 +273,4 @@ | ||
* The Knifecycle instance (for chaining) | ||
* @deprecated | ||
* Use the `initializer` decorator with the `Knifecycle.register` method | ||
* @example | ||
@@ -297,3 +283,3 @@ * | ||
* | ||
* $.provider('config', function configProvider() { | ||
* $.register(provider('config', function configProvider() { | ||
* return new Promise((resolve, reject) { | ||
@@ -315,3 +301,3 @@ * fs.readFile('config.js', function(err, data) { | ||
* }); | ||
* }); | ||
* })); | ||
*/ | ||
@@ -325,6 +311,12 @@ provider(serviceName, initializer, options = {}) { | ||
); | ||
debug('Registered a new service provider:', serviceName); | ||
return this; | ||
} | ||
/** | ||
* Register an initializer | ||
* @param {Function} initializer | ||
* An initializer | ||
* @return {Knifecycle} | ||
* The Knifecycle instance (for chaining) | ||
*/ | ||
register(initializer) { | ||
@@ -351,2 +343,3 @@ if (typeof initializer !== 'function') { | ||
E_BAD_INITIALIZER_TYPE, | ||
initializer[SPECIAL_PROPS.NAME], | ||
initializer[SPECIAL_PROPS.TYPE], | ||
@@ -356,4 +349,29 @@ ALLOWED_INITIALIZER_TYPES, | ||
} | ||
if (initializer[SPECIAL_PROPS.TYPE] === 'constant') { | ||
if ('undefined' === typeof initializer[SPECIAL_PROPS.VALUE]) { | ||
throw new YError( | ||
E_UNDEFINED_CONSTANT_INITIALIZER, | ||
initializer[SPECIAL_PROPS.NAME], | ||
); | ||
} | ||
if (!initializer[SPECIAL_PROPS.OPTIONS].singleton) { | ||
throw new YError( | ||
E_NON_SINGLETON_CONSTANT_INITIALIZER, | ||
initializer[SPECIAL_PROPS.NAME], | ||
); | ||
} | ||
} else if ('undefined' !== typeof initializer[SPECIAL_PROPS.VALUE]) { | ||
throw new YError( | ||
E_BAD_VALUED_NON_CONSTANT_INITIALIZER, | ||
initializer[SPECIAL_PROPS.NAME], | ||
); | ||
} | ||
if ('service' === initializer[SPECIAL_PROPS.TYPE]) { | ||
// Temporary cast service/constant initializers into | ||
// providers. Best would be to threat each differently | ||
// at dependencies initialization level to boost performances | ||
if ( | ||
'service' === initializer[SPECIAL_PROPS.TYPE] || | ||
'constant' === initializer[SPECIAL_PROPS.TYPE] | ||
) { | ||
initializer = reuseSpecialProps( | ||
@@ -364,2 +382,3 @@ initializer, | ||
initializer[SPECIAL_PROPS.TYPE] = 'provider'; | ||
initializer[SPECIAL_PROPS.VALUE] = {}.undef; | ||
} | ||
@@ -432,3 +451,3 @@ | ||
* | ||
* import { Knifecycle, inject } from 'knifecycle'; | ||
* import { Knifecycle, inject, constant, service } from 'knifecycle'; | ||
* import appInitializer from './app'; | ||
@@ -438,5 +457,5 @@ * | ||
* | ||
* $.constant('ENV', process.env); | ||
* $.constant('OS', require('os')); | ||
* $.service('app', inject(['ENV', 'OS'], appInitializer)); | ||
* $.register(constant('ENV', process.env)); | ||
* $.register(constant('OS', require('os'))); | ||
* $.register(service('app', inject(['ENV', 'OS'], appInitializer))); | ||
* $.toMermaidGraph(); | ||
@@ -520,3 +539,3 @@ * | ||
* | ||
* $.constant('ENV', process.env); | ||
* $.register(constant('ENV', process.env)); | ||
* $.run(['ENV']) | ||
@@ -984,3 +1003,20 @@ * .then(({ ENV }) => { | ||
export const getInstance = Knifecycle.getInstance; | ||
export { Knifecycle, initializer, name, inject, type, options }; | ||
export { | ||
SPECIAL_PROPS, | ||
DECLARATION_SEPARATOR, | ||
OPTIONAL_FLAG, | ||
Knifecycle, | ||
initializer, | ||
name, | ||
type, | ||
inject, | ||
options, | ||
extra, | ||
reuseSpecialProps, | ||
wrapInitializer, | ||
constant, | ||
service, | ||
provider, | ||
handler, | ||
}; | ||
@@ -987,0 +1023,0 @@ function _pickServiceNameFromDeclaration(dependencyDeclaration) { |
@@ -7,4 +7,13 @@ /* eslint max-nested-callbacks:0 */ | ||
import { Knifecycle, inject, options } from './index'; | ||
import { initializer, SPECIAL_PROPS } from './util'; | ||
import { | ||
SPECIAL_PROPS, | ||
Knifecycle, | ||
initializer, | ||
inject, | ||
options, | ||
constant, | ||
service, | ||
provider, | ||
} from './index'; | ||
import { ALLOWED_INITIALIZER_TYPES } from './util'; | ||
@@ -50,8 +59,2 @@ describe('Knifecycle', () => { | ||
}); | ||
it('should fail with dependencies since it makes no sense', () => { | ||
assert.throws(() => { | ||
$.constant('time', inject(['hash3'], time)); | ||
}, /E_CONSTANT_INJECTION/); | ||
}); | ||
}); | ||
@@ -61,3 +64,3 @@ | ||
it('should register service', () => { | ||
$.service('time', timeService); | ||
$.register(service('time', timeService)); | ||
}); | ||
@@ -104,4 +107,5 @@ }); | ||
assert.deepEqual(err.params, [ | ||
'test', | ||
'not_allowed_type', | ||
['provider', 'service'], | ||
ALLOWED_INITIALIZER_TYPES, | ||
]); | ||
@@ -113,2 +117,55 @@ return true; | ||
it('should fail with an undefined constant', () => { | ||
assert.throws( | ||
() => { | ||
const fn = () => {}; | ||
fn[SPECIAL_PROPS.NAME] = 'THE_NUMBER'; | ||
fn[SPECIAL_PROPS.TYPE] = 'constant'; | ||
fn[SPECIAL_PROPS.VALUE] = {}.undef; | ||
fn[SPECIAL_PROPS.OPTIONS] = { singleton: true }; | ||
$.register(fn); | ||
}, | ||
err => { | ||
assert.deepEqual(err.code, 'E_UNDEFINED_CONSTANT_INITIALIZER'); | ||
assert.deepEqual(err.params, ['THE_NUMBER']); | ||
return true; | ||
}, | ||
); | ||
}); | ||
it('should fail with a constant that is not a singleton', () => { | ||
assert.throws( | ||
() => { | ||
const fn = () => {}; | ||
fn[SPECIAL_PROPS.NAME] = 'THE_NUMBER'; | ||
fn[SPECIAL_PROPS.TYPE] = 'constant'; | ||
fn[SPECIAL_PROPS.VALUE] = NaN; | ||
fn[SPECIAL_PROPS.OPTIONS] = { singleton: false }; | ||
$.register(fn); | ||
}, | ||
err => { | ||
assert.deepEqual(err.code, 'E_NON_SINGLETON_CONSTANT_INITIALIZER'); | ||
assert.deepEqual(err.params, ['THE_NUMBER']); | ||
return true; | ||
}, | ||
); | ||
}); | ||
it('should fail with a non constant that has a value', () => { | ||
assert.throws( | ||
() => { | ||
const fn = () => {}; | ||
fn[SPECIAL_PROPS.NAME] = 'myService'; | ||
fn[SPECIAL_PROPS.TYPE] = 'service'; | ||
fn[SPECIAL_PROPS.VALUE] = 42; | ||
$.register(fn); | ||
}, | ||
err => { | ||
assert.deepEqual(err.code, 'E_BAD_VALUED_NON_CONSTANT_INITIALIZER'); | ||
assert.deepEqual(err.params, ['myService']); | ||
return true; | ||
}, | ||
); | ||
}); | ||
it('should fail with special autoload intitializer that is not a singleton', () => { | ||
@@ -230,4 +287,4 @@ assert.throws( | ||
it('should work with constant dependencies', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
@@ -244,9 +301,11 @@ const dependencies = await $.run(['time', 'ENV']); | ||
it('should work with service dependencies', async () => { | ||
$.service( | ||
'sample', | ||
inject(['time'], function sampleService({ time }) { | ||
return Promise.resolve(typeof time); | ||
}), | ||
$.register( | ||
service( | ||
'sample', | ||
inject(['time'], function sampleService({ time }) { | ||
return Promise.resolve(typeof time); | ||
}), | ||
), | ||
); | ||
$.constant('time', time); | ||
$.register(constant('time', time)); | ||
@@ -262,5 +321,5 @@ const dependencies = await $.run(['sample']); | ||
it('should work with simple dependencies', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
@@ -277,6 +336,6 @@ const dependencies = await $.run(['time', 'hash']); | ||
it('should work with given optional dependencies', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('DEBUG', {}); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV', '?DEBUG'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('DEBUG', {})); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV', '?DEBUG'], hashProvider))); | ||
@@ -293,5 +352,5 @@ const dependencies = await $.run(['time', 'hash']); | ||
it('should work with lacking optional dependencies', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV', '?DEBUG'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV', '?DEBUG'], hashProvider))); | ||
@@ -308,10 +367,10 @@ const dependencies = await $.run(['time', 'hash']); | ||
it('should work with deeper dependencies', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.provider('hash1', inject(['hash'], hashProvider)); | ||
$.provider('hash2', inject(['hash1'], hashProvider)); | ||
$.provider('hash3', inject(['hash2'], hashProvider)); | ||
$.provider('hash4', inject(['hash3'], hashProvider)); | ||
$.provider('hash5', inject(['hash4'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
$.register(provider('hash1', inject(['hash'], hashProvider))); | ||
$.register(provider('hash2', inject(['hash1'], hashProvider))); | ||
$.register(provider('hash3', inject(['hash2'], hashProvider))); | ||
$.register(provider('hash4', inject(['hash3'], hashProvider))); | ||
$.register(provider('hash5', inject(['hash4'], hashProvider))); | ||
@@ -326,7 +385,7 @@ const dependencies = await $.run(['hash5', 'time']); | ||
$.constant('ENV', ENV); | ||
$.service('time', timeServiceStub); | ||
$.provider('hash', inject(['ENV', 'time'], hashProvider)); | ||
$.provider('hash2', inject(['ENV', 'time'], hashProvider)); | ||
$.provider('hash3', inject(['ENV', 'time'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(service('time', timeServiceStub)); | ||
$.register(provider('hash', inject(['ENV', 'time'], hashProvider))); | ||
$.register(provider('hash2', inject(['ENV', 'time'], hashProvider))); | ||
$.register(provider('hash3', inject(['ENV', 'time'], hashProvider))); | ||
@@ -356,4 +415,6 @@ const dependencies = await $.run(['hash', 'hash2', 'hash3', 'time']); | ||
$.provider('mappedStub', inject(['stub2>mappedStub2'], providerStub)); | ||
$.provider('mappedStub2', providerStub2); | ||
$.register( | ||
provider('mappedStub', inject(['stub2>mappedStub2'], providerStub)), | ||
); | ||
$.register(provider('mappedStub2', providerStub2)); | ||
@@ -377,7 +438,13 @@ const dependencies = await $.run(['stub>mappedStub']); | ||
$.constant('ENV', ENV); | ||
$.service('aTime', timeServiceStub); | ||
$.provider('aHash', inject(['ENV', 'time>aTime'], hashProvider)); | ||
$.provider('aHash2', inject(['ENV', 'hash>aHash'], hashProvider)); | ||
$.provider('aHash3', inject(['ENV', 'hash>aHash'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(service('aTime', timeServiceStub)); | ||
$.register( | ||
provider('aHash', inject(['ENV', 'time>aTime'], hashProvider)), | ||
); | ||
$.register( | ||
provider('aHash2', inject(['ENV', 'hash>aHash'], hashProvider)), | ||
); | ||
$.register( | ||
provider('aHash3', inject(['ENV', 'hash>aHash'], hashProvider)), | ||
); | ||
@@ -395,3 +462,3 @@ const dependencies = await $.run([ | ||
it('should fail with bad service', async () => { | ||
$.service('lol', () => {}); | ||
$.register(service('lol', () => {})); | ||
@@ -408,3 +475,3 @@ try { | ||
it('should fail with bad provider', async () => { | ||
$.provider('lol', () => {}); | ||
$.register(provider('lol', () => {})); | ||
try { | ||
@@ -420,3 +487,3 @@ await $.run(['lol']); | ||
it('should fail with bad service in a provider', async () => { | ||
$.provider('lol', () => Promise.resolve()); | ||
$.register(provider('lol', () => Promise.resolve())); | ||
try { | ||
@@ -442,6 +509,6 @@ await $.run(['lol']); | ||
it('should fail with undeclared dependencies upstream', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV', 'hash2'], hashProvider)); | ||
$.provider('hash2', inject(['ENV', 'lol'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV', 'hash2'], hashProvider))); | ||
$.register(provider('hash2', inject(['ENV', 'lol'], hashProvider))); | ||
@@ -458,7 +525,7 @@ try { | ||
it('should provide a fatal error handler', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.provider('db', inject(['ENV'], dbProvider)); | ||
$.provider('process', inject(['$fatalError'], processProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
$.register(provider('db', inject(['ENV'], dbProvider))); | ||
$.register(provider('process', inject(['$fatalError'], processProvider))); | ||
@@ -526,5 +593,5 @@ function processProvider({ $fatalError }) { | ||
); | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV', '?DEBUG'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV', '?DEBUG'], hashProvider))); | ||
@@ -568,8 +635,8 @@ const dependencies = await $.run(['time', 'hash']); | ||
); | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.provider('hash1', inject(['hash'], hashProvider)); | ||
$.provider('hash3', inject(['hash2'], hashProvider)); | ||
$.provider('hash5', inject(['hash4'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
$.register(provider('hash1', inject(['hash'], hashProvider))); | ||
$.register(provider('hash3', inject(['hash2'], hashProvider))); | ||
$.register(provider('hash5', inject(['hash4'], hashProvider))); | ||
@@ -582,5 +649,5 @@ const dependencies = await $.run(['hash5', 'time']); | ||
it('should work with various dependencies', async () => { | ||
$.provider('hash', inject(['hash2'], hashProvider)); | ||
$.provider('hash3', inject(['?ENV'], hashProvider)); | ||
$.constant('DEBUG', 1); | ||
$.register(provider('hash', inject(['hash2'], hashProvider))); | ||
$.register(provider('hash3', inject(['?ENV'], hashProvider))); | ||
$.register(constant('DEBUG', 1)); | ||
$.register( | ||
@@ -640,6 +707,10 @@ initializer( | ||
$.constant('ENV', ENV); | ||
$.service('time', timeServiceStub); | ||
$.provider('hash', inject(['hash1', 'hash2', 'hash3'], hashProvider)); | ||
$.provider('hash_', inject(['hash1', 'hash2', 'hash3'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(service('time', timeServiceStub)); | ||
$.register( | ||
provider('hash', inject(['hash1', 'hash2', 'hash3'], hashProvider)), | ||
); | ||
$.register( | ||
provider('hash_', inject(['hash1', 'hash2', 'hash3'], hashProvider)), | ||
); | ||
@@ -782,5 +853,5 @@ const dependencies = await $.run(['hash', 'hash_', 'hash3']); | ||
it('should work with no dependencies', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
@@ -800,5 +871,5 @@ const dependencies = await $.run(['time', 'hash', '$injector']); | ||
it('should work with same dependencies then the running silo', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
@@ -821,5 +892,5 @@ const dependencies = await $.run(['time', 'hash', '$injector']); | ||
it('should fail with non instanciated dependencies', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
@@ -838,4 +909,4 @@ const dependencies = await $.run(['time', '$injector']); | ||
it('should create dependencies when not declared as singletons', async () => { | ||
$.constant('ENV', ENV); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
@@ -855,15 +926,19 @@ const [{ hash }, { hash: sameHash }] = await Promise.all([ | ||
it('should reuse dependencies when declared as singletons', async () => { | ||
$.constant('ENV', ENV); | ||
$.provider('hash', inject(['ENV'], hashProvider), { | ||
singleton: true, | ||
}); | ||
$.provider( | ||
'hash2', | ||
inject( | ||
['ENV'], | ||
options( | ||
{ | ||
singleton: true, | ||
}, | ||
hashProvider, | ||
$.register(constant('ENV', ENV)); | ||
$.register( | ||
provider('hash', inject(['ENV'], hashProvider), { | ||
singleton: true, | ||
}), | ||
); | ||
$.register( | ||
provider( | ||
'hash2', | ||
inject( | ||
['ENV'], | ||
options( | ||
{ | ||
singleton: true, | ||
}, | ||
hashProvider, | ||
), | ||
), | ||
@@ -900,7 +975,11 @@ ), | ||
it('should work with several silos and dependencies', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider), { singleton: true }); | ||
$.provider('hash1', inject(['ENV'], hashProvider)); | ||
$.provider('hash2', inject(['ENV'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register( | ||
provider('hash', inject(['ENV'], hashProvider), { | ||
singleton: true, | ||
}), | ||
); | ||
$.register(provider('hash1', inject(['ENV'], hashProvider))); | ||
$.register(provider('hash2', inject(['ENV'], hashProvider))); | ||
@@ -919,7 +998,7 @@ const [dependencies] = await Promise.all([ | ||
it('should work when trigered from several silos simultaneously', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.provider('hash1', inject(['ENV'], hashProvider)); | ||
$.provider('hash2', inject(['ENV'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
$.register(provider('hash1', inject(['ENV'], hashProvider))); | ||
$.register(provider('hash2', inject(['ENV'], hashProvider))); | ||
@@ -938,7 +1017,7 @@ const dependenciesBuckets = await Promise.all([ | ||
it('should work when a silo shutdown is in progress', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.provider('hash1', inject(['ENV'], hashProvider)); | ||
$.provider('hash2', inject(['ENV'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
$.register(provider('hash1', inject(['ENV'], hashProvider))); | ||
$.register(provider('hash2', inject(['ENV'], hashProvider))); | ||
@@ -954,6 +1033,6 @@ const [dependencies1, dependencies2] = await Promise.all([ | ||
it('should disallow new runs', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.provider('hash1', inject(['ENV'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
$.register(provider('hash1', inject(['ENV'], hashProvider))); | ||
@@ -984,4 +1063,4 @@ const dependencies = await $.run(['$destroy']); | ||
it('should work with constant dependencies', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
@@ -995,5 +1074,5 @@ const dependencies = await $.run(['time', 'ENV', '$dispose']); | ||
it('should work with simple dependencies', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
@@ -1019,20 +1098,22 @@ const dependencies = await $.run(['time', 'hash', '$dispose']); | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.provider('hash1', inject(['hash'], hashProvider)); | ||
$.provider('hash2', inject(['hash1'], hashProvider)); | ||
$.provider('hash3', inject(['hash2'], hashProvider)); | ||
$.provider('hash4', inject(['hash3'], hashProvider)); | ||
$.provider('hash5', inject(['hash4'], hashProvider)); | ||
$.provider( | ||
'shutdownChecker', | ||
inject(['hash4'], () => | ||
Promise.resolve({ | ||
service: { | ||
shutdownStub, | ||
shutdownResolve, | ||
}, | ||
dispose: shutdownStub, | ||
}), | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
$.register(provider('hash1', inject(['hash'], hashProvider))); | ||
$.register(provider('hash2', inject(['hash1'], hashProvider))); | ||
$.register(provider('hash3', inject(['hash2'], hashProvider))); | ||
$.register(provider('hash4', inject(['hash3'], hashProvider))); | ||
$.register(provider('hash5', inject(['hash4'], hashProvider))); | ||
$.register( | ||
provider( | ||
'shutdownChecker', | ||
inject(['hash4'], () => | ||
Promise.resolve({ | ||
service: { | ||
shutdownStub, | ||
shutdownResolve, | ||
}, | ||
dispose: shutdownStub, | ||
}), | ||
), | ||
), | ||
@@ -1076,18 +1157,20 @@ ); | ||
$.constant('ENV', ENV); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.provider( | ||
'shutdownChecker', | ||
inject(['hash'], () => | ||
Promise.resolve({ | ||
service: { | ||
shutdownStub, | ||
shutdownResolve, | ||
}, | ||
dispose: shutdownStub, | ||
}), | ||
$.register(constant('ENV', ENV)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
$.register( | ||
provider( | ||
'shutdownChecker', | ||
inject(['hash'], () => | ||
Promise.resolve({ | ||
service: { | ||
shutdownStub, | ||
shutdownResolve, | ||
}, | ||
dispose: shutdownStub, | ||
}), | ||
), | ||
), | ||
); | ||
$.provider('hash1', inject(['shutdownChecker'], hashProvider)); | ||
$.provider('hash2', inject(['shutdownChecker'], hashProvider)); | ||
$.register(provider('hash1', inject(['shutdownChecker'], hashProvider))); | ||
$.register(provider('hash2', inject(['shutdownChecker'], hashProvider))); | ||
@@ -1119,26 +1202,32 @@ const dependencies = await $.run([ | ||
$.provider('hash', () => | ||
Promise.resolve({ | ||
service: {}, | ||
dispose: servicesShutdownCalls.bind(null, 'hash'), | ||
}), | ||
); | ||
$.provider( | ||
'hash1', | ||
inject(['hash'], () => | ||
$.register( | ||
provider('hash', () => | ||
Promise.resolve({ | ||
service: {}, | ||
dispose: servicesShutdownCalls.bind(null, 'hash1'), | ||
dispose: servicesShutdownCalls.bind(null, 'hash'), | ||
}), | ||
), | ||
); | ||
$.provider( | ||
'hash2', | ||
inject(['hash1', 'hash'], () => | ||
Promise.resolve({ | ||
service: {}, | ||
dispose: servicesShutdownCalls.bind(null, 'hash2'), | ||
}), | ||
$.register( | ||
provider( | ||
'hash1', | ||
inject(['hash'], () => | ||
Promise.resolve({ | ||
service: {}, | ||
dispose: servicesShutdownCalls.bind(null, 'hash1'), | ||
}), | ||
), | ||
), | ||
); | ||
$.register( | ||
provider( | ||
'hash2', | ||
inject(['hash1', 'hash'], () => | ||
Promise.resolve({ | ||
service: {}, | ||
dispose: servicesShutdownCalls.bind(null, 'hash2'), | ||
}), | ||
), | ||
), | ||
); | ||
@@ -1157,7 +1246,9 @@ const dependencies = await $.run(['hash2', '$dispose']); | ||
it('should not shutdown singleton dependencies if used elsewhere', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider), { | ||
singleton: true, | ||
}); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register( | ||
provider('hash', inject(['ENV'], hashProvider), { | ||
singleton: true, | ||
}), | ||
); | ||
@@ -1176,7 +1267,9 @@ const { hash } = await $.run(['time', 'hash']); | ||
it('should shutdown singleton dependencies if not used elsewhere', async () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider), { | ||
singleton: true, | ||
}); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register( | ||
provider('hash', inject(['ENV'], hashProvider), { | ||
singleton: true, | ||
}), | ||
); | ||
@@ -1194,4 +1287,4 @@ const { hash, $dispose } = await $.run(['time', 'hash', '$dispose']); | ||
it('should print nothing when no dependency', () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
assert.equal($.toMermaidGraph(), ''); | ||
@@ -1201,10 +1294,10 @@ }); | ||
it('should print a dependency graph', () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.provider('hash1', inject(['hash'], hashProvider)); | ||
$.provider('hash2', inject(['hash1'], hashProvider)); | ||
$.provider('hash3', inject(['hash2'], hashProvider)); | ||
$.provider('hash4', inject(['hash3'], hashProvider)); | ||
$.provider('hash5', inject(['hash4'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
$.register(provider('hash1', inject(['hash'], hashProvider))); | ||
$.register(provider('hash2', inject(['hash1'], hashProvider))); | ||
$.register(provider('hash3', inject(['hash2'], hashProvider))); | ||
$.register(provider('hash4', inject(['hash3'], hashProvider))); | ||
$.register(provider('hash5', inject(['hash4'], hashProvider))); | ||
assert.equal( | ||
@@ -1223,10 +1316,10 @@ $.toMermaidGraph(), | ||
it('should allow custom shapes', () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.provider('hash1', inject(['hash'], hashProvider)); | ||
$.provider('hash2', inject(['hash1'], hashProvider)); | ||
$.provider('hash3', inject(['hash2'], hashProvider)); | ||
$.provider('hash4', inject(['hash3'], hashProvider)); | ||
$.provider('hash5', inject(['hash4'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
$.register(provider('hash1', inject(['hash'], hashProvider))); | ||
$.register(provider('hash2', inject(['hash1'], hashProvider))); | ||
$.register(provider('hash3', inject(['hash2'], hashProvider))); | ||
$.register(provider('hash4', inject(['hash3'], hashProvider))); | ||
$.register(provider('hash5', inject(['hash4'], hashProvider))); | ||
assert.equal( | ||
@@ -1260,10 +1353,10 @@ $.toMermaidGraph({ | ||
it('should allow custom styles', () => { | ||
$.constant('ENV', ENV); | ||
$.constant('time', time); | ||
$.provider('hash', inject(['ENV'], hashProvider)); | ||
$.provider('hash1', inject(['hash'], hashProvider)); | ||
$.provider('hash2', inject(['hash1'], hashProvider)); | ||
$.provider('hash3', inject(['hash2'], hashProvider)); | ||
$.provider('hash4', inject(['hash3'], hashProvider)); | ||
$.provider('hash5', inject(['hash4'], hashProvider)); | ||
$.register(constant('ENV', ENV)); | ||
$.register(constant('time', time)); | ||
$.register(provider('hash', inject(['ENV'], hashProvider))); | ||
$.register(provider('hash1', inject(['hash'], hashProvider))); | ||
$.register(provider('hash2', inject(['hash1'], hashProvider))); | ||
$.register(provider('hash3', inject(['hash2'], hashProvider))); | ||
$.register(provider('hash4', inject(['hash3'], hashProvider))); | ||
$.register(provider('hash5', inject(['hash4'], hashProvider))); | ||
assert.equal( | ||
@@ -1270,0 +1363,0 @@ $.toMermaidGraph({ |
158
src/util.js
@@ -13,2 +13,3 @@ import YError from 'yerror'; | ||
EXTRA: `${SPECIAL_PROPS_PREFIX}extra`, | ||
VALUE: `${SPECIAL_PROPS_PREFIX}value`, | ||
}; | ||
@@ -20,3 +21,7 @@ export const ALLOWED_SPECIAL_PROPS = Object.keys(SPECIAL_PROPS).map( | ||
export const OPTIONAL_FLAG = '?'; | ||
export const ALLOWED_INITIALIZER_TYPES = ['provider', 'service', 'constant']; | ||
const E_BAD_INJECT_IN_CONSTANT = 'E_BAD_INJECT_IN_CONSTANT'; | ||
const E_CONSTANT_INJECTION = 'E_CONSTANT_INJECTION'; | ||
/** | ||
@@ -47,3 +52,3 @@ * Apply special props to the given function from another one | ||
/** | ||
* Allows to wrap an initializer to add extra | ||
* Allows to wrap an initializer to add extra initialization steps | ||
* @param {Function} wrapper | ||
@@ -90,2 +95,10 @@ * A function taking dependencies and the base | ||
export function inject(dependenciesDeclarations, initializer, merge = false) { | ||
if ('constant' === initializer[SPECIAL_PROPS.TYPE]) { | ||
throw new YError( | ||
E_BAD_INJECT_IN_CONSTANT, | ||
initializer[SPECIAL_PROPS.NAME], | ||
dependenciesDeclarations, | ||
); | ||
} | ||
const uniqueInitializer = reuseSpecialProps(initializer, initializer, { | ||
@@ -278,2 +291,145 @@ [SPECIAL_PROPS.INJECT]: merge | ||
/** | ||
* Decorator that creates an initializer for a constant value | ||
* @param {String} name | ||
* The constant's name. | ||
* @param {any} initializer | ||
* The constant's value | ||
* @return {Function} | ||
* Returns a new initializer | ||
* @example | ||
* import { Knifecycle, constant, service } from 'knifecycle'; | ||
* | ||
* const { printAnswer } = new Knifecycle() | ||
* .register(constant('THE_NUMBER', value)) | ||
* .register(constant('log', console.log.bind(console))) | ||
* .register(service( | ||
* 'printAnswer', | ||
* async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
* { | ||
* inject: ['THE_NUMBER', 'log'], | ||
* } | ||
* .run(['printAnswer']); | ||
* | ||
* printAnswer(); // 42 | ||
*/ | ||
export function constant(name, value) { | ||
const contantLooksLikeAnInitializer = | ||
value instanceof Function && value[SPECIAL_PROPS.INJECT]; | ||
if (contantLooksLikeAnInitializer) { | ||
throw new YError(E_CONSTANT_INJECTION, value[SPECIAL_PROPS.INJECT]); | ||
} | ||
const uniqueInitializer = initializer( | ||
{ | ||
name: name, | ||
type: 'constant', | ||
options: { singleton: true }, | ||
inject: [], | ||
value: value, | ||
}, | ||
deliverConstantValue.bind(null, value), | ||
); | ||
debug(`Created an initializer from a constant: ${name}.`); | ||
return uniqueInitializer; | ||
} | ||
/** | ||
* Decorator that creates an initializer for a service | ||
* @param {String} name | ||
* The service's name. | ||
* @param {Function} initializer | ||
* An initializer returning the service promise | ||
* @param {Object} options | ||
* Options attached to the initializer | ||
* @return {Function} | ||
* Returns a new initializer | ||
* @example | ||
* import { Knifecycle, constant, service } from 'knifecycle'; | ||
* | ||
* const { printAnswer } = new Knifecycle() | ||
* .register(constant('THE_NUMBER', value)) | ||
* .register(constant('log', console.log.bind(console))) | ||
* .register(service( | ||
* 'printAnswer', | ||
* async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
* { | ||
* inject: ['THE_NUMBER', 'log'], | ||
* } | ||
* .run(['printAnswer']); | ||
* | ||
* printAnswer(); // 42 | ||
*/ | ||
export function service(serviceName, serviceBuilder, options = {}) { | ||
const uniqueInitializer = reuseSpecialProps(serviceBuilder, serviceBuilder, { | ||
[SPECIAL_PROPS.NAME]: serviceName, | ||
[SPECIAL_PROPS.TYPE]: 'service', | ||
[SPECIAL_PROPS.OPTIONS]: options, | ||
}); | ||
debug(`Created an initializer from a service builder: ${name}.`); | ||
return uniqueInitializer; | ||
} | ||
/** | ||
* Decorator that creates an initializer for a provider | ||
* @param {String} name | ||
* The provider's name. | ||
* @param {Function} provider | ||
* A provider returning the service builder promise | ||
* @param {Object} options | ||
* Options attached to the initializer | ||
* @return {Function} | ||
* Returns a new initializer | ||
* @example | ||
* | ||
* import Knifecycle from 'knifecycle' | ||
* import fs from 'fs'; | ||
* | ||
* const $ = new Knifecycle(); | ||
* | ||
* $.register(provider('config', async function configProvider() { | ||
* return new Promise((resolve, reject) { | ||
* fs.readFile('config.js', function(err, data) { | ||
* let config; | ||
* | ||
* if(err) { | ||
* reject(err); | ||
* return; | ||
* } | ||
* | ||
* try { | ||
* config = JSON.parse(data.toString); | ||
* } catch (err) { | ||
* reject(err); | ||
* return; | ||
* } | ||
* | ||
* resolve({ | ||
* service: config, | ||
* }); | ||
* }); | ||
* }); | ||
* })); | ||
*/ | ||
export function provider(providerName, provider, options = {}) { | ||
const uniqueInitializer = reuseSpecialProps(provider, provider, { | ||
[SPECIAL_PROPS.NAME]: providerName, | ||
[SPECIAL_PROPS.TYPE]: 'provider', | ||
[SPECIAL_PROPS.OPTIONS]: options, | ||
}); | ||
debug(`Created an initializer from a provider builder: ${name}.`); | ||
return uniqueInitializer; | ||
} | ||
async function deliverConstantValue(value) { | ||
return value; | ||
} | ||
/** | ||
* Shortcut to create an initializer with a simple handler | ||
@@ -280,0 +436,0 @@ * @param {Function} handlerFunction |
@@ -13,2 +13,4 @@ import assert from 'assert'; | ||
initializer, | ||
constant, | ||
service, | ||
handler, | ||
@@ -205,2 +207,49 @@ SPECIAL_PROPS, | ||
describe('constant', () => { | ||
it('should allow to create an initializer from a constant', async () => { | ||
const baseValue = 'THE_VALUE'; | ||
const baseName = 42; | ||
const newInitializer = constant(baseName, baseValue); | ||
assert.notEqual(newInitializer, aProvider); | ||
assert.deepEqual(newInitializer[SPECIAL_PROPS.INJECT], []); | ||
assert.deepEqual(newInitializer[SPECIAL_PROPS.OPTIONS], { | ||
singleton: true, | ||
}); | ||
assert.equal(newInitializer[SPECIAL_PROPS.NAME], baseName); | ||
assert.equal(newInitializer[SPECIAL_PROPS.TYPE], 'constant'); | ||
assert.equal(newInitializer[SPECIAL_PROPS.VALUE], baseValue); | ||
assert.equal(await newInitializer(), baseValue); | ||
}); | ||
it('should fail with dependencies since it makes no sense', () => { | ||
assert.throws(() => { | ||
constant('time', inject(['hash3'], async () => {})); | ||
}, /E_CONSTANT_INJECTION/); | ||
}); | ||
}); | ||
describe('service', () => { | ||
it('should allow to create an initializer from a service builder', async () => { | ||
const aServiceBuilder = async () => {}; | ||
const dependencies = ['ANOTHER_ENV>ENV']; | ||
const baseOptions = { singleton: true }; | ||
const baseName = 'hash'; | ||
const baseType = 'service'; | ||
const newInitializer = service( | ||
baseName, | ||
inject(dependencies, aServiceBuilder), | ||
baseOptions, | ||
); | ||
assert.notEqual(newInitializer, aProvider); | ||
assert.notEqual(newInitializer[SPECIAL_PROPS.INJECT], dependencies); | ||
assert.deepEqual(newInitializer[SPECIAL_PROPS.INJECT], dependencies); | ||
assert.notEqual(newInitializer[SPECIAL_PROPS.OPTIONS], baseOptions); | ||
assert.deepEqual(newInitializer[SPECIAL_PROPS.OPTIONS], baseOptions); | ||
assert.equal(newInitializer[SPECIAL_PROPS.NAME], baseName); | ||
assert.equal(newInitializer[SPECIAL_PROPS.TYPE], baseType); | ||
}); | ||
}); | ||
describe('handler', () => { | ||
@@ -207,0 +256,0 @@ it('should work', async () => { |
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
292845
6631
1056