0

I want to show session timeout popup for every 10 min . I am putting an issued time in session storage in a service file and also from same file I am starting the timer. The reason I am doing this is that in every response I get a new issued time . So I am passing issued time from session storage to a popup. The problem I am getting here is I cannot call a component from that particular service, the reason is "Circular dependency in service -> component -> service -> component"

This is my service file

@Injectable({
providedIn: 'root'
})


export class HttpBaseService {

 constructor(public authHttp: HttpClient, private modal: MatDialog, 
private toastr: ToastrManager) { }

public handleAPIResponse(response: any) {

    if (response.accessToken) {
        const tokenInfo = 
this.getDecodedAccessToken(response.accessToken);
        const issuedTime = tokenInfo.exp;
        this.setToken(response.accessToken, issuedTime);
//Here I am starting the timer with latest  issued time.
          this.startTimer(issuedTime); 
    }
    return response.result;
 }
}

startTimer(issuedTime: number) {
///login to calculate time 
//call a modal popup here if time is up. 
this.modal.open(ModalPopupComponent, {

 }

 }

This is my ModalPopupComponent

@Component({
  selector: 'app-modal-popup',
  templateUrl: './modal-popup.component.html',
  styleUrls: ['./modal-popup.component.scss'],
})

export class ModalPopupComponent implements OnInit { 

//logic to show countdown of One min to logout automatically

}

Need to know how can I call modal component from this service, without any circular dependency. Thanks in Advance

Chetan Birajdar
  • 461
  • 9
  • 22
  • why you want call component from service? the normal flow component call services – Hien Nguyen Mar 29 '19 at 08:02
  • I want to show a popup from this service, actually this is for making code generic. This service will check for new issued date from the response token , (here I am getting a new issued date for every singe response.) and if the time is up I have to show a modal popup from this service. Is there any other simple way I can acheive this ? – Chetan Birajdar Mar 29 '19 at 08:06
  • you can use Observable timer like this https://stackoverflow.com/questions/43763430/angular2-observable-timer-condition and call open dialog on subscribe – Hien Nguyen Mar 29 '19 at 08:09
  • thats the problem here , I am not able to show a popup here from service .the Warning Says there is a circular dependency. – Chetan Birajdar Mar 29 '19 at 08:17
  • can you tell me how to pass values from service to component eg. In My Service private myComponent = Mycopmponent; const value = 123; this.mycomponent.someMethod(value) this. – Chetan Birajdar Mar 29 '19 at 10:21

1 Answers1

0

You can set timer in component witch check you variable in service and run Modal like this:

setInterval(() => { this.loading = true; this.getSignals(); }, 60000);

You can activate this in Component ngOninit Good luck!

user3804427
  • 432
  • 2
  • 12
  • I have the logic to show timer, exactly as I want , but the problem I am facing is I cannot show a modal popup here, gives me warning , saying , there is a circular dependency – Chetan Birajdar Mar 29 '19 at 08:15
  • You must call popup in component by timer, not in service. In component you can check any service variable. – user3804427 Mar 29 '19 at 09:06
  • can you tell me how to pass values from service to component eg. In My Service , private myComponent = Mycopmponent; const value = 123; this.mycomponent.someMethod(value); – Chetan Birajdar Mar 29 '19 at 10:18
  • You must import service in component like this: ```import { SelectedService } from '../selected.service';``` then you must declare variable in constructor like this ```constructor(public ch_serv: ChService) {}``` then you can check service variable in component like this: ``ch_serv.var``. May be you create service like component, but service is not component. You can create service by: ```ng g s service_name``` You cannot import components in service. – user3804427 Mar 29 '19 at 10:23