Comparing version 1.0.1 to 1.1.0
{ | ||
"name": "pdi", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Minimal Promise based dependency injection framework", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -0,3 +1,3 @@ | ||
PDI - Minimal Promise based Dependency Injection framework | ||
This is a simple library that provides a powerful abstraction for dealing with dependency injection both for system start up and for complex async tasks. | ||
@@ -73,1 +73,23 @@ | ||
This function is useful for where the DI container will be used to perform a particular operation, rather then for system start-up. It returns a DI container with the `add`, `start` and `clear` methods. | ||
While being small this library is powerful enough to be used for async flow | ||
control, for example: | ||
```javascript | ||
const flow = pdi() | ||
flow.add("body", req.body) | ||
flow.add("userId", ["body"], prop("userId")) | ||
flow.add("user", ["userId"], getUser) | ||
flow.add("friends", ["user"], getFriends) | ||
flow.add("result", ["friends", "user"], assoc("friends")) | ||
flow.start(["result"]) | ||
.then((result) => res.send(result)) | ||
.catch((err) => res.sendStatus(500)) | ||
``` | ||
Extra utils for testing: | ||
- `pdi._test.clear()` | ||
- `pdi._test.` |
@@ -56,2 +56,3 @@ const Promise = require("bluebird") | ||
const names = pluck("name", items) | ||
console.log(`Initialising ${names.join(", ")}`) | ||
return Promise.map(items, (item) => { | ||
@@ -72,4 +73,7 @@ const args = map(flip(prop)(memo), item.deps) | ||
let nameIdx = 0 | ||
const startTime = Date.now() | ||
let firstAdd | ||
function add(name, deps, fn) { | ||
if (!firstAdd) firstAdd = Date.now() | ||
if (activated) { | ||
@@ -100,10 +104,16 @@ throw new Error(`DI already activated - can't register: ${name} `) | ||
} | ||
if (deps && fn) { | ||
add(deps, fn) | ||
} | ||
console.log("First add", firstAdd - startTime) | ||
console.log("Activation started", Date.now() - startTime) | ||
const sorted = checkAndSortDependencies(registry) | ||
return startActivation(sorted).then((_modules) => { | ||
console.log("Activation complete", Date.now() - startTime) | ||
modules = _modules | ||
activated = true | ||
return modules | ||
let result = true | ||
if (deps && fn) { | ||
console.log(`Running start function with ${deps.join(", ")}`) | ||
result = apply(fn, map(flip(prop)(modules), deps)) | ||
} | ||
return Promise.resolve(result) | ||
}) | ||
@@ -110,0 +120,0 @@ } |
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
11461
257
95