1.0.2 • Published 3 years ago
pollinglib v1.0.2
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
Properties | default | DataType | Description |
---|---|---|---|
requestCountInParallel | 1 | Number | How many request to place at same time from task list. |
pollingIntervalInSeconds | 3 | Number | Time interval for polling in seconds |
taskKey | _key | String | Every task must have key. This will be used to identify the task during the API request. |
retryOnFailure | 0 | Number | Retry the api in case of failure error codes 500,502,503,504 default no retry. |
apiCallback | - | function | Callback to return the response |
restCallApi | - | function | It is a mandatory and it should return api request, e.g, {url: 'http://...', method: 'get/post', payload: {}} |
Methods
Method | Params | Description |
---|---|---|
addToTaskList | param1: object/array param2: boolean | Add 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}) |
removeFromTaskList | remove the task from list | |
start | Start the interval | |
stop | Stop the interval | |
createInstance | taskName, options | Initialize the instance |
getInstance | taskName | To access the instance anywhere in the application |
deleteInstance | taskName | It will stop the iteration and remove the object instance |
deleteAllInstance | This 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