Skip to content

Commit b3e0183

Browse files
Corey Robertsonelasticmachine
andauthored
Only fire appState changes when there is a change (#56183) (#56399)
Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
1 parent b916251 commit b3e0183

File tree

3 files changed

+71
-26
lines changed

3 files changed

+71
-26
lines changed

x-pack/legacy/plugins/canvas/public/components/router/index.js

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { connect } from 'react-redux';
8+
// @ts-ignore untyped local
9+
import { setFullscreen } from '../../state/actions/transient';
10+
import {
11+
enableAutoplay,
12+
setRefreshInterval,
13+
setAutoplayInterval,
14+
// @ts-ignore untyped local
15+
} from '../../state/actions/workpad';
16+
// @ts-ignore untyped local
17+
import { Router as Component } from './router';
18+
import { State } from '../../../types';
19+
20+
const mapDispatchToProps = {
21+
enableAutoplay,
22+
setAutoplayInterval,
23+
setFullscreen,
24+
setRefreshInterval,
25+
};
26+
27+
const mapStateToProps = (state: State) => ({
28+
refreshInterval: state.transient.refresh.interval,
29+
autoplayInterval: state.transient.autoplay.interval,
30+
autoplay: state.transient.autoplay.enabled,
31+
fullscreen: state.transient.fullScreen,
32+
});
33+
34+
export const Router = connect(
35+
mapStateToProps,
36+
mapDispatchToProps,
37+
(stateProps, dispatchProps, ownProps) => {
38+
return {
39+
...ownProps,
40+
...dispatchProps,
41+
setRefreshInterval: (interval: number) => {
42+
if (interval !== stateProps.refreshInterval) {
43+
dispatchProps.setRefreshInterval(interval);
44+
}
45+
},
46+
setAutoplayInterval: (interval: number) => {
47+
if (interval !== stateProps.autoplayInterval) {
48+
dispatchProps.setRefreshInterval(interval);
49+
}
50+
},
51+
enableAutoplay: (autoplay: boolean) => {
52+
if (autoplay !== stateProps.autoplay) {
53+
dispatchProps.enableAutoplay(autoplay);
54+
}
55+
},
56+
setFullscreen: (fullscreen: boolean) => {
57+
if (fullscreen !== stateProps.fullscreen) {
58+
dispatchProps.setFullscreen(fullscreen);
59+
}
60+
},
61+
};
62+
}
63+
)(Component);

x-pack/legacy/plugins/canvas/public/lib/app_state.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { getWindow } from './get_window';
1313
import { historyProvider } from './history_provider';
1414
// @ts-ignore untyped local
1515
import { routerProvider } from './router_provider';
16-
import { createTimeInterval, isValidTimeInterval } from './time_interval';
16+
import { createTimeInterval, isValidTimeInterval, getTimeInterval } from './time_interval';
1717
import { AppState, AppStateKeys } from '../../types';
1818

1919
export function getDefaultAppState(): AppState {
@@ -112,7 +112,12 @@ export function setRefreshInterval(payload: string) {
112112
const appValue = appState[AppStateKeys.REFRESH_INTERVAL];
113113

114114
if (payload !== appValue) {
115-
appState[AppStateKeys.REFRESH_INTERVAL] = payload;
116-
routerProvider().updateAppState(appState);
115+
if (getTimeInterval(payload)) {
116+
appState[AppStateKeys.REFRESH_INTERVAL] = payload;
117+
routerProvider().updateAppState(appState);
118+
} else {
119+
delete appState[AppStateKeys.REFRESH_INTERVAL];
120+
routerProvider().updateAppState(appState);
121+
}
117122
}
118123
}

0 commit comments

Comments
 (0)