level-trigger
Advanced tools
Comparing version 1.1.5 to 1.2.0
40
index.js
@@ -11,7 +11,29 @@ var shasum = require('shasum') | ||
//create a section inside the input | ||
var pending = {}, running = {} | ||
var pending = {}, running = {}, runningCount = 0, pendingCount = 0 | ||
if('string' === typeof jobs) | ||
jobs = input.sublevel(jobs) | ||
var working = false | ||
function checkIncomplete () { | ||
if(working) return | ||
if(0 === runningCount + pendingCount) return | ||
working = true | ||
jobs.emit('incomplete') | ||
} | ||
function checkComplete () { | ||
var _working = (0 !== (runningCount + pendingCount)) | ||
if(_working === working) return | ||
working = _working | ||
jobs.emit(working ? 'incomplete' : 'complete') | ||
} | ||
jobs.isComplete = function () { | ||
return !working | ||
} | ||
var retry = [] | ||
@@ -24,4 +46,7 @@ | ||
if(!running[hash]) | ||
if(!running[hash]) { | ||
runningCount ++ | ||
running[hash] = true | ||
checkComplete() | ||
} | ||
else return | ||
@@ -43,7 +68,13 @@ | ||
if(err) return retry.push(data) | ||
runningCount -- | ||
delete running[hash] | ||
if(pending[hash]) { | ||
pendingCount -- | ||
delete pending[hash] | ||
doJob(data) | ||
} | ||
checkComplete() | ||
}) | ||
@@ -60,4 +91,7 @@ }) | ||
add({key: timestamp(), value: key, type: 'put', prefix: jobs}) | ||
else | ||
else if(!pending[hash]) { | ||
pendingCount ++ | ||
pending[hash] = true | ||
checkComplete() | ||
} | ||
} | ||
@@ -64,0 +98,0 @@ |
{ | ||
"name": "level-trigger", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"homepage": "https://github.com/dominictarr/level-trigger", | ||
@@ -5,0 +5,0 @@ "repository": { |
var levelup = require('level-test')() | ||
var sublevel = require('level-sublevel') | ||
var assert = require('assert') | ||
@@ -39,2 +40,8 @@ var mac = require('macgyver')().autoValidate() | ||
var _done = false | ||
trigDb.on('complete', mac(function (k, n) { | ||
_done = true | ||
console.log('jobs finished') | ||
}).atLeast(1)) | ||
//if we don't wait for the queue to drain, | ||
@@ -50,4 +57,13 @@ //then it will read puts from these twice. | ||
var i = setInterval(function () { | ||
assert.equal(typeof trigDb.isComplete(), 'boolean') | ||
if(trigDb.isComplete()) | ||
clearInterval(i) | ||
}, 20) | ||
db.on('test:reduce', mac().times(5)) | ||
process.on('exit', function () { | ||
assert.equal(_done, true) | ||
}) | ||
9797
236