- initInitializerBuilder(services) ⇒
Promise.<function()> Instantiate the initializer builder service
- initDispose()
Allow to dispose the services of an initialized silo content.
- constant(name, value) ⇒
function Decorator that creates an initializer for a constant value
- service(serviceBuilder, [name], [dependencies], [singleton], [extra]) ⇒
function Decorator that creates an initializer from a service builder
- autoService(serviceBuilder) ⇒
function Decorator that creates an initializer from a service builder by automatically detecting its name and dependencies
- provider(providerBuilder, [name], [dependencies], [singleton], [extra]) ⇒
function Decorator that creates an initializer for a provider builder
- autoProvider(providerBuilder) ⇒
function Decorator that creates an initializer from a provider builder by automatically detecting its name and dependencies
- parseDependencyDeclaration(dependencyDeclaration) ⇒
Object Explode a dependency declaration an returns its parts.
- stringifyDependencyDeclaration(dependencyDeclarationParts) ⇒
String Stringify a dependency declaration from its parts.
Instantiate the initializer builder service
Kind: global function
Returns: Promise.<function()> - A promise of the buildInitializer function
| Param | Type | Description |
|---|---|---|
| services | Object |
The services to inject |
| services.$autoload | Object |
The dependencies autoloader |
Example
import initInitializerBuilder from 'knifecycle/dist/build';
const buildInitializer = await initInitializerBuilder({
$autoload: async () => {},
});Create a JavaScript module that initialize a set of dependencies with hardcoded import/awaits.
Kind: inner method of initInitializerBuilder
Returns: Promise.<String> - The JavaScript module content
| Param | Type | Description |
|---|---|---|
| dependencies | Array.<String> |
The main dependencies |
Example
import initInitializerBuilder from 'knifecycle/dist/build';
const buildInitializer = await initInitializerBuilder({
$autoload: async () => {},
});
const content = await buildInitializer(['entryPoint']);Allow to dispose the services of an initialized silo content.
Decorator that creates an initializer for a constant value
Kind: global function
Returns: function - Returns a new constant initializer
| Param | Type | Description |
|---|---|---|
| name | String |
The constant's name. |
| value | any |
The constant's value |
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(
async ({ THE_NUMBER, log }) => () => log(THE_NUMBER),
'printAnswer',
['THE_NUMBER', 'log'],
))
.run(['printAnswer']);
printAnswer(); // 42Decorator that creates an initializer from a service builder
Kind: global function
Returns: function - Returns a new initializer
| Param | Type | Description |
|---|---|---|
| serviceBuilder | function |
An async function to build the service |
| [name] | String |
The service's name |
| [dependencies] | Array.<String> |
The service's injected dependencies |
| [singleton] | Boolean |
Whether the service is a singleton or not |
| [extra] | any |
Eventual extra information |
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(
async ({ THE_NUMBER, log }) => () => log(THE_NUMBER),
'printAnswer',
['THE_NUMBER', 'log'],
true
))
.run(['printAnswer']);
printAnswer(); // 42Decorator that creates an initializer from a service builder by automatically detecting its name and dependencies
Kind: global function
Returns: function - Returns a new initializer
| Param | Type | Description |
|---|---|---|
| serviceBuilder | function |
An async function to build the service |
Decorator that creates an initializer for a provider builder
Kind: global function
Returns: function - Returns a new provider initializer
| Param | Type | Description |
|---|---|---|
| providerBuilder | function |
An async function to build the service provider |
| [name] | String |
The service's name |
| [dependencies] | Array.<String> |
The service's dependencies |
| [singleton] | Boolean |
Whether the service is a singleton or not |
| [extra] | any |
Eventual extra information |
Example
import Knifecycle, { provider } from 'knifecycle'
import fs from 'fs';
const $ = new Knifecycle();
$.register(provider(configProvider, '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,
});
});
});
}Decorator that creates an initializer from a provider builder by automatically detecting its name and dependencies
Kind: global function
Returns: function - Returns a new provider initializer
| Param | Type | Description |
|---|---|---|
| providerBuilder | function |
An async function to build the service provider |
Explode a dependency declaration an returns its parts.
Kind: global function
Returns: Object - The various parts of it
| Param | Type | Description |
|---|---|---|
| dependencyDeclaration | String |
A dependency declaration string |
Example
parseDependencyDeclaration('pgsql>db');
// Returns
{
serviceName: 'pgsql',
mappedName: 'db',
optional: false,
}Stringify a dependency declaration from its parts.
Kind: global function
Returns: String - The various parts of it
| Param | Type | Description |
|---|---|---|
| dependencyDeclarationParts | Object |
A dependency declaration string |
Example
stringifyDependencyDeclaration({
serviceName: 'pgsql',
mappedName: 'db',
optional: false,
});
// Returns
'pgsql>db'