Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify logging middleware #40

Merged
merged 6 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions src/combineReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,31 @@ import { ReducerMap } from "./ReducerMap";

const hasOwnProperty = Object.prototype.hasOwnProperty;

function reduce<S>(state: S, action: Action, reducerMap: ReducerMap<S>) {
function reduce<S>(
state: S,
action: Action,
reducerMap: ReducerMap<S>,
reducerKeys: (keyof S)[],
) {
let newState: S | undefined = undefined;

for (const key in reducerMap) {
if (!hasOwnProperty.call(reducerMap, key)) {
continue;
}

for (let i = 0; i < reducerKeys.length; i++) {
const key = reducerKeys[i];
const statePart = state[key];
const newStatePart = reducerMap[key](statePart, action);
if (newStatePart !== statePart) {
if (!newState) {
newState = __assign(Object.create(Object.getPrototypeOf(state)), state) as S;
newState = __assign(
Object.create(Object.getPrototypeOf(state)),
state,
) as S;
}

newState[key] = newStatePart;
}
}

return newState !== undefined
? newState
: state;
return newState !== undefined ? newState : state;
}

function verifySameShape<S>(state: S, reducerMap: ReducerMap<S>) {
Expand All @@ -52,7 +55,9 @@ function verifySameShape<S>(state: S, reducerMap: ReducerMap<S>) {
hasOwnProperty.call(state, key) &&
!hasOwnProperty.call(reducerMap, key)
) {
throw new Error(`mismatched shapes in combineReducers(): reducers missing '${key}'`);
throw new Error(
`mismatched shapes in combineReducers(): reducers missing '${key}'`,
);
}
}

Expand All @@ -61,7 +66,9 @@ function verifySameShape<S>(state: S, reducerMap: ReducerMap<S>) {
hasOwnProperty.call(reducerMap, key) &&
!hasOwnProperty.call(state, key)
) {
throw new Error(`mismatched shapes in combineReducers(): state missing '${key}'`);
throw new Error(
`mismatched shapes in combineReducers(): state missing '${key}'`,
);
}
}
}
Expand All @@ -86,17 +93,17 @@ function verifySameShape<S>(state: S, reducerMap: ReducerMap<S>) {
*/
export function combineReducers<S>(reducerMap: ReducerMap<S>): Reducer<S> {
let hasVerifiedShape = false;
const reducerKeys = Object.keys(reducerMap) as (keyof S)[];

return (state: S, action: Action) => {
if (
typeof process === "undefined" ||
process.env.NODE_ENV !== "production"
) {
if (
state === undefined ||
state === null ||
typeof state !== "object"
) {
throw new Error("combineReducers() requires object state; received " + state);
if (state === undefined || state === null || typeof state !== "object") {
throw new Error(
"combineReducers() requires object state; received " + state,
);
}

if (!hasVerifiedShape) {
Expand All @@ -105,6 +112,6 @@ export function combineReducers<S>(reducerMap: ReducerMap<S>): Reducer<S> {
}
}

return reduce(state, action, reducerMap);
return reduce(state, action, reducerMap, reducerKeys);
};
}
166 changes: 0 additions & 166 deletions src/internal/prettyPrintPayload.ts

This file was deleted.

28 changes: 0 additions & 28 deletions src/internal/utils/formatLogTimestamp.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/internal/utils/isObject.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/internal/utils/leftPad.ts

This file was deleted.

23 changes: 8 additions & 15 deletions src/internal/utils/shallowEqualsPartial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,15 @@
*/

const hasOwnProperty = Object.prototype.hasOwnProperty;

export function shallowEqualsPartial(state: any, partial: any): boolean {
if (state === partial) {
return true;
}

for (const key in partial) {
if (!hasOwnProperty.call(partial, key)) {
continue;
}

if (!hasOwnProperty.call(state, key)) {
return false;
}

if (partial[key] !== state[key]) {
const partialKeys = Object.keys(partial);
for (let i = 0; i < partialKeys.length; i++) {
const key = partialKeys[i];
const partialValue = partial[key];
if (
state[key] !== partialValue ||
(partialValue === undefined && !hasOwnProperty.call(state, key))
) {
return false;
}
}
Expand Down
Loading