-
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
refactor(carto): Refactor fetchMap() for deck.gl v9.1 #9232
Draft
donmccurdy
wants to merge
1
commit into
master
Choose a base branch
from
donmccurdy/carto-fetchmap-v9.1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+47
−60
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,7 +3,7 @@ | |||||
// Copyright (c) vis.gl contributors | ||||||
|
||||||
import { | ||||||
SOURCE_DEFAULTS, | ||||||
DEFAULT_API_BASE_URL, | ||||||
APIErrorContext, | ||||||
CartoAPIError, | ||||||
GeojsonResult, | ||||||
|
@@ -12,6 +12,7 @@ import { | |||||
Format, | ||||||
MapType, | ||||||
QueryParameters, | ||||||
SourceOptions, | ||||||
buildPublicMapUrl, | ||||||
buildStatsUrl, | ||||||
h3QuerySource, | ||||||
|
@@ -45,14 +46,7 @@ type Dataset = { | |||||
|
||||||
/* global clearInterval, setInterval, URL */ | ||||||
/* eslint-disable complexity, max-statements, max-params */ | ||||||
async function _fetchMapDataset( | ||||||
dataset: Dataset, | ||||||
accessToken: string, | ||||||
apiBaseUrl: string, | ||||||
clientId?: string, | ||||||
headers?: Record<string, string>, | ||||||
maxLengthURL = SOURCE_DEFAULTS.maxLengthURL | ||||||
) { | ||||||
async function _fetchMapDataset(dataset: Dataset, context: _FetchMapContext) { | ||||||
const { | ||||||
aggregationExp, | ||||||
aggregationResLevel, | ||||||
|
@@ -66,16 +60,12 @@ async function _fetchMapDataset( | |||||
} = dataset; | ||||||
|
||||||
const cache: {value?: number} = {}; | ||||||
const globalOptions: any = { | ||||||
accessToken, | ||||||
apiBaseUrl, | ||||||
const globalOptions = { | ||||||
...context, | ||||||
cache, | ||||||
clientId, | ||||||
connectionName, | ||||||
format, | ||||||
headers, | ||||||
maxLengthURL | ||||||
}; | ||||||
format | ||||||
} as SourceOptions; | ||||||
|
||||||
if (type === 'tileset') { | ||||||
// TODO do we want a generic tilesetSource? | ||||||
|
@@ -120,14 +110,9 @@ async function _fetchMapDataset( | |||||
return cacheChanged; | ||||||
} | ||||||
|
||||||
async function _fetchTilestats( | ||||||
attribute: string, | ||||||
dataset: Dataset, | ||||||
accessToken: string, | ||||||
apiBaseUrl: string, | ||||||
maxLengthURL = SOURCE_DEFAULTS.maxLengthURL | ||||||
) { | ||||||
async function _fetchTilestats(attribute: string, dataset: Dataset, context: _FetchMapContext) { | ||||||
const {connectionName, data, id, source, type, queryParameters} = dataset; | ||||||
const {apiBaseUrl} = context; | ||||||
const errorContext: APIErrorContext = { | ||||||
requestType: 'Tile stats', | ||||||
connection: connectionName, | ||||||
|
@@ -140,7 +125,7 @@ async function _fetchTilestats( | |||||
|
||||||
const baseUrl = buildStatsUrl({attribute, apiBaseUrl, ...dataset}); | ||||||
const client = new URLSearchParams(data.tiles[0]).get('client'); | ||||||
const headers = {Authorization: `Bearer ${accessToken}`}; | ||||||
const headers = {Authorization: `Bearer ${context.accessToken}`}; | ||||||
const parameters: Record<string, string> = {}; | ||||||
if (client) { | ||||||
parameters.client = client; | ||||||
|
@@ -156,7 +141,7 @@ async function _fetchTilestats( | |||||
headers, | ||||||
parameters, | ||||||
errorContext, | ||||||
maxLengthURL | ||||||
maxLengthURL: context.maxLengthURL | ||||||
}); | ||||||
|
||||||
// Replace tilestats for attribute with value from API | ||||||
|
@@ -166,23 +151,14 @@ async function _fetchTilestats( | |||||
return true; | ||||||
} | ||||||
|
||||||
async function fillInMapDatasets( | ||||||
{datasets, token}: {datasets: Dataset[]; token: string}, | ||||||
clientId: string, | ||||||
apiBaseUrl: string, | ||||||
headers?: Record<string, string>, | ||||||
maxLengthURL = SOURCE_DEFAULTS.maxLengthURL | ||||||
) { | ||||||
const promises = datasets.map(dataset => | ||||||
_fetchMapDataset(dataset, token, apiBaseUrl, clientId, headers, maxLengthURL) | ||||||
); | ||||||
async function fillInMapDatasets({datasets}: {datasets: Dataset[]}, context: _FetchMapContext) { | ||||||
const promises = datasets.map(dataset => _fetchMapDataset(dataset, context)); | ||||||
return await Promise.all(promises); | ||||||
} | ||||||
|
||||||
async function fillInTileStats( | ||||||
{datasets, keplerMapConfig, token}: {datasets: Dataset[]; keplerMapConfig: any; token: string}, | ||||||
apiBaseUrl: string, | ||||||
maxLengthURL = SOURCE_DEFAULTS.maxLengthURL | ||||||
{datasets, keplerMapConfig}: {datasets: Dataset[]; keplerMapConfig: any}, | ||||||
context: _FetchMapContext | ||||||
) { | ||||||
const attributes: {attribute: string; dataset: any}[] = []; | ||||||
const {layers} = keplerMapConfig.config.visState; | ||||||
|
@@ -211,7 +187,7 @@ async function fillInTileStats( | |||||
} | ||||||
|
||||||
const promises = filteredAttributes.map(({attribute, dataset}) => | ||||||
_fetchTilestats(attribute, dataset, token, apiBaseUrl, maxLengthURL) | ||||||
_fetchTilestats(attribute, dataset, context) | ||||||
); | ||||||
return await Promise.all(promises); | ||||||
} | ||||||
|
@@ -260,6 +236,14 @@ export type FetchMapOptions = { | |||||
maxLengthURL?: number; | ||||||
}; | ||||||
|
||||||
/** | ||||||
* Context reused while fetching and updating a map with fetchMap(). | ||||||
*/ | ||||||
type _FetchMapContext = {apiBaseUrl: string} & Pick< | ||||||
FetchMapOptions, | ||||||
'accessToken' | 'clientId' | 'headers' | 'maxLengthURL' | ||||||
>; | ||||||
|
||||||
export type FetchMapResult = ParseMapResult & { | ||||||
/** | ||||||
* Basemap properties. | ||||||
|
@@ -271,16 +255,15 @@ export type FetchMapResult = ParseMapResult & { | |||||
/* eslint-disable max-statements */ | ||||||
export async function fetchMap({ | ||||||
accessToken, | ||||||
apiBaseUrl = SOURCE_DEFAULTS.apiBaseUrl, | ||||||
apiBaseUrl = DEFAULT_API_BASE_URL, | ||||||
cartoMapId, | ||||||
clientId = SOURCE_DEFAULTS.clientId, | ||||||
headers = {}, | ||||||
clientId, | ||||||
headers, | ||||||
autoRefresh, | ||||||
onNewData, | ||||||
maxLengthURL = SOURCE_DEFAULTS.maxLengthURL | ||||||
maxLengthURL | ||||||
}: FetchMapOptions): Promise<FetchMapResult> { | ||||||
assert(cartoMapId, 'Must define CARTO map id: fetchMap({cartoMapId: "XXXX-XXXX-XXXX"})'); | ||||||
assert(apiBaseUrl, 'Must define apiBaseUrl'); | ||||||
|
||||||
if (accessToken) { | ||||||
headers = {Authorization: `Bearer ${accessToken}`, ...headers}; | ||||||
|
@@ -298,23 +281,27 @@ export async function fetchMap({ | |||||
const baseUrl = buildPublicMapUrl({apiBaseUrl, cartoMapId}); | ||||||
const errorContext: APIErrorContext = {requestType: 'Public map', mapId: cartoMapId}; | ||||||
const map = await requestWithParameters({baseUrl, headers, errorContext, maxLengthURL}); | ||||||
const context: _FetchMapContext = { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Why the underscore? |
||||||
accessToken: map.token || accessToken, | ||||||
apiBaseUrl, | ||||||
clientId, | ||||||
headers, | ||||||
maxLengthURL | ||||||
}; | ||||||
|
||||||
// Periodically check if the data has changed. Note that this | ||||||
// will not update when a map is published. | ||||||
let stopAutoRefresh: (() => void) | undefined; | ||||||
if (autoRefresh) { | ||||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||||||
const intervalId = setInterval(async () => { | ||||||
const changed = await fillInMapDatasets( | ||||||
map, | ||||||
clientId, | ||||||
apiBaseUrl, | ||||||
{ | ||||||
const changed = await fillInMapDatasets(map, { | ||||||
...context, | ||||||
headers: { | ||||||
...headers, | ||||||
'If-Modified-Since': new Date().toUTCString() | ||||||
}, | ||||||
maxLengthURL | ||||||
); | ||||||
} | ||||||
}); | ||||||
if (onNewData && changed.some(v => v === true)) { | ||||||
onNewData(parseMap(map)); | ||||||
} | ||||||
|
@@ -343,11 +330,11 @@ export async function fetchMap({ | |||||
fetchBasemapProps({config: map.keplerMapConfig.config, errorContext}), | ||||||
|
||||||
// Mutates map.datasets so that dataset.data contains data | ||||||
fillInMapDatasets(map, clientId, apiBaseUrl, headers, maxLengthURL) | ||||||
fillInMapDatasets(map, context) | ||||||
]); | ||||||
|
||||||
// Mutates attributes in visualChannels to contain tile stats | ||||||
await fillInTileStats(map, apiBaseUrl, maxLengthURL); | ||||||
await fillInTileStats(map, context); | ||||||
|
||||||
const out = {...parseMap(map), basemap, ...{stopAutoRefresh}}; | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this a change of behavior?