GithubHelp home page GithubHelp logo

izica / decorators-es6 Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 22 KB

Decorators for es6+, with promise support

JavaScript 100.00%
es6 es6-javascript javascript js decorator decorators decorator-es6 debounce memoize throttle throttle-function throttle-promises debounce-promises debounce-function memoize-decorator memoize-async

decorators-es6's Introduction

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

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

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

decorators-es6's People

Contributors

izica avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.