當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。