@technogise/rn-background-queue-processor
v2.0.0
Published
Library which creates a background queue processor for react native apps.
Downloads
5
Readme
React Native Queue Processor
A package with the help of which users will be able to add and process different kind of jobs in a queues with background persistent queue processing
Prerequisite
This package does not have any prerequisites that need to be installed.
Features
- A React Native Queue Processor with persistent queues using rn-background-queue-processor-realm-adapter
- Jobs will be executed according to priorities using the queues.
- Job will retry until the failed count matches job's number of retry attempts with retryInterval.
- Has functionality for Worker instances as well.
- Background Persistent queue processing using WorkManager.
- It also plays well with Workers so your jobs can be thrown on the queue, then processed in dedicated worker threads for improved processing performance.
- Failed Jobs can also be processed.
Compatibility
While we ensure to keep the library updated with newer and improved upcoming releases for React Native, our current version works well for React Native: v0.62.1
Installation steps
npm i @technogise/rn-background-queue-processor
Usage
Here is a Sample Application
constructor(dbAdapter) {
this.adapter = dbAdapter;
}
The dbAdapter here is an additional feature with which we enable our developers to use this queue with any db of their choice.
enqueue(job)
: Enqueues the job in the queuedequeue()
: Dequeues the job from queue on successful executionpeek()
: To get the top job from QueuegetSize()
: Returns the length of QueuegetItems()
: Gives all items of QueuefailedJobsEnqueue()
Enqueue all the Failed Jobs for processingIt is an abstract class which needs to be implemented by users of this package for easy use of any database for storing and processing the queues.
For the easier understanding and reference, we have added an
InMemoryAdapter.js
. Here is a list of all functions available in the Adapter class which can be used,getAllItems()
: To get all jobs in current queueremove()
: Method to remove a job from current queueaddItem(item)
: Method to add a job in the current queuegetLength()
: Method to get length of current queuegetTopItem()
: Method to get the current job for processingaddFailedItems()
Method to add failed items
Job While Job class has methods to get id, name and param of current job, we have made the
execute
,jobSuccess
andjobFail
function to be abstract for the developer so that he/she decides what to do on execute call.The
execute
method can be used for performing the action in current job. For example, if the current job is an API call then developer can add the steps to fetch data from API in thisexecute
,method.The
jobSuccess
method tells the developer defined action that needs to be taken if the API response is 200.The
jobFail
method tells the developer defined action that needs to be taken if the API response is 4XX.Worker class is a singleton class. In this class, user can process queue using
process()
Communication with native
- Process function in worker class schedule a job(STARTPROCESSINGJOB) which calls native module
- Native module emits events which are listened on the Js side, based on the event emitted queueProcessor will start processing.
Made with :heart: from Technogise
You can explore our other works on GitHub