This is a fork of developit's decko that renames the incorrectly named "debounce" to "throttle", and adds an actual "debounce".
A concise implementation of the three four most useful decorators:
@bind
: make the value ofthis
constant within a method@throttle
: throttle calls to a method@debounce
: debounce calls to a method@memoize
: cache return values based on arguments
Decorators help simplify code by replacing the noise of common patterns with declarative annotations.
Conversely, decorators can also be overused and create obscurity.
Decko establishes 3 4 standard decorators that are immediately recognizable, so you can avoid creating decorators in your own codebase.
💡 Tip: decko is particularly well-suited to Preact Classful Components.
💫 Note:
- For Babel 6+, be sure to install babel-plugin-transform-decorators-legacy.
- For Typescript, be sure to enable
{"experimentalDecorators": true}
in your tsconfig.json.
Available on npm:
npm i -S @agentepsilon/decko
Each decorator method is available as a named import.
import { bind, memoize, throttle, debounce } from '@agentepsilon/decko';
class Example {
@bind
foo() {
// the value of `this` is always the object from which foo() was referenced.
return this;
}
}
let e = new Example();
assert.equal(e.foo.call(null), e);
Cache values returned from the decorated function. Uses the first argument as a cache key. Cache keys are always converted to strings.
caseSensitive: false
- Makes cache keys case-insensitive
cache: {}
- Presupply cache storage, for seeding or sharing entries
class Example {
@memoize
expensive(key) {
let start = Date.now();
while (Date.now()-start < 500) key++;
return key;
}
}
let e = new Example();
// this takes 500ms
let one = e.expensive(1);
// this takes 0ms
let two = e.expensive(1);
// this takes 500ms
let three = e.expensive(2);
Throttle calls to the decorated function. To throttle means "call this at most once per N ms". All outward function calls get collated into a single inward call, and only the latest (most recent) arguments as passed on to the throttled function.
delay: 0
- The number of milliseconds to buffer calls for.
class Example {
@throttle
foo() {
return this;
}
}
let e = new Example();
// this will only call foo() once:
for (let i=1000; i--) e.foo();
Debounce calls to the decorated function. To debounce means "call this only after it has not been called for N ms". All outward function calls clear any currently queued calls, then schedule a new call, effectively delaying the call further. As in
@throttle
, only the most recent arguments are passed on.
delay: 0
- The number of milliseconds to wait for repeat calls for.
class Example {
@debounce
foo() {
return this;
}
}
``
---
License
-------
MIT