Skip to content
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

Merged
merged 9 commits into from
Jun 19, 2024

Conversation

jmgaya
Copy link
Collaborator

@jmgaya jmgaya commented Jun 17, 2024

Background

Change List

  • Adapt fetchMap to support HeatmapTileLayer

@jmgaya jmgaya requested review from felixpalmer and zbigg June 17, 2024 09:18
@coveralls
Copy link

Coverage Status

coverage: 89.56% (-0.003%) from 89.563%
when pulling 55daf5d on jmgaya/adapt-fetchmap-heatmap-tile
into f1a163a on master.

Copy link
Collaborator

@felixpalmer felixpalmer left a 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 Show resolved Hide resolved
modules/carto/src/api/layer-map.ts Outdated Show resolved Hide resolved
test/apps/carto-map/app.ts Show resolved Hide resolved
modules/carto/src/api/layer-map.ts Outdated Show resolved Hide resolved
@jmgaya jmgaya requested a review from padawannn June 17, 2024 14:04
@coveralls
Copy link

Coverage Status

coverage: 89.564% (+0.001%) from 89.563%
when pulling 6c250df on jmgaya/adapt-fetchmap-heatmap-tile
into f1a163a on master.

@jmgaya jmgaya requested a review from felixpalmer June 18, 2024 11:10
modules/carto/src/api/layer-map.ts Outdated Show resolved Hide resolved
modules/carto/src/api/layer-map.ts Show resolved Hide resolved
@coveralls
Copy link

Coverage Status

coverage: 89.568% (+0.005%) from 89.563%
when pulling cff8c6e on jmgaya/adapt-fetchmap-heatmap-tile
into f1a163a on master.

@jmgaya jmgaya requested a review from felixpalmer June 18, 2024 14:03
modules/carto/src/api/layer-map.ts Outdated Show resolved Hide resolved
@@ -46,6 +46,20 @@ const SCALE_FUNCS = {
};
export type SCALE_TYPE = keyof typeof SCALE_FUNCS;

const RASTER_LAYER_TYPE = 'raster';
Copy link
Collaborator

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>

Copy link
Collaborator Author

@jmgaya jmgaya Jun 18, 2024

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 🤷

Copy link
Collaborator Author

@jmgaya jmgaya Jun 18, 2024

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?

Copy link
Collaborator

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:

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

Screenshot 2024-06-19 at 09 20 33

Copy link
Collaborator Author

@jmgaya jmgaya Jun 19, 2024

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 as LayerType
  • 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 👍

Copy link
Collaborator

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!)

@coveralls
Copy link

Coverage Status

coverage: 89.567% (+0.004%) from 89.563%
when pulling 5a6777b on jmgaya/adapt-fetchmap-heatmap-tile
into f1a163a on master.

@jmgaya jmgaya requested a review from felixpalmer June 19, 2024 07:59
@coveralls
Copy link

Coverage Status

coverage: 89.565% (+0.002%) from 89.563%
when pulling 9cdbe7f on jmgaya/adapt-fetchmap-heatmap-tile
into f1a163a on master.

modules/carto/src/api/layer-map.ts Outdated Show resolved Hide resolved
modules/carto/src/api/layer-map.ts Show resolved Hide resolved
@jmgaya jmgaya requested a review from felixpalmer June 19, 2024 08:27
@coveralls
Copy link

Coverage Status

coverage: 89.564% (+0.001%) from 89.563%
when pulling 6b32032 on jmgaya/adapt-fetchmap-heatmap-tile
into f1a163a on master.

@coveralls
Copy link

Coverage Status

coverage: 89.564% (+0.001%) from 89.563%
when pulling ddb0917 on jmgaya/adapt-fetchmap-heatmap-tile
into f1a163a on master.

modules/carto/src/api/layer-map.ts Outdated Show resolved Hide resolved
@jmgaya jmgaya requested a review from felixpalmer June 19, 2024 11:25
@coveralls
Copy link

Coverage Status

coverage: 89.564% (+0.001%) from 89.563%
when pulling 7b19588 on jmgaya/adapt-fetchmap-heatmap-tile
into f1a163a on master.

@felixpalmer felixpalmer merged commit fc27dbf into master Jun 19, 2024
2 checks passed
@felixpalmer felixpalmer deleted the jmgaya/adapt-fetchmap-heatmap-tile branch June 19, 2024 13:09
@coveralls
Copy link

Coverage Status

coverage: 89.564%. remained the same
when pulling e121a3a on jmgaya/adapt-fetchmap-heatmap-tile
into a745330 on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants