Last active
October 24, 2020 14:46
-
-
Save anthonyjoeseph/24b7114c7e81fc8c057a01d2114b274b to your computer and use it in GitHub Desktop.
Attempt at redux-immutable integration with @morphic-ts/adt
This file contains 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 { makeADT, ADTType, ofType } from '@morphic-ts/adt' | |
import { combineReducers } from 'redux-immutable'; | |
import { Action, createStore, Reducer } from 'redux'; | |
import * as Immutable from 'immutable' | |
interface Ignition { type: 'Ignition' } | |
interface Accelerate { type: 'Accelerate' } | |
interface Brake { type: 'Brake' } | |
const AppAction = makeADT('type')({ | |
Ignition: ofType<Ignition>(), | |
Accelerate: ofType<Accelerate>(), | |
Brake: ofType<Brake>(), | |
}) | |
type AppAction = ADTType<typeof AppAction> | |
// type AppAction = Ignition | Accelerate | Brake | |
const initialState = Immutable.Record<{ vehicle: string, other: number }>({ | |
vehicle: 'Idle', | |
other: 0 | |
}) | |
const morphicReducer: Reducer<string, AppAction> = AppAction.createReducer<string>('Idle')({ | |
Ignition: (a: Ignition) => (s: string) => 'Starting Up', | |
Accelerate: (a: Accelerate) => (s: string) => 'Accelerating', | |
Brake: (a: Brake) => (s: string) => 'Braking', | |
}) | |
const otherReducer: Reducer<number, AppAction> = (s?: number, a: AppAction): number => s ? s + 3 : 0 | |
const rootReducer: Reducer<{ vehicle: string; other: number; }, Action> = combineReducers({ | |
vehicle: morphicReducer, | |
other: otherReducer, | |
}) | |
const store = createStore(rootReducer, initialState()) |
Ah yes it typechecks but I never actually ran it. I didn't know that about combineReducers
. So glad it worked!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So by some miracle I was able to integrate the Morphic generated reducer with Immutables
combineReducer
's and everything "Just Worked"! At least I was able to get dispatch ButtonClicked action to fire off a console.log in the reducer so woo!