Last active
March 23, 2020 10:42
-
-
Save GregOnNet/8e2eac5ada22c6e15e23d3d4bd01487c to your computer and use it in GitHub Desktop.
he-ngrx-article-snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EffectsModule.forRoot([]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createAction, props } from '@ngrx/store'; | |
export const increment = createAction( | |
'[Counter] Increment', | |
props<{ payload: number }>() | |
); | |
export const decrement = createAction( | |
'[Counter] Decrement', | |
props<{ payload: number }>() | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constructor(private store: Store) { | |
// Vorher | |
this.count = this.store.pipe( | |
select((state: any) => state.counter.count) | |
); | |
// Nachher | |
this.count = this.store.pipe(select(count)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
import { Store, select } from '@ngrx/store'; | |
import { Observable } from 'rxjs'; | |
import { decrement, increment } from './store/counter.actions'; | |
@Component({ | |
selector: 'app-counter', | |
template: ` | |
<strong>{{ count | async }}</strong> | |
<button (click)="increment(1)">Increment</button> | |
<button (click)="decrement(1)">Decrement</button> | |
` | |
}) | |
export class CounterComponent { | |
count: Observable<number>; | |
constructor(private store: Store) { | |
this.count = this.store.pipe( | |
select((state: any) => state.counter.count) | |
); | |
} | |
increment(payload: number) { | |
this.store.dispatch(increment({ payload })); | |
} | |
decrement(payload: number) { | |
this.store.dispatch(decrement({ payload })); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class CounterEffects { | |
randomAdd = createEffect(() => | |
this.actions.pipe( | |
ofType(randomAdd), | |
concatMap(() => | |
randomizedNumber().pipe( | |
map(value => add({ payload: { value } })), | |
catchError(message => of(logInfo(message))) | |
) | |
) | |
) | |
); | |
constructor(private actions: Actions) {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EffectsModule.forFeature([CounterEffects]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { StoreModule } from '@ngrx/store'; | |
import * as fromCounter from './store/counter.reducer'; | |
// … @NgModule Definition | |
StoreModule.forFeature( | |
fromCounter.counterFeatureKey, | |
fromCounter.reducer | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createReducer, on } from '@ngrx/store'; | |
import { increment, decrement } from './counter.actions'; | |
export const initialState: State = { | |
count: 0 | |
}; | |
export const reducer = createReducer( | |
initialState, | |
on(increment, (state, { payload }) => ({ | |
...state, | |
count: state.count + payload | |
})), | |
on(decrement, (state, { payload }) => ({ | |
...state, | |
count: state.count - payload | |
})) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createFeatureSelector, createSelector } from '@ngrx/store'; | |
import * as fromCounter from './counter.reducer'; | |
export const counterState = createFeatureSelector<fromCounter.State> ( | |
fromCounter.counterFeatureKey | |
); | |
export const count = createSelector( | |
counterState, | |
state => state.count | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.store.subscribe(state => { | |
console.log('Counter', state.counter.count); | |
console.log('Log Messages', state.log.infos); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
randomAdd = createEffect(() => | |
this.actions.pipe(...), | |
{ dispatch: false } | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createEntityAdapter, ... } from '@ngrx/store'; | |
export const adapter = createEntityAdapter<Message>(); | |
export const loggerReducer = createReducer( | |
adapter.getInitialState(), | |
on(logInfo, (state, { payload }) => adapter.addOne(payload, state)) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { adapter } from './logger.reducer'; | |
export const loggerFeatureKey = 'logger'; | |
const featureLogger = createFeatureSelector<LoggerState>(loggerFeatureKey); | |
export const { | |
selectAll, | |
selectTotal, | |
selectEntities, | |
selectIds | |
} = adapter.getSelectors(featureLogger); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
counter/ | |
┣ store/ | |
┃ ┣ counter.actions.spec.ts | |
┃ ┣ counter.actions.ts | |
┃ ┣ counter.effects.spec.ts | |
┃ ┣ counter.effects.ts | |
┃ ┣ counter.reducer.spec.ts | |
┃ ┣ counter.reducer.ts | |
┃ ┣ counter.selectors.spec.ts | |
┃ ┗ counter.selectors.ts | |
┣ counter.component.ts | |
┗ counter.module.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const initialState: State = { count: 0 }; | |
function counterReducer( | |
state = initialState, | |
action: Action | |
): State { | |
// switch-case action.type ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function counterReducer(state: State, action: Action): State { | |
switch (action.type) { | |
case '[Counter] Add': | |
return { | |
...state, | |
count: state.count + action.payload | |
}; | |
default: return state; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StoreModule.forRoot({ | |
counter: counterReducer, | |
log: logReducer | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!environment.production | |
? StoreDevtoolsModule.instrument() | |
: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const action = { | |
type: '[Counter] Add', | |
payload: 3 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({/*... */}) | |
export class ListComponent { | |
constructor( | |
private route: ActivatedRoute, | |
private userService: UserService, | |
private shoppingService: ShoppingService, | |
private invoiceService: InvoiceService, | |
) {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StoreModule.forRoot(reducers, { | |
metaReducers, | |
runtimeChecks: { | |
strictStateImmutability: true, | |
strictActionImmutability: true, | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { EntityState } from '@ngrx/entity'; | |
export interface Message { | |
id: string; | |
text: string; | |
} | |
export interface LoggerState extends EntityState<Message> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
counter: { | |
count: 0 | |
}, | |
log: { | |
infos: [] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
featureA: { ... }, | |
featureB: { ... } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
storeActions.pipe( | |
filter(action => action.type === 'Initiate'), | |
switchMap(action => apiCall.execute(action.payload)), | |
map(result => store.dispatch({ | |
type: 'Success', payload: result | |
})), | |
catchError(err => store.dispatch({ | |
type: 'ERROR', payload: err.message | |
})) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ /* ... */ }) | |
export class CounterComponent { | |
constructor(private store: Store<State>) {} | |
add(count: number) { | |
this.store.dispatch({ | |
type: '[Counter] Add', | |
payload: count | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment