当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Store.asObservable方法代码示例

本文整理汇总了TypeScript中@ngrx/store.Store.asObservable方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Store.asObservable方法的具体用法?TypeScript Store.asObservable怎么用?TypeScript Store.asObservable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在@ngrx/store.Store的用法示例。


在下文中一共展示了Store.asObservable方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: constructor

 constructor(
     store: Store<IState>
 ) { 
     // In our example we're treating any change in global state 
     //  as an example of user activity. So to start we need to 
     //  just get an observable for the stream of state changes
     //
     let state$ = store.asObservable() as Observable<IState>;
     
     // Now here's where the power of RxJS comes into play. We're going to user 
     //  a technique where we react to state changes by mapping them to a new 
     //  observable that emits a value after the timeour period expires.
     //
     state$
         // We only want to start an activity timer when a user is actually logged 
         //  in, so filter out any state changes where that flag is false.
         //
         .filter((x: IState) => x.loggedIn)
         //
         // Now we don't really care what the state change was in this context, and 
         //  what we really want is an observable for our specific timeout value. So 
         //  we map the state change to an observable that emits a single value after
         //  the timeout.
         //
         // Note, this also means that any state change we care about creates a *new* 
         //  observable...and this is how we "reset" the timer when that happens.
         //
         .map((x: IState) => Observable.timer(5000))
         //
         // This is just for logging purposes in the demo so we can see when a new 
         //  timer is started.
         //
         .do((x: any) => console.log("Activity detected! Timer has reset to 5 seconds"))
         //
         // Now here's the slick part (IMO). Each time a new "timeout" observable is 
         //  created we want to make sure it replaces any timer we were subscribed to 
         //  before. In other words, as soon as a new timer is started we want to use that 
         //  one and just ignore anything we had before.
         //
         // In RxJS that's a simple operator called switch().
         //
         .switch()
         //
         // Finally, we need to subscribe to the timer observable to decide what to do 
         //  should it actually fire (i.e., the timeout actually expired). Here we're 
         //  just going to dispatch a new action that indicates the timeout expired. Then
         //  the reducer can decide what it actually means based on the state of the 
         //  application at the time.
         //
         .subscribe((x) => {
             console.log("Inactivity interval expired! Dispatching timeout event")
             store.dispatch({type: ACTIVITY_TIMEOUT_OCCURRED});
         });
     
 }
开发者ID:AttentiaHVDA,项目名称:experiments,代码行数:55,代码来源:auto-logout.service.ts


注:本文中的@ngrx/store.Store.asObservable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。