Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/juliendargelos/duration-estimator
Agnostic duration estimator for asynchronous tasks
https://github.com/juliendargelos/duration-estimator
Last synced: 2 months ago
JSON representation
Agnostic duration estimator for asynchronous tasks
- Host: GitHub
- URL: https://github.com/juliendargelos/duration-estimator
- Owner: juliendargelos
- License: mit
- Created: 2020-08-17T18:08:34.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-04-30T10:29:19.000Z (9 months ago)
- Last Synced: 2024-10-03T19:46:10.454Z (3 months ago)
- Language: TypeScript
- Size: 341 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# duration-estimator
[![test](https://github.com/juliendargelos/duration-estimator/workflows/test/badge.svg?branch=master)](https://github.com/juliendargelos/duration-estimator/actions?workflow=test)
[![build](https://github.com/juliendargelos/duration-estimator/workflows/build/badge.svg?branch=master)](https://github.com/juliendargelos/duration-estimator/actions?workflow=build)
[![version](https://img.shields.io/github/package-json/v/juliendargelos/duration-estimator)](https://github.com/juliendargelos/duration-estimator)Agnostic duration estimator for asynchronous tasks. Estimates remaining time using a moving average whose length is configurable.
### Install
Using npm:
```bash
npm install duration-estimator --save
```Using umd bundle from cdn:
```html
```
Using esm bundle from cdn:
```html
import { DurationEstimator } from 'https://cdn.skypack.dev/duration-estimator'
```
### Usage
```typescript
import { DurationEstimator } from 'duration-estimator'// Asynchronous task yielding progress in range 0-1
function task(onProgress) {
let n = 0
const steps = 10onProgress(0)
const interval = setInterval(() => {
onProgress(++n / steps)
n === steps && clearInterval(interval)
}, 500)
}const estimator = new DurationEstimator()
task(progress => console.log(`${estimator.estimate(progress)}ms remaining`))
// Infinityms remaining
// 4536ms remaining
// 4027.9960278053622ms remaining
// 3520.9907223204477ms remaining
// 3014.985074579578ms remaining
// 2512.9896480589387ms remaining
// 2010.6595867972594ms remaining
// 1507.709410925819ms remaining
// 1005.2470758800504ms remaining
// 502.44262539511163ms remaining
// 0ms remaining
```The estimator can be reused after calling `reset()`:
```typescript
estimator.reset() // ready to make estimations for a new task
```By default, the estimator computes remaining time based on the last 10 samples it got. You can change that by setting the moving average length:
```typescript
new DurationEstimator(20) // Takes the last 20 samples in account
new DurationEstimator(Infinity) // Takes all samples in account
```