Skip to content
karelsteinmetz edited this page May 19, 2016 · 2 revisions

Action

  • returns new instances of modified state and its sub states
  • beware on array operations like push etc.
  • use as much as possible specific cursors
  • if you want to modify more sub states then you should create two actions with specified cursors. Then invoke actions synchronously. b.invalidate waits for both actions. If actions take a long time then intermediate state will be rendered between actions.
  • function should not be passed through parameters into action
  • never throw in actions!!!

Common creation and invoking

  • implementation:
export let removeTodo = f.createAction<s.ITodo[], number>(c.todos, (todos, id) => {
    return [...todos.filter(t => t.id !== id)];
});
  • invoking:
actions.removeTodo(t.id);

Without parameters

  • implementation:
export let removeTodoId1 = bobflux.createAction(cursors.todos, (todos: states.ITodo[]): states.ITodo[] => {
    return [...todos.filter(t => t.id !== 1)];
});
  • invoking:
actions.removeStaticTodo();

If you need pass more parameters into action

  • implementation:
export interface IChangeDoneStatusParams {
    id: number;
    isDone: boolean;
}

export let changeDoneStatus = f.createAction<s.ITodo[], IChangeDoneStatusParams>(c.todos, (todos, params) => {
    return todos.map(t => {
        if (t.id === params.id)
            return f.shallowCopy(t, (nT) => {
                nT.isDone = params.isDone;
                return nT;
            });
        return t;
    })
});
  • invoking:
 actions.changeDoneStatus({ id: t.id, isDone: value })

With cursor factory

  • implementation:
let testAction = af.createAction<tds.ITodo, tds.ITodoParams>(
    {
        create: (params) => {
            return { key: `todos.${params.index}` };
        }
    },
    (state, params) => { return params.todo }
);
  • invoking:
testAction({ index: 1, todo: { done: false, name: 'New second todo' } });