Consider using axios-observable instead of axios for managing subscriptions. Using subscriptions over promises allows for easier unsubscription, which is a big advantage. To implement this, you can place your log sending request subscription within a BehaviorSubject (import { BehaviorSubject } from 'rxjs';) and schedule the call to the behavior subject on a timer. Here's an example:
export default class PollValue<T> {
date: Date;
value: T;
constructor(value: T) {
this.date = new Date();
this.value = value;
}
// Ensures a minimum amount of time has elapsed
getTimeout(minTimeMS: number) {
const timeElapsed = new Date().getTime() - this.date.getTime();
return timeElapsed < minTimeMS ? minTimeMS - timeElapsed : 0;
}
}
// using true or false to indicate whether we sent logs the last time or not
const sendLogs = new BehaviorSubject(new PollValue<boolean>(false));
sendLogs.subscribe(lastValue => {
setTimeout(() => {
if (thereAreLogsToSend) {
// sendLogs is a function containing your axios-observable instance and request
this.yourLogService.sendLogs(<pass logs here>).subscribe(result => {
sendLogs.next(new PollValue(true));
});
} else {
sendLogs.next(new PollValue(false));
}
}, lastValue.getTimeout(900000)); // ensure at least 15 mins have elapsed before sending again
if (someStopSendingCondition) {
sendLogs.unsubscribe();
}
});
If you're looking to send your request as a stream, you may find examples here helpful. I don't have much experience working with streams in REST architecture, so I may not be the best resource for that specific topic.
I hope this information proves useful, even though my response may be delayed...