Skip to content

Commit

Permalink
Address PR Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xenown committed Feb 24, 2023
1 parent f4395d8 commit fdbe7bf
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 43 deletions.
10 changes: 5 additions & 5 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const FILTER_REGEX = new RegExp(

/** ----- Application Interfaces ----- */

export interface Filters {
export interface IFiltersMap {
[key: string]: IFilter;
} // key is the same id as whats in IFilter

Expand All @@ -71,12 +71,12 @@ export interface IState {
isAutoPreAmpOn: boolean;
isGraphViewOn: boolean;
preAmp: number;
filters: Filters;
filters: IFiltersMap;
}

export interface IPreset {
preAmp: number;
filters: Filters;
filters: IFiltersMap;
}

/** ----- Default Values ----- */
Expand All @@ -99,8 +99,8 @@ export const getDefaultFilterWithId = (): IFilter => {
};
};

const getDefaultFilters = (): Filters => {
const filters: Filters = {};
const getDefaultFilters = (): IFiltersMap => {
const filters: IFiltersMap = {};
FIXED_FREQUENCIES.forEach((f) => {
const filter: IFilter = { ...getDefaultFilterWithId(), frequency: f };
filters[filter.id] = filter;
Expand Down
8 changes: 4 additions & 4 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
Filters,
IFiltersMap,
IFilter,
MAX_FREQUENCY,
MIN_FREQUENCY,
Expand Down Expand Up @@ -27,10 +27,10 @@ export const computeAvgFreq = (
export const isRestrictedPresetName = (newName: string) =>
RESERVED_FILE_NAMES_SET.has(newName.toUpperCase());

export const cloneFilters = (filters: Filters) => {
const filtersClone: Filters = {};
export const cloneFilters = (filters: IFiltersMap) => {
const filtersClone: IFiltersMap = {};
Object.entries(filters).forEach(([id, filter]) => {
filtersClone[id] = filter;
filtersClone[id] = { ...filter };
});
return filtersClone;
};
32 changes: 14 additions & 18 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { app, BrowserWindow, ipcMain, shell } from 'electron';
import log from 'electron-log';
import { autoUpdater } from 'electron-updater';
import path from 'path';
import { uid } from 'uid';
import fs from 'fs';
import { exec } from 'child_process';
import {
Expand All @@ -34,6 +33,7 @@ import {
FilterTypeEnum,
IState,
IPreset,
IFilter,
MAX_FREQUENCY,
MAX_GAIN,
MAX_NUM_FILTERS,
Expand Down Expand Up @@ -199,7 +199,7 @@ const handleUpdate = async (
return handleUpdateHelper<void>(event, channel, undefined);
};

const checkFilterIdExistence = (
const doesFilterIdExist = (
event: Electron.IpcMainEvent,
channel: ChannelEnum,
filterId: string
Expand Down Expand Up @@ -422,7 +422,7 @@ ipcMain.on(ChannelEnum.GET_FILTER_GAIN, async (event, arg) => {
const filterId = arg[0];

// Filter id must exist
if (!checkFilterIdExistence(event, channel, filterId)) {
if (!doesFilterIdExist(event, channel, filterId)) {
return;
}

Expand All @@ -438,7 +438,7 @@ ipcMain.on(ChannelEnum.SET_FILTER_GAIN, async (event, arg) => {
const gain = parseFloat(arg[1]) || 0;

// Filter id must exist
if (!checkFilterIdExistence(event, channel, filterId)) {
if (!doesFilterIdExist(event, channel, filterId)) {
return;
}

Expand All @@ -456,7 +456,7 @@ ipcMain.on(ChannelEnum.GET_FILTER_FREQUENCY, async (event, arg) => {
const filterId = arg[0];

// Filter id must exist
if (!checkFilterIdExistence(event, channel, filterId)) {
if (!doesFilterIdExist(event, channel, filterId)) {
return;
}

Expand All @@ -472,7 +472,7 @@ ipcMain.on(ChannelEnum.SET_FILTER_FREQUENCY, async (event, arg) => {
const frequency = parseInt(arg[1], 10) || 0;

// Filter id must exist
if (!checkFilterIdExistence(event, channel, filterId)) {
if (!doesFilterIdExist(event, channel, filterId)) {
return;
}

Expand All @@ -490,7 +490,7 @@ ipcMain.on(ChannelEnum.GET_FILTER_QUALITY, async (event, arg) => {
const filterId = arg[0];

// Filter id must exist
if (!checkFilterIdExistence(event, channel, filterId)) {
if (!doesFilterIdExist(event, channel, filterId)) {
return;
}

Expand All @@ -506,7 +506,7 @@ ipcMain.on(ChannelEnum.SET_FILTER_QUALITY, async (event, arg) => {
const quality = parseFloat(arg[1]) || 0;

// Filter id must exist
if (!checkFilterIdExistence(event, channel, filterId)) {
if (!doesFilterIdExist(event, channel, filterId)) {
return;
}

Expand All @@ -524,7 +524,7 @@ ipcMain.on(ChannelEnum.GET_FILTER_TYPE, async (event, arg) => {
const filterId = arg[0];

// Filter id must exist
if (!checkFilterIdExistence(event, channel, filterId)) {
if (!doesFilterIdExist(event, channel, filterId)) {
return;
}

Expand All @@ -540,7 +540,7 @@ ipcMain.on(ChannelEnum.SET_FILTER_TYPE, async (event, arg) => {
const filterType = arg[1];

// Filter id must exist
if (!checkFilterIdExistence(event, channel, filterId)) {
if (!doesFilterIdExist(event, channel, filterId)) {
return;
}

Expand Down Expand Up @@ -575,13 +575,9 @@ ipcMain.on(ChannelEnum.ADD_FILTER, async (event, arg) => {
return;
}

const filterId = uid(8);
state.filters[filterId] = {
...getDefaultFilterWithId(),
id: filterId,
frequency,
};
await handleUpdateHelper(event, channel, filterId);
const newFilter: IFilter = { ...getDefaultFilterWithId(), frequency };
state.filters[newFilter.id] = newFilter;
await handleUpdateHelper(event, channel, newFilter.id);
});

ipcMain.on(ChannelEnum.REMOVE_FILTER, async (event, arg) => {
Expand All @@ -595,7 +591,7 @@ ipcMain.on(ChannelEnum.REMOVE_FILTER, async (event, arg) => {
}

// Filter id must exist
if (!checkFilterIdExistence(event, channel, filterId)) {
if (!doesFilterIdExist(event, channel, filterId)) {
return;
}

Expand Down
9 changes: 2 additions & 7 deletions src/renderer/MainContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useAquaContext } from './utils/AquaContext';
import './styles/MainContent.scss';
import AddSliderDivider from './components/AddSliderDivider';
import Spinner from './icons/Spinner';
import { sortHelper } from './utils/utils';

const MainContent = () => {
const { filters, isLoading } = useAquaContext();
Expand All @@ -21,13 +22,7 @@ const MainContent = () => {
);

// Obtain a visually sorted list of the filters
const visualSort = Object.values(filters).sort(
(a, b) =>
a.frequency - b.frequency ||
a.gain - b.gain ||
a.quality - b.quality ||
a.type.localeCompare(b.type)
);
const visualSort = Object.values(filters).sort(sortHelper);

// Compute a mapping from a filter id to its sorted index
const map: { [key: string]: number } = {};
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/graph/FrequencyResponseChart.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Filters, IFilter } from 'common/constants';
import { IFiltersMap, IFilter } from 'common/constants';
import {
useCallback,
useEffect,
Expand Down Expand Up @@ -47,7 +47,7 @@ const FrequencyResponseChart = () => {
preAmp,
setPreAmp,
} = useAquaContext();
const prevFilters = useRef<Filters>({});
const prevFilters = useRef<IFiltersMap>({});
const prevFilterLines = useRef<IChartLineDataPointsById>({});

const { chartData, autoPreAmpValue }: IGraphData = useMemo(() => {
Expand All @@ -64,7 +64,7 @@ const FrequencyResponseChart = () => {
};

// New filters have no previous data
if (!prevFilters.current[filter.id]) {
if (!(filter.id in prevFilters.current)) {
updatedFilterLines[filter.id] = getFilterLineData(filter);
return;
}
Expand Down
12 changes: 7 additions & 5 deletions src/renderer/utils/AquaContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
FilterTypeEnum,
getDefaultFilterWithId,
getDefaultState,
Filters,
IFiltersMap,
IState,
} from '../../common/constants';
import { ErrorDescription } from '../../common/errors';
Expand All @@ -34,7 +34,7 @@ type NumericalFilterAction =
| FilterActionEnum.QUALITY;

export type FilterAction =
| { type: FilterActionEnum.INIT; filters: Filters }
| { type: FilterActionEnum.INIT; filters: IFiltersMap }
| { type: NumericalFilterAction; id: string; newValue: number }
| { type: FilterActionEnum.TYPE; id: string; newValue: FilterTypeEnum }
| { type: FilterActionEnum.ADD; id: string; frequency: number }
Expand All @@ -56,10 +56,13 @@ export interface IAquaContext extends IState {

const AquaContext = createContext<IAquaContext | undefined>(undefined);

type IFilterReducer = (filters: Filters, action: FilterAction) => Filters;
type IFilterReducer = (
filters: IFiltersMap,
action: FilterAction
) => IFiltersMap;

const filterReducer: IFilterReducer = (
filters: Filters,
filters: IFiltersMap,
action: FilterAction
) => {
switch (action.type) {
Expand All @@ -72,7 +75,6 @@ const filterReducer: IFilterReducer = (
}
case FilterActionEnum.GAIN: {
const filtersCloned = cloneFilters(filters);
console.log(filters);
filtersCloned[action.id].gain = action.newValue;
return filtersCloned;
}
Expand Down
6 changes: 5 additions & 1 deletion src/renderer/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export const clamp = (num: number, min: number, max: number) => {
return Math.min(Math.max(num, min), max);
};

export const sortHelper = (a: IFilter, b: IFilter) => a.frequency - b.frequency;
export const sortHelper = (a: IFilter, b: IFilter) =>
a.frequency - b.frequency ||
a.gain - b.gain ||
a.quality - b.quality ||
a.type.localeCompare(b.type);

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#sequence_generator_range
export const range = (start: number, stop: number, step: number) =>
Expand Down

0 comments on commit fdbe7bf

Please sign in to comment.