Skip to content

Instantly share code, notes, and snippets.

@ducin
Last active February 15, 2021 01:02
Show Gist options
  • Select an option

  • Save ducin/f8d4b1fffcc84c1bf16876dd8b6876d9 to your computer and use it in GitHub Desktop.

Select an option

Save ducin/f8d4b1fffcc84c1bf16876dd8b6876d9 to your computer and use it in GitHub Desktop.

Revisions

  1. Tomasz Ducin revised this gist May 26, 2019. 6 changed files with 84 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions components.tsx
    Original 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>)
    1 change: 1 addition & 0 deletions constants.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    export const INC = "INC"
    24 changes: 24 additions & 0 deletions epics.ts
    Original 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
    )
    16 changes: 16 additions & 0 deletions reducers.ts
    Original 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
    }
    }
    3 changes: 3 additions & 0 deletions state.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    export type AppState = {
    count: number
    }
    28 changes: 28 additions & 0 deletions store.ts
    Original 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;
    }
  2. ducin created this gist May 26, 2019.
    8 changes: 8 additions & 0 deletions actions.ts
    Original 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>