0.3.2 ā€¢ Published 2 years ago

pollinator v0.3.2

Weekly downloads
8
License
MIT
Repository
github
Last release
2 years ago

npm bundle size (version) GitHub GitHub Repo stars npm npm type definitions

Pollinator


šŸ Pollinator is a super lightweight library for lazy people to poll any function (API gateway anyone?). Supports retries, pausing, cancelling, emits events you can subscribe to. It has a nice, natural API and works in Node and browsers.

Main features

  • simple API šŸ”Ø
  • start, pause and stop at any time šŸŽ®
  • add event listeners to receive polling results and polling status changes šŸŽ­
  • written in TypeScript šŸ‘·ā€ā™‚ļø
  • cancel polling when certain conditions are met based on current or previous results šŸ”š
  • supports sync and async functions šŸŽ›
  • configurable delay between polling ā²
  • configurable retry attempts in case of error šŸ”„
  • well tested, no unnecessary calls ā›°
  • no bs - does only one thing and gets the job done šŸ‘šŸ»

Installation

For node just do your:

yarn add pollinator
#or
npm install pollinator

For the browsers add the CDN link:

<!-- either use unpkg.com -->
<script src="https://unpkg.com/pollinator@0.3.2/dist/index.umd.min.js"></script>

<!-- or use JSDelivr -->
<script src="https://cdn.jsdelivr.net/npm/pollinator@0.3.2/dist/index.umd.min.js"></script>

Usage

Here's a minimal example

// es6
import Pollinator from 'pollinator'

function pollingFunction() {
  return fetch('http://some-url.com/endpoint')
}

// configure your poller
const poller = new Pollinator(pollingFunction)
// remember to start
poller.start()

This will poll pollingFunction indefinitely.

To get the return from the polling function you need subscribe to an event.

poller.on(Pollinator.Event.POLL, handlePoll)

function handlePoll(response, status) {
  // do something with the response and current polling status
  console.log('The response is:', response)
  console.log('Current polling status is:', status)
}

Say you want to stop polling when a specific response comes back from the polling function.

const poller = new Pollinator(pollingFunction, {
  conditionFn: stopPollingOnCondition,
})

function stopPollingOnCondition(currentResponse, previousResponse) {
  // return true if you wish to stop polling
  if (currentResponse === 'Half a Bee' && previousResponse === 'Eric')
    return true

  // return false if you don't want to stop and keep on polling
  return false
}

Reference

Pollinator constructor params

NameTypeRequiredDescriptionDefault
pollFnfunctionrequiredA function you want to poll. Can be sync or async.
configPollinatorConfigoptionalOptional configuration. Check details belowsee PollinatorConfig type below

PollinatorConfig type

NameTypeRequiredDescriptionDefault
polFnParamsunknownoptionalUse this option to pass some parameters to the pollFnundefined
conditionFnfunctionoptionalUse this function to stop polling. The function will be called with two parameters of current and previous response from the pollFn. Must return a boolean where true stops polling.(current: unkown, previous: unkown) => bool() => false
delaynumberoptionalA value in milliseconds setting the timeout between consequent pollFn calls5000
failRetryCountnumberoptionalA value that indicates the number of attempts to call pollFn after catching an error. Zero means that polling will fail immediately after catching error. Any positive number means that Pollinator will try to poll that many times until it emits the Event.ERROR.3

Status enum

{
  IDLE, POLLING, FINISHED
}

Event enum

enum Event {
  POLL = 'poll',
  END = 'end',
  ERROR = 'error',
  STATUS_CHANGE = 'statusChange',
}

Pollinator instance methods and properties

NameTypeParamsDescription
startmethodStarts polling and changes status to Status.POLLING
stopmethodStops polling and changes status to Status.FINISHED
pausemethodPauses polling and status changes to Status.IDLE
onmethod(event: Event, listener: function) => voidRegisters an event listener for a given Event type.
offmethod(event: Event, listener: function) => voidRemoves an event listener for a given Event type.
statuspropertyGet current status of your poller. Value is of type Status

Building and contributing

If you want to contribute then please do. PRs are welcome. Before squashing any bug or adding a new feature please create an issue first. Also add a test case(s) before contributing any code.

After cloning the repo do the usual:

yarn
#or
npm i

To run tests use this script:

yarn test

To build run:

yarn build

To do

  • max attempts - stop polling when a max number of attempts is achieved
  • current attempts number in the event data
  • timeout - configure max amount of time after which polling stops
  • total polling time in the event data

Spread the pollen šŸŒ»

If you like this lib consider hitting that star ā­ļø button.

You can hit me up here

Twitter Follow