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 0.6.0 to 1.0.0

4

package.json
{
"name": "diogenes",
"version": "0.6.0",
"version": "1.0.0",
"description": "Defines and executes functions with a common interface (services) sorting out the execution order automatically.",

@@ -32,3 +32,3 @@ "main": "src/index.js",

"dependencies": {
"async-deco": "^4.2.1",
"async-deco": "^4.3.0",
"memoize-cache": "^1.0.2",

@@ -35,0 +35,0 @@ "object-assign": "^4.0.1",

@@ -68,3 +68,3 @@ Diogenes

```
This second version is not only more concise but is also much more efficient as it executes some operations in parallel. Also automatic propagation of errors is a very cool feature!
This second version is not only more concise but is also much more efficient as it executes two functions in parallel (decodeURL and retrieveTemplate). Also automatic propagation of errors allows to be much more coincise!
But still is not perfect: handling the passage of values can be clumsy, swallowing exception when you forgot the "catch" can be dangerous, mixing promises and callback annoying ...

@@ -246,3 +246,3 @@ But there is a worst issue, you are designing how these components interact between them, in an imperative way. Wouldn't it better if the system could figure out the execution order ? Even better working flawlessly with promises, synchronous functions and callbacks ?

The "provides", "returns" and "returnsValue" methods can take 2 validators. The first one will match the config, the second the dependencies.
So you can change on the fly which function use depending on the arguments (config and deps).
So you can change on the fly which function to use depending on the arguments (config and deps).

@@ -273,3 +273,3 @@ ![Registry as graph](https://cloud.githubusercontent.com/assets/460811/11994528/0fade84a-aa38-11e5-92d2-4f4d8f60dc4d.png)

```
You just extended the system without changing the original code!
You have just extended the system without changing the original code!

@@ -356,3 +356,3 @@ Caching a service

For synchronous or promise based function you can do the same, the original function is always converted to a callback based function before applying the decorators.
For synchronous or promise based function you can do the same, the original function is always converted to a "callback based function" before applying the decorators.
```js

@@ -368,10 +368,12 @@ registry.service('myservice').returns([

Logging
=======
You can pass a logger function to the instance method:
Logging and events
==================
Events are fired every time something is logged in a function. You can listen to all log events using:
```js
registry.instance(config, {logger: logger});
registry.events.on(function (name, id, ts, evt, payload, instance) {
//
});
```
These are the arguments passed to the event handler:
logger is a function that takes these arguments:
* name: the name of the service

@@ -382,4 +384,16 @@ * id: a random id that changes every time you call "run"

* payload: an object with additional information about this event
* instance: an instance object
Services don't log anything by default. You can rely on what is logged by the decorators added [async-deco](https://github.com/sithmel/async-deco).
Services don't log anything by default. You can rely on what is logged by the [decorators](https://github.com/sithmel/async-deco).
```js
var logDecorator = require('async-deco/callback/log');
registry.service('myservice').provides([
logDecorator(), // this logs start, error and error events
function (config, deps, next) {
next(...);
}
]);
```
You can also add your own custom log:

@@ -414,2 +428,22 @@ ```js

Registry's attributes
=====================
events
------
Events are fired every time something is logged in one function.
```js
registry.events.on(function (name, id, ts, evt, payload, instance) {
//
});
```
These are the arguments passed to the event handler:
* name: the name of the service
* id: a random id that changes every time you call "run"
* ts: the timestamp for this event
* evt: the name of the event
* payload: an object with additional information about this event
* instance: the registry instance
The "events" object is an occamsrazor registry. You can find its API [here](https://github.com/sithmel/occamsrazor.js)
Registry's methods

@@ -431,13 +465,5 @@ ==================

```
The config argument will be passed to all services (calling the run method). Currently there are 2 options:
The config argument will be passed to all services (calling the run method). The only option available is:
* limit: limit the number of services executed in parallel (defaults to Infinity)
* logger: a function that takes these arguments:
* name: the name of the service
* id: a random id that changes every time you call "run"
* ts: the timestamp for this event
* evt: the name of the event
* payload: an object with additional information about this event
See the section above on how to log.
remove

@@ -570,6 +596,2 @@ ------

options
-------
Set the options for this object. They are the same defined in the "instance" method (registry object).
getExecutionOrder

@@ -576,0 +598,0 @@ -----------------

@@ -39,6 +39,2 @@ var depSort = require('./lib/dep-sort');

RegistryInstance.prototype.options = function registryInstance_options(options) {
this._options = options;
};
RegistryInstance.prototype._filterByConfig = function registryInstance__filterByConfig() {

@@ -75,2 +71,3 @@ var registry = this._registry;

RegistryInstance.prototype._run = function registryInstance__run(name, done) {
var instance = this;
var config = this._config;

@@ -85,3 +82,6 @@ var getAdjlists = this._filterByConfig();

var id = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
var logger = 'logger' in this._options ? this._options.logger : undefined;
var logger = function (name, id, ts, evt, payload) {
// not using trigger because it introduces a timeout
instance._registry.events.all(name, id, ts, evt, payload, instance);
};

@@ -149,3 +149,3 @@ if (!done) {

try {
context = logger ? buildLogger(currentService, currentService.name, id, logger) : currentService;
context = buildLogger(currentService, currentService.name, id, logger);
func = currentService._getFunc(config, currentServiceDeps, context, resolve);

@@ -152,0 +152,0 @@ }

var assign = require('object-assign');
var or = require('occamsrazor');

@@ -15,2 +16,3 @@ var Service = require('./service');

this.services = {};
this.events = or();
}

@@ -37,2 +39,6 @@

var events = Array.prototype.map.call(arguments, function (reg) {
return reg.events;
});
var services = Array.prototype.map.call(arguments, function (reg) {

@@ -45,2 +51,4 @@ return reg.services;

registry.events = this.events.merge.apply(null, events);
registry.services = assign.apply(null, services);

@@ -47,0 +55,0 @@ return registry;

@@ -91,7 +91,8 @@ var Diogenes = require('../src');

var logs = [];
var logger = function (name, id, ts, evt, payload) {
logs.push({name: name, evt: evt});
};
var registry = Diogenes.getRegistry();
registry.events.on(function (name, id, ts, evt, payload) {
logs.push({name: name, evt: evt});
});
registry.service('hello').provides([

@@ -113,3 +114,3 @@ logDecorator(),

registry.instance({}, {logger: logger}).run('world', function (err, dep) {
registry.instance({}).run('world', function (err, dep) {
assert.instanceOf(err, Error);

@@ -116,0 +117,0 @@ assert.equal(err.message, 'broken');

@@ -15,2 +15,4 @@ var Diogenes = require('../src');

registry2.service('question').returnsValue('the answer to life the universe and everything');
registry1.events.on(function () {});
registry2.events.on(function () {});
registry3 = registry1.merge(registry2);

@@ -24,2 +26,6 @@ });

it('must copy the events', function () {
assert.equal(registry3.events.size(), 2);
});
it('must copy the services', function () {

@@ -49,3 +55,3 @@ assert.equal(Object.keys(registry3.services).length, 2);

registry.service('hello').provides(function (config, deps, next) {
assert.equal(registry.service('hello'), this);
assert(registry.service('hello').isPrototypeOf(this));
assert.deepEqual(deps, {});

@@ -52,0 +58,0 @@ next(undefined, 'hello');

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