export type PropertyType<T> = T extends StateToken<any>
? Observable<ExtractTokenType<T>>
: T extends (...args: any[]) => any
? Observable<ReturnType<T>>
: any;
This file contains hidden or 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 { BehaviorSubject, Observable } from 'rxjs'; | |
import { Injectable } from '@angular/core'; | |
@Injectable() | |
export abstract class Store<T> { | |
private _state$: BehaviorSubject<T>; | |
protected constructor(defaults: T) { | |
this.state$ = new BehaviorSubject(defaults); | |
} |
This file contains hidden or 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
@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) | |
); |
This file contains hidden or 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
@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)); | |
} |
This file contains hidden or 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 TodosEffects { | |
@Effect() | |
public setTodos$ = this.actions$.pipe( | |
ofType(Actions.GET_TODOS), | |
exhaustMap(() => someService.getTodos()), | |
map((todos) => ({ | |
type: Actions.SET_TODOS, | |
payload: todos | |
})) |
This file contains hidden or 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 TodosEffects { | |
@Effect() | |
public setTodos$ = this.actions$.pipe( | |
ofType(Actions.GET_TODOS), | |
exhaustMap(() => someService.getTodos()), | |
map((todos) => ({ | |
type: Actions.SET_TODOS, | |
payload: todos | |
})) |
This file contains hidden or 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 { 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; |
This file contains hidden or 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 { Action } from '@ngrx/store'; | |
export enum ActionTypes { | |
Increment = '[Counter] Increment', | |
Decrement = '[Counter] Decrement' | |
} |
This file contains hidden or 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 { ActionTypes } from './store/actions/counter'; | |
@Component({ | |
selector: 'my-app', | |
template: ` | |
<p> |
This file contains hidden or 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 { BehaviorSubject } from 'rxjs'; | |
@Component({ | |
selector: 'my-app', | |
template: ` | |
<p> | |
RxJS counter: {{ count$ | async }} | |
<button (click)="increment()">+</button> | |
<button (click)="decrement()">-</button> |