-
-
Notifications
You must be signed in to change notification settings - Fork 856
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b658851
commit fdf4be6
Showing
3 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import {assert, _} from "spec.ts" | ||
import produce, { | ||
produce as produce2, | ||
applyPatches, | ||
Patch, | ||
nothing, | ||
Draft, | ||
Immutable | ||
} from "../src/" | ||
import * as redux from "redux" | ||
|
||
export interface State { | ||
counter: number | ||
} | ||
|
||
export interface Action { | ||
type: string | ||
payload: number | ||
} | ||
|
||
export const initialState: State = { | ||
counter: 0 | ||
} | ||
|
||
/// =============== Actions | ||
|
||
export function addToCounter(addNumber: number) { | ||
return { | ||
type: "ADD_TO_COUNTER", | ||
payload: addNumber | ||
} | ||
} | ||
|
||
export function subFromCounter(subNumber: number) { | ||
return { | ||
type: "SUB_FROM_COUNTER", | ||
payload: subNumber | ||
} | ||
} | ||
|
||
export const reduceCounterProducer = ( | ||
state: State = initialState, | ||
action: Action | ||
) => | ||
produce(state, draftState => { | ||
switch (action.type) { | ||
case "ADD_TO_COUNTER": | ||
draftState.counter += action.payload | ||
break | ||
case "SUB_FROM_COUNTER": | ||
draftState.counter -= action.payload | ||
break | ||
} | ||
}) | ||
|
||
export const reduceCounterCurriedProducer = produce( | ||
(draftState: Draft<State>, action: Action) => { | ||
switch (action.type) { | ||
case "ADD_TO_COUNTER": | ||
draftState.counter += action.payload | ||
break | ||
case "SUB_FROM_COUNTER": | ||
draftState.counter -= action.payload | ||
break | ||
} | ||
}, | ||
initialState | ||
) | ||
|
||
/// =============== Reducers | ||
|
||
export const reduce = redux.combineReducers({ | ||
counterReducer: reduceCounterProducer | ||
}) | ||
|
||
export const curredReduce = redux.combineReducers({ | ||
counterReducer: reduceCounterCurriedProducer | ||
}) | ||
|
||
// reducing the current state to get the next state! | ||
// console.log(reduce(initialState, addToCounter(12)); | ||
|
||
// ================ store | ||
|
||
export const store = redux.createStore(reduce) | ||
export const curriedStore = redux.createStore(curredReduce) | ||
|
||
it("#470 works with Redux combine reducers", () => { | ||
assert( | ||
store.getState().counterReducer, | ||
_ as { | ||
counter: number | ||
} | ||
) | ||
assert( | ||
curriedStore.getState().counterReducer, | ||
_ as { | ||
readonly counter: number | ||
} | ||
) | ||
}) |
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
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