Typings cleanup and improvements

(cherry picked from commit b2c43fb2a67965d68d3d35b72302b0cddb5aca23)
This commit is contained in:
Mark McDowall
2023-04-04 09:21:34 -07:00
committed by Bogdan
parent 5764950b10
commit 4bfaab4b21
45 changed files with 468 additions and 319 deletions

View File

@@ -108,7 +108,7 @@ function sort(items, state) {
return _.orderBy(items, clauses, orders);
}
function createCustomFiltersSelector(type, alternateType) {
export function createCustomFiltersSelector(type, alternateType) {
return createSelector(
(state) => state.customFilters.items,
(customFilters) => {

View File

@@ -1,10 +1,10 @@
import { createSelector } from 'reselect';
import createIndexerSelector from './createIndexerSelector';
import { createIndexerSelectorForHook } from './createIndexerSelector';
function createIndexerAppProfileSelector(indexerId) {
return createSelector(
(state) => state.settings.appProfiles.items,
createIndexerSelector(indexerId),
createIndexerSelectorForHook(indexerId),
(appProfiles, indexer = {}) => {
return appProfiles.find((profile) => {
return profile.id === indexer.appProfileId;

View File

@@ -1,22 +1,22 @@
import { createSelector } from 'reselect';
function createIndexerSelector(id) {
if (id == null) {
return createSelector(
(state, { indexerId }) => indexerId,
(state) => state.indexers.itemMap,
(state) => state.indexers.items,
(indexerId, itemMap, allIndexers) => {
return allIndexers[itemMap[indexerId]];
}
);
}
export function createIndexerSelectorForHook(indexerId) {
return createSelector(
(state) => state.indexers.itemMap,
(state) => state.indexers.items,
(itemMap, allIndexers) => {
return allIndexers[itemMap[id]];
return indexerId ? allIndexers[itemMap[indexerId]]: undefined;
}
);
}
function createIndexerSelector() {
return createSelector(
(state, { indexerId }) => indexerId,
(state) => state.indexers.itemMap,
(state) => state.indexers.items,
(indexerId, itemMap, allIndexers) => {
return allIndexers[itemMap[indexerId]];
}
);
}

View File

@@ -1,5 +0,0 @@
const scrollPositions = {
indexerIndex: 0
};
export default scrollPositions;

View File

@@ -0,0 +1,5 @@
const scrollPositions: Record<string, number> = {
indexerIndex: 0,
};
export default scrollPositions;

View File

@@ -1,28 +0,0 @@
const thunks = {};
function identity(payload) {
return payload;
}
export function createThunk(type, identityFunction = identity) {
return function(payload = {}) {
return function(dispatch, getState) {
const thunk = thunks[type];
if (thunk) {
return thunk(getState, identityFunction(payload), dispatch);
}
throw Error(`Thunk handler has not been registered for ${type}`);
};
};
}
export function handleThunks(handlers) {
const types = Object.keys(handlers);
types.forEach((type) => {
thunks[type] = handlers[type];
});
}

View File

@@ -0,0 +1,39 @@
import { Dispatch } from 'redux';
import AppState from 'App/State/AppState';
type GetState = () => AppState;
type Thunk = (
getState: GetState,
identityFn: never,
dispatch: Dispatch
) => unknown;
const thunks: Record<string, Thunk> = {};
function identity<T, TResult>(payload: T): TResult {
return payload as unknown as TResult;
}
export function createThunk(type: string, identityFunction = identity) {
return function <T>(payload?: T) {
return function (dispatch: Dispatch, getState: GetState) {
const thunk = thunks[type];
if (thunk) {
const finalPayload = payload ?? {};
return thunk(getState, identityFunction(finalPayload), dispatch);
}
throw Error(`Thunk handler has not been registered for ${type}`);
};
};
}
export function handleThunks(handlers: Record<string, Thunk>) {
const types = Object.keys(handlers);
types.forEach((type) => {
thunks[type] = handlers[type];
});
}