Skip to content

Commit 5cf4c42

Browse files
committed
.
0 parents  commit 5cf4c42

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules/
2+
coverage/
3+
tmp/
4+
dist/
5+
npm-debug.log*
6+
.DS_Store
7+
.nyc_output

.travis.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_js:
2+
- "4"
3+
- "5"
4+
- "6"
5+
- "7"
6+
sudo: false
7+
language: node_js
8+
script: "npm run test"
9+
# after_success: "npm i -g codecov && npm run coverage && codecov"

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Yoshua Wuyts
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# microbounce [![stability][0]][1]
2+
[![npm version][2]][3] [![build status][4]][5]
3+
[![downloads][8]][9] [![js-standard-style][10]][11]
4+
5+
Smol debounce package.
6+
7+
## Usage
8+
```js
9+
var Microbounce = reuqire('microbounce')
10+
var html = require('bel')
11+
12+
var microbounce = Microbounce()
13+
html`
14+
<input onkeydown=${onkeydown}>click me</button>
15+
`
16+
function onkeydown (e) {
17+
var value = e.target.value
18+
microbounce(function () {
19+
console.log('called at the start of a new frame', value)
20+
})
21+
}
22+
```
23+
24+
## Why?
25+
Because most debounce functions don't work well when doing DOM diffing. This
26+
package is specifically made to work well with DOM diffing.
27+
28+
## API
29+
### `microbounce = Microbounce([timeout])`
30+
Create a new instance. Timeout defaults to `256ms`.
31+
32+
### `microbounce(callback)`
33+
Debounce a callback for the duration of the timeout. The last callback wins if
34+
called multiple times in a row.
35+
36+
## License
37+
[MIT](https://tldrlegal.com/license/mit-license)
38+
39+
[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
40+
[1]: https://nodejs.org/api/documentation.html#documentation_stability_index
41+
[2]: https://img.shields.io/npm/v/microbounce.svg?style=flat-square
42+
[3]: https://npmjs.org/package/microbounce
43+
[4]: https://img.shields.io/travis/yoshuawuyts/microbounce/master.svg?style=flat-square
44+
[5]: https://travis-ci.org/yoshuawuyts/microbounce
45+
[6]: https://img.shields.io/codecov/c/github/yoshuawuyts/microbounce/master.svg?style=flat-square
46+
[7]: https://codecov.io/github/yoshuawuyts/microbounce
47+
[8]: http://img.shields.io/npm/dm/microbounce.svg?style=flat-square
48+
[9]: https://npmjs.org/package/microbounce
49+
[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
50+
[11]: https://github.com/feross/standard

index.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = Microbounce
2+
3+
function Microbounce (timeout) {
4+
timeout = timeout || 256
5+
6+
var callback = null
7+
var last = null
8+
var inFlight = false
9+
10+
return function (cb) {
11+
callback = cb
12+
last = Date.now()
13+
14+
if (!inFlight) {
15+
inFlight = true
16+
window.setTimeout(next, timeout)
17+
}
18+
19+
function next () {
20+
var diff = Date.now() - last
21+
if (diff > timeout) {
22+
last = null
23+
inFlight = false
24+
callback()
25+
callback = null
26+
} else {
27+
window.setTimeout(next, diff)
28+
}
29+
}
30+
}
31+
}

package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "microbounce",
3+
"description": "Smol debounce package",
4+
"repository": "yoshuawuyts/microbounce",
5+
"version": "1.0.0",
6+
"scripts": {
7+
"deps": "dependency-check . && dependency-check . --extra --no-dev",
8+
"start": "node .",
9+
"test": "standard && npm run deps",
10+
"coverage": "nyc report --reporter=text-lcov > coverage.lcov"
11+
},
12+
"dependencies": {},
13+
"devDependencies": {
14+
"dependency-check": "^2.8.0",
15+
"nyc": "^10.1.2",
16+
"standard": "^9.0.2",
17+
"tape": "^4.6.3"
18+
},
19+
"keywords": [
20+
"debounce"
21+
]
22+
}

0 commit comments

Comments
 (0)