Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule, transition, Injectable, Type, ReflectiveInjector, InjectionToken, Injector, ValueProvider, Inject } from '@angular/core';
- import { StoreModule, Action, ActionReducer, ActionReducerMap, createSelector, createFeatureSelector } from "@ngrx/store";
- import { Effect, Actions, EffectsModule } from "@ngrx/effects"
- import { StoreDevtoolsModule } from '@ngrx/store-devtools';
- import { AppComponent } from './app.component';
- import { of } from 'rxjs/observable/of'
- import 'rxjs/add/operator/do';
- import 'rxjs/add/operator/take';
- import { MakeEffect, ResolveData } from "./with";
- type ReducerDef<T, S> = {
- [P in keyof (T)]?: (s: S, p: T[P]) => Partial<S>;
- };
- type ActionsCreator<T> = {
- [P in keyof (T)]: (p: T[P]) => Action;
- };
- type EffectsDef<T> = {
- [P in keyof (T)]?: ResolveData
- };
- const createReducer = <S, T>(initial: S, def: ReducerDef<T, S>): ActionReducer<S> => {
- let reducer = (state = initial, action: Action): S => {
- if (def[action.type]) {
- let r = <S>def[action.type](state, action['value']);
- if (typeof (r) === "undefined") {
- return state;
- } else if (typeof (r) === "object" && typeof (state) === "object") {
- return Object.assign({}, state, r);
- }
- return r;
- }
- return state
- }
- return reducer;
- }
- type CounterState = number
- interface CounterActions {
- Plus: number
- Minus: number
- }
- const CounterActions: ActionsCreator<CounterActions> = {
- Minus: (value) => ({ type: 'Minus', value }),
- Plus: (value) => ({ type: 'Plus', value }),
- }
- const counterReduced = createReducer<CounterState, CounterActions>(0, {
- Plus: (x, y) => x + y,
- Minus: (x, y) => x - y
- })
- interface FilterState {
- selectAll: boolean
- selectedIds: number[]
- }
- interface FilterActions {
- SelectAll: boolean
- Toggle: { id: number, selected: boolean }
- 'Filter.ClearAll': any
- }
- const FilterActions: ActionsCreator<FilterActions> = {
- "Filter.ClearAll": value => ({ type: 'Minus', value }),
- SelectAll: value => ({ type: 'SelectAll', value }),
- Toggle: value => ({ type: 'Toggle', value }),
- }
- FilterActions['Filter.ClearAll'](0)
- const a = FilterActions.Toggle({ id: 1, selected: true })
- const filterInitialState = {
- selectAll: false,
- selectedIds: []
- }
- const filtersReducer = createReducer<FilterState, FilterActions>(filterInitialState, {
- SelectAll: (state, b) => ({ selectAll: b }),
- Toggle: (state, action) => {
- const selectedIds = state.selectedIds
- if (action.selected && selectedIds.indexOf(action.id) > -1) {
- return { selectedIds: [...selectedIds, action.id] }
- } else if (!action.selected) {
- return { selectedIds: selectedIds.filter(id => id !== action.id) }
- }
- return {}
- },
- 'Filter.ClearAll': (state) => filterInitialState
- })
- interface Item {
- id: number
- title: string
- }
- type ItemsState = Item[]
- interface ItemsActions {
- LoadItems: void
- LoadSuccess: Item[]
- LoadFail: string
- }
- const itemsInitialState: ItemsState = []
- const itemsReducer = createReducer<ItemsState, ItemsActions>(itemsInitialState, {
- LoadSuccess: (state, items) => items
- })
- interface AppState {
- couter: number
- filter: FilterState
- items: ItemsState
- }
- const reducers: ActionReducerMap<AppState> = {
- couter: counterReduced,
- filter: filtersReducer,
- items: itemsReducer
- };
- const filterItems = (state: AppState) => state.items
- const itemsView = createSelector((state: AppState) => state, filterItems)
- @Injectable()
- class AppEffects {
- @Effect({ dispatch: false })
- ef$ = this.actions$
- .do(a => {
- console.log(0)
- });
- constructor(private actions$: Actions) { }
- }
- const myEffects = {
- doSomething: of({ type: 'test' }).do(() => console.log(123))
- }
- const MyEffectToken = new InjectionToken<ResolveData[]>("myEffects")
- const provider: ValueProvider = { provide: 'myEffects', useValue: myEffects };
- class MyService {
- }
- @Injectable()
- class MyService2 {
- }
- const MyServiceToken = new InjectionToken<MyService>("EmailService");
- const effects2: EffectsDef<CounterActions> = {
- Minus: MakeEffect((obs) => obs),
- Plus: MakeEffect(MyService2,(service, obs) => obs),
- }
- @Injectable()
- class EffectWrapper {
- constructor(private _injector: Injector, @Inject(MyEffectToken) effects) {
- }
- }
- const effects = EffectsModule.forRoot([AppEffects])
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- StoreModule.forRoot(reducers),
- effects,
- StoreDevtoolsModule.instrument()
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
- /////////////////////////////////////////////
- class EmailService { };
- class MandrillService extends EmailService { };
- class SendGridService extends EmailService { };
- let injector = ReflectiveInjector.resolveAndCreate([
- { provide: EmailService, useClass: SendGridService }
- ]);
- let emailService = injector.get(EmailService);
- console.log(emailService);
- //////////////////////////////////////////
Add Comment
Please, Sign In to add comment