occamsrazor
Advanced tools
Comparing version 9.1.0 to 9.1.1
Changelog | ||
9.1.1 (2018/02/23) | ||
================== | ||
- fix: batch queue empties after execution | ||
- fix: remove duplicated events when comparator returns 0 | ||
9.1.0 (2018/02/22) | ||
@@ -4,0 +9,0 @@ ================== |
@@ -13,3 +13,5 @@ var ut = require('./utils') | ||
function all () { | ||
return _queue.map(function (item) { | ||
var q = _queue | ||
_queue = [] | ||
return q.map(function (item) { | ||
return or.all.apply(item.context, item.args) | ||
@@ -36,3 +38,5 @@ }).reduce(function (a, b) { | ||
function adapt () { | ||
return _queue.map(function (item) { | ||
var q = _queue | ||
_queue = [] | ||
return q.map(function (item) { | ||
return or.adapt.apply(item.context, item.args) | ||
@@ -39,0 +43,0 @@ }) |
@@ -124,3 +124,3 @@ require('setimmediate') | ||
if (comparator(value, array[start]) <= 0) { // !! | ||
if (comparator(value, array[start]) < 0) { // !! | ||
array.splice(start, 0, value) | ||
@@ -139,3 +139,3 @@ return | ||
if (comparator(value, array[m]) >= 0) { | ||
if (comparator(value, array[m]) > 0) { | ||
binaryInsert(array, value, comparator, m + 1, end) | ||
@@ -142,0 +142,0 @@ } |
{ | ||
"name": "occamsrazor", | ||
"version": "9.1.0", | ||
"version": "9.1.1", | ||
"description": "A plugin system for Javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -321,5 +321,26 @@ Occamsrazor | ||
Trigger takes a callback that returns the result (or, in case an error). | ||
Calling trigger/all empties the batch. It can be reused to queue other messages. | ||
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. | ||
A classic use case is to collect a sequence of messages and then executing them on request animation frame. | ||
```js | ||
var events = occamsrazor(); | ||
var batch = events.batch(); | ||
function mainLoop() { | ||
batch.triggerSync(); | ||
requestAnimationFrame(mainLoop); | ||
} | ||
requestAnimationFrame(mainLoop); | ||
document.addEventListener('keydown', function(event) { | ||
batch.queue('keydown', event.keyCode); | ||
}); | ||
events.on('keydown', 38, function () { | ||
... dealing with up arrow ... | ||
}); | ||
``` | ||
Namespace | ||
@@ -398,3 +419,5 @@ ========= | ||
This should sort by the first argument (assuming is a number), from the smallest. | ||
The comparator works in a special way when returning 0 (items with same priority): it doesn't proceed on storing the new event. You can use this special behaviour to remove duplicated events in the queue. | ||
Function registry API | ||
@@ -548,2 +571,6 @@ ===================== | ||
If a comparator is set, the queue respect that order. | ||
It accepts to be bound to a context. | ||
```js | ||
batch.queue.apply(something, [...args...])) | ||
``` | ||
@@ -558,2 +585,3 @@ .adapt | ||
``` | ||
It empties the queue. | ||
@@ -567,2 +595,3 @@ .all/.triggerSync | ||
``` | ||
It empties the queue. | ||
@@ -580,3 +609,3 @@ .trigger | ||
``` | ||
It returns the occamsrazor object. | ||
It empties the queue, and returns the adapter object. | ||
@@ -583,0 +612,0 @@ registry |
@@ -59,2 +59,12 @@ /* eslint-env node, mocha */ | ||
it('return adapters (empty array)', function () { | ||
var adapter = occamsrazor() | ||
adapter.add(isNumber, function (n) { | ||
return n * n | ||
}) | ||
var batch = adapter.batch() | ||
assert.deepEqual(batch.adapt(), []) | ||
assert.deepEqual(batch.all(), []) | ||
}) | ||
it('return all adapters', function () { | ||
@@ -61,0 +71,0 @@ var adapter = occamsrazor() |
@@ -23,3 +23,3 @@ /* eslint-env node, mocha */ | ||
ut.binaryInsert(array, 10, comparator) | ||
assert.deepEqual(array, [10, 10, 12, 12, 13, 15]) | ||
assert.deepEqual(array, [10, 12, 12, 13, 15]) | ||
}) | ||
@@ -34,3 +34,3 @@ | ||
ut.binaryInsert(array, 13, comparator) | ||
assert.deepEqual(array, [10, 12, 12, 13, 13, 15]) | ||
assert.deepEqual(array, [10, 12, 12, 13, 15]) | ||
}) | ||
@@ -40,3 +40,3 @@ | ||
ut.binaryInsert(array, 12, comparator) | ||
assert.deepEqual(array, [10, 12, 12, 12, 13, 15]) | ||
assert.deepEqual(array, [10, 12, 12, 13, 15]) | ||
}) | ||
@@ -51,3 +51,3 @@ | ||
ut.binaryInsert(array, 15, comparator) | ||
assert.deepEqual(array, [10, 12, 12, 13, 15, 15]) | ||
assert.deepEqual(array, [10, 12, 12, 13, 15]) | ||
}) | ||
@@ -54,0 +54,0 @@ |
130056
27
2998
658