Ejemplo n.º 1
0
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);
};
Ejemplo n.º 2
0
const filtersReducer = (initial: Filters, action$: Observable<Action>): Observable<Filters> => {
    return action$
        .scan((filters: Filters, action: FilterAction) => {
            if (action instanceof SetVisibilityFilterAction) {
                //noinspection TypeScriptUnresolvedFunction
                return Object.assign({}, filters, {visibility: action.type});
            }
            if (action instanceof SetSortOrderAction) {
                //noinspection TypeScriptUnresolvedFunction
                return Object.assign({}, filters, {sortOrder: action.direction});
            }
            return filters;

        }, initial);
};
Ejemplo n.º 3
0
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);
}
Ejemplo n.º 4
0
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);
};
Ejemplo n.º 5
0
export default function accumulate<T>(observable: Observable<T>): Observable<T[]> {
	return observable.scan<T[]>((acc, input) => acc.concat(input), []);
}