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


TypeScript Observable.scan方法代码示例

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


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

示例1:

const logReducer = (initial: string[], action$: Observable<Action>): Observable<string[]> => {
    return action$
        .scan((log: string[], action: LogAction) => {
            if (action instanceof LogAction) {
                return [...log, action.message];
            }
            return log;
        }, initial);
};
开发者ID:gwhn,项目名称:ng-rxjs-state-dispatch,代码行数:9,代码来源:state-fn.ts

示例2: heroReducer

function heroReducer(initHeroes: Hero[], dispatcher$: Observable<Action>): Observable<Hero[]> {
  return dispatcher$
    .scan<Hero[]>((heroes: Hero[], action: Action) => {
      const oldHeroes = heroes;
      if (action instanceof EditHero) {
        const editedHero = action.hero;
        heroes = lodash.uniqBy([editedHero, ...heroes], 'id');
      } else if (action instanceof AddHero) {
        const newHero = action.hero;
        heroes = lodash.uniqBy([newHero, ...heroes], 'id');
      } else if (action instanceof DeleteHero) {
        const deleteId = action.id;
        heroes = lodash.reject(heroes, { id: deleteId });
      }
      logger('Store - heroReducer', oldHeroes, heroes);
      return lodash.orderBy(heroes, ['id'], ['asc']);
    }, initHeroes);
}
开发者ID:ovrmrw,项目名称:ng2-heroes-editor,代码行数:18,代码来源:store.ts

示例3: id

const todosReducer = (initial: Todo[], action$: Observable<Action>): Observable<Todo[]> => {
    const id = (todos: Todo[]) => {
        return todos.reduce((a, v) => v.id > a ? v.id : a, 0) + 1;
    };

    return action$
        .scan((todos: Todo[], action: TodoAction) => {
            if (action instanceof AddTodoAction) {
                const newTodo = {
                    id: id(todos),
                    text: action.text,
                    completed: false
                } as Todo;
                return [...todos, newTodo];
            }
            if (action instanceof ToggleTodoAction) {
                return todos.map((todo: Todo) => {
                    //noinspection TypeScriptUnresolvedFunction
                    return action.id !== todo.id
                        ? todo
                        : Object.assign({}, todo, {completed: !todo.completed});
                });
            }
            if (action instanceof SortAscendingAction) {
                return [...todos].sort((a, b) => {
                    return a.text < b.text ? -1 : a.text > b.text ? 1 : 0;
                });
            }
            if (action instanceof SortDescendingAction) {
                return [...todos].sort((a, b) => {
                    return a.text > b.text ? -1 : a.text < b.text ? 1 : 0;
                });
            }
            return todos;
        }, initial);
};
开发者ID:gwhn,项目名称:ng-rxjs-state-dispatch,代码行数:36,代码来源:state-fn.ts

示例4:

export default function accumulate<T>(observable: Observable<T>): Observable<T[]> {
	return observable.scan<T[]>((acc, input) => acc.concat(input), []);
}
开发者ID:novemberborn,项目名称:dojo2-dataviz,代码行数:3,代码来源:accumulate.ts


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