Skip to content

React Context based npm package for batching function calls, specifically developed for batching API-calls and autosave functionality..

License

Notifications You must be signed in to change notification settings

anthonwellsjo/react-batch-tracker

Repository files navigation

react-batch-tracker

this is a react context based npm package for batching calls to a function. it supports creating several trackers that can track state change by following individual data objects. the goal with the lib is to facilitate autosave and undo functionality, and for optimising client -> API requests with callbacks containing a batched data set.

getting started

installation

npm install
yarn install

usage

first wrap your app with the provider

Screenshot 2022-11-20 at 06 36 23

then get access to the api in a component by using the useBatchTracker function

Screenshot 2022-11-20 at 06 39 09

create a batch tracker

before anything doing else, you need to create a new batch tracker. you need to give it a name, a timeout (in ms) and a callback function. Screenshot 2022-11-20 at 09 21 29

since this lib is based on react context, you will be able to get access to all the initiated trackers globally in your app.

it makes sense to create a new tracker for each component or group of component whose state has some common goal. they might have a common state that needs to be CRUD:ed with a database with autosave functionality. or maybe it makes sense from a UX perspective to keep track of their combined history of states to implement something like an undo/redo functionality.

register actions

whenever an event occurs that should trigger the countdown for the callback function, you need it to register an action. it makes sense to have this happen on an onChange type of event, as shown below.

Screenshot 2022-11-20 at 07 48 08

specific use cases

auto save

Screenshot 2022-11-20 at 09 44 18

the code above will produce a behaviour as shown in the gif below autosave

auto save with undo functionality

tbc

About

React Context based npm package for batching function calls, specifically developed for batching API-calls and autosave functionality..

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published