https://github.com/izica/decorators-es6
Decorators for es6+, with promise support
https://github.com/izica/decorators-es6
debounce debounce-function debounce-promises decorator decorator-es6 decorators es6 es6-javascript javascript js memoize memoize-async memoize-decorator throttle throttle-function throttle-promises
Last synced: 7 months ago
JSON representation
Decorators for es6+, with promise support
- Host: GitHub
- URL: https://github.com/izica/decorators-es6
- Owner: izica
- Created: 2019-09-21T16:12:18.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-10-25T11:23:18.000Z (over 6 years ago)
- Last Synced: 2024-10-30T07:14:20.382Z (over 1 year ago)
- Topics: debounce, debounce-function, debounce-promises, decorator, decorator-es6, decorators, es6, es6-javascript, javascript, js, memoize, memoize-async, memoize-decorator, throttle, throttle-function, throttle-promises
- Language: JavaScript
- Homepage:
- Size: 21.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# decorators-es6
Decorators for es6+, with promise support(Async support)
### Install
```
npm install decorators-es6
```
```
yarn add decorators-es6
```
### Decorators
* debounce
* memoize
* cache - alias for memoize
* throttle
### Features
* Easy to use as decorator in es6
* Promise support
* Decorated function return value in promise chain
* Async(work with async as promise)
### Warning
For use this package your app need to support these features(Babel or Polyfills):
* Promise
* arrow functions () => {}
* import, export
### Usage
#### debounce(time)
Default debounce time = 500ms
```javascript
import { debounce } from 'decorators-es6';
class Store {
message = 'msg';
@debounce(1000)
changeMessage = (message) => {
this.message = message;
return 'updated';
};
@debounce(1000)
updateOnServer = (message) => {
return axios.post('/message/update', {message});
}
}
const store = new Store();
store.changeMessage('msg2');
store.changeMessage('msg2').then(value => {
//debounce firing;
console.log(value) // 'updated'
});
store.updateOnServer('msg2').then(response => {
//debounce firing;
console.log(response) // axios response from server(Promise resolve)
});
```
#### memoize(time) or cache(time)
Default memoize time = 9999999ms
```javascript
import { memoize} from 'decorators-es6';
class Service {
@memoize()
calc = (number) => {
return number * 5;
};
// memoize product info for 100 secs
@memoize(100000)
getProductFromServer = (productId) => {
return axios.post('/product', {productId});
}
}
const service = new Service();
service.calc(10).then(result => {
console.log(result);
})
service.getProductFromServer().then(response => {
console.log(response); // response from axios for example
store.product = response.data.product;
})
```
#### throtle(time)
Default debounce time = 500ms
Example: see debounce example