tl; dr, with Aurelia, how can I call a function that is inside the view model from outside the view model?
I need to perform client-side logout for a user who has not performed an action (route change, server request, etc.) for a certain period of time. After reading this GitHub problem, I created an inactive view and view model and included it in my app.html and in my attached () function, I start my timer and register the user when the time is up.
It all works great, but I ran into a problem that makes me feel like it was a huge rabbit hole. How to call the resetInactivityTimer () function from outside the view model, is it possible to make one function in the class public? For example, when a server request is executed, I want to call the resetInactivityTimer () function from my service class
inactivity-logout.ts
import {Aurelia} from 'aurelia-framework';
import {Router} from 'aurelia-router';
import {inject} from 'aurelia-dependency-injection';
@inject(Aurelia, Router)
export class InactivityLogout {
inactivityWarningDuration: number;
initialInactivityWarningDuration: number;
inactivityDuration: number;
inactivityIntervalHandle: any;
constructor(aurelia, router) {
this.aurelia = aurelia;
this.router = router;
this.initialInactivityWarningDuration = 5;
this.inactivityWarningDuration = this.initialInactivityWarningDuration;
this.inactivityDuration = 5;
}
attached() {
this.queueInactivityTimer();
}
resetInactivityTimer(){
$("#LogoutWarningPopup").modal("hide");
this.inactivityWarningDuration = this.initialInactivityWarningDuration;
clearInterval(this.warningInterval);
this.queueInactivityTimer();
}
queueInactivityTimer(){
clearTimeout(this.inactivityIntervalHandle);
this.inactivityIntervalHandle = setTimeout(() => {
this.displayWarning();
}, 1000 * this.inactivityDuration);
}
displayWarning(){
$("#LogoutWarningPopup").modal({ backdrop: 'static', keyboard: false });
this.warningInterval = setInterval(()=>{
this.inactivityWarningDuration--;
if(this.inactivityWarningDuration <= 0){
clearInterval(this.warningInterval);
this.logout();
}
}, 1000);
}
logout(){
$("#LogoutWarningPopup").modal("hide");
console.log("due to inactivity, you've been logged out.")
this.aurelia.setRoot('views/login');
}
}
Run codeHide resultapp.html
<require from='./inactivity-logout.js'></require>
<inactivity-logout></inactivity-logout>
Run codeHide resultSearch-service.ts
performSearch(searchText: String) {
return this.httpClient.put("/api/Search", searchText)
.then(response => {
return response;
});
}
Run codeHide result
source
share