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 --saveconst { 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 cleannpm run buildnpm run -- build --no-lint --no-cleannpm run -- build --prepacknpm run -- build --prepack --no-minifynpm run lintnpm run -- lint --fixnpm run testnpm run -- test --no-lint --no-clean --no-buildnpm run covernpm run -- cover --no-lint --no-clean --no-buildnpm run docsnpm publishnpm publish --access=public© 2020 dizmo AG, Switzerland