npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-deboucing-results

v0.1.7

Published

ts-deboucing-results is a TypeScript library that applies debouncing principles to ensure response stability and consistency. It waits for the result to remain unchanged for a specified period before returning, providing a reliable and consistent output f

Downloads

20

Readme

The ts-deboucing-results is a library that applies debouncing principles to ensure response stability and consistency.

It waits for the result to remain unchanged for a specified period before returning, then you can safely assume that the result is stable and consistent.

Use it to debounce your asynchronous functions that may change their results over time, such as consuming logs.

TL;DR

Why?

Sometimes we need to consume an external service that is still running, but every time we call it, its results may change.
To determine when the task is complete, you can utilize this library to efficiently call the asynchronous function multiple times until its response remains unchanged.
I am using this library to gather logs from a downstream service execution. I am unable to determine if the downstream has finished and if the logs are complete. Therefore, I debounce the response multiple times until the logs remain unchanged. At that point, I safely assume that the downstream has finished.

Features

deboucingResults
(config: DeboucingConfig, func: Function, customMatcher?: Function):
Promise<any[]>

DeboucingConfig

    const config: DeboucingConfig = {
        debounceLimit: 3,
        debounceIntervalMs: 2000,
        timeoutMs: 40000,
    }

debounceLimit: how many times the function should be called before returning.

debounceIntervalMs: how many milliseconds the function should wait before calling it again.

timeoutMs: how long the deboucing function should wait before timing out.

func: the function to be debounced. It should return a promise that resolves to the result of the function call.

customMatcher: an optional function that can be used to determine if the result is unchanged. Internally use _.isEqual from underscore to compare the results.

Install

npm i ts-deboucing-results

Usage

import { DeboucingConfig, deboucingResults } from 'ts-deboucing-results'

Testing Example

In this example, every time you call the input function, it executes a pop in the results list. It only returns when the result is the same 3 times (debounceLimit).

    it('should debounce correctly 3 times and return 5.', async () => {
        const results = [5, 5, 5, 1, 1]
        const inputFunction = () => results.pop()
        const config: DeboucingConfig = {
            debounceLimit: 3,
            debounceIntervalMs: 2000,
            timeoutMs: 40000,
        }
        const actual = await deboucingResults(config, inputFunction)
        expect(actual).toEqual(5)
    })

And if you change the debounceLimit to 2 the return should be 1:

    it('should debounce correctly 2 times and return 1.', async () => {
        const results = [5, 5, 5, 1, 1]
        const inputFunction = () => results.pop()
        const config: DeboucingConfig = {
            debounceLimit: 2,
            debounceIntervalMs: 2000,
            timeoutMs: 40000,
        }
        const actual = await deboucingResults(config, inputFunction)
        expect(actual).toEqual(1)
    })

Real scenarios

In this example, the axios.get will execute until its result remains the same for 3 consecutive times due to debounceLimit: 3. If this does not happen, it will return the last result obtained before the timeout.

        const apiCall = async () => axios.get(...)
        const config: DeboucingConfig = {
            debounceLimit: 3,
            debounceIntervalMs: 2000,
            timeoutMs: 40000,
        }
        const data = await deboucingResults(config, apiCall)