Answer the question
In order to leave comments, you need to log in
Where and how to handle non-ui logic in spa?
stack
export class Tracker {
constructor({trackEvents, dropActivityTimeMs}) {
this.dropActivityTimeMs = dropActivityTimeMs;
this.trackEvents = trackEvents;
this.wasActivity = false;
this.dropActivityIntervalId = null;
}
restartDropActivity() {
clearInterval(this.dropActivityIntervalId);
console.log('Обнуление активности перезапущено');
this.dropActivityIntervalId = setInterval(() => {
this.wasActivity = false;
}, this.dropActivityTimeMs);
}
init() {
this.trackEvents.forEach(event => {
document.addEventListener(event, () => {
console.log(`Было событие ${event}`);
this.wasActivity = true;
this.restartDropActivity();
});
})
}
}
//в корне приложения вызываю
const events = ['mousemove'];
const tracker = new Tracker({
trackEvents: events,
dropActivityTimeMs: 5000,
});
const prolongateAfterMs = 3000;
tracker.init();
setInterval(() => {
console.log(`активность в последние ${tracker.dropActivityTimeMs}ms`, tracker.wasActivity);
if (tracker.wasActivity) {
api.updateToken();
}
}, prolongateAfterMs);
Answer the question
In order to leave comments, you need to log in
I can't answer more on the phone, and I don't quite see the whole situation, but why not assign the time of the token when the page is closed? That is, when we open, we log in, sit at least until old age, and for example, in componentWillUnmount, or something similar, when the page is closed, we tell the token that it has 10 minutes left to live. Of course, I have not yet encountered such a task, but still it looks better than watching the mouse. For when the mouse moves, a lot, VERY many actions are performed, and I'm afraid that at best it will consume a huge amount of resources, and at worst it can even put an open page.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question