Skip to content

Commit

Permalink
feat: make DeepImmutable optional in input/prev state
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The input/prev state argument in createHandlerMap (AKA handleAction) and returning
reducer of createReducer will not obligated to be Immutable data structure by Deox which in turn can
lead to changes in handler's and reducer's return type.

closes #58 and #55
  • Loading branch information
Mohammad Hasani authored and the-dr-lazy committed Jun 15, 2019
1 parent 39509ea commit 0f35d7f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/__snapshots__/create-reducer.dts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

exports[`createReducer counter scenario (type) should match snapshot: createReducer counter scenario 1`] = `"(state: number | undefined, action: { type: \\"INCREMENT\\"; } | { type: \\"DECREMENT\\"; } | { type: \\"RESET\\"; payload: number; }) => number"`;

exports[`createReducer immutable items list scenario (type) should match snapshot: createReducer immutable items list scenario 1`] = `"(state: DeepImmutableArray<DeepImmutableObject<Item>> | undefined, action: { type: \\"IDENTITY\\"; } | { type: \\"ADD_ITEM\\"; payload: string; } | { type: \\"REMOVE_ITEM\\"; payload: string; }) => DeepImmutableArray<DeepImmutableObject<Item>> | DeepImmutableObject<DeepImmutableObject<Item>>[]"`;
exports[`createReducer immutable items list scenario (type) should match snapshot: createReducer immutable items list scenario 1`] = `"(state: DeepImmutableArray<Item> | undefined, action: { type: \\"IDENTITY\\"; } | { type: \\"ADD_ITEM\\"; payload: string; } | { type: \\"REMOVE_ITEM\\"; payload: string; }) => DeepImmutableArray<Item> | DeepImmutableObject<Item>[]"`;
exports[`createReducer mutable items list scenario (type) should match snapshot: createReducer mutable items list scenario 1`] = `"(state: DeepImmutableArray<Item> | undefined, action: { type: \\"IDENTITY\\"; } | { type: \\"ADD_ITEM\\"; payload: string; } | { type: \\"REMOVE_ITEM\\"; payload: string; }) => DeepImmutableArray<Item> | DeepImmutableObject<Item>[]"`;
exports[`createReducer mutable items list scenario (type) should match snapshot: createReducer mutable items list scenario 1`] = `"(state: Item[] | undefined, action: { type: \\"IDENTITY\\"; } | { type: \\"ADD_ITEM\\"; payload: string; } | { type: \\"REMOVE_ITEM\\"; payload: string; }) => Item[]"`;
9 changes: 3 additions & 6 deletions src/create-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
InferNextStateFromHandlerMap,
} from './create-handler-map'
import { merge } from './utils'
import { DeepImmutable } from './types'

/**
* Reducer factory
Expand All @@ -19,17 +18,15 @@ import { DeepImmutable } from './types'
*/
export function createReducer<
TPrevState,
THandlerMap extends HandlerMap<DeepImmutable<TPrevState>, any, any>
THandlerMap extends HandlerMap<TPrevState, any, any>
>(
defaultState: TPrevState,
handlerMapsCreator: (
handle: CreateHandlerMap<DeepImmutable<TPrevState>>
) => THandlerMap[]
handlerMapsCreator: (handle: CreateHandlerMap<TPrevState>) => THandlerMap[]
) {
const handlerMap = merge(...handlerMapsCreator(createHandlerMap))

return (
state = <DeepImmutable<TPrevState>>defaultState,
state = defaultState,
action: InferActionFromHandlerMap<THandlerMap>
): InferNextStateFromHandlerMap<THandlerMap> => {
const handler = handlerMap[action.type]
Expand Down

0 comments on commit 0f35d7f

Please sign in to comment.