Skip to content

Commit 392b6e8

Browse files
authored
Merge pull request #9 from nasa/feature/GUUI-3595-data-rods-default-plot
feature(data-rods): add variable caching to avoid race conditions
2 parents e55e391 + 7c71483 commit 392b6e8

1 file changed

Lines changed: 57 additions & 13 deletions

File tree

src/metadata-catalog/tasks.ts

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ import { Task } from '@lit/task'
22
import { GiovanniVariableCatalog } from './giovanni-variable-catalog.js'
33
import type { HostWithMaybeProperties } from './types.js'
44
import { getVariableEntryId } from './utilities.js'
5+
import type { Variable } from '../components/browse-variables/browse-variables.types.js'
6+
7+
// Global cache for variable data to prevent duplicate requests
8+
const variableCache = new Map<string, Variable>()
9+
const pendingRequests = new Map<string, Promise<Variable | null>>()
10+
11+
function setHostPropertiesFromVariable(
12+
host: HostWithMaybeProperties,
13+
variable: Variable,
14+
variableEntryId: string
15+
) {
16+
host.startDate = host.startDate ?? variable.exampleInitialStartDate?.toISOString()
17+
host.endDate = host.endDate ?? variable.exampleInitialEndDate?.toISOString()
18+
host.catalogVariable = variable
19+
host.variableEntryId = variableEntryId
20+
}
521

622
export function getFetchVariableTask(
723
host: HostWithMaybeProperties,
@@ -10,31 +26,59 @@ export function getFetchVariableTask(
1026
const catalog = new GiovanniVariableCatalog() // TODO: replace this with a factory call when we switch to CMR
1127

1228
return new Task(host, {
13-
task: async (_args, { signal }) => {
29+
task: async _args => {
1430
const variableEntryId = getVariableEntryId(host)
1531

16-
console.debug('fetch variable ', variableEntryId)
32+
console.debug('Fetch variable ', variableEntryId)
1733

1834
if (!variableEntryId) {
1935
return
2036
}
2137

22-
const variable = await catalog.getVariable(variableEntryId, {
23-
signal,
24-
})
38+
// Check if we already have this variable cached
39+
if (variableCache.has(variableEntryId)) {
40+
console.debug('Using cached variable ', variableEntryId)
41+
const cachedVariable = variableCache.get(variableEntryId)!
42+
setHostPropertiesFromVariable(host, cachedVariable, variableEntryId)
43+
return
44+
}
2545

26-
console.debug('found variable ', variable)
46+
// Check if there's already a pending request for this variable
47+
if (pendingRequests.has(variableEntryId)) {
48+
console.debug(
49+
'Waiting for pending request for variable ',
50+
variableEntryId
51+
)
52+
const variable = await pendingRequests.get(variableEntryId)!
2753

28-
if (!variable) {
54+
if (variable) {
55+
console.debug('Using cached variable ', variableEntryId)
56+
setHostPropertiesFromVariable(host, variable, variableEntryId)
57+
}
2958
return
3059
}
3160

32-
host.startDate =
33-
host.startDate ?? variable.exampleInitialStartDate?.toISOString()
34-
host.endDate =
35-
host.endDate ?? variable.exampleInitialEndDate?.toISOString()
36-
host.catalogVariable = variable
37-
host.variableEntryId = variableEntryId
61+
// Create a new request and cache the promise
62+
const requestPromise = catalog.getVariable(variableEntryId)
63+
pendingRequests.set(variableEntryId, requestPromise)
64+
65+
try {
66+
const variable = await requestPromise
67+
68+
console.debug('Found variable ', variable)
69+
70+
if (!variable) {
71+
return
72+
}
73+
74+
// Cache the variable for future use
75+
variableCache.set(variableEntryId, variable)
76+
77+
setHostPropertiesFromVariable(host, variable, variableEntryId)
78+
} finally {
79+
// Clean up the pending request
80+
pendingRequests.delete(variableEntryId)
81+
}
3882
},
3983
args: () => [host.variableEntryId, host.collection, host.variable],
4084
autoRun,

0 commit comments

Comments
 (0)