Giter Club home page Giter Club logo

store's Introduction

@ngrx/store

Join the chat at https://gitter.im/ngrx/store Codeship Status for ngrx/store npm version

RxJS powered state management inspired by Redux for Angular2 apps

Demo

http://plnkr.co/edit/Hb4pJP3jGtOp6b7JubzS?p=preview

installation

  • Make sure you have angular2 installed via npm : npm install angular2
  • Install from npm : npm install @ngrx/store

usage

  • Create a reducer function for each data type you have:
//counter.ts
import {Reducer, Action} from '@ngrx/store';

export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
export const RESET = 'RESET';

export const counter:Reducer<number> = (state:number = 0, action:Action) => {

	switch (action.type) {
		case INCREMENT:
			return state + 1;

		case DECREMENT:
			return state - 1;

		case RESET:
			return 0;

		default:
			return state;
	}
}
  • In your app's main module, import those reducers and use the provideStore(reducers, initialState) function to provide them to Angular's injector:
import {bootstrap} from 'angular2/platform/bootstrap';
import {provideStore} from '@ngrx/store';
import {App} from './myapp';

import {counter} from './counter';

bootstrap(App, [ provideStore({counter}, {counter: 0}) ]);
  • You can then inject the Store service into your Components and Services:
import {Store} from '@ngrx/store';
import {INCREMENT, DECREMENT, RESET} from './counter';

interface AppState {
  counter: number;
}

@Component({
	selector: 'my-app',
	template: `
	  <button (click)="increment()">Increment</button>
		<div>Current Count: {{ counter | async }}</div>
		<button (click)="decrement()">Decrement</button>
	`
})
class MyApp {
	counter: Observable<number>;
	constructor(public store: Store<AppState>){
		this.counter = store.select('counter');
	}
	increment(){
		this.store.dispatch({ type: INCREMENT });
	}
	decrement(){
		this.store.dispatch({ type: DECREMENT });
	}
	reset(){
		this.store.dispatch({ type: RESET });
	}
}

###Middleware ####(observable: Observable<any>): Observable<any> Store middleware provides pre and post reducer entry points for all dispatched actions. Middleware can be used for everything from logging, asynchronous event handling, to action or post action manipulation.

#####Dispatched action pipeline:

  1. Pre-Middleware : (observable: Observable<Action>): Observable<Action>
  2. Reducer
  3. Post-Middleware : (observable: Observable<State>): Observable<State>

Middleware can be configured during application bootstrap by utilizing the usePreMiddleware(...middleware: Middleware[]) and usePostMiddleware(...middleware: Middleware[]) helper functions.

####usage Warning: Remember to import all utilized RxJS operators.

import {bootstrap} from 'angular2/platform/browser';
import {App} from './myapp';
import {provideStore, usePreMiddleware, usePostMiddleware, Middleware} from "@ngrx/store";
import {counter} from "./counter";

const actionLog : Middleware = action => {
    return action.do(val => {
        console.warn('DISPATCHED ACTION: ', val)
    });
};

const stateLog : Middleware = state => {
    return state.do(val => {
        console.info('NEW STATE: ', val)
    });
};

bootstrap(App, [
  provideStore({counter},{counter : 0}),
  usePreMiddleware(actionLog),
  usePostMiddleware(stateLog)
]);

####Middleware with Dependencies For middleware requiring dependencies the createMiddleware(useFactory: (...deps: any[]) => Middleware, deps?: any[]): Provider helper function is supplied. This allows you to quickly create middleware that relies on other Angular services, such as the Dispatcher. ####usage

  • Create middleware provider:
export const thunk = createMiddleware(function(dispatcher: Dispatcher<Action>) {
  return function(all$: Observable<Action | Thunk>){
    const [thunks$, actions$] = all$.partition(t => typeof t === 'function');

    thunks$.forEach(thunk => thunk(action => dispatcher.dispatch(action));

    return actions$;
  }
}, [Dispatcher]);
  • Initialize with usePreMiddleware(...middleware: Middleware[]) or usePostMiddleware(...middleware: Middleware[]) on application bootstrap:
import {bootstrap} from 'angular2/platform/browser';
import {App} from './myapp';
import {provideStore, usePreMiddleware, Middleware} from '@ngrx/store';
import {thunk} from './thunk';
import {exampleReducer} from './exampleReducer';

bootstrap(App, [
  provideStore({exampleReducer}),
  usePreMiddleware(thunk)
]);

Contributing

Please read contributing guidelines here.

store's People

Contributors

robwormald avatar mikeryandev avatar btroncone avatar meenie avatar cschiavolini avatar nathanwalker avatar wesleycho avatar mikehaas763 avatar gitter-badger avatar

Watchers

James Cloos avatar Mark Pieszak avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.