-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CARTO: Adapt fetchMap for supporting HeatmapTileLayer #8952
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job :) just a few small comments
modules/carto/src/api/layer-map.ts
Outdated
@@ -46,6 +46,20 @@ const SCALE_FUNCS = { | |||
}; | |||
export type SCALE_TYPE = keyof typeof SCALE_FUNCS; | |||
|
|||
const RASTER_LAYER_TYPE = 'raster'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need the constants, instead do:
type LayerType = 'raster' | 'quadbin' | 'h3' | 'heatmapTile';
and then type LAYER_FROM_LAYER_TYPE
as Record<LayerType, Layer>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need the constants
But that means repeating strings like 'raster'
everywhere, so isn't it better to create a constant and use it 🤔 ?
I see much more valuable holding this into constants, because human mistakes are lower, and moreover, you don't have to remember what if it was heatmapTile
, heatmap-tile
or heatmap_tile
, it just works 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then type LAYER_FROM_LAYER_TYPE as Record<LayerType, Layer>
Isn't that inferred automagically by Typescript?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is the pattern we've adopted since the move to Typescript, e.g. take a look at the defaultProps
in Layer:
deck.gl/modules/core/src/lib/layer.ts
Lines 145 to 158 in 963bbcc
operation: 'draw', | |
onHover: {type: 'function', value: null, optional: true}, | |
onClick: {type: 'function', value: null, optional: true}, | |
onDragStart: {type: 'function', value: null, optional: true}, | |
onDrag: {type: 'function', value: null, optional: true}, | |
onDragEnd: {type: 'function', value: null, optional: true}, | |
coordinateSystem: COORDINATE_SYSTEM.DEFAULT, | |
coordinateOrigin: {type: 'array', value: [0, 0, 0], compare: true}, | |
modelMatrix: {type: 'array', value: null, compare: true, optional: true}, | |
wrapLongitude: false, | |
positionFormat: 'XYZ', | |
colorFormat: 'RGBA', |
Here we directly type the string, and rely on TypeScript to enforce that it is correct. If you try changing one of those types, the compiler will stop you.
With my approach, you reduce the 9 lines defining (needless) constants into one and you also verify that LAYER_FROM_LAYER_TYPE
contains all the required mappings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood! Added the Record<...>
I have to disagree with the "needless" of defining constants. Defining constants has many benefits:
- It's self documenting, because constants could have descriptive names, rather than "just values", for example, the
const UNKNOWN_COLOR = '#868d91';
in this same file - It's a single point of change, in case the LayerType gets modified, meaning with your proposal we'll have to modify several places
- We are avoiding human mistakes as typos, because maybe in some places it's typed as
string
, but not asLayerType
- Maybe I'm wrong, but modern JS engines optimize better constants than hard-coded literals
- Refactors are much easier with constants, because of the single point of change
In this particular case, all the layer types were repeated at least three times, that's exactly why I thought a const
could be a good idea 🤷
Said so, if that's the agreement, I'll follow it 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case of "magic" values you may have a points, but
const layerType: LayerType = 'raster';
is more readable as I actually know what the variable will hold. With a constant I have to go look it up.
As for the single point of change, we just update the type and fix the errors the compiler instantly throws up.
Humans shouldn't type it as string
😉 it won't pass code review (as you can see!)
Background
Change List
fetchMap
to support HeatmapTileLayer