@laconia/core
Advanced tools
Comparing version 0.9.0 to 0.10.0
{ | ||
"name": "@laconia/core", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"description": "Micro dependency injection framework", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -157,9 +157,11 @@ # @laconia/core | ||
#### `register(factoryFn, options)` | ||
#### `register(factory, options)` | ||
Registers objects into LaconiaContext. Objects registered here will be made | ||
available in the Lambda function execution. | ||
Registers objects created by the factory function into LaconiaContext. | ||
Objects registered here will be made available in the Lambda function execution. | ||
You can pass an array for the list of array to be called in parallel. | ||
* `factoryFn(laconiaContext)` | ||
* `factory(laconiaContext)` | ||
* This `Function` is called when your Lambda is invoked | ||
* When an `Array` is specified, the list of factories within the array will be called concurrently with Promise.all | ||
* An object which contains the instances to be registered must be returned | ||
@@ -171,3 +173,3 @@ * `options`: | ||
* `maxAge = 300000` | ||
* Your factoryFn will be called when the cache has reached its maximum age specified by this option | ||
* Your factory will be called when the cache has reached its maximum age specified by this option | ||
@@ -182,2 +184,9 @@ Example: | ||
// Register concurrent factories | ||
const handler = () => {}; | ||
laconia(handler).register([ | ||
ssmConfig.envVarInstances(), | ||
s3Config.envVarInstances() | ||
]); | ||
// Reduce maxAge | ||
@@ -184,0 +193,0 @@ const handler = () => {}; |
@@ -22,4 +22,8 @@ const CoreLaconiaContext = require("./CoreLaconiaContext"); | ||
}, | ||
register: (factoryFn, options = {}) => { | ||
laconiaContext.registerFactory(factoryFn, options.cache); | ||
register: (factory, options = {}) => { | ||
if (Array.isArray(factory)) { | ||
laconiaContext.registerFactories(factory, options.cache); | ||
} else { | ||
laconiaContext.registerFactory(factory, options.cache); | ||
} | ||
return laconia; | ||
@@ -26,0 +30,0 @@ } |
@@ -9,2 +9,10 @@ const prefixKeys = (prefix, object) => { | ||
const parallelFactoryFns = factoryFns => async (...args) => { | ||
const responses = await Promise.all(factoryFns.map(f => f(...args))); | ||
return responses.reduce( | ||
(allInstances, instances) => Object.assign(allInstances, instances), | ||
{} | ||
); | ||
}; | ||
module.exports = class LaconiaContext { | ||
@@ -22,6 +30,10 @@ constructor(baseContext) { | ||
registerFactory(factory) { | ||
registerFactory(factory, options = {}) { | ||
this._factoryFns.push(factory); | ||
} | ||
registerFactories(factories, options = {}) { | ||
this.registerFactory(parallelFactoryFns(factories), options); | ||
} | ||
async refresh() { | ||
@@ -28,0 +40,0 @@ for (const factoryFn of this._factoryFns) { |
12508
144
223