Skip to content

Commit

Permalink
Update normalizeFrequencies flag via redux actions
Browse files Browse the repository at this point in the history
Upon initial parsing of the frequencies JSON as well as frequency data updates we may set redux→controls→normalizeFrequencies→false. This commit modifies the LOAD_FREQUENCIES and FREQUENCY_MATRIX actions to pass this information to the reducer, rather than updating the redux state directly from within the actions.

I couldn't find any bugs caused by the previous implementation, but this change is more in line with suggested behaviour and should help future-proof work here.
  • Loading branch information
jameshadfield committed Mar 9, 2021
1 parent bbef8f8 commit 0de2197
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
20 changes: 7 additions & 13 deletions src/actions/frequencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { computeMatrixFromRawData, checkIfNormalizableFromRawData, processFreque

export const loadFrequencies = (json) => (dispatch, getState) => {
const { tree, controls } = getState();
const { data, pivots, matrix, projection_pivot, normalizeFrequencies } = processFrequenciesJSON(json, tree, controls);
dispatch({
type: types.LOAD_FREQUENCIES,
frequencies: {loaded: true, version: 1, ...processFrequenciesJSON(json, tree, controls)}
frequencies: {loaded: true, version: 1, data, pivots, matrix, projection_pivot},
normalizeFrequencies
});
};

Expand All @@ -23,16 +25,8 @@ const updateFrequencyData = (dispatch, getState) => {
return;
}

const allowNormalization = checkIfNormalizableFromRawData(
frequencies.data,
frequencies.pivots,
tree.nodes,
tree.visibility
);

if (!allowNormalization) {
controls.normalizeFrequencies = false;
}
const normalizeFrequencies = controls.normalizeFrequencies &&
checkIfNormalizableFromRawData(frequencies.data, frequencies.pivots, tree.nodes, tree.visibility);

const matrix = computeMatrixFromRawData(
frequencies.data,
Expand All @@ -41,10 +35,10 @@ const updateFrequencyData = (dispatch, getState) => {
tree.visibility,
controls.colorScale,
controls.colorBy,
controls.normalizeFrequencies
normalizeFrequencies
);
timerEnd("updateFrequencyData");
dispatch({type: types.FREQUENCY_MATRIX, matrix});
dispatch({type: types.FREQUENCY_MATRIX, matrix, normalizeFrequencies});
};

/* debounce works better than throttle, as it _won't_ update while events are still coming in (e.g. dragging the date slider) */
Expand Down
2 changes: 2 additions & 0 deletions src/reducers/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ const Controls = (state = getDefaultControlsState(), action) => {
case types.TOGGLE_TRANSMISSION_LINES:
return Object.assign({}, state, { showTransmissionLines: action.data });

case types.LOAD_FREQUENCIES:
return {...state, normalizeFrequencies: action.normalizeFrequencies};
case types.FREQUENCY_MATRIX: {
if (Object.hasOwnProperty.call(action, "normalizeFrequencies")) {
return Object.assign({}, state, { normalizeFrequencies: action.normalizeFrequencies });
Expand Down
17 changes: 5 additions & 12 deletions src/util/processFrequencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,8 @@ export const processFrequenciesJSON = (rawJSON, tree, controls) => {
});
});

const allowNormalization = checkIfNormalizableFromRawData(
data,
pivots,
tree.nodes,
tree.visibility
);

if (!allowNormalization) {
controls.normalizeFrequencies = false;
}
const normalizeFrequencies = controls.normalizeFrequencies &&
checkIfNormalizableFromRawData(data, pivots, tree.nodes, tree.visibility);

const matrix = computeMatrixFromRawData(
data,
Expand All @@ -134,12 +126,13 @@ export const processFrequenciesJSON = (rawJSON, tree, controls) => {
tree.visibility,
controls.colorScale,
controls.colorBy,
controls.normalizeFrequencies
normalizeFrequencies
);
return {
data,
pivots,
matrix,
projection_pivot
projection_pivot,
normalizeFrequencies
};
};

0 comments on commit 0de2197

Please sign in to comment.