Skip to content

Commit

Permalink
fix(throttle): remove the trailing occurrence from throttle
Browse files Browse the repository at this point in the history
  • Loading branch information
solkimicreb committed Dec 3, 2016
1 parent b3fa9e7 commit fdd7de3
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions limiters/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ module.exports = function throttle (next, context, threshold) {
if (threshold === undefined || isNaN(threshold)) {
threshold = 200
}

const last = context[lastExecution]
if (last && Date.now() < (last + threshold)) {
clearTimeout(context[timer])
context[timer] = setTimeout(execute, threshold, context, next)
} else {
execute(context, next)
const now = Date.now()
if (!last || (last + threshold) < now) {
context[lastExecution] = now
next()
}
}

function execute (context, next) {
context[lastExecution] = Date.now()
next()
}

0 comments on commit fdd7de3

Please sign in to comment.