English 中文(简体)
未在另一笔订阅中工作的ng
原标题:ng-idle subscriptions not working inside another subscription

我正在使用“黄单”/双芯包,在我的单体2应用中(目前为14英寸)实行一次性的排位。 超过闲置时间,代码将用户排出。 单项探测和伐木功能已编入我的备份档案。

在编造单面值时,该法典完全有效。 然而,我想,我的背后可以想象的闲.时间。 因此, 我写了一门服务班,该班在申请负荷时从后台取值。 守则的其余部分与硬编码的闲置时间版本完全相同(但现在属于我服务订阅的订阅部分)。 然而,ng的订阅从来就没有火灾。 该法典如下:

    constructor(
        private router: Router,
        private idle: Idle, cd: ChangeDetectorRef,
        private authSvc: AuthService,
        private sessionDataSvc: SessionDataService,
        private countdownSvc: CountdownService
      ) {

    // set idle parameters
    this.sessionDataSvc.getSessionProperties().subscribe(( {data} ) => {
      let session_timeout = data.sessionProperties.session_idle_timeout
      // set the idle and timeout values
      // let session_timeout = 30;
      idle.setIdle(session_timeout) // how long can they be inactive before considered idle, in seconds
      idle.setTimeout(this.TIMEOUT); // how long can they be idle before considered timed out, in seconds
      idle.setInterrupts(DEFAULT_INTERRUPTSOURCES); // provide sources that will "interrupt" aka provide events indicating the user is active

      console.log("Setting idle timeout to " + session_timeout + " seconds")
       
      // Some code snipped for brevity

      // do something when the user has timed out
      idle.onTimeout.subscribe(() => {
        console.log("onTimeout called")
        this.idleState = "TIMED_OUT"
        this.authSvc.logout()
        this.reset()
        this.router.navigate([ login ], { queryParams: { reason: "Session Timeout. Please re-login" } });
      });
      // do something as the timeout countdown does its thing
      });
  

        console.log("Done with setting idle timeout")
        });
      }
    
    reset() 
    {
        // we ll call this method when we want to start/reset the idle process
        // reset any component state and be sure to call idle.watch()
        this.idle.watch();
        this.idleState = "NOT_IDLE";
        this.countdown = null;
        this.lastPing = null;
    }

值得注意的是,包裹有4种订阅方法,即IdleStart、IdleEnd、实时停战和停工。 我故意从上述法典中删除了头三个,以便在我的发言中作简短的发言,否则在法典中就存在。 这本书是作为青少年运作的。 日志信息正在显示日志信息

Setting idle timeout to 30 seconds

正在黄色记录中显示(如上面代码中其他标识信息,“设定闲置时间”。 但是,没有哪怕是哪怕是哪一subscription。

As I mentioned above, if I was to comment out the lines

this.sessionDataSvc.getSessionProperties().subscribe(( {data} ) => {
  let session_timeout = data.sessionProperties.session_idle_timeout

and enable the line:

// let session_timeout = 30;

instead (this is the hard-coded case), the @ng-idle subscriptions work as designed and documented. So why are they not inside a service subscription?

问题回答

It s a bad practice to subscribe to an observable within another one, that is called "callback hell", for this, you need to handle your observables with high order operators from RxJs such as switchMap, concatMap, mergeMap and exhaustMap depending on what you want to achieve.

If you are not familiar with these operators, you could read this short explanation I gave a while back, or read the official docs

鉴于你的密码例子,你可以适当证明你的逻辑,在此情况下,我使用<条码>switchMap,但再次取决于要求:

...
someFunction(): void {
 this.sessionDataSvc.getSessionProperties().pipe(
   switchMap(({ data }) => {
     // it should be const rather than let, you are not modifying its value
     const sessionTimeout = data.sessionProperties.session_idle_timeout;
  
     idle.setIdle(sessionTimeout);
     idle.setTimeout(this.TIMEOUT);
     idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);
     return idle.onTimeout; // returns an Observable
   })
 )
 .subscribe(() => {
   this.idleState = "TIMED_OUT"
   this.authSvc.logout()
   this.reset()
   this.router.navigate([ login ], { queryParams: { reason: "Session Timeout. Please re-login" } });
 });
}

reset(): void {
 this.idle.watch();
 this.idleState = "NOT_IDLE";
 this.countdown = null;
 this.lastPing = null;
}




相关问题
Block Websites Under Certain Conditions

I d like to create a simple program that will block certain websites for the user under certain conditions (e.g. after 7:00pm if homework is due the next day). This would be just a programming ...

Looking for a good exercise in building a website

I d like to learn how to build a website, say using .Net (Monorail comes to mind). I d like a pet project, something that: Will take a fair yet reasonble amount of time I can I can build on my own ...

Recoding a Podcast Website - What should I use?

OK here is the deal. Me and some buddies are doing a Podcast(for over 4 year now) and in April we relaunched the site, that included a new Backend. Since I only had 2 weeks back then for getting ...

Cache And Compare Files In C#

Ok, I m trying to make an application for an online Radio Station. I have it set to read the song title and artist and write it to a text file on the webserver. I want to have the application store ...

How does Drupal provide an edit/review/publish model?

How does Drupal support a means to update and review a website before it is published? Does it only allow you to preview a page at a time before you publish it or is there a way to create a site ...

热门标签