Skip to content

Commit

Permalink
middleware receive immutable versions of state and actions
Browse files Browse the repository at this point in the history
  • Loading branch information
oatkiller committed Apr 16, 2020
1 parent 4e8ff57 commit 30b22d2
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { RoutingAction } from './routing';
import { PolicyListAction } from './policy_list';
import { PolicyDetailsAction } from './policy_details';

/**
* The entire set of redux actions recognized by our reducer.
*/
export type AppAction =
| HostAction
| AlertAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { IIndexPattern } from 'src/plugins/data/public';
import { AlertResultList, AlertDetails } from '../../../../../common/types';
import { AppAction } from '../action';
import { MiddlewareFactory, AlertListState } from '../../types';
import { isOnAlertPage, apiQueryParams, hasSelectedAlert, uiQueryParams } from './selectors';
import { cloneHttpFetchQuery } from '../../../../common/clone_http_fetch_query';
Expand All @@ -29,7 +28,7 @@ export const alertMiddlewareFactory: MiddlewareFactory<AlertListState> = (coreSt
return [indexPattern];
}

return api => next => async (action: AppAction) => {
return api => next => async action => {
next(action);
const state = api.getState();
if (action.type === 'userChangedUrl' && isOnAlertPage(state)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import { MiddlewareFactory } from '../../types';
import { pageIndex, pageSize, isOnHostPage, hasSelectedHost, uiQueryParams } from './selectors';
import { HostListState } from '../../types';
import { AppAction } from '../action';

export const hostMiddlewareFactory: MiddlewareFactory<HostListState> = coreStart => {
return ({ getState, dispatch }) => next => async (action: AppAction) => {
return ({ getState, dispatch }) => next => async action => {
next(action);
const state = getState();
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { policyDetailsMiddlewareFactory } from './policy_details';
import { GlobalState, MiddlewareFactory } from '../types';
import { AppAction } from './action';
import { EndpointPluginStartDependencies } from '../../../plugin';
import { Immutable } from '../../../../common/types';

const composeWithReduxDevTools = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
? (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ name: 'EndpointApp' })
Expand All @@ -37,10 +38,10 @@ export type Selector<S, R> = (state: S) => R;
*/
export const substateMiddlewareFactory = <Substate>(
selector: Selector<GlobalState, Substate>,
middleware: Middleware<{}, Substate, Dispatch<AppAction>>
): Middleware<{}, GlobalState, Dispatch<AppAction>> => {
middleware: Middleware<{}, Substate, Dispatch<Immutable<AppAction> | AppAction>>
): Middleware<{}, GlobalState, Dispatch<AppAction | Immutable<AppAction>>> => {
return api => {
const substateAPI: MiddlewareAPI<Dispatch<AppAction>, Substate> = {
const substateAPI: MiddlewareAPI<Dispatch<AppAction | Immutable<AppAction>>, Substate> = {
...api,
getState() {
return selector(api.getState());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const isOnPolicyDetailsPage = (state: Immutable<PolicyDetailsState>) => {
};

/** Returns the policyId from the url */
export const policyIdFromParams: (state: PolicyDetailsState) => string = createSelector(
(state: PolicyDetailsState) => state.location,
export const policyIdFromParams: (state: Immutable<PolicyDetailsState>) => string = createSelector(
state => state.location,
(location: PolicyDetailsState['location']) => {
if (location) {
return location.pathname.split('/')[2];
Expand Down
11 changes: 9 additions & 2 deletions x-pack/plugins/endpoint/public/applications/endpoint/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ import {
} from '../../../../ingest_manager/common';

export { AppAction };

/**
* Takes application-standard middleware dependencies
* and returns a redux middleware.
*/
export type MiddlewareFactory<S = GlobalState> = (
coreStart: CoreStart,
depsStart: EndpointPluginStartDependencies
) => (
api: MiddlewareAPI<Dispatch<AppAction>, S>
) => (next: Dispatch<AppAction>) => (action: AppAction) => unknown;
api: MiddlewareAPI<Dispatch<Immutable<AppAction> | AppAction>, Immutable<S>>
) => (
next: Dispatch<Immutable<AppAction> | AppAction>
) => (action: Immutable<AppAction>) => unknown;

export interface HostListState {
hosts: HostMetadata[];
Expand Down

0 comments on commit 30b22d2

Please sign in to comment.