Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

diogenes

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

diogenes - npm Package Compare versions

Comparing version 3.1.0 to 3.2.0

src/lib/diogenes-shutdown.js

5

package.json
{
"name": "diogenes",
"version": "3.1.0",
"version": "3.2.0",
"description": "A dependency injection framework.",

@@ -38,4 +38,5 @@ "main": "src/index.js",

"es6-promisify": "^6.0.0",
"object-assign": "^4.1.1"
"object-assign": "^4.1.1",
"uuid": "^3.2.1"
}
}

@@ -304,2 +304,17 @@ Diogenes

shutdown
--------
The purpose of this method is allow all asynchronous call to be terminated before a system shutdown.
After calling this method the service won't execute the "run" method anymore (It will return an exception). The method returns a promise (or a callback). This will be fulfilled when all previous "run" has been fulfilled of rejected.
```js
const A = registry.run('A')
const C = registry.run('C')
registry.shutdown()
.then(() => {
// "A" and "C" are fulfilled
registry.run('B') // rejected with DiogenesShutdownError
})
```
Service

@@ -306,0 +321,0 @@ =======

53

src/registry.js
var assign = require('object-assign')
var uuid = require('uuid/v1')
var Service = require('./service')
var DiogenesError = require('./lib/diogenes-error')
var DiogenesShutdownError = require('./lib/diogenes-shutdown')

@@ -11,2 +13,4 @@ /*

this.services = {}
this.running = {}
this._isShuttingDown = false
}

@@ -30,3 +34,3 @@

if (!(name in this.services)) {
this.services[name] = new Service(name, this)
this.services[name] = new Service(name)
}

@@ -58,3 +62,9 @@

var c = 0
var runId = uuid()
var promise
if (this._isShuttingDown) {
return Promise.reject(new DiogenesShutdownError('Diogenes: shutting down'))
}
function getPromiseFromStr (str) {

@@ -71,6 +81,6 @@ if (c++ > 1000) {

if (deps.length === 0) {
return registry.services[str]._run({})
return registry.services[str]._run(runId, {})
}
return getPromisesFromStrArray(deps)
.then(registry.services[str]._run.bind(registry.services[str]))
.then(registry.services[str]._run.bind(registry.services[str], runId))
}

@@ -90,4 +100,15 @@

try {
return getPromiseFromStr(name)
promise = getPromiseFromStr(name)
.then(function (res) {
delete registry.running[runId]
return Promise.resolve(res)
})
.catch(function (err) {
delete registry.running[runId]
return Promise.reject(err)
})
registry.running[runId] = promise
return promise
} catch (e) {
delete registry.running[runId]
return Promise.reject(e)

@@ -143,2 +164,26 @@ }

Registry.prototype.shutdown = function registryShutdown (done) {
var registry = this
registry._isShuttingDown = true
var promise = Promise.all(Object.keys(registry.running)
.map(function (key) {
return registry.running[key]
.catch(function () { return Promise.resolve(null) })
}))
if (done) {
promise
.then(function (res) {
done(null, res)
})
.catch(function (err) {
done(err)
})
return this
} else {
return promise
}
}
module.exports = Registry

@@ -27,5 +27,4 @@ /*

function Service (name, registry) {
function Service (name) {
this.name = name
this._registry = registry // backreference
this._deps = function () { return [] }

@@ -37,6 +36,2 @@ this._func = function () { return Promise.resolve() }

Service.prototype.registry = function serviceRegistry () {
return this._registry
}
Service.prototype.doc = function serviceDoc (text) {

@@ -88,8 +83,9 @@ if (typeof text === 'undefined') {

Service.prototype._run = function serviceRun (deps) {
Service.prototype._run = function serviceRun (id, deps) {
var service = this
var context = { id: id, service: service }
if (service._cache) {
return service._cache
}
service._cache = service._func(deps)
service._cache = service._func.call(context, deps)
.catch(function (err) {

@@ -96,0 +92,0 @@ service._cache = undefined

@@ -136,2 +136,4 @@ /* eslint-env node, mocha */

registry.service('hello').provides(function (deps, next) {
assert.typeOf(this.id, 'string')
assert.isDefined(this.service)
assert.deepEqual(deps, {})

@@ -142,2 +144,4 @@ next(undefined, 'hello ')

registry.service('world').dependsOn(['hello']).provides(function (deps, next) {
assert.typeOf(this.id, 'string')
assert.isDefined(this.service)
assert.deepEqual(deps, {hello: 'hello '})

@@ -241,2 +245,72 @@ next(undefined, deps.hello + 'world!')

})
describe('shutdown', function () {
it('must wait to shutdown', function (done) {
var services = ''
registry.service('A').provides(function (deps, next) {
setTimeout(function () {
services += 'A'
next(undefined, undefined)
}, 100)
})
registry.service('B').dependsOn(['A']).provides(function (deps, next) {
setTimeout(function () {
services += 'B'
next(undefined, undefined)
}, 100)
})
registry.service('C').dependsOn(['A']).provides(function (deps, next) {
setTimeout(function () {
services += 'C'
next(undefined, undefined)
}, 100)
})
registry.run('B')
registry.run('C')
registry.shutdown(function () {
registry.run('B', function (err) {
assert.equal(err.message, 'Diogenes: shutting down')
})
assert.equal(services, 'ABC')
done()
})
})
it('must wait to shutdown, also failing methods', function (done) {
var services = ''
registry.service('A').provides(function (deps, next) {
services += 'A'
next(new Error('broken'), undefined)
})
registry.service('B').provides(function (deps, next) {
setTimeout(function () {
services += 'B'
next(undefined, undefined)
}, 100)
})
registry.service('C').dependsOn(['B']).provides(function (deps, next) {
setTimeout(function () {
services += 'C'
next(undefined, undefined)
}, 100)
})
registry.run('A')
registry.run('C')
registry.shutdown(function () {
registry.run('B', function (err) {
assert.equal(err.message, 'Diogenes: shutting down')
})
assert.equal(services, 'ABC')
done()
})
})
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc