Guest User

Untitled

a guest
Jan 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule, transition, Injectable, Type, ReflectiveInjector, InjectionToken, Injector, ValueProvider, Inject } from '@angular/core';
  3. import { StoreModule, Action, ActionReducer, ActionReducerMap, createSelector, createFeatureSelector } from "@ngrx/store";
  4. import { Effect, Actions, EffectsModule } from "@ngrx/effects"
  5. import { StoreDevtoolsModule } from '@ngrx/store-devtools';
  6. import { AppComponent } from './app.component';
  7. import { of } from 'rxjs/observable/of'
  8. import 'rxjs/add/operator/do';
  9. import 'rxjs/add/operator/take';
  10.  
  11. import { MakeEffect, ResolveData } from "./with";
  12.  
  13.  
  14. type ReducerDef<T, S> = {
  15. [P in keyof (T)]?: (s: S, p: T[P]) => Partial<S>;
  16. };
  17.  
  18. type ActionsCreator<T> = {
  19. [P in keyof (T)]: (p: T[P]) => Action;
  20. };
  21.  
  22. type EffectsDef<T> = {
  23. [P in keyof (T)]?: ResolveData
  24. };
  25.  
  26.  
  27. const createReducer = <S, T>(initial: S, def: ReducerDef<T, S>): ActionReducer<S> => {
  28. let reducer = (state = initial, action: Action): S => {
  29. if (def[action.type]) {
  30. let r = <S>def[action.type](state, action['value']);
  31. if (typeof (r) === "undefined") {
  32. return state;
  33. } else if (typeof (r) === "object" && typeof (state) === "object") {
  34. return Object.assign({}, state, r);
  35. }
  36. return r;
  37. }
  38. return state
  39. }
  40.  
  41. return reducer;
  42. }
  43.  
  44. type CounterState = number
  45.  
  46. interface CounterActions {
  47. Plus: number
  48. Minus: number
  49. }
  50.  
  51. const CounterActions: ActionsCreator<CounterActions> = {
  52. Minus: (value) => ({ type: 'Minus', value }),
  53. Plus: (value) => ({ type: 'Plus', value }),
  54. }
  55.  
  56. const counterReduced = createReducer<CounterState, CounterActions>(0, {
  57. Plus: (x, y) => x + y,
  58. Minus: (x, y) => x - y
  59. })
  60.  
  61. interface FilterState {
  62. selectAll: boolean
  63. selectedIds: number[]
  64. }
  65.  
  66. interface FilterActions {
  67. SelectAll: boolean
  68. Toggle: { id: number, selected: boolean }
  69. 'Filter.ClearAll': any
  70. }
  71.  
  72.  
  73.  
  74.  
  75. const FilterActions: ActionsCreator<FilterActions> = {
  76. "Filter.ClearAll": value => ({ type: 'Minus', value }),
  77. SelectAll: value => ({ type: 'SelectAll', value }),
  78. Toggle: value => ({ type: 'Toggle', value }),
  79. }
  80.  
  81. FilterActions['Filter.ClearAll'](0)
  82. const a = FilterActions.Toggle({ id: 1, selected: true })
  83.  
  84.  
  85. const filterInitialState = {
  86. selectAll: false,
  87. selectedIds: []
  88. }
  89.  
  90. const filtersReducer = createReducer<FilterState, FilterActions>(filterInitialState, {
  91. SelectAll: (state, b) => ({ selectAll: b }),
  92. Toggle: (state, action) => {
  93. const selectedIds = state.selectedIds
  94. if (action.selected && selectedIds.indexOf(action.id) > -1) {
  95. return { selectedIds: [...selectedIds, action.id] }
  96. } else if (!action.selected) {
  97. return { selectedIds: selectedIds.filter(id => id !== action.id) }
  98. }
  99. return {}
  100. },
  101. 'Filter.ClearAll': (state) => filterInitialState
  102. })
  103.  
  104.  
  105.  
  106.  
  107.  
  108. interface Item {
  109. id: number
  110. title: string
  111. }
  112.  
  113. type ItemsState = Item[]
  114.  
  115. interface ItemsActions {
  116. LoadItems: void
  117. LoadSuccess: Item[]
  118. LoadFail: string
  119. }
  120.  
  121.  
  122. const itemsInitialState: ItemsState = []
  123. const itemsReducer = createReducer<ItemsState, ItemsActions>(itemsInitialState, {
  124. LoadSuccess: (state, items) => items
  125. })
  126.  
  127. interface AppState {
  128. couter: number
  129. filter: FilterState
  130. items: ItemsState
  131. }
  132.  
  133. const reducers: ActionReducerMap<AppState> = {
  134. couter: counterReduced,
  135. filter: filtersReducer,
  136. items: itemsReducer
  137. };
  138.  
  139.  
  140. const filterItems = (state: AppState) => state.items
  141.  
  142. const itemsView = createSelector((state: AppState) => state, filterItems)
  143.  
  144. @Injectable()
  145. class AppEffects {
  146.  
  147. @Effect({ dispatch: false })
  148. ef$ = this.actions$
  149. .do(a => {
  150. console.log(0)
  151. });
  152.  
  153. constructor(private actions$: Actions) { }
  154. }
  155.  
  156.  
  157. const myEffects = {
  158. doSomething: of({ type: 'test' }).do(() => console.log(123))
  159. }
  160.  
  161. const MyEffectToken = new InjectionToken<ResolveData[]>("myEffects")
  162. const provider: ValueProvider = { provide: 'myEffects', useValue: myEffects };
  163. class MyService {
  164.  
  165. }
  166.  
  167. @Injectable()
  168. class MyService2 {
  169.  
  170. }
  171.  
  172. const MyServiceToken = new InjectionToken<MyService>("EmailService");
  173.  
  174. const effects2: EffectsDef<CounterActions> = {
  175. Minus: MakeEffect((obs) => obs),
  176. Plus: MakeEffect(MyService2,(service, obs) => obs),
  177. }
  178.  
  179.  
  180.  
  181.  
  182. @Injectable()
  183. class EffectWrapper {
  184. constructor(private _injector: Injector, @Inject(MyEffectToken) effects) {
  185.  
  186. }
  187. }
  188.  
  189.  
  190. const effects = EffectsModule.forRoot([AppEffects])
  191.  
  192. @NgModule({
  193. declarations: [
  194. AppComponent
  195. ],
  196. imports: [
  197. BrowserModule,
  198. StoreModule.forRoot(reducers),
  199. effects,
  200. StoreDevtoolsModule.instrument()
  201. ],
  202. providers: [],
  203. bootstrap: [AppComponent]
  204. })
  205. export class AppModule { }
  206. /////////////////////////////////////////////
  207.  
  208. class EmailService { };
  209. class MandrillService extends EmailService { };
  210. class SendGridService extends EmailService { };
  211.  
  212. let injector = ReflectiveInjector.resolveAndCreate([
  213. { provide: EmailService, useClass: SendGridService }
  214. ]);
  215.  
  216. let emailService = injector.get(EmailService);
  217. console.log(emailService);
  218.  
  219. //////////////////////////////////////////
Add Comment
Please, Sign In to add comment