1.0.2 • Published 3 years ago

pollinglib v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Pollinglib

What is this?

Polling.js is a super lightweight library for lazy people to poll any API gateway. Supports retries, pausing, reesuming, stoping and callback to retrive the responses. It has a nice a natural API and Works in Node and browsers.

Installation

npm i poolinglib --save or yarn add poolinglib

Then...

import Polling from 'pollinglib';

Polling.createInstance("TestInstance", {
    requestCountInParallel: 2,
    pollingIntervalInSeconds: 3,
    taskKey: "id",
    retryOnFailure: 3,
    apiCallback: (err, response) => {
      console.log(response);
    },
    restCallApi: (task) => {
        return {
          url: "https://gorest.co.in/public/v1/users",
          method: "get",
          payload: task
        }
      },
  });

  Polling.getInstance("TestInstance").addToTaskList({id: 123});

  Polling.getInstance("TestInstance").start();

Options

PropertiesdefaultDataTypeDescription
requestCountInParallel1NumberHow many request to place at same time from task list.
pollingIntervalInSeconds3NumberTime interval for polling in seconds
taskKey_keyStringEvery task must have key. This will be used to identify the task during the API request.
retryOnFailure0NumberRetry the api in case of failure error codes 500,502,503,504 default no retry.
apiCallback-functionCallback to return the response
restCallApi-functionIt is a mandatory and it should return api request, e.g, {url: 'http://...', method: 'get/post', payload: {}}

Methods

MethodParamsDescription
addToTaskListparam1: object/array param2: booleanAdd task to the list. either object or array of list. if you want clear the list and recreate pass second parameter as true. e.g, instance.addToTaskList({_key: 123})
removeFromTaskListremove the task from list
startStart the interval
stopStop the interval
createInstancetaskName, optionsInitialize the instance
getInstancetaskNameTo access the instance anywhere in the application
deleteInstancetaskNameIt will stop the iteration and remove the object instance
deleteAllInstanceThis helps to clear all the instance craeted. Can use in logout.

Note: This is the initial iteration there may be bug. Please contribute if found anything. Thanks