dizmo/functions-buffered


dizmoFun: buffered invocations

https://www.npmjs.com/package/@dizmo/functions-buffered

License: ISC

Language: TypeScript

Keywords: buffered, dizmo, function, invocation, javascript, library, typescript


NPM version Build Status Coverage Status

@dizmo/functions-buffered

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.

Usage

Install

npm install @dizmo/functions-buffered --save

Require

let lib = require("@dizmo/functions-buffered");

Examples

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); });

Development

Build

npm run build

without linting:

npm run -- build --no-lint

Lint

npm run lint

with auto-fixing (for JavaScript and TypeScript):

npm run -- lint --fix

Test

npm run test

without (re-)building:

npm run -- test --no-build

Cover

npm run cover

without (re-)building:

npm run -- cover --no-build

Copyright

© 2018 dizmo AG, Switzerland

Project Statistics

Sourcerank 3
Repository Size 295 KB
Stars 0
Forks 0
Watchers 4
Open issues 0
Dependencies 0
Tags 12
Created
Last updated
Last pushed

Packages Referencing this Repo

@dizmo/functions-buffered
buffered invocations
Latest release 2.0.2 - Updated
@dizmo/buffered
buffered invocations
Latest release 1.1.20 - Published

Recent Tags See all

v1.1.5 July 03, 2018
v1.1.4 July 03, 2018
v1.1.3 July 03, 2018
v1.1.2 July 03, 2018
v1.1.1 July 03, 2018
v1.1.0 July 03, 2018
v1.0.6 July 03, 2018
v1.0.5 July 03, 2018
v1.0.4 July 03, 2018
v1.0.3 July 03, 2018
v1.0.2 July 03, 2018
v1.0.1 July 03, 2018

Something wrong with this page? Make a suggestion

Login to resync this repository