@aegenet/belt-anti-bounce
v2.3.0
Published
Anti-bounce, debounce
Downloads
91
Readme
@aegenet/belt-anti-bounce
@antiBounce
a debounce decorator for TypeScript classes to prevent multiple calls in a short period of time.
Note: Stage 3
decorator (for stage 2, you can use an older version (< 2.0.0) of this package).
💾 Installation
yarn add @aegenet/belt-anti-bounce@^2.0.0
# or
npm i @aegenet/belt-anti-bounce@^2.0.0
📝 Usage
import { IAntiBounceSupport, IAntiBounce, disposeAntiBounces } from '@aegenet/belt-anti-bounce';
class Sample implements IAntiBounceSupport {
public declare $antiBounces?: Map<string, IAntiBounce>;
private _i = 0;
@antiBounce({ duration: 300 })
public inc(): void {
this._i++;
}
public main() {
this.inc();
this.inc();
this.inc();
// this._i => 1
}
public dispose() {
disposeAntiBounces(this);
}
}
AntiBounce, utility
import { AntiBounce } from '@aegenet/belt-anti-bounce';
let i = 0;
const diposable = new AntiBounce(() => i++, 300);
assert.strictEqual(i, 0);
// Go !
diposable.call();
diposable.call();
diposable.call();
diposable.call();
// i = 0
await delay(300);
// i = 1
diposable.dispose();