knifecycle
Advanced tools
Comparing version 6.0.0 to 7.0.0
@@ -0,1 +1,10 @@ | ||
# [7.0.0](https://github.com/nfroidure/knifecycle/compare/v6.0.0...v7.0.0) (2019-11-28) | ||
### Bug Fixes | ||
* **types:** Fix types and optional parameters ([e49c4e7](https://github.com/nfroidure/knifecycle/commit/e49c4e70a7bd0f9c199501292327f2ddfe344156)), closes [#104](https://github.com/nfroidure/knifecycle/issues/104) | ||
# [6.0.0](https://github.com/nfroidure/knifecycle/compare/v5.3.0...v6.0.0) (2019-03-15) | ||
@@ -2,0 +11,0 @@ |
@@ -11,4 +11,6 @@ "use strict"; | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -15,0 +17,0 @@ |
@@ -157,3 +157,2 @@ "use strict"; | ||
const DISPOSE = '$dispose'; | ||
const DESTROY = '$destroy'; | ||
const AUTOLOAD = '$autoload'; | ||
@@ -212,3 +211,4 @@ const INJECTOR = '$injector'; | ||
* Create a new Knifecycle instance | ||
* @return {Knifecycle} The Knifecycle instance | ||
* @return {Knifecycle} | ||
* The Knifecycle instance | ||
* @example | ||
@@ -246,19 +246,2 @@ * | ||
}))); | ||
this.register((0, _util.initializer)({ | ||
name: DESTROY, | ||
type: 'provider', | ||
inject: [], | ||
options: { | ||
singleton: true | ||
} | ||
}, async () => ({ | ||
service: () => { | ||
this.shutdownPromise = this.shutdownPromise || Promise.all([...this._silosContexts].map(siloContext => { | ||
const $dispose = siloContext.servicesDescriptors.get(DISPOSE).service; | ||
return $dispose(); | ||
})); | ||
debug('Shutting down Knifecycle instance.'); | ||
return this.shutdownPromise; | ||
} | ||
}))); | ||
} | ||
@@ -297,2 +280,6 @@ /* Architecture Note #1.3: Registering initializers | ||
register(initializer) { | ||
if (this.shutdownPromise) { | ||
throw new _yerror.default(E_INSTANCE_DESTROYED); | ||
} | ||
if (typeof initializer !== 'function') { | ||
@@ -408,7 +395,12 @@ throw new _yerror.default(E_BAD_INITIALIZER, initializer); | ||
* See [Mermaid docs](https://github.com/knsv/mermaid) | ||
* @param {Object} options Options for generating the graph (destructured) | ||
* @param {Array<Object>} options.shapes Various shapes to apply | ||
* @param {Array<Object>} options.styles Various styles to apply | ||
* @param {Object} options.classes A hash of various classes contents | ||
* @return {String} Returns a string containing the Mermaid dependency graph | ||
* @param {Object} options | ||
* Options for generating the graph (destructured) | ||
* @param {Array<Object>} options.shapes | ||
* Various shapes to apply | ||
* @param {Array<Object>} options.styles | ||
* Various styles to apply | ||
* @param {Object} options.classes | ||
* A hash of various classes contents | ||
* @return {String} | ||
* Returns a string containing the Mermaid dependency graph | ||
* @example | ||
@@ -480,4 +472,6 @@ * | ||
* Creates a new execution silo | ||
* @param {String[]} dependenciesDeclarations Service name. | ||
* @return {Promise} Service descriptor promise | ||
* @param {String[]} dependenciesDeclarations | ||
* Service name. | ||
* @return {Promise} | ||
* Service descriptor promise | ||
* @example | ||
@@ -598,2 +592,31 @@ * | ||
/** | ||
* Destroy the Knifecycle instance | ||
* @return {Promise} | ||
* Full destruction promise | ||
* @example | ||
* | ||
* import Knifecycle, { constant } from 'knifecycle' | ||
* | ||
* const $ = new Knifecycle(); | ||
* | ||
* $.register(constant('ENV', process.env)); | ||
* $.run(['ENV']) | ||
* .then(({ ENV }) => { | ||
* // Here goes your code | ||
* | ||
* // Finally destroy the instance | ||
* $.destroy() | ||
* }) | ||
*/ | ||
async destroy() { | ||
this.shutdownPromise = this.shutdownPromise || Promise.all([...this._silosContexts].map(siloContext => { | ||
const $dispose = siloContext.servicesDescriptors.get(DISPOSE).service; | ||
return $dispose(); | ||
})); | ||
debug('Shutting down Knifecycle instance.'); | ||
return this.shutdownPromise; | ||
} | ||
/** | ||
* Initialize or return a service descriptor | ||
@@ -610,4 +633,6 @@ * @param {Object} siloContext | ||
* Flag to indicating $autoload dependencies on the fly loading | ||
* @param {String} serviceProvider Service provider. | ||
* @return {Promise} Service dependencies hash promise. | ||
* @param {String} serviceProvider | ||
* Service provider. | ||
* @return {Promise} | ||
* Service dependencies hash promise. | ||
*/ | ||
@@ -760,4 +785,6 @@ | ||
* Initialize a service descriptor | ||
* @param {Object} siloContext Current execution silo context | ||
* @param {String} serviceName Service name. | ||
* @param {Object} siloContext | ||
* Current execution silo context | ||
* @param {String} serviceName | ||
* Service name. | ||
* @param {Object} options | ||
@@ -769,3 +796,4 @@ * Options for service retrieval | ||
* Flag to indicating $autoload dependendencies on the fly loading. | ||
* @return {Promise} Service dependencies hash promise. | ||
* @return {Promise} | ||
* Service dependencies hash promise. | ||
*/ | ||
@@ -833,5 +861,8 @@ | ||
* Initialize a service dependencies | ||
* @param {Object} siloContext Current execution silo siloContext | ||
* @param {String} serviceName Service name. | ||
* @param {String} servicesDeclarations Dependencies declarations. | ||
* @param {Object} siloContext | ||
* Current execution silo siloContext | ||
* @param {String} serviceName | ||
* Service name. | ||
* @param {String} servicesDeclarations | ||
* Dependencies declarations. | ||
* @param {Object} options | ||
@@ -843,3 +874,4 @@ * Options for service retrieval | ||
* Flag to indicating $autoload dependendencies on the fly loading. | ||
* @return {Promise} Service dependencies hash promise. | ||
* @return {Promise} | ||
* Service dependencies hash promise. | ||
*/ | ||
@@ -846,0 +878,0 @@ |
@@ -934,9 +934,8 @@ "use strict"; | ||
}); | ||
describe('$destroy', () => { | ||
describe('destroy', () => { | ||
it('should work even with one silo and no dependencies', async () => { | ||
const dependencies = await $.run(['$destroy']); | ||
_assert.default.equal(typeof $.destroy, 'function'); | ||
_assert.default.equal(typeof dependencies.$destroy, 'function'); | ||
await dependencies.$destroy(); | ||
const dependencies = await $.run(['$instance']); | ||
await dependencies.$instance.destroy(); | ||
}); | ||
@@ -951,7 +950,7 @@ it('should work with several silos and dependencies', async () => { | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['ENV'])); | ||
const [dependencies] = await Promise.all([$.run(['$destroy']), $.run(['ENV', 'hash', 'hash1', 'time']), $.run(['ENV', 'hash', 'hash2'])]); | ||
const [dependencies] = await Promise.all([$.run(['$instance']), $.run(['ENV', 'hash', 'hash1', 'time']), $.run(['ENV', 'hash', 'hash2'])]); | ||
_assert.default.equal(typeof dependencies.$destroy, 'function'); | ||
_assert.default.equal(typeof dependencies.$instance.destroy, 'function'); | ||
await dependencies.$destroy(); | ||
await $.destroy(); | ||
}); | ||
@@ -964,4 +963,4 @@ it('should work when trigered from several silos simultaneously', async () => { | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['ENV'])); | ||
const dependenciesBuckets = await Promise.all([$.run(['$destroy']), $.run(['$destroy', 'ENV', 'hash', 'hash1', 'time']), $.run(['$destroy', 'ENV', 'hash', 'hash2'])]); | ||
await Promise.all(dependenciesBuckets.map(dependencies => dependencies.$destroy())); | ||
const dependenciesBuckets = await Promise.all([$.run(['$instance']), $.run(['$instance', 'ENV', 'hash', 'hash1', 'time']), $.run(['$instance', 'ENV', 'hash', 'hash2'])]); | ||
await Promise.all(dependenciesBuckets.map(dependencies => dependencies.$instance.destroy())); | ||
}); | ||
@@ -974,4 +973,4 @@ it('should work when a silo shutdown is in progress', async () => { | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['ENV'])); | ||
const [dependencies1, dependencies2] = await Promise.all([$.run(['$destroy']), $.run(['$dispose', 'ENV', 'hash', 'hash1', 'time']), $.run(['ENV', 'hash', 'hash2'])]); | ||
await Promise.all([dependencies2.$dispose(), dependencies1.$destroy()]); | ||
const [dependencies1, dependencies2] = await Promise.all([$.run(['$instance']), $.run(['$dispose', 'ENV', 'hash', 'hash1', 'time']), $.run(['ENV', 'hash', 'hash2'])]); | ||
await Promise.all([dependencies2.$dispose(), dependencies1.$instance.destroy()]); | ||
}); | ||
@@ -983,7 +982,7 @@ it('should disallow new runs', async () => { | ||
$.register((0, _index.provider)(hashProvider, 'hash1', ['ENV'])); | ||
const dependencies = await $.run(['$destroy']); | ||
const dependencies = await $.run(['$instance']); | ||
_assert.default.equal(typeof dependencies.$destroy, 'function'); | ||
_assert.default.equal(typeof dependencies.$instance.destroy, 'function'); | ||
await dependencies.$destroy(); | ||
await dependencies.$instance.destroy(); | ||
@@ -990,0 +989,0 @@ try { |
@@ -11,4 +11,6 @@ "use strict"; | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
@@ -369,3 +371,3 @@ | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], _objectSpread({}, rootOptions, baseOptions)); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], _objectSpread({}, rootOptions, {}, baseOptions)); | ||
}); | ||
@@ -523,3 +525,3 @@ }); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.EXTRA], _objectSpread({}, baseExtraInformations, baseExtraInformations)); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.EXTRA], _objectSpread({}, baseExtraInformations, {}, baseExtraInformations)); | ||
}); | ||
@@ -526,0 +528,0 @@ }); |
{ | ||
"name": "knifecycle", | ||
"version": "6.0.0", | ||
"version": "7.0.0", | ||
"description": "Manage your NodeJS processes's lifecycle automatically with an unobtrusive dependency injection implementation.", | ||
@@ -19,6 +19,6 @@ "main": "dist/index.js", | ||
"eslint", | ||
"travis", | ||
"codeclimate", | ||
"babel", | ||
"mocha", | ||
"travis", | ||
"karma", | ||
@@ -81,20 +81,20 @@ "jsdocs", | ||
"devDependencies": { | ||
"@babel/cli": "^7.2.3", | ||
"@babel/core": "^7.2.2", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.3.1", | ||
"@babel/preset-env": "^7.3.1", | ||
"@babel/register": "^7.0.0", | ||
"babel-eslint": "^10.0.1", | ||
"@babel/cli": "^7.7.4", | ||
"@babel/core": "^7.7.4", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.7.4", | ||
"@babel/preset-env": "^7.7.4", | ||
"@babel/register": "^7.7.4", | ||
"babel-eslint": "^10.0.3", | ||
"browserify": "^16.2.3", | ||
"commitizen": "^3.0.5", | ||
"conventional-changelog-cli": "^2.0.11", | ||
"coveralls": "^3.0.2", | ||
"cz-conventional-changelog": "^2.1.0", | ||
"eslint": "^5.13.0", | ||
"eslint-plugin-prettier": "^3.0.1", | ||
"jsarch": "^2.0.1", | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"karma": "^4.0.0", | ||
"commitizen": "^4.0.3", | ||
"conventional-changelog-cli": "^2.0.28", | ||
"coveralls": "^3.0.5", | ||
"cz-conventional-changelog": "^3.0.2", | ||
"eslint": "^6.7.1", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"jsarch": "^2.0.3", | ||
"jsdoc-to-markdown": "^5.0.3", | ||
"karma": "^4.2.0", | ||
"karma-browserify": "^6.0.0", | ||
"karma-chrome-launcher": "^2.2.0", | ||
"karma-chrome-launcher": "^3.0.0", | ||
"karma-firefox-launcher": "^1.1.0", | ||
@@ -104,6 +104,6 @@ "karma-mocha": "^1.3.0", | ||
"metapak": "^3.1.6", | ||
"metapak-nfroidure": "9.6.2", | ||
"mocha": "^5.2.0", | ||
"nyc": "^13.1.0", | ||
"prettier": "^1.16.3", | ||
"metapak-nfroidure": "9.7.2", | ||
"mocha": "^6.2.0", | ||
"nyc": "^14.1.1", | ||
"prettier": "^1.19.1", | ||
"sinon": "^7.2.6" | ||
@@ -110,0 +110,0 @@ }, |
@@ -261,5 +261,5 @@ [//]: # ( ) | ||
// services, we have to create an execution silo using | ||
// them. Note that we required the `$destroy` service | ||
// them. Note that we required the `$instance` service | ||
// implicitly created by `knifecycle` | ||
$.run(['command', '$destroy', 'exit', 'log']) | ||
$.run(['command', '$instance', 'exit', 'log']) | ||
// Here, command contains the initializer eventually | ||
@@ -269,3 +269,3 @@ // found by automatically loading a NodeJS module | ||
// will only be instanciated if that command needs it | ||
.then(async ({ command, $destroy, exit, log }) => { | ||
.then(async ({ command, $instance, exit, log }) => { | ||
try { | ||
@@ -279,4 +279,7 @@ command(); | ||
// Here we ensure every db connections are closed | ||
// properly | ||
await $destroy().catch(err => { | ||
// properly. We could have use `$.destroy()` the same | ||
// way but this is to illustrate that the Knifecycle | ||
// instance can be injected in services contexts | ||
// (rarely done but good to know it exists) | ||
await $instance.destroy().catch(err => { | ||
console.error('Could not exit gracefully:', err); | ||
@@ -504,2 +507,3 @@ exit(1); | ||
* [.run(dependenciesDeclarations)](#Knifecycle+run) ⇒ <code>Promise</code> | ||
* [.destroy()](#Knifecycle+destroy) ⇒ <code>Promise</code> | ||
* [._getServiceDescriptor(siloContext, serviceName, options, serviceProvider)](#Knifecycle+_getServiceDescriptor) ⇒ <code>Promise</code> | ||
@@ -590,2 +594,24 @@ * [._initializeServiceDescriptor(siloContext, serviceName, options)](#Knifecycle+_initializeServiceDescriptor) ⇒ <code>Promise</code> | ||
``` | ||
<a name="Knifecycle+destroy"></a> | ||
### knifecycle.destroy() ⇒ <code>Promise</code> | ||
Destroy the Knifecycle instance | ||
**Kind**: instance method of [<code>Knifecycle</code>](#Knifecycle) | ||
**Returns**: <code>Promise</code> - Full destruction promise | ||
**Example** | ||
```js | ||
import Knifecycle, { constant } from 'knifecycle' | ||
const $ = new Knifecycle(); | ||
$.register(constant('ENV', process.env)); | ||
$.run(['ENV']) | ||
.then(({ ENV }) => { | ||
// Here goes your code | ||
// Finally destroy the instance | ||
$.destroy() | ||
}) | ||
``` | ||
<a name="Knifecycle+_getServiceDescriptor"></a> | ||
@@ -592,0 +618,0 @@ |
@@ -1,4 +0,5 @@ | ||
type Dependencies = { [name: string]: any }; | ||
export type DependencyName = string; | ||
export type Dependencies = { [name: string]: any }; | ||
interface ProviderInitializer<D extends Dependencies, S> { | ||
export interface ProviderInitializer<D extends Dependencies, S> { | ||
(services?: D): Promise<{ | ||
@@ -10,25 +11,25 @@ service: S; | ||
} | ||
interface ServiceInitializer<D extends Dependencies, S> { | ||
export interface ServiceInitializer<D extends Dependencies, S> { | ||
(services?: D): Promise<S>; | ||
} | ||
interface HandlerInitializer<D extends Dependencies, U extends any[], V> { | ||
export interface HandlerInitializer<D extends Dependencies, U extends any[], V> { | ||
(services?: D, ...args: U): Promise<V>; | ||
} | ||
interface Handler<U extends any[], V> { | ||
export interface Handler<U extends any[], V> { | ||
(...args: U): Promise<V>; | ||
} | ||
type Initializer<D extends Dependencies, S> = | ||
export type Initializer<D extends Dependencies, S> = | ||
| ServiceInitializer<D, S> | ||
| ProviderInitializer<D, S>; | ||
type InitializerType = 'service' | 'provider' | 'constant'; | ||
export type InitializerType = 'service' | 'provider' | 'constant'; | ||
type DependenciesDeclarations = Array<string>; | ||
export type DependenciesDeclarations = Array<DependencyName>; | ||
interface InitializerOptions { | ||
export interface InitializerOptions { | ||
singleton: boolean; | ||
} | ||
interface InitializerDeclaration { | ||
export interface InitializerDeclaration { | ||
name: string; | ||
@@ -41,5 +42,26 @@ type: InitializerType; | ||
export interface Injector { | ||
(dependencies: DependenciesDeclarations): Promise<Dependencies>; | ||
} | ||
export interface Disposer { | ||
(): Promise<void>; | ||
} | ||
export interface Autoloader<D> { | ||
(name: DependencyName): Promise<D>; | ||
} | ||
export interface FatalErrorProvider { | ||
promise: Promise<void>; | ||
} | ||
export interface SiloContext<D> { | ||
name: string, | ||
servicesDescriptors: Map<DependencyName, D>, | ||
servicesSequence: DependencyName[], | ||
servicesShutdownsPromises: Map<DependencyName, Promise<void>>, | ||
errorsPromises: Promise<void>[], | ||
} | ||
export class Knifecycle { | ||
constructor(); | ||
run(dependencies: DependenciesDeclarations): Promise<Dependencies>; | ||
destroy(): Promise<void>; | ||
register<D extends Dependencies, S, T extends Initializer<D, S>>( | ||
@@ -56,3 +78,3 @@ initializer: T, | ||
export function name<D extends Dependencies, S, T extends Initializer<D, S>>( | ||
name: string, | ||
name: DependencyName, | ||
initializer: T, | ||
@@ -86,3 +108,3 @@ ): T; | ||
initializer: T, | ||
merge: boolean, | ||
merge?: boolean, | ||
): T; | ||
@@ -110,3 +132,3 @@ export function extra<D extends Dependencies, S, T extends Initializer<D, S>>( | ||
serviceBuilder: T, | ||
name?: string, | ||
name?: DependencyName, | ||
dependencies?: DependenciesDeclarations, | ||
@@ -126,3 +148,3 @@ options?: InitializerOptions, | ||
providerBuilder: T, | ||
name?: string, | ||
name?: DependencyName, | ||
dependencies?: DependenciesDeclarations, | ||
@@ -138,3 +160,3 @@ options?: InitializerOptions, | ||
handlerInitializer: HandlerInitializer<D, U, V>, | ||
name?: string, | ||
name?: DependencyName, | ||
dependencies?: DependenciesDeclarations, | ||
@@ -147,3 +169,10 @@ options?: InitializerOptions, | ||
export const SPECIAL_PROPS: Array<string>; | ||
export const SPECIAL_PROPS: { | ||
INJECT: String, | ||
OPTIONS: String, | ||
NAME: String, | ||
TYPE: String, | ||
EXTRA: String, | ||
VALUE: String, | ||
}; | ||
export const DECLARATION_SEPARATOR: string; | ||
@@ -150,0 +179,0 @@ export const OPTIONAL_FLAG: string; |
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
218807
3876
1207