Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dizmo/functions-buffered
dizmoFun: buffered invocations
https://github.com/dizmo/functions-buffered
buffered dizmo function invocation javascript library typescript
Last synced: about 15 hours ago
JSON representation
dizmoFun: buffered invocations
- Host: GitHub
- URL: https://github.com/dizmo/functions-buffered
- Owner: dizmo
- License: isc
- Created: 2018-07-18T17:08:09.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-10-18T13:42:24.000Z (about 1 year ago)
- Last Synced: 2024-11-07T22:07:17.593Z (8 days ago)
- Topics: buffered, dizmo, function, invocation, javascript, library, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@dizmo/functions-buffered
- Size: 1.24 MB
- Stars: 0
- Watchers: 5
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![NPM version](https://badge.fury.io/js/%40dizmo%2Ffunctions-buffered.svg)](https://npmjs.org/package/@dizmo/functions-buffered)
[![Build Status](https://travis-ci.com/dizmo/functions-buffered.svg?branch=master)](https://travis-ci.com/dizmo/functions-buffered)
[![Coverage Status](https://coveralls.io/repos/github/dizmo/functions-buffered/badge.svg?branch=master)](https://coveralls.io/github/dizmo/functions-buffered?branch=master)# @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
```sh
npm install @dizmo/functions-buffered --save
```### Require
```javascript
const { buffered } = require("@dizmo/functions-buffered");
```### Examples
```typescript
import { buffered } from "@dizmo/functions-buffered";
``````typescript
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);
});
``````typescript
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();
``````typescript
class Class {
@buffered.decorator(100)
public async f1(t: Date): Promise {
return new Date().getTime() - t.getTime();
}
@buffered.decorator // 200ms default
public async f2(t: Date) {
return new Date().getTime() - t.getTime();
}
}const p1: Promise
= 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
### Clean
```sh
npm run clean
```### Build
```sh
npm run build
```#### without linting and cleaning:
```sh
npm run -- build --no-lint --no-clean
```#### with UMD bundling (incl. minimization):
```sh
npm run -- build --prepack
```#### with UMD bundling (excl. minimization):
```sh
npm run -- build --prepack --no-minify
```### Lint
```sh
npm run lint
```#### with auto-fixing:
```sh
npm run -- lint --fix
```### Test
```sh
npm run test
```#### without linting, cleaning and (re-)building:
```sh
npm run -- test --no-lint --no-clean --no-build
```### Cover
```sh
npm run cover
```#### without linting, cleaning and (re-)building:
```sh
npm run -- cover --no-lint --no-clean --no-build
```## Documentation
```sh
npm run docs
```## Publish
```sh
npm publish
```#### initially (if public):
```sh
npm publish --access=public
```## Copyright
© 2020 [dizmo AG](http://dizmo.com/), Switzerland