本文整理汇总了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);
};
示例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);
}
示例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);
};
示例4:
export default function accumulate<T>(observable: Observable<T>): Observable<T[]> {
return observable.scan<T[]>((acc, input) => acc.concat(input), []);
}