Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: typed cascade reducer #426

Merged
merged 10 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
46 changes: 30 additions & 16 deletions client/src/reducers/cascade.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export default function cascadeReducers(arg: any) {
import { AnyAction } from "redux";
import type { RootState } from ".";

export type ReducerFunction = (
prevStateForKey: any,
action: AnyAction,
nexState?: RootState,
atarashansky marked this conversation as resolved.
Show resolved Hide resolved
prevState?: RootState
) => any;
atarashansky marked this conversation as resolved.
Show resolved Hide resolved

type CascadedReducers =
| [string, ReducerFunction][]
| Map<string, ReducerFunction>;

export default function cascadeReducers(arg: CascadedReducers) {
/*
Combine a set of cascading reducers into a single reducer. Cascading
reducers are reducers which may rely on state computed by another reducer.
Expand All @@ -24,23 +37,24 @@ export default function cascadeReducers(arg: any) {
*/
const reducers = arg instanceof Map ? arg : new Map(arg);
const reducerKeys = [...reducers.keys()];
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
return (prevState: any, action: any) => {
const nextState = {};

return (prevState: RootState, action: AnyAction) => {
const nextState: RootState = {};
let stateChange = false;
for (let i = 0, l = reducerKeys.length; i < l; i += 1) {
const key = reducerKeys[i];
const key = reducerKeys[i] as string;
atarashansky marked this conversation as resolved.
Show resolved Hide resolved
const reducer = reducers.get(key);
const prevStateForKey = prevState ? prevState[key] : undefined;
const nextStateForKey = reducer(
prevStateForKey,
action,
nextState,
prevState
);
// @ts-expect-error ts-migrate(7053) FIXME: Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
nextState[key] = nextStateForKey;
stateChange = stateChange || nextStateForKey !== prevStateForKey;
if (reducer) {
const prevStateForKey = prevState ? prevState[key] : undefined;
const nextStateForKey = reducer(
prevStateForKey,
action,
nextState,
prevState
);
nextState[key] = nextStateForKey;
stateChange = stateChange || nextStateForKey !== prevStateForKey;
}
}
return stateChange ? nextState : prevState;
};
Expand Down
8 changes: 2 additions & 6 deletions client/src/reducers/centroidLabels.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import type { Action } from "redux";
import type { AnyAction } from "redux";

export interface CentroidLabelsState {
showLabels: boolean;
}

export interface CentroidLabelsAction extends Action<string> {
showLabels: boolean;
}

const initialState: CentroidLabelsState = {
showLabels: false,
};

const centroidLabels = (
state = initialState,
action: CentroidLabelsAction,
atarashansky marked this conversation as resolved.
Show resolved Hide resolved
action: AnyAction,
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
sharedNextState: any
): CentroidLabelsState => {
Expand Down
13 changes: 2 additions & 11 deletions client/src/reducers/continuousSelection.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import type { Action } from "redux";

import type { AnyAction } from "redux";
import { makeContinuousDimensionName } from "../util/nameCreators";

import type { ContinuousNamespace } from "../util/nameCreators";

export interface ContinuousSelectionAction extends Action<string> {
continuousNamespace: ContinuousNamespace;
selection: string;
range: [number, number];
}

export interface ContinuousSelectionState {
[name: string]: [number, number];
}

const ContinuousSelection = (
state: ContinuousSelectionState = {},
action: ContinuousSelectionAction
atarashansky marked this conversation as resolved.
Show resolved Hide resolved
action: AnyAction
): ContinuousSelectionState => {
switch (action.type) {
case "reset subset":
Expand Down
13 changes: 2 additions & 11 deletions client/src/reducers/datasetMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@
*/

// Core dependencies
import { Action } from "redux";
import { AnyAction } from "redux";

// App dependencies
import { DatasetMetadata as IDatasetMetadata } from "../common/types/entities";

/*
Action dispatched on successful response from dataset-metadata endpoint.
*/
export interface DatasetMetdataAction extends Action<string> {
datasetMetadata: IDatasetMetadata;
error: string;
portalUrl: string;
}

/*
Dataset metdata state; selected dataset ID and corresponding collection information.
*/
Expand All @@ -36,7 +27,7 @@ const DatasetMetadata = (
datasetMetadata: null,
portalUrl: null,
},
action: DatasetMetdataAction
atarashansky marked this conversation as resolved.
Show resolved Hide resolved
action: AnyAction
): DatasetMetadataState => {
switch (action.type) {
case "initial data load start":
Expand Down
12 changes: 4 additions & 8 deletions client/src/reducers/layoutChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ about commonly used names. Preferentially, pick in the following order:
4. give up, use the first available
*/

import type { Action } from "redux";
import type { AnyAction } from "redux";
import { EmbeddingSchema, Schema } from "../common/types/schema";
import type { RootState } from ".";

function bestDefaultLayout(layouts: Array<string>): string {
const preferredNames = ["umap", "tsne", "pca"];
Expand All @@ -33,15 +34,10 @@ export interface LayoutChoiceState {
currentDimNames: Array<string>;
}

export interface LayoutChoiceAction extends Action<string> {
atarashansky marked this conversation as resolved.
Show resolved Hide resolved
layoutChoice: string;
}

const LayoutChoice = (
state: LayoutChoiceState,
action: LayoutChoiceAction,
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
nextSharedState: any
action: AnyAction,
nextSharedState: RootState
): LayoutChoiceState => {
switch (action.type) {
case "initial data load complete": {
Expand Down
9 changes: 2 additions & 7 deletions client/src/reducers/quickGenes.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import uniq from "lodash.uniq";
import filter from "lodash.filter";
import { AnyAction } from "redux";
import type { RootState } from ".";
import { track } from "../analytics";
import { EVENTS } from "../analytics/events";

interface QuickGenesActions {
type: string;
gene: string;
selection: string;
data: string;
}
const quickGenes = (
state: { userDefinedGenes: string[]; userDefinedGenesLoading: boolean } = {
userDefinedGenes: [],
userDefinedGenesLoading: false,
},
action: QuickGenesActions,
atarashansky marked this conversation as resolved.
Show resolved Hide resolved
action: AnyAction,
nextSharedState: RootState
) => {
switch (action.type) {
Expand Down