typescript-debounce-decorator

0.0.18 • Public • Published

typescript-debounce-decorator

NPM version NPM downloads Build status

A debounce decorator for typescript class method

  • Tiny (1KB after uglify compressed)
  • No dependency
  • Easy to use

Install

npm install typescript-debounce-decorator --save

Usage

Syntax:

@debounce(debounceTime, options)

Params:

  • debounceTime: number Function execute interval in milliseconds.
  • options: object Options.
    • leading: boolean Should function invoke on the leading or trailing of the wait timeout.

NOTE: Return value of function which applied debounce decorator will be eaten.

Basic usage:

import { debounce } from "typescript-debounce-decorator";
 
class Foo {
    @debounce
    bar() {
        console.log("foobar");
    }
}

With debounce time:

import { debounce } from "typescript-debounce-decorator";
 
class Foo {
    @debounce(1000)
    bar() {
        console.log("foobar");
    }
}

With options:

import { debounce } from "typescript-debounce-decorator";
 
class Foo {
    @debounce(1000, { leading: true })
    bar() {
        console.log("foobar");
    }
}

Cancel:

import { debounce, cancel } from "typescript-debounce-decorator";
 
class Foo {
    @debounce(1000, { leading: true })
    bar() {
        console.log("foobar");
    }
 
    cancel() {
        cancel(this.bar);
    }
}

Changelog

  • 0.0.18: [BREAKCHANGE] leading option now default to false

License

MIT

Package Sidebar

Install

npm i typescript-debounce-decorator

Weekly Downloads

1,501

Version

0.0.18

License

MIT

Unpacked Size

15 kB

Total Files

7

Last publish

Collaborators

  • 297951292