<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Slicing State with Map</title>
<script src="https://npmcdn.com/@reactivex/rxjs@5.0.0-beta.2/dist/global/Rx.umd.js"></script>
</head>
<body>
</body>
</html>
class Dispatcher extends Rx.Subject{
dispatch(value : any) : void {
this.next(value);
}
}
class Store extends Rx.BehaviorSubject{
constructor(
private dispatcher,
private reducer,
preMiddleware,
postMiddleware,
initialState = {}
){
super(initialState);
this.dispatcher
.let(preMiddleware)
.scan((state, action) => this.reducer(state, action), initialState)
.let(postMiddleware)
.subscribe(state => super.next(state));
}
//Map makes it easy to select slices of state that will be needed for your components
//This is a simple helper function to make grabbing sections of state more concise
select(key : string) {
return this.map(state => state[key]).distinctUntilChanged();
}
dispatch(value){
this.dispatcher.dispatch(value);
}
next(value){
this.dispatcher.dispatch(value);
}
}
const combineReducers = reducers => (state = {}, action) => {
return Object.keys(reducers).reduce((nextState, key) => {
nextState[key] = reducers[key](state[key], action);
return nextState;
}, {});
};
//Add a few basic reducers
const person = (state = {}, action) => {
switch(action.type){
case 'ADD_INFO':
return Object.assign({}, state, action.payload)
default:
return state;
}
}
const hoursWorked = (state = 0, action) => {
switch(action.type){
case 'ADD_HOUR':
return state + 1;
case 'SUBTRACT_HOUR':
return state - 1;
default:
return state;
}
}
//this is handled behind the scenes with @ngrx/store provideStore
const appReducer = combineReducers({person, hoursWorked});
const preMiddleware = obs => { return obs.do(val => console.log('ACTION:', val))};
const postMiddleware = obs => { return obs.do(val => console.log('STATE:', val))};
//create our store and dependencies
const dispatcher = new Dispatcher();
const store = new Store(dispatcher, appReducer, preMiddleware, postMiddleware, {hoursWorked: 10});
const subscriber = store
//utilize our new select helper
.select('person')
.subscribe(val => console.log('VALUE OF PERSON:', val));
//dispatch a few actions
dispatcher.dispatch({
type: 'ADD_INFO',
payload: {
name: 'Brian',
message: 'Exploring Reduce!'
}
});
dispatcher.dispatch({
type: 'ADD_INFO',
payload: {
goal: 'Learn @ngrx/store!'
}
});
Output
This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account
Dismiss xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |