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

[Endpoint] add Redux saga Middleware and Actions factory #53906

Merged
merged 23 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b9d2bb2
Added saga library
paul-tavares Jan 2, 2020
4aa7cd1
Added action creator helper
paul-tavares Jan 2, 2020
460efb1
Added action creator exports to `/lib/index`
paul-tavares Jan 2, 2020
3f86360
Merge remote-tracking branch 'upstream/master' into task/redux-saga-m…
paul-tavares Jan 2, 2020
bbafe28
Moved files under `public/applications/endpoint`
paul-tavares Jan 3, 2020
a73c2d8
Deleted `action_creator`
paul-tavares Jan 3, 2020
ade91f3
Initialize endpoint app redux store
paul-tavares Jan 3, 2020
d7c9ced
Added Redux Dev tools support + selectors for app
paul-tavares Jan 3, 2020
66f585a
Refactor: moved files around inside of `store/` to desired dir structure
paul-tavares Jan 3, 2020
3c639b2
tests for app seletors
paul-tavares Jan 3, 2020
37147e1
Tests for app reducers
paul-tavares Jan 3, 2020
414717b
Merge remote-tracking branch 'upstream/master' into task/redux-saga-m…
paul-tavares Jan 13, 2020
bd33f5f
Deleted actions/reducers/selectors directories
paul-tavares Jan 14, 2020
b155be6
Revised structure for Store
paul-tavares Jan 14, 2020
4c090a3
Merge remote-tracking branch 'upstream/master' into task/redux-saga-m…
paul-tavares Jan 14, 2020
2d55b2f
Add store Provider to endpoint <AppRoot/>
paul-tavares Jan 14, 2020
482dab0
Add name to store for ReduxDevTools
paul-tavares Jan 14, 2020
da1a7ba
Added sample selectors to endpoint_list + actions to store
paul-tavares Jan 14, 2020
df93581
Make `SagaContext` type generic that accepts Action types
paul-tavares Jan 14, 2020
58d9036
Correction to Saga types
paul-tavares Jan 15, 2020
6cc2464
Merge remote-tracking branch 'upstream/master' into task/redux-saga-m…
paul-tavares Jan 15, 2020
03798c7
tests for endpoint_list store concerns
paul-tavares Jan 15, 2020
e6ee039
Tests for endpont_list saga
paul-tavares Jan 15, 2020
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
78 changes: 45 additions & 33 deletions x-pack/plugins/endpoint/public/applications/endpoint/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ import ReactDOM from 'react-dom';
import { CoreStart, AppMountParameters } from 'kibana/public';
import { I18nProvider, FormattedMessage } from '@kbn/i18n/react';
import { Route, BrowserRouter, Switch } from 'react-router-dom';
import { Provider } from 'react-redux';
import { Store } from 'redux';
import { appStoreFactory } from './store';

