@appolo/helpers
v8.0.2
Published
useful decorators
Downloads
640
Readme
Appolo Decorators
useful decorators The cache will remove the oldest used item when reached max capacity
Installation:
npm install appolo-decorators --save
Delay
delay call method by given time in milliseconds
import { delay } from 'appolo-decorators';
class SomeClass {
@delay(1000)
method() {
// ...
}
}
Cache
cache method results using appolo-cache
options:
maxSize
- max cache size default1000
maxAge
- optional set maximum age in ms of all cache items default unlimitedclone
- clone the cache result defaultfalse
interval
- set cache refresh intervalresolver
- function to get the cache key by default the fist argument will be used as the cache keymulti
- boolean if no resolver defined use all the arguments as key else use the first argument as key defaultfalse
peek
- boolean usepeek
method instead of get defaultfalse
refresh
- boolean refresh cache on halfmaxAge
expire defaultfalse
import { cache } from 'appolo-decorators';
class SomeClass {
private counter = 0;
@cache()
method() {
return ++this.counter
}
}
let someClass = new SomeClass();
someClass.method() // 1
someClass.method()// 1
Bind
bind method to class instance
import { bind } from 'appolo-decorators';
class SomeClass {
@bind
method() {
// ...
}
}
document.body.addEventListener('click', new SomeClass().method);
Debounce
debounce method using lodash debounce
import { debounce } from 'appolo-decorators';
class SomeClass {
@debounce(1000,{trailing:true})
method() {
// ...
}
}
Throttle
throttle method using lodash debounce
import { throttle } from 'appolo-decorators';
class SomeClass {
@throttle(1000,{trailing:true})
method() {
// ...
}
}
Memoize
memoize method using lodash debounce
import { memoize } from 'appolo-decorators';
class SomeClass {
@memoize()
method() {
// ...
}
}
Once
method will be called max n times and return last call result
import { once } from 'appolo-decorators';
class SomeClass {
@once(2)
method() {
// ...
}
}
Interval
set interval to method once called
import { interval } from 'appolo-decorators';
class SomeClass {
@interval(100)
method() {
// ...
}
}
//start the interval
new SomeClass().method()
Mixins
add prototype to class
import { mixins } from 'appolo-decorators';
@mixins(EventDispacher)
class SomeClass {
method() {
// ...
}
}
new SomeClass().on("some event")
License
MIT