From 21cd1b6b913f0c15ebd109e903464c283a31f176 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Mon, 6 May 2019 11:05:35 +0200 Subject: [PATCH] improve persistable state types --- .../plugins/lens/public/editor_frame_plugin/plugin.tsx | 2 +- .../public/indexpattern_plugin/indexpattern.test.ts | 6 +++--- .../lens/public/indexpattern_plugin/indexpattern.tsx | 10 +++++++--- x-pack/plugins/lens/public/types.ts | 10 +++++----- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/lens/public/editor_frame_plugin/plugin.tsx b/x-pack/plugins/lens/public/editor_frame_plugin/plugin.tsx index 9d9224e462a5a..c755b160a7496 100644 --- a/x-pack/plugins/lens/public/editor_frame_plugin/plugin.tsx +++ b/x-pack/plugins/lens/public/editor_frame_plugin/plugin.tsx @@ -33,7 +33,7 @@ class EditorFramePlugin { // on it's own because we are loosing type information here. // So it's basically explicitly saying "I'm dropping the information about type T here // because this information isn't useful to me." but without using any which can leak - this.datasources[name] = datasource as Datasource; + this.datasources[name] = datasource as Datasource; }, registerVisualization: (name, visualization) => { this.visualizations[name] = visualization as Visualization; diff --git a/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.test.ts b/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.test.ts index fa1b88f90b1c6..d974c43fa3ac6 100644 --- a/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.test.ts +++ b/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.test.ts @@ -4,11 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ -import { indexPatternDatasource } from './indexpattern'; +import { indexPatternDatasource, PersistableIndexPatternPrivateState } from './indexpattern'; import { DatasourcePublicAPI, Operation } from '../types'; describe('IndexPattern Data Source', () => { - let persistedState: object; + let persistedState: PersistableIndexPatternPrivateState; beforeEach(() => { persistedState = { @@ -54,7 +54,7 @@ describe('IndexPattern Data Source', () => { it('should persist from saved state', async () => { const state = await indexPatternDatasource.initialize(persistedState); - expect(indexPatternDatasource.getPersistedState(state)).toEqual(persistedState); + expect(indexPatternDatasource.getPersistableState(state)).toEqual(persistedState); }); }); diff --git a/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.tsx b/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.tsx index a9c651ce1f4e0..b7e78ad6c9b29 100644 --- a/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.tsx +++ b/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.tsx @@ -49,9 +49,13 @@ export interface IndexPatternPrivateState { indexPatterns: { [id: string]: IndexPattern }; } +type PersistedKeys = 'currentIndexPattern' | 'columns' | 'columnOrder'; + +export type PersistableIndexPatternPrivateState = Pick; + // Not stateful. State is persisted to the frame -export const indexPatternDatasource: Datasource = { - async initialize(state?: IndexPatternPrivateState | any) { +export const indexPatternDatasource: Datasource = { + async initialize(state?: PersistableIndexPatternPrivateState) { // TODO: Make fetch request to load indexPatterns from saved objects if (state) { return { @@ -67,7 +71,7 @@ export const indexPatternDatasource: Datasource = { }; }, - getPersistedState({ currentIndexPattern, columns, columnOrder }: IndexPatternPrivateState) { + getPersistableState({ currentIndexPattern, columns, columnOrder }: IndexPatternPrivateState) { return { currentIndexPattern, columns, columnOrder }; }, diff --git a/x-pack/plugins/lens/public/types.ts b/x-pack/plugins/lens/public/types.ts index 39a17f5e250ff..3f60cb1e98487 100644 --- a/x-pack/plugins/lens/public/types.ts +++ b/x-pack/plugins/lens/public/types.ts @@ -7,7 +7,7 @@ export interface EditorFrameSetup { render: (domElement: Element) => void; // generic type on the API functions to pull the "unknown vs. specific type" error into the implementation - registerDatasource: (name: string, datasource: Datasource) => void; + registerDatasource: (name: string, datasource: Datasource) => void; registerVisualization: (name: string, visualization: Visualization) => void; } @@ -37,12 +37,12 @@ export interface DatasourceSuggestion { /** * Interface for the datasource registry */ -export interface Datasource { +export interface Datasource { // For initializing, either from an empty state or from persisted state - initialize: (state?: T | any) => Promise; + initialize: (state?: P) => Promise; - // Given the current state, which parts should be saved? - getPersistedState: (state: T) => object; + // Return the subset of the state which should be persisted in a saved object + getPersistableState: (state: T) => P; renderDataPanel: (domElement: Element, props: DatasourceDataPanelProps) => void;