Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Improvement/events perf #362

Merged
merged 6 commits into from
Apr 17, 2018
Merged
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
36 changes: 28 additions & 8 deletions src/victory-util/events.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
assign, extend, merge, partial, isEmpty, isFunction, without, pickBy, uniq, includes
assign, partial, isEmpty, isFunction, without, pickBy, uniq, includes
} from "lodash";

export default {
Expand Down Expand Up @@ -92,6 +92,9 @@ export default {

// returns all eventKeys to modify for a targeted childName
const getKeys = (childName) => {
if (target === "parent") {
return "parent";
}
if (eventReturn.eventKey === "all") {
return baseProps[childName] ?
without(Object.keys(baseProps[childName]), "parent") :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: we're using both lodash.keys and Object.keys in this file. idk which is faster, but we might want to go with just one

Expand All @@ -115,15 +118,32 @@ export default {
assign({}, mutationTargetProps, mutationTargetState), baseProps
);
const childState = baseState[childName] || {};

const filterState = (state) => {
const stateTargets = Object.keys(state[key]);
if (target === "parent" || stateTargets.length === 1 && stateTargets[0] === target) {
delete state[key];
return state;
} else if (state[key] && state[key][target]) {
delete state[key][target];
return state;
}
return state;
};

const extendState = (state) => {
return target === "parent" ?
extend(state[key], mutatedProps) : extend(state[key], { [target]: mutatedProps });
assign(state, { [key]: assign(state[key], mutatedProps) }) :
assign(state, { [key]: assign(state[key], { [target]: mutatedProps }) });
};

const updateState = (state) => {
return mutatedProps ? extendState(state) : filterState(state);
};

return childName !== undefined && childName !== null ?
extend(baseState, {
[childName]: extend(childState, { [key]: extendState(childState) })
}) :
extend(baseState, { [key]: extendState(baseState) });
assign(baseState, { [childName]: updateState(childState) }) :
updateState(baseState);
};

// returns entire mutated state for a given childName
Expand All @@ -148,7 +168,7 @@ export default {
const parseEventReturn = (eventReturn, eventKey) => {
return Array.isArray(eventReturn) ?
eventReturn.reduce((memo, props) => {
memo = merge({}, memo, parseEvent(props, eventKey));
memo = assign({}, memo, parseEvent(props, eventKey));
return memo;
}, {}) :
parseEvent(eventReturn, eventKey);
Expand Down Expand Up @@ -321,7 +341,7 @@ export default {
return keyMutations.reduce((memo, curr) => {
const mutationFunction = curr && isFunction(curr.mutation) ? curr.mutation : () => undefined;
const currentMutation = mutationFunction(assign({}, baseProps, baseState));
return merge({}, memo, currentMutation);
return assign({}, memo, currentMutation);
}, {});
},

Expand Down