/**
* This module will be loaded asynchronously to reduce the bundle size of your plugin's main bundle.
*/
export function renderApp(coreStart: CoreStart, { appBasePath, element }: AppMountParameters) {
coreStart.http.get('/api/endpoint/hello-world');

ReactDOM.render(<AppRoot basename={appBasePath} />, element);
const store = appStoreFactory(coreStart);

ReactDOM.render(<AppRoot basename={appBasePath} store={store} />, element);

return () => {
ReactDOM.unmountComponentAtNode(element);
Expand All @@ -25,38 +30,45 @@ export function renderApp(coreStart: CoreStart, { appBasePath, element }: AppMou

interface RouterProps {
basename: string;
store: Store;
}

const AppRoot: React.FunctionComponent<RouterProps> = React.memo(({ basename }) => (
<I18nProvider>
<BrowserRouter basename={basename}>
<Switch>
<Route
exact
path="/"
render={() => (
<h1 data-test-subj="welcomeTitle">
<FormattedMessage id="xpack.endpoint.welcomeTitle" defaultMessage="Hello World" />
</h1>
)}
/>
<Route
path="/management"
render={() => (
<h1 data-test-subj="endpointManagement">
<FormattedMessage
id="xpack.endpoint.endpointManagement"
defaultMessage="Manage Endpoints"
/>
</h1>
)}
/>
<Route
render={() => (
<FormattedMessage id="xpack.endpoint.notFound" defaultMessage="Page Not Found" />
)}
/>
</Switch>
</BrowserRouter>
</I18nProvider>
const AppRoot: React.FunctionComponent<RouterProps> = React.memo(({ basename, store }) => (
<Provider store={store}>
<I18nProvider>
<BrowserRouter basename={basename}>
<Switch>
<Route
exact
path="/"
render={() => (
<h1 data-test-subj="welcomeTitle">
<FormattedMessage id="xpack.endpoint.welcomeTitle" defaultMessage="Hello World" />
</h1>
)}
/>
<Route
path="/management"
render={() => {
store.dispatch({ type: 'userEnteredEndpointListPage' });

return (
<h1 data-test-subj="endpointManagement">
<FormattedMessage
id="xpack.endpoint.endpointManagement"
defaultMessage="Manage Endpoints"
/>
</h1>
);
}}
/>
<Route
render={() => (
<FormattedMessage id="xpack.endpoint.notFound" defaultMessage="Page Not Found" />
)}
/>
</Switch>
</BrowserRouter>
</I18nProvider>
</Provider>
));
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export * from './saga';
101 changes: 101 additions & 0 deletions x-pack/plugins/endpoint/public/applications/endpoint/lib/saga.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { createSagaMiddleware, SagaContext } from './index';
import { applyMiddleware, createStore, Reducer } from 'redux';

describe('saga', () => {
const INCREMENT_COUNTER = 'INCREMENT';
const DELAYED_INCREMENT_COUNTER = 'DELAYED INCREMENT COUNTER';
const STOP_SAGA_PROCESSING = 'BREAK ASYNC ITERATOR';

const sleep = (ms = 1000) => new Promise(resolve => setTimeout(resolve, ms));
let reducerA: Reducer;
let sideAffect: (a: unknown, s: unknown) => void;
let sagaExe: (sagaContext: SagaContext) => Promise<void>;

beforeEach(() => {
reducerA = jest.fn((prevState = { count: 0 }, { type }) => {
switch (type) {
case INCREMENT_COUNTER:
return { ...prevState, count: prevState.count + 1 };
default:
return prevState;
}
});

sideAffect = jest.fn();

sagaExe = jest.fn(async ({ actionsAndState, dispatch }: SagaContext) => {
for await (const { action, state } of actionsAndState()) {
expect(action).toBeDefined();
expect(state).toBeDefined();

if (action.type === STOP_SAGA_PROCESSING) {
break;
}

sideAffect(action, state);

if (action.type === DELAYED_INCREMENT_COUNTER) {
await sleep(1);
dispatch({
type: INCREMENT_COUNTER,
});
}
}
});
});

test('it returns Redux Middleware from createSagaMiddleware()', () => {
const sagaMiddleware = createSagaMiddleware(async () => {});
expect(sagaMiddleware).toBeInstanceOf(Function);
});
test('it does nothing if saga is not started', () => {
const store = createStore(reducerA, applyMiddleware(createSagaMiddleware(sagaExe)));
expect(store.getState().count).toEqual(0);
expect(reducerA).toHaveBeenCalled();
expect(sagaExe).toHaveBeenCalled();
expect(sideAffect).not.toHaveBeenCalled();
expect(store.getState()).toEqual({ count: 0 });
});
test('it updates store once running', async () => {
const sagaMiddleware = createSagaMiddleware(sagaExe);
const store = createStore(reducerA, applyMiddleware(sagaMiddleware));

expect(store.getState()).toEqual({ count: 0 });
expect(sagaExe).toHaveBeenCalled();

store.dispatch({ type: DELAYED_INCREMENT_COUNTER });
expect(store.getState()).toEqual({ count: 0 });

await sleep(100);

expect(sideAffect).toHaveBeenCalled();
expect(store.getState()).toEqual({ count: 1 });
});
test('it stops processing if break out of loop', async () => {
const sagaMiddleware = createSagaMiddleware(sagaExe);
const store = createStore(reducerA, applyMiddleware(sagaMiddleware));

store.dispatch({ type: DELAYED_INCREMENT_COUNTER });
await sleep(100);

expect(store.getState()).toEqual({ count: 1 });
expect(sideAffect).toHaveBeenCalledTimes(2);

store.dispatch({ type: STOP_SAGA_PROCESSING });
await sleep(100);

expect(store.getState()).toEqual({ count: 1 });
expect(sideAffect).toHaveBeenCalledTimes(2);

store.dispatch({ type: DELAYED_INCREMENT_COUNTER });
await sleep(100);

expect(store.getState()).toEqual({ count: 1 });
expect(sideAffect).toHaveBeenCalledTimes(2);
});
});
129 changes: 129 additions & 0 deletions x-pack/plugins/endpoint/public/applications/endpoint/lib/saga.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { AnyAction, Dispatch, Middleware, MiddlewareAPI } from 'redux';
import { GlobalState } from '../store';

interface QueuedAction<TAction = AnyAction> {
/**
* The Redux action that was dispatched
*/
action: TAction;
/**
* The Global state at the time the action was dispatched
*/
state: GlobalState;
}

interface IteratorInstance {
queue: QueuedAction[];
nextResolve: null | ((inst: QueuedAction) => void);
}

type Saga = (storeContext: SagaContext) => Promise<void>;

type StoreActionsAndState<TAction = AnyAction> = AsyncIterableIterator<QueuedAction<TAction>>;

export interface SagaContext<TAction extends AnyAction = AnyAction> {
/**
* A generator function that will `yield` `Promise`s that resolve with a `QueuedAction`
*/
actionsAndState: () => StoreActionsAndState<TAction>;
dispatch: Dispatch<TAction>;
}

const noop = () => {};

/**
* Creates Saga Middleware for use with Redux.
*
* @param {Saga} saga The `saga` should initialize a long-running `for await...of` loop against
* the return value of the `actionsAndState()` method provided by the `SagaContext`.
*
* @return {Middleware}
*
* @example
*
* type TPossibleActions = { type: 'add', payload: any[] };
* //...
* const endpointsSaga = async ({ actionsAndState, dispatch }: SagaContext<TPossibleActions>) => {
* for await (const { action, state } of actionsAndState()) {
* if (action.type === "userRequestedResource") {
* const resourceData = await doApiFetch('of/some/resource');
* dispatch({
* type: 'add',
* payload: [ resourceData ]
* });
* }
* }
* }
* const endpointsSagaMiddleware = createSagaMiddleware(endpointsSaga);
* //....
* const store = createStore(reducers, [ endpointsSagaMiddleware ]);
*/
export function createSagaMiddleware(saga: Saga): Middleware {
const iteratorInstances = new Set<IteratorInstance>();
let runSaga: () => void = noop;

async function* getActionsAndStateIterator(): StoreActionsAndState {
const instance: IteratorInstance = { queue: [], nextResolve: null };
iteratorInstances.add(instance);
try {
while (true) {
yield await nextActionAndState();
}
} finally {
// If the consumer stops consuming this (e.g. `break` or `return` is called in the `for await`
// then this `finally` block will run and unregister this instance and reset `runSaga`
iteratorInstances.delete(instance);
runSaga = noop;
}

function nextActionAndState() {
if (instance.queue.length) {
return Promise.resolve(instance.queue.shift() as QueuedAction);
} else {
return new Promise<QueuedAction>(function(resolve) {
instance.nextResolve = resolve;
});
}
}
}

function enqueue(value: QueuedAction) {
for (const iteratorInstance of iteratorInstances) {
iteratorInstance.queue.push(value);
if (iteratorInstance.nextResolve !== null) {
iteratorInstance.nextResolve(iteratorInstance.queue.shift() as QueuedAction);
iteratorInstance.nextResolve = null;
}
}
}

function middleware({ getState, dispatch }: MiddlewareAPI) {
if (runSaga === noop) {
runSaga = saga.bind<null, SagaContext, any[], Promise<void>>(null, {
actionsAndState: getActionsAndStateIterator,
dispatch,
});
runSaga();
}
return (next: Dispatch<AnyAction>) => (action: AnyAction) => {
// Call the next dispatch method in the middleware chain.
const returnValue = next(action);

enqueue({
action,
state: getState(),
});

// This will likely be the action itself, unless a middleware further in chain changed it.
return returnValue;
};
}

return middleware;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { EndpointListAction } from './endpoint_list';

export type AppAction = EndpointListAction;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { EndpointListData } from './types';

interface ServerReturnedEndpointList {
type: 'serverReturnedEndpointList';
payload: EndpointListData;
}

interface UserEnteredEndpointListPage {
type: 'userEnteredEndpointListPage';
}

interface UserExitedEndpointListPage {
type: 'userExitedEndpointListPage';
}

export type EndpointListAction =
| ServerReturnedEndpointList
| UserEnteredEndpointListPage
| UserExitedEndpointListPage;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export { endpointListReducer } from './reducer';
export { EndpointListAction } from './action';
export { endpointsListSaga } from './saga';
export * from './types';
Loading