occamsrazor
Advanced tools
Comparing version 9.0.0 to 9.1.0
Changelog | ||
9.1.0 (2018/02/22) | ||
================== | ||
- fix: it doesn't throw an exception when a single adapter is not found. Just undefined | ||
- added "batch" feature | ||
9.0.0 (2018/02/12) | ||
@@ -9,3 +14,2 @@ ================== | ||
- introduced a comparator to set the order of the posted events | ||
- changed constructor API (using an object) | ||
@@ -12,0 +16,0 @@ 8.0.0 (2018/02/06) |
var ut = require('./utils') | ||
var _batch = require('./batch') | ||
@@ -55,5 +56,7 @@ function occamsrazor (options) { | ||
var filteredAdapter = ut.filterAndSortOne(args, _adapters) // the most specific | ||
ut.countdown(filteredAdapter) | ||
ut.filterArray(_adapters, ut.notExausted) | ||
return filteredAdapter.func.apply(context, args) | ||
if (filteredAdapter) { | ||
ut.countdown(filteredAdapter) | ||
ut.filterArray(_adapters, ut.notExausted) | ||
return filteredAdapter.func.apply(context, args) | ||
} | ||
} | ||
@@ -145,2 +148,6 @@ | ||
function batch () { | ||
return _batch(or, comparator) | ||
} | ||
// returns a callable object | ||
@@ -174,2 +181,3 @@ function or () { | ||
or.proxy = or.namespace = proxy | ||
or.batch = batch | ||
@@ -176,0 +184,0 @@ or.getAdapters = function () { return _adapters } |
@@ -51,8 +51,10 @@ require('setimmediate') | ||
function triggerOne (context, args, func) { | ||
setImmediate(function () { func.apply(context, args) }) | ||
} | ||
function triggerAll (context, args, adapters) { | ||
for (var i = 0; i < adapters.length; i++) { | ||
setImmediate((function (adapter) { | ||
return function () { adapter.func.apply(context, args) } | ||
}(adapters[i]))) | ||
} | ||
adapters.forEach(function (adapter) { | ||
triggerOne(context, args, adapter.func) | ||
}) | ||
} | ||
@@ -83,3 +85,3 @@ | ||
if (results.length === 0) { | ||
throw new Error('Occamsrazor (get): Function not found') | ||
return | ||
} | ||
@@ -141,4 +143,2 @@ | ||
} | ||
// we don't insert duplicates | ||
} | ||
@@ -151,2 +151,3 @@ | ||
triggerAll: triggerAll, | ||
triggerOne: triggerOne, | ||
countdown: countdown, | ||
@@ -153,0 +154,0 @@ filterAndSort: filterAndSort, |
{ | ||
"name": "occamsrazor", | ||
"version": "9.0.0", | ||
"version": "9.1.0", | ||
"description": "A plugin system for Javascript", | ||
@@ -38,3 +38,4 @@ "main": "index.js", | ||
"mocha": "^5.0.0", | ||
"npm-release": "^1.0.0" | ||
"npm-release": "^1.0.0", | ||
"occamsrazor-match": "^4.1.0" | ||
}, | ||
@@ -41,0 +42,0 @@ "dependencies": { |
@@ -212,3 +212,2 @@ Occamsrazor | ||
Getting all | ||
@@ -275,3 +274,2 @@ =========== | ||
Consume/consumeOne | ||
@@ -298,2 +296,32 @@ ================== | ||
Batches | ||
======= | ||
This feature allows to queue many "messages" (calls to trigger/all) and then trigger them all at once. | ||
```js | ||
var events = occamsrazor(); | ||
events.on(isNumber, function (n) { | ||
return n * n; | ||
}) | ||
var batch = events.batch(); | ||
batch | ||
.queue(2) | ||
.queue(3); | ||
var results = batch.all(); // [4, 9]) | ||
``` | ||
You can queue messages using queue (you can chain that), and use .all (or the alias triggerSync) to call them in synchronously. | ||
You can also use trigger to call them asynchronously: | ||
```js | ||
batch.trigger(function (err, res) { | ||
// if any function throws an exception, this is captured and | ||
// res is [4, 9]) | ||
}); | ||
``` | ||
Trigger takes a callback that returns the result (or, in case an error). | ||
Important detail: using the events are triggered either synchronously (all/triggerSync) or asynchronously (trigger) in the same microtask. If any function throws an exception the execution stops. | ||
Namespace | ||
@@ -386,2 +414,3 @@ ========= | ||
It retains the context (this). If more than one function matches with the same score it throws an exception. | ||
If no function match, it returns undefined. | ||
@@ -405,3 +434,3 @@ .all (alias .triggerSync) | ||
.post (alias .stick) | ||
------ | ||
-------------------- | ||
```js | ||
@@ -413,3 +442,3 @@ funcs.post([arg1, arg2 ...]); | ||
.unpost (alias .unstick) | ||
------ | ||
------------------------ | ||
```js | ||
@@ -491,4 +520,4 @@ funcs.unpost(validator, validator, validator, ...); | ||
.functions | ||
---------------- | ||
.getAdapters | ||
------------ | ||
Syntax: | ||
@@ -501,2 +530,56 @@ ```js | ||
.batch | ||
------ | ||
It returns a batch object. | ||
Syntax: | ||
```js | ||
var batch = funcs.batch(); | ||
``` | ||
batch | ||
===== | ||
This object allows to queue multiple calls. | ||
.queue | ||
------ | ||
Queue arguments in the batch. | ||
Syntax: | ||
```js | ||
batch.queue(... args ...); | ||
``` | ||
This method returns the batch (it is chainable). | ||
If a comparator is set, the queue respect that order. | ||
.adapt | ||
------ | ||
It takes any item on the queue, search the most specific function, and execute that function with the arguments. It returns an array with the results. If a group of arguments doesn't match any function, it returns undefined (for that item). | ||
As usual if a group of arguments matches multiple times with the same score, it throws an exception. | ||
Syntax: | ||
```js | ||
var results = batch.adapt(); | ||
``` | ||
.all/.triggerSync | ||
----------------- | ||
It takes any item on the queue, search the all the functions matching these arguments, and execute those function with the arguments. It returns an array with the results (flattened). If a group of arguments doesn't match any function, it returns undefined (for that item). | ||
Syntax: | ||
```js | ||
var results = batch.all(); | ||
``` | ||
.trigger | ||
-------- | ||
It takes any item on the queue, search the all the functions matching these arguments, and execute those function with the arguments. | ||
The functions are executed, in the next tick. | ||
It takes an optional callback, returning the error (or null) and an array with the results (flattened). If a group of arguments doesn't match any function, it returns undefined (for that item). | ||
Syntax: | ||
```js | ||
batch.trigger(function (err, results) { | ||
... | ||
}); | ||
``` | ||
It returns the occamsrazor object. | ||
registry | ||
@@ -544,3 +627,3 @@ ======== | ||
``` | ||
This will work with all methods returning asynchronously. So these won't work: adapt, all, triggerSync, size, proxy | ||
This will work with all methods returning asynchronously. So these won't work: adapt, all, triggerSync, size, proxy, batch | ||
@@ -547,0 +630,0 @@ About the name |
@@ -118,7 +118,2 @@ /* eslint-env node, mocha */ | ||
it('must throw if no matches', function () { | ||
var test = occamsrazor() | ||
assert.throws(test, 'Occamsrazor (get): Function not found') | ||
}) | ||
describe('adapter hierarchy', function () { | ||
@@ -125,0 +120,0 @@ var isAnything, isNumber, |
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
629
65563
11
1156