Skip to content

Commit

Permalink
feat: 新增防抖和节流中间件
Browse files Browse the repository at this point in the history
  • Loading branch information
SystemLight committed Jul 11, 2022
1 parent 6512c75 commit 6c44257
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@systemlight/event-proxy",
"version": "1.1.1",
"version": "1.1.2",
"scripts": {
"lint:prettier": "prettier src/ -w",
"lint:eslint-check": "eslint src/ --ext .jsx,.js,ts,tsx",
Expand Down
34 changes: 34 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,38 @@ $e.removeActiveMiddleware = function (e, next) {
})
next()
}

/**
* 节流中间件
* @param delay
* @return {callback}
*/
$e.throttleMiddleware = function (delay = 500) {
let flags = true
return function (e, next) {
if (flags) {
flags = false
setTimeout(function () {
flags = true
next(e)
}, delay)
}
}
}

/**
* 防抖中间件
* @param delay
* @return {callback}
*/
$e.debounceMiddleware = function (delay = 500) {
let timer = null
return function (e, next) {
clearTimeout(timer)
timer = setTimeout(function () {
next(e)
}, delay)
}
}

export default $e

0 comments on commit 6c44257

Please sign in to comment.