@janiscommerce/app-tracking-time
v2.3.0
Published
A package for managing the times of actions in applications
Downloads
51
Maintainers
Readme
@janiscommerce/app-tracking-time

This package manages events in your application by interacting with a database. It provides functionalities to add, retrieve, and validate events. Also handles application state changes to pause ongoing events when the app goes into the background.
Dependencies
To ensure the proper functioning of this library, make sure that the following dependencies are installed in your project:
Required Peer Dependencies
date-fns:>=2.0.0 <3.0.0react:>=18.2.0react-native:>=0.71.5realm:^11.0.0
These dependencies are required for the library to work correctly. Ensure that your project has these versions installed to avoid compatibility issues.
Installation
npm i @janiscommerce/app-tracking-timeHow to use?
First, creates a new instance of the EventTracker class and declare the name for the database:
import EventTrackerClass from '@janiscommerce/app-tracking-time';
const EventTracker = new EventTrackerClass('events_database')
export default EventTracker;
Save your first record:
To save an event you must call the addEvent method of the EventTracker class and pass it an object with the following parameters:
const saveInitEvent = async (id) => {
try {
await EventTracker.addEvent({id,type:'start'})
} catch(error) {
console.warn(error);
}
}
<Button title='save new event' onPress={() => saveInitEvent('66e99577e128deb19d57cd74')}/>This action will save a start type event associated with the id 66e99577e128deb19d57cd74
Search all events associated with your id:
To get all the records you saved related to an id, you can call the getEventsById function:
const fetchEventsById = async (id) => {
try {
const [events, fetchError] = await promiseWrapper(EventTracker.getEventsById(id))
console.log(events);
} catch(error) {
console.warn(error);
}
}
<Button title='fetch events' onPress={() => fetchEventsById('66e99577e128deb19d57cd74')}/>Delete all events of an id:
You can delete all records of an id using the deleteEventsById.
This method will be responsible for eliminating all the events that have been registered that have the id that you pass as an argument
const deleteEvents = async (id) => {
try {
await EventTracker.deleteEventsById(id)
} catch(error) {
console.warn(error);
}
}
<Button title='remove events' onPress={() => deleteEvents('66e99577e128deb19d57cd74')}/>The cleanup method will be called once the component is unmounted and will unlisten for state changes from the app. If any of the events you want to pause is already paused, what will happen is that the console will throw a warning with the error reported by the package.
Sequence of recorded events
The package has internal validations that prevent events from being saved consecutively or that do not have coherence depending on the event that has been saved prior to this one. For example, you will not be able to store 2 pause type events consecutively. Additionally, saving any event related to an ID that has already been finished is also not allowed.
If you want to know what was the last event that was stored for a particular id, you can call the getLastEventById method, which will return an object with the information of the last stored event, including the type.
