Skip to content

Commit a2d6484

Browse files
Share data cache
1 parent 20079aa commit a2d6484

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

src/layers/data.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const cache = new Map()
2+
export const getLayerData = <T>(layer: { getData: () => Promise<T> }): Promise<T> => {
3+
if (cache.has(layer)) return cache.get(layer)
4+
5+
const promise = layer.getData()
6+
cache.set(layer, promise)
7+
return promise
8+
}

src/layers/huts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { fromLonLat } from 'ol/proj';
44
import { Circle, Fill, Stroke, Style, Text } from 'ol/style';
55
import type { Place } from '../search/places';
66
import fragment from '../stores/fragment';
7+
import { getLayerData } from './data';
78

89
const styleCache = {};
910
export default {
@@ -43,7 +44,7 @@ export default {
4344
return data as Place[]
4445
},
4546
async getFeatures() {
46-
const data = await this.getData()
47+
const data = await getLayerData(this)
4748
return data.map(hut => {
4849
const coords = fromLonLat([hut.lon, hut.lat]);
4950
const feature = new Feature(new Point(coords));

src/layers/mountains.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Circle, Fill, Stroke, Style, Text } from 'ol/style';
55

66
import fragment from '../stores/fragment';
77
import mountains, { type Mountains } from '../stores/mountains';
8+
import { getLayerData } from './data';
89

910
const styleCache = {};
1011

@@ -42,7 +43,7 @@ export default {
4243
return result
4344
},
4445
async getFeatures() {
45-
const data = await this.getData();
46+
const data = await getLayerData(this);
4647
const points = Object.values(data).filter(a => a.latlng);
4748
return points.map(mountain => {
4849
const coords = fromLonLat([mountain.latlng![1], mountain.latlng![0]]);

src/search/places.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getLayerData } from '../layers/data';
12
import huts from '../layers/huts'
23
import mountains from '../layers/mountains';
34

@@ -19,8 +20,8 @@ const makePlacesPromise = async (sources: (() => Promise<Place[]>)[]) => {
1920
export const getPlaces = () => {
2021
if (!placesPromise) {
2122
placesPromise = makePlacesPromise([
22-
huts.getData,
23-
() => mountains.getData()
23+
() => getLayerData(huts),
24+
() => getLayerData(mountains)
2425
.then(r => Object.entries(r)
2526
.filter(([, m]) => m.latlng?.length && m.name)
2627
.map(([url, m]) => ({ name: m.name, lat: m.latlng![0], lon: m.latlng![1], type: 'peak', href: url }))),

0 commit comments

Comments
 (0)