├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ 4 | dist/ 5 | npm-debug.log* 6 | .DS_Store 7 | .nyc_output 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 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" 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nanobounce [![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 Nanobounce = require('nanobounce') 10 | var html = require('bel') 11 | 12 | var nanobounce = Nanobounce() 13 | html` 14 | click me 15 | ` 16 | function onkeydown (e) { 17 | var value = e.target.value 18 | nanobounce(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 | ### `nanobounce = Nanobounce([timeout])` 30 | Create a new instance. Timeout defaults to `256ms`. 31 | 32 | ### `nanobounce(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/nanobounce.svg?style=flat-square 42 | [3]: https://npmjs.org/package/nanobounce 43 | [4]: https://img.shields.io/travis/yoshuawuyts/nanobounce/master.svg?style=flat-square 44 | [5]: https://travis-ci.org/yoshuawuyts/nanobounce 45 | [6]: https://img.shields.io/codecov/c/github/yoshuawuyts/nanobounce/master.svg?style=flat-square 46 | [7]: https://codecov.io/github/yoshuawuyts/nanobounce 47 | [8]: http://img.shields.io/npm/dm/nanobounce.svg?style=flat-square 48 | [9]: https://npmjs.org/package/nanobounce 49 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 50 | [11]: https://github.com/feross/standard 51 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = Nanobounce 2 | 3 | function Nanobounce (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 | 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 | setTimeout(next, diff) 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nanobounce", 3 | "description": "Smol debounce package", 4 | "repository": "yoshuawuyts/nanobounce", 5 | "version": "1.1.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 | } 23 | --------------------------------------------------------------------------------