# tiny-polling

> A very tiny (~200B) solution for polling that is offline aware

Latest version **1.1.0** (published 2019-09-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install tiny-polling
pnpm add tiny-polling
yarn add tiny-polling
bun add tiny-polling
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2019-09-07 |
| First published | 2019-09-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Deric Cain |
| Maintainers | dericcain |
| Keywords | poll, polling, sockets, poller, interval, timeout |

## Links

- npm: https://www.npmjs.com/package/tiny-polling
- Repository: git@github.com:dericgw/tiny-polling
- npm.io page: https://npm.io/package/tiny-polling

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2019-09-07
- 1.0.2 — 2019-09-07
- 1.0.1 — 2019-09-07
- 1.0.0 — 2019-09-07

## README

# Tiny Polling

[![Coverage Status](https://coveralls.io/repos/github/dericgw/tiny-polling/badge.svg?branch=master)](https://coveralls.io/github/dericgw/tiny-polling?branch=master)

🎯A very tiny (~200B) solution for polling that is offline aware

## Features
- It's tiny, of course (~200B)
- Polls any function you pass to it at the specific interval you pass to it
- Online/Offline aware so it stops polling when offline and starts back when online
- Written in TypeScript so you get good autocompletion

## Installation
`npm i tiny-polling` or `yarn add tiny-polling`

## Example
Tiny Polling is a function that takes two parameters and returns two methods - that's it. Here it is:

```js
import { Polling } from 'tiny-polling';
import updateState from '../src/update-state'; // This could be any type of state updater like Redux, etc.

function updatePosts() {
  const posts = async () => await axios.get('/posts');
  updateState(posts);
}

// The first param is the function to poll
// The second param is the interval in seconds at which it will poll
const poll = Polling(updatePosts, 30);
poll.start(); // Starts the polling

function onExit() {
  poll.stop(); // Stop the polling
}

// Or, maybe you need to pass a callback to Polling because you have a function that takes arguments
function doSomething(params) {
  // Does something with the params
}

// Notice the second parameter is missing? The default interval is 10 seconds
const anotherPoll = Polling(() => {
  const params = { foo: false, bar: true };
  doSomething(params);
});
```

> NOTE: The callback will not be called as soon as `start()` is invoked. It will start after the
> initial interval has passed, so you may need to fire your method the first time.

## Params
| Name | Type | Required | Default | Description |
|---------------------|------------|----------|---------|---------------------------------------------------------------------------|
| `callback` | `function` | true |  | This is the callback or function that is called at the specific interval. |
| `intervalInSeconds` | `number` | false | 10 | This is the interval at which the `callback` will be called. |

## Contributing
If you find a bug, submit an issue with enough information to reproduce the bug. If you have a fix, please do not 
hesitate to submit a PR. If you feel that the API needs to be modified, open an issue so that we can discuss it first.

### Running the dev environment
1. Clone the repo - `git clone https://github.com/dericgw/tiny-polling.git` && `cd tiny-polling`
2. Install the dependencies - `yarn`
3. Run the dev server - `yarn start` (Use tests for verification)

### Testing
This package is only one JS file and it is tested pretty good. Make sure that none of the tests are breaking if changes
are made. Also, if you add new functionality and it *warrants* testing, please add tests. If you need help with this, I
will gladly help.

## Issues
If you find an issue, head over to the [Issues](https://github.com/dericgw/tiny-polling/issues) section and let me 
know about it. If you want to be super cool, you can submit a PR that fixes the issue.

## License (MIT)
[Check it out here.](./LICENSE.md)

---
_Source: https://npm.io/package/tiny-polling · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
