redstore
v0.1.0
Published
Syntactic sugar for `ngrx/store` reducer
Downloads
2
Readme
redstore (experimental)
Syntactic sugar for ngrx/store
reducer.
redstore
(REDucerSTORE) will helps you writing your action-creators and reducers compact
in just a few lines, integrated in Angular2
's dependency injection system.
Example (TodoList)
It only takes 4 little steps:
create your state structure. We want a TodoList where we can add and remove todos and give a name for the list.
export interface IState { name?: string; todos?: string[]; }
create your
Redstore
which is action-creator and reducer in one class.@Redstore() @Injectable() export class TodoListRedstore { @Action() addTodo(item: string) { return item; } @StateCopy() static addTodo(_state: IState, item: string) { const _todos = _state.todos = flatarraycopy(_state.todos); _todos.push(item); } @Action() removeTodo(index: number) { return index; } @StateCopy() static removeTodo(_state: IState, index: number) { const _todos = _state.todos = flatarraycopy(_state.todos); _todos.splice(index, 1); } // shortcut `set...` for a reducer copying state and setting `name` @Action() setName(name: string) { return name; } // can be skipped as this is the default reducer for `set...`-Actions // @StateCopy() // static setName(_state: IState, name: string) { // _state.name = name // } }
provide
ngrx/store
module to your application (also see here)const storeModule = StoreModule.provideStore( getReducer(TodoListRedstore), {} ); @NgModule({ imports: [ // ... storeModule ] }) export class AppModule {}
inject your
Redstore
-class (e.g.TodoListRedstore
) where you want and use its methods to dispatch actions.@Component({ selector: 'myTodo', template: require('./todo.component.html') }) export class TodoComponent { todos$: Observable<string[]>; constructor( private todoListRedstore: TodoListRedstore, private store: Store<IState> ) { this.todos$ = store.map((state: IState) => state.todos); } addTodo(item: string) { // automagically dispatches addTodo-action this.todoListRedstore.addTodo(item); } removeTodo(index: number) { // automagically dispatches removeTodo-action this.todoListRedstore.removeTodo(index); } setName(name: string) { // automagically dispatches setName-action this.todoListRedstore.setName(name); } }
Ideas
no boilerplate code
no action-types have to be defined by the developer.
Type is per convention
<className>.<methodName>
.no action-creators, but only payloads.
@Action()
decorated methods only returns the payload.no dispatch calls.
@Action()
decorated methods magically dispatch the corresponding action.no state copy and return code, but
@StateCopy
decorator.@StateCopy
decorated reducer getting aflatcopy
of state as first parameter and shall not return it.
straight reducers
- no switch blocks, but invocation of reducers per convention.
- directly testable, because just static methods.
- optional default reducers, with name
setX
for@Action
decorated methods per default adds a reducer settingx
(lower camel-case forX
) to the payload.