-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Adds redux toolkit example for
response_stream
to developer ex…
…amples. (#182690) ## Summary Follow up to #132590. Part of #181111. This updates the developer examples for `@kbn/ml-response-stream` to include a variant with a full Redux Toolkit setup. For this case, the `@kbn/ml-response-stream` now includes a generic slice `streamSlice` that can be used. This allows the actions created to be streamed via NDJSON to be shared across server and client. Functional tests for the examples were added too. To run these tests you can use the following commands: ``` # Start the test server (can continue running) node scripts/functional_tests_server.js --config test/examples/config.js # Start a test run node scripts/functional_test_runner.js --config test/examples/config.js ``` ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [x] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
- Loading branch information
Showing
31 changed files
with
1,000 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
examples/response_stream/common/api/reducer_stream/reducer_actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export const API_ACTION_NAME = { | ||
UPDATE_PROGRESS: 'update_progress', | ||
ADD_TO_ENTITY: 'add_to_entity', | ||
DELETE_ENTITY: 'delete_entity', | ||
ERROR: 'error', | ||
} as const; | ||
export type ApiActionName = typeof API_ACTION_NAME[keyof typeof API_ACTION_NAME]; | ||
|
||
interface ApiActionUpdateProgress { | ||
type: typeof API_ACTION_NAME.UPDATE_PROGRESS; | ||
payload: number; | ||
} | ||
|
||
export function updateProgressAction(payload: number): ApiActionUpdateProgress { | ||
return { | ||
type: API_ACTION_NAME.UPDATE_PROGRESS, | ||
payload, | ||
}; | ||
} | ||
|
||
interface ApiActionAddToEntity { | ||
type: typeof API_ACTION_NAME.ADD_TO_ENTITY; | ||
payload: { | ||
entity: string; | ||
value: number; | ||
}; | ||
} | ||
|
||
export function addToEntityAction(entity: string, value: number): ApiActionAddToEntity { | ||
return { | ||
type: API_ACTION_NAME.ADD_TO_ENTITY, | ||
payload: { | ||
entity, | ||
value, | ||
}, | ||
}; | ||
} | ||
|
||
interface ApiActionDeleteEntity { | ||
type: typeof API_ACTION_NAME.DELETE_ENTITY; | ||
payload: string; | ||
} | ||
|
||
export function deleteEntityAction(payload: string): ApiActionDeleteEntity { | ||
return { | ||
type: API_ACTION_NAME.DELETE_ENTITY, | ||
payload, | ||
}; | ||
} | ||
|
||
interface ApiActionError { | ||
type: typeof API_ACTION_NAME.ERROR; | ||
payload: string; | ||
} | ||
|
||
export function errorAction(payload: string): ApiActionError { | ||
return { | ||
type: API_ACTION_NAME.ERROR, | ||
payload, | ||
}; | ||
} | ||
|
||
export type ReducerStreamApiAction = | ||
| ApiActionUpdateProgress | ||
| ApiActionAddToEntity | ||
| ApiActionDeleteEntity | ||
| ApiActionError; |
46 changes: 46 additions & 0 deletions
46
examples/response_stream/common/api/redux_stream/data_slice.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { createSlice } from '@reduxjs/toolkit'; | ||
import type { PayloadAction } from '@reduxjs/toolkit'; | ||
|
||
import { getInitialState } from '../stream_state'; | ||
|
||
export const dataSlice = createSlice({ | ||
name: 'development', | ||
initialState: getInitialState(), | ||
reducers: { | ||
updateProgress: (state, action: PayloadAction<number>) => { | ||
state.progress = action.payload; | ||
}, | ||
addToEntity: ( | ||
state, | ||
action: PayloadAction<{ | ||
entity: string; | ||
value: number; | ||
}> | ||
) => { | ||
const { entity, value } = action.payload; | ||
state.entities[entity] = (state.entities[entity] || 0) + value; | ||
}, | ||
deleteEntity: (state, action: PayloadAction<string>) => { | ||
delete state.entities[action.payload]; | ||
}, | ||
error: (state, action: PayloadAction<string>) => { | ||
state.errors.push(action.payload); | ||
}, | ||
reset: () => { | ||
return getInitialState(); | ||
}, | ||
}, | ||
}); | ||
|
||
export const { updateProgress, addToEntity, deleteEntity, error, reset } = dataSlice.actions; | ||
export type ReduxStreamApiAction = ReturnType< | ||
typeof dataSlice.actions[keyof typeof dataSlice.actions] | ||
>; |
37 changes: 37 additions & 0 deletions
37
examples/response_stream/common/api/redux_stream/options_slice.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { createSlice } from '@reduxjs/toolkit'; | ||
import type { PayloadAction } from '@reduxjs/toolkit'; | ||
|
||
const getInitialState = () => ({ | ||
simulateErrors: false, | ||
compressResponse: true, | ||
flushFix: false, | ||
}); | ||
|
||
export const optionsSlice = createSlice({ | ||
name: 'options', | ||
initialState: getInitialState(), | ||
reducers: { | ||
setSimulateErrors: (state, action: PayloadAction<boolean>) => { | ||
state.simulateErrors = action.payload; | ||
}, | ||
setCompressResponse: (state, action: PayloadAction<boolean>) => { | ||
state.compressResponse = action.payload; | ||
}, | ||
setFlushFix: (state, action: PayloadAction<boolean>) => { | ||
state.flushFix = action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { setSimulateErrors, setCompressResponse, setFlushFix } = optionsSlice.actions; | ||
export type ReduxOptionsApiAction = ReturnType< | ||
typeof optionsSlice.actions[keyof typeof optionsSlice.actions] | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export interface StreamState { | ||
errors: string[]; | ||
progress: number; | ||
entities: Record<string, number>; | ||
} | ||
|
||
export const getInitialState = (): StreamState => ({ | ||
errors: [], | ||
progress: 0, | ||
entities: {}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.