Skip to content

Instantly share code, notes, and snippets.

View splincode's full-sized avatar
💬
Typing...

Maksim Ivanov splincode

💬
Typing...
View GitHub Profile
export type PropertyType<T> = T extends StateToken<any>
  ? Observable<ExtractTokenType<T>>
  : T extends (...args: any[]) => any
  ? Observable<ReturnType<T>>
  : any;
@State<PreferencesStateModel>({ ... })
export class PreferencesState { ... }
@State<string[]>({ ... })
export class ZooState {
@Selector([ZooState, PreferencesState])
public static firstLocalPanda(state: string[], preferencesState: PreferencesStateModel) {
return state.find(val => val.indexOf('panda') > -1 && val.indexOf(preferencesState.location)
);
@State<PreferencesStateModel>({ ... })
export class PreferencesState { ... }
@State<string[]>({ ... })
export class ZooState {
@Selector([PreferencesState])
public static firstLocalPanda(state: string[], preferencesState: PreferencesStateModel) {
return state.find(val => val.indexOf('panda') > -1 && val.indexOf(preferencesState.location));
}
@Injectable()
export class TodosEffects {
@Effect()
public setTodos$ = this.actions$.pipe(
ofType(Actions.GET_TODOS),
exhaustMap(() => someService.getTodos()),
map((todos) => ({
type: Actions.SET_TODOS,
payload: todos
}))
@Injectable()
export class TodosEffects {
@Effect()
public setTodos$ = this.actions$.pipe(
ofType(Actions.GET_TODOS),
exhaustMap(() => someService.getTodos()),
map((todos) => ({
type: Actions.SET_TODOS,
payload: todos
}))
import { Action } from '@ngrx/store';
import { ActionTypes } from '../actions/counter';
export function counterReducer(state = 0, action: Action) {
switch (action.type) {
case ActionTypes.Increment:
return state + 1;
case ActionTypes.Decrement:
return state - 1;
import { Action } from '@ngrx/store';
export enum ActionTypes {
Increment = '[Counter] Increment',
Decrement = '[Counter] Decrement'
}
import { Component } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';
import { ActionTypes } from './store/actions/counter';
@Component({
selector: 'my-app',
template: `
<p>
import { Component } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Component({
selector: 'my-app',
template: `
<p>
RxJS counter: {{ count$ | async }}
<button (click)="increment()">+</button>
<button (click)="decrement()">-</button>
import { Component } from '@angular/core';
import { Store, Select } from '@ngxs/store';
import { Observable } from 'rxjs';
import { Dispatch } from '@ngxs-labs/dispatch-decorator'
import { CountState } from './store/counter/count.state';
import { Increment, Decrement } from './store/counter/count.actions';
@Component({
selector: 'my-app',