Returns a buffered and cancelable version for the provided function. The buffered function does not execute before the specified delay passes upon which it executes exactly once, no matter have many times it gets invoked in between.
The cancellation of a particular invocation is only possible while the specified delay has not passed yet. Further, upon the invocation of the buffered function a promise is returned.
npm install @dizmo/functions-buffered --save
const { buffered } = require("@dizmo/functions-buffered");
import { buffered } from "@dizmo/functions-buffered";
let fn = buffered((t: Date) => {
return new Date().getTime() - t.getTime();
}, 200);
fn(new Date()).then((res: number) => {
console.debug(res);
}).catch((err: Error) => {
console.error(err);
});
let fn = buffered(() => {
throw new Error("won't be thrown");
}, 600);
fn().then((res: any) => { // won't execute!
console.debug(res);
}).catch((err: Error) => {
console.error(err);
});
fn.cancel();
class Class {
@buffered.decorator(100)
public async f1(t: Date): Promise<number> {
return new Date().getTime() - t.getTime();
}
@buffered.decorator // 200ms default
public async f2(t: Date) {
return new Date().getTime() - t.getTime();
}
}
const p1: Promise<number>
= new Class().f1(new Date());
const p2
= new Class().f2(new Date());
p1.then((res: number) => { console.debug(res); });
p2.then((res: number) => { console.debug(res); });
npm run clean
npm run build
npm run -- build --no-lint --no-clean
npm run -- build --prepack
npm run -- build --prepack --no-minify
npm run lint
npm run -- lint --fix
npm run test
npm run -- test --no-lint --no-clean --no-build
npm run cover
npm run -- cover --no-lint --no-clean --no-build
npm run docs
npm publish
npm publish --access=public
© 2020 dizmo AG, Switzerland