webamp/js/actionCreators/equalizer.ts
Jordan Eldredge 67441f3c1b Change how actions are typed
If we want to use the new version of react-redux we need to be compatible with their types.

Sadly it does not seem to be possible to create a Dispatchable type which can be eiher a plain action or a thunk and still be compatible with react-redux's types becuase they use redux's Store type under the hood which assumes you have an object with a string type property.
2019-05-01 22:29:10 -07:00

70 lines
1.5 KiB
TypeScript

import { BANDS } from "../constants";
import {
SET_EQ_ON,
SET_EQ_OFF,
SET_BAND_VALUE,
SET_EQ_AUTO,
} from "../actionTypes";
import { Band, Thunk, Action } from "../types";
const BAND_SNAP_DISTANCE = 5;
const BAND_MID_POINT_VALUE = 50;
function _snapBandValue(value: number): number {
if (
value < BAND_MID_POINT_VALUE + BAND_SNAP_DISTANCE &&
value > BAND_MID_POINT_VALUE - BAND_SNAP_DISTANCE
) {
return BAND_MID_POINT_VALUE;
}
return value;
}
export function setEqBand(band: Band, value: number): Action {
return { type: SET_BAND_VALUE, band, value: _snapBandValue(value) };
}
function _setEqTo(value: number): Thunk {
return dispatch => {
Object.values(BANDS).forEach(band => {
dispatch({
type: SET_BAND_VALUE,
value,
band: band,
});
});
};
}
export function setEqToMax(): Thunk {
return _setEqTo(100);
}
export function setEqToMid(): Thunk {
return _setEqTo(50);
}
export function setEqToMin(): Thunk {
return _setEqTo(0);
}
export function setPreamp(value: number): Action {
return { type: SET_BAND_VALUE, band: "preamp", value: _snapBandValue(value) };
}
export function toggleEq(): Thunk {
return (dispatch, getState) => {
if (getState().equalizer.on) {
dispatch({ type: SET_EQ_OFF });
} else {
dispatch({ type: SET_EQ_ON });
}
};
}
export function toggleEqAuto(): Thunk {
return (dispatch, getState) => {
dispatch({ type: SET_EQ_AUTO, value: !getState().equalizer.auto });
};
}