Skip to content

Commit d5988af

Browse files
[Lens] Reload on runtime edit (#97161) (#97444)
Co-authored-by: Joe Reuter <johannes.reuter@elastic.co>
1 parent c6a1760 commit d5988af

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

x-pack/plugins/lens/public/app_plugin/app.test.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import React from 'react';
9-
import { Observable } from 'rxjs';
9+
import { Observable, Subject } from 'rxjs';
1010
import { ReactWrapper } from 'enzyme';
1111
import { act } from 'react-dom/test-utils';
1212
import { App } from './app';
@@ -77,13 +77,16 @@ function createMockFrame(): jest.Mocked<EditorFrameInstance> {
7777
};
7878
}
7979

80+
const sessionIdSubject = new Subject<string>();
81+
8082
function createMockSearchService() {
8183
let sessionIdCounter = 1;
8284
return {
8385
session: {
8486
start: jest.fn(() => `sessionId-${sessionIdCounter++}`),
8587
clear: jest.fn(),
8688
getSessionId: jest.fn(() => `sessionId-${sessionIdCounter}`),
89+
getSession$: jest.fn(() => sessionIdSubject.asObservable()),
8790
},
8891
};
8992
}
@@ -1328,6 +1331,24 @@ describe('Lens App', () => {
13281331
);
13291332
});
13301333

1334+
it('re-renders the frame if session id changes from the outside', async () => {
1335+
const services = makeDefaultServices();
1336+
const { frame } = mountWith({ props: undefined, services });
1337+
1338+
act(() => {
1339+
sessionIdSubject.next('new-session-id');
1340+
});
1341+
await act(async () => {
1342+
await new Promise((r) => setTimeout(r, 0));
1343+
});
1344+
expect(frame.mount).toHaveBeenCalledWith(
1345+
expect.any(Element),
1346+
expect.objectContaining({
1347+
searchSessionId: `new-session-id`,
1348+
})
1349+
);
1350+
});
1351+
13311352
it('updates the searchSessionId when the active saved query is cleared', () => {
13321353
const { component, frame, services } = mountWith({});
13331354
act(() =>

x-pack/plugins/lens/public/app_plugin/app.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Toast } from 'kibana/public';
1414
import { VisualizeFieldContext } from 'src/plugins/ui_actions/public';
1515
import { Datatable } from 'src/plugins/expressions/public';
1616
import { EuiBreadcrumb } from '@elastic/eui';
17-
import { finalize, switchMap, tap } from 'rxjs/operators';
17+
import { delay, finalize, switchMap, tap } from 'rxjs/operators';
1818
import { downloadMultipleAs } from '../../../../../src/plugins/share/public';
1919
import {
2020
createKbnUrlStateStorage,
@@ -221,11 +221,29 @@ export function App({
221221
kbnUrlStateStorage
222222
);
223223

224+
const sessionSubscription = data.search.session
225+
.getSession$()
226+
// wait for a tick to filter/timerange subscribers the chance to update the session id in the state
227+
.pipe(delay(0))
228+
// then update if it didn't get updated yet
229+
.subscribe((newSessionId) => {
230+
if (newSessionId) {
231+
setState((prevState) => {
232+
if (prevState.searchSessionId !== newSessionId) {
233+
return { ...prevState, searchSessionId: newSessionId };
234+
} else {
235+
return prevState;
236+
}
237+
});
238+
}
239+
});
240+
224241
return () => {
225242
stopSyncingQueryServiceStateWithUrl();
226243
filterSubscription.unsubscribe();
227244
timeSubscription.unsubscribe();
228245
autoRefreshSubscription.unsubscribe();
246+
sessionSubscription.unsubscribe();
229247
};
230248
}, [
231249
data.query.filterManager,

x-pack/plugins/lens/public/indexpattern_datasource/datapanel.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,8 @@ export const InnerIndexPatternDataPanel = function InnerIndexPatternDataPanel({
503503
patterns: [currentIndexPattern.id],
504504
});
505505
onUpdateIndexPattern(newlyMappedIndexPattern[currentIndexPattern.id]);
506+
// start a new session so all charts are refreshed
507+
data.search.session.start();
506508
}, [data, currentIndexPattern, onUpdateIndexPattern]);
507509

508510
const editField = useMemo(

0 commit comments

Comments
 (0)