Skip to content

Commit

Permalink
refactor(core): move the $destroy provider to the Knifecycle instance
Browse files Browse the repository at this point in the history
concerns #87 fix #89
  • Loading branch information
nfroidure committed Nov 28, 2019
1 parent 52fc8bf commit 7f0fd3b
Show file tree
Hide file tree
Showing 10 changed files with 3,266 additions and 2,810 deletions.
23 changes: 23 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
* [.register(initializer)](#Knifecycle+register)[<code>Knifecycle</code>](#Knifecycle)
* [.toMermaidGraph(options)](#Knifecycle+toMermaidGraph) ⇒ <code>String</code>
* [.run(dependenciesDeclarations)](#Knifecycle+run) ⇒ <code>Promise</code>
* [.destroy()](#Knifecycle+destroy) ⇒ <code>Promise</code>
* [._getServiceDescriptor(siloContext, serviceName, options, serviceProvider)](#Knifecycle+_getServiceDescriptor) ⇒ <code>Promise</code>
* [._initializeServiceDescriptor(siloContext, serviceName, options)](#Knifecycle+_initializeServiceDescriptor) ⇒ <code>Promise</code>
* [._initializeDependencies(siloContext, serviceName, servicesDeclarations, options)](#Knifecycle+_initializeDependencies) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -177,6 +178,28 @@ $.run(['ENV'])
// Here goes your 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>

### knifecycle.\_getServiceDescriptor(siloContext, serviceName, options, serviceProvider) ⇒ <code>Promise</code>
Expand Down
8 changes: 4 additions & 4 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ It is designed to have a low footprint on services code.
at all since they are just simple functions with annotations
set as a property.

[See in context](./src/index.js#L60-L76)
[See in context](./src/index.js#L59-L75)



Expand All @@ -39,7 +39,7 @@ A service provider is full of state since its concern is
[encapsulate](https://en.wikipedia.org/wiki/Encapsulation_(computer_programming))
your application global states.

[See in context](./src/index.js#L78-L87)
[See in context](./src/index.js#L77-L86)



Expand Down Expand Up @@ -107,7 +107,7 @@ Initializers can be of three types:
executions silos using them (we will cover this
topic later on).

[See in context](./src/index.js#L166-L189)
[See in context](./src/index.js#L136-L159)



Expand All @@ -123,7 +123,7 @@ Depending on your application design, you could run it
in only one execution silo or into several ones
according to the isolation level your wish to reach.

[See in context](./src/index.js#L429-L439)
[See in context](./src/index.js#L408-L418)



Expand Down
36 changes: 31 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,14 @@ $.register(

// At this point, nothing is running. To instanciate the
// 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
// found by automatically loading a NodeJS module
// in the above `$autoload` service. The db connection
// will only be instanciated if that command needs it
.then(async ({ command, $destroy, exit, log }) => {
.then(async ({ command, $instance, exit, log }) => {
try {
command();

Expand All @@ -274,8 +274,11 @@ $.run(['command', '$destroy', 'exit', 'log'])
log('It failed!', err);
} finally {
// 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);
exit(1);
});
Expand Down Expand Up @@ -498,6 +501,7 @@ Notice that those modules remains usable without using Knifecycle at
* [.register(initializer)](#Knifecycle+register)[<code>Knifecycle</code>](#Knifecycle)
* [.toMermaidGraph(options)](#Knifecycle+toMermaidGraph) ⇒ <code>String</code>
* [.run(dependenciesDeclarations)](#Knifecycle+run) ⇒ <code>Promise</code>
* [.destroy()](#Knifecycle+destroy) ⇒ <code>Promise</code>
* [._getServiceDescriptor(siloContext, serviceName, options, serviceProvider)](#Knifecycle+_getServiceDescriptor) ⇒ <code>Promise</code>
* [._initializeServiceDescriptor(siloContext, serviceName, options)](#Knifecycle+_initializeServiceDescriptor) ⇒ <code>Promise</code>
* [._initializeDependencies(siloContext, serviceName, servicesDeclarations, options)](#Knifecycle+_initializeDependencies) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -583,6 +587,28 @@ $.run(['ENV'])
// Here goes your 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>

### knifecycle.\_getServiceDescriptor(siloContext, serviceName, options, serviceProvider) ⇒ <code>Promise</code>
Expand Down
Loading

0 comments on commit 7f0fd3b

Please sign in to comment.