Replies: 7 comments
-
Avoid unnecessary types guards and undefined/null fallbackskibana/x-pack/plugins/lens/public/visualizations/xy/visualization_helpers.tsx Lines 131 to 150 in 5c0f034
Possible cleanup// all type guards semantically related are moved close together
export const isDataLayer = (layer: XYLayerConfig): layer is XYDataLayerConfig =>
layer.layerType === layerTypes.DATA || !layer.layerType;
export const isReferenceLayer = (layer: XYLayerConfig): layer is XYReferenceLineLayerConfig =>
layer.layerType === layerTypes.REFERENCELINE;
export const isAnnotationsLayer = (layer: XYLayerConfig): layer is XYAnnotationLayerConfig =>
layer.layerType === layerTypes.ANNOTATIONS && 'indexPatternId' in layer;
// no need for the logical OR as the argument is always an array as the argument type expect
// no need for the typed type guard inside the filter function
// we can also pass directly the typeguard to the filter instead of extracting more variables
export const getDataLayers = (layers: XYLayerConfig[]) => layers.filter(isDataLayer);
export const getFirstDataLayer = (layers: XYLayerConfig[]) => layers.find(isDataLayer);
export const getReferenceLayers = (layers: XYLayerConfig[]) => layers.filter(isReferenceLayer);
export const getAnnotationsLayers = (layers: XYLayerConfig[]) => layers.filter(isAnnotationsLayer); |
Beta Was this translation helpful? Give feedback.
-
Write meaningful type guardsIn the code below we are returning an array of kibana/x-pack/plugins/lens/public/visualizations/xy/to_expression.ts Lines 198 to 205 in fde9539 kibana/x-pack/plugins/lens/public/visualizations/xy/types.ts Lines 95 to 109 in fde9539 kibana/x-pack/plugins/lens/public/visualizations/xy/types.ts Lines 183 to 186 in fde9539 My suggestions here are:
|
Beta Was this translation helpful? Give feedback.
-
Replace
|
Beta Was this translation helpful? Give feedback.
-
Strengthen type earlierI've noticed that we don't straighten our type when we apply some default values like in the code sample below. In the code below for example we are adding some
There is also a third issue here: we are using 2 times the same type name kibana/x-pack/plugins/lens/public/visualizations/xy/to_expression.ts Lines 237 to 259 in 94e850d kibana/x-pack/plugins/lens/public/visualizations/xy/types.ts Lines 73 to 75 in 94e850d |
Beta Was this translation helpful? Give feedback.
-
This function returns the first property from the existing datasources on the doc as the active datasource. If there are 2, it doesnt guarantee that the correct datasourceId will be returned. We should refactor it and check if the datasources in the state have layers. But for now I fixed that on our builder
|
Beta Was this translation helpful? Give feedback.
-
This interface should be updated. Lens state has the kql/lucene query (
When this is done the LensConfigOptions on the builder should also be updated.
|
Beta Was this translation helpful? Give feedback.
-
Our type for We should update this type to be more helpful. The first step is to figure out what is actually going on with our saved objects so that we craft a type that is accurate both to what is really happening and to how our application should be viewing the state. For example, if datasourceStates: { formBased: FormBasedPersistedState, textBased?: never } | { formBased?: never, textBased: TextBasedPersistedState }; |
Beta Was this translation helpful? Give feedback.
-
Here is a collection of possible improvements in our code, and when I have time I also like to propose a viable alternative.
They are in random order, as I found them/remember them.
I want to add a comment per issue/improvement so that everybody can contribute to it by commenting right below each comment in a thread.
Beta Was this translation helpful? Give feedback.
All reactions