Last active
February 15, 2021 01:02
-
-
Save ducin/f8d4b1fffcc84c1bf16876dd8b6876d9 to your computer and use it in GitHub Desktop.
Revisions
-
Tomasz Ducin revised this gist
May 26, 2019 . 6 changed files with 84 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ import React from 'react' import { connect } from 'react-redux' import { AppState } from './state'; export const ReduxComponent = connect( (state: AppState) => ({ count: state.count }), (dispatch) => ({ // }))((props: { count: number }) => <span>{props.count}</span>) 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ export const INC = "INC" 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ import { Observable, empty, interval } from "rxjs"; import { ActionsObservable, ofType, combineEpics, StateObservable } from "redux-observable"; import { switchMapTo, tap, mapTo, withLatestFrom } from "rxjs/operators"; import { Actions, Inc } from "./actions"; import { AppState } from "./state"; export const logIncEpic = (action$: ActionsObservable<Actions>, state$: StateObservable<AppState>) => action$.pipe( ofType("INC"), withLatestFrom(state$), tap(([action, state]) => console.log(action.type, state)), switchMapTo(empty()), ) export const each5secEpic = (action$: ActionsObservable<Actions>) => interval(5000).pipe( mapTo(Inc()) ) export const rootEpic = combineEpics( logIncEpic, each5secEpic ) 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ import { AppState } from './state'; import { Actions } from './actions'; const initialState: AppState = { count: 1 } export const rootReducer = (state = initialState, action: Actions) => { switch(action.type) { case "INC": return { ...state, count: state.count + 1 } default: return state } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ export type AppState = { count: number } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ import { createStore, applyMiddleware, Store } from 'redux' import { composeWithDevTools } from 'redux-devtools-extension' import thunk from 'redux-thunk' import { createEpicMiddleware, EpicMiddleware } from 'redux-observable' import { rootReducer } from './reducers' import { rootEpic } from './epics' const epicMiddleware: EpicMiddleware<Actions, Actions, AppState> = createEpicMiddleware(); const middleware = [thunk, epicMiddleware] import { AppState } from './state'; import { Actions } from './actions'; const composeEnhancers = composeWithDevTools({ name: "IT Corpo React App" }); export const getStore = (): Store<AppState> => { const store = createStore(rootReducer, composeEnhancers( applyMiddleware(...middleware), // other store enhancers... )) epicMiddleware.run(rootEpic); return store; } -
ducin created this gist
May 26, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ import { INC } from './constants'; export const Inc = () => ({ type: INC as typeof INC }) export type Actions = | ReturnType<typeof Inc>