├── .babelrc ├── .editorconfig ├── .gitignore ├── .travis.yml ├── package-lock.json ├── package.json ├── readme.md ├── src └── index.js └── test └── index.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015" 4 | ], 5 | "plugins": [ 6 | "transform-es2015-modules-commonjs" 7 | ] 8 | } -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | lib 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '8' 5 | notifications: 6 | email: false -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "throttle-decorator", 3 | "version": "0.2.0", 4 | "description": "ES decorator that make a function throttled.", 5 | "license": "MIT", 6 | "repository": "1000ch/throttle-decorator", 7 | "main": "lib/index.js", 8 | "scripts": { 9 | "clean": "rm -rf lib", 10 | "build": "npm run clean && babel src --out-dir lib", 11 | "prepublishOnly": "npm run build", 12 | "postinstall": "npm run build", 13 | "test": "xo src && ava" 14 | }, 15 | "author": { 16 | "name": "1000ch", 17 | "email": "shogo.sensui@gmail.com", 18 | "web": "https://github.com/1000ch" 19 | }, 20 | "files": [ 21 | "lib" 22 | ], 23 | "engines": { 24 | "node": ">=8.0.0" 25 | }, 26 | "bugs": { 27 | "url": "https://github.com/1000ch/throttle-decorator/issues" 28 | }, 29 | "xo": { 30 | "space": 2 31 | }, 32 | "ava": { 33 | "babel": { 34 | "presets": [ 35 | "@ava/stage-4", 36 | "@ava/transform-test-files" 37 | ], 38 | "plugins": [ 39 | "transform-decorators-legacy" 40 | ] 41 | } 42 | }, 43 | "dependencies": { 44 | "lodash.throttle": "^4.1.1" 45 | }, 46 | "devDependencies": { 47 | "ava": "^0.23.0", 48 | "babel-cli": "^6.24.1", 49 | "babel-plugin-transform-decorators-legacy": "^1.3.4", 50 | "babel-plugin-transform-es2015-modules-commonjs": "^6.24.1", 51 | "babel-preset-es2015": "^6.24.1", 52 | "delay": "^2.0.0", 53 | "xo": "^0.18.2" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # throttle-decorator 2 | 3 | > ES decorator that make a function throttled. 4 | 5 | [![Build Status](https://travis-ci.org/1000ch/throttle-decorator.svg?branch=master)](https://travis-ci.org/1000ch/throttle-decorator) 6 | [![Dependency Status](https://david-dm.org/1000ch/throttle-decorator.svg)](https://david-dm.org/1000ch/throttle-decorator) 7 | [![devDependency Status](https://david-dm.org/1000ch/throttle-decorator/dev-status.svg)](https://david-dm.org/1000ch/throttle-decorator?type=dev) 8 | 9 | ## Install 10 | 11 | ```bash 12 | $ npm install --save-dev throttle-decorator 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```javascript 18 | import throttle from 'throttle-decorator'; 19 | 20 | class Cat { 21 | @throttle(100) 22 | meow() { 23 | console.log('meow'); 24 | } 25 | } 26 | ``` 27 | 28 | ## License 29 | 30 | [MIT](https://1000ch.mit-license.org) © [Shogo Sensui](https://github.com/1000ch) 31 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import throttle from 'lodash.throttle'; 2 | 3 | export default function (wait, options = {}) { 4 | return function (target, name, descriptor) { 5 | return { 6 | configurable: true, 7 | enumerable: descriptor.enumerable, 8 | get: function () { 9 | Object.defineProperty(this, name, { 10 | configurable: true, 11 | enumerable: descriptor.enumerable, 12 | value: throttle(descriptor.value, wait, options) 13 | }); 14 | 15 | return this[name]; 16 | } 17 | }; 18 | }; 19 | } 20 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import delay from 'delay'; 3 | import throttle from '../lib'; 4 | 5 | class Test { 6 | constructor() { 7 | this.foo = null; 8 | this.bar = null; 9 | this.timerId = null; 10 | } 11 | 12 | setUp() { 13 | this.foo = 0; 14 | this.bar = 0; 15 | 16 | this.timerId = setInterval(() => { 17 | this.incrementFoo(); 18 | this.incrementBar(); 19 | }, 100); 20 | } 21 | 22 | tearDown() { 23 | if (this.timerId === null) { 24 | return; 25 | } 26 | 27 | clearInterval(this.timerId); 28 | } 29 | 30 | @throttle(300) 31 | incrementFoo() { 32 | this.foo++; 33 | } 34 | 35 | incrementBar() { 36 | this.bar++; 37 | } 38 | } 39 | 40 | test('test', async t => { 41 | const instance = new Test(); 42 | instance.setUp(); 43 | 44 | t.is(instance.foo, 0); 45 | t.is(instance.bar, 0); 46 | 47 | await delay(1000); 48 | 49 | t.is(instance.foo, 3); 50 | t.is(instance.bar, 9); 51 | 52 | instance.tearDown(); 53 | }); --------------------------------------------------------------------------------