前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Angular @Effect监听指定Action类型的实现原理

Angular @Effect监听指定Action类型的实现原理

作者头像
Jerry Wang
发布2020-10-14 09:52:16
3280
发布2020-10-14 09:52:16
举报

源代码:

代码语言:javascript
复制
@Effect()
  searchBookICanbeAnyName$: Observable<Action> = this.actions$.pipe(
    ofType(bookManage.SEARCH), // 监听bookManager.SEARCH action?
    debounceTime(300),
    mergeMap((action: bookManage.SearchAction) => {
      const nextSearch$ = this.actions$.pipe(ofType(bookManage.SEARCH), skip(1));
      return this.service.searchBooks(action.payload).pipe(
        takeUntil(nextSearch$),
        // If successful, dispatch success action with result
        map((data: BookResult) => ({type: bookManage.SEARCH_COMPLETE, payload: data.items})),
        // If request fails, dispatch failed action
        catchError(() => of({type: bookManage.SEARCH_COMPLETE, payload: []}))
      );
    })
  );

this.action$是Effect类构造函数参数,被Angular DI框架注入:

effects.js.createEffectInstances:

action$.pipe里传入的三个操作:

(1) ofType - 通过filter operator实现 (2) debounceTime (3) mergeMap

这三个operations的运行时结构:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-10-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档