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

polling-connection

v0.0.2

Published

Event-based polling module written in TS with time tracking and aborting support.

Downloads

7

Readme

polling-connection

build downloads npm

Event-based polling module written in TS with time tracking and aborting support.

:white_check_mark: Agnostic (works with axios, fetch or any async function) :white_check_mark: Written in TypeScript :white_check_mark: Time tracking :white_check_mark: Abort signal support

installation

NPM:

npm install polling-connection

Yarn:

yarn add polling-connection

examples

usage

Create a new connection:

import { polling } from "polling-connection";

const connection = polling({
  task: async ({ done, signal }) => {
    const response = await axios.get("user/status", { signal });
    if (response.data.status === 1) {
      done(response.data);
    }
  },
  timeout: 30000,
  delay: 3000,
});

Listen to the events:

connection.on("success", (data) => {
  console.log(data);
  console.log("The connection is automatically closed on success");
});

connection.on("timeout", () => {
  console.log("Connection is automatically closed on timeout");
});

connnection.on("second", ({ passed, remaining }) => {
  console.log("seconds passed:", passed);
  console.log("seconds remamining:", remaining);
});

connection.on("error", (err) => {
  console.error(err);
  console.log("The connection is still active");
});

connection.on("close", () => {
  console.log("The connection is closed");
});

Start the polling:

connection.start();

Manually close the connection:

connection.close();

Clear all event listeners:

connection.removeAllEventListeners();

Close connection and remove all event listeners:

connection.destroy();

api

polling(options: PollingOptions) => PollingConnection

Creates a new polling connection.

Available options:

| Option | Description | Default | | ------- | -------------------------------------------------------------------- | ------------------ | | task | An async function that will be executed while the connection is open | Required | | delay | Delay in milliseconds after each call | 3000 (3 seconds) | | timeout | Time in milliseconds for timeout | 30000 (30 seconds) |

Example:

const connection = polling<ResultType>({
  task: async ({ done, signal }) => {
    const result = await someAsyncTask({ signal });
    if (result === "ok") {
      done(result);
    }
  },
});

The connection instance provides the following methods:

start() => void

Starts the connection and executes the task until it reaches the timeout or the connection is closed (either by success or manually closed)

Before calling this method you must add the event listeners.

Example:

connection.start();

on(event: PollingEvent, data) => Unsubscribe

Adds a new event listener.

Supported events:

| Event | Description | Arguments | | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------- | | start | When the connection is started | {passed: number; remaining: number} | | success | When the done callback is called. The connection is also automatically closed, which means that the close event will also be called after success | The payload given to done callback | | error | When the task throws an exception. This does not closes the connection. | err: unknown | | timeout | When the polling reaches the timeout. This closes the event, which means the close event are also triggered after this event. | None | | second | When a second passes while the polling is active. The arguments are the passed and remaining time in seconds. | {passed: number; remaining: number} | | close | When the connection is closed either my manualling calling the close or destroy events, or when the polling reaches the timeout | None |

Example:

connection.on("start", () => {
  console.log("The connection is started");
});

connection.on("success", (data) => {
  console.log("done called with:", data);
});

connection.on("second", ({ passed, remaining }) => {
  console.log("seconds passed:", passed);
  console.log("seconds remaining:", remaining);
});

connection.on("error", (err) => {
  console.log("thrown exception:", err);
  console.log("...but the polling is still active");

  // Depending on the error you can close the connection
  if (isBadError(err)) {
    connection.close();
  }
});

connection.on("timeout", () => {
  console.log("polling reached the timeout");
  console.log("the polling will be closed and trigger the `close` event");
});

console.log("close", () => {
  console.log("the polling is closed");
});

This method also return the unsubscribe function:

const unsubscribe = connection("success", handleSuccess);

// ...

unsubscribe(); // remove the success listener

close() => void

Closes the connection.

removeAllEventListeners() => void

Removes all the event listeners.

destroy() => void

Closes the connection and remove all the event listeners.

license

MIT