only update overlay if there was a change in the data

This commit is contained in:
Gwendolyn 2024-12-27 00:17:26 +01:00
parent 366c9ab5ad
commit 0a10de795d
2 changed files with 31 additions and 9 deletions

View file

@ -3170,6 +3170,7 @@ class DataOverlay {
levels = null; levels = null;
feature_geometries = {}; feature_geometries = {};
fetch_timeout = null; fetch_timeout = null;
etag = null;
constructor(options) { constructor(options) {
this.id = options.id; this.id = options.id;
@ -3185,10 +3186,14 @@ class DataOverlay {
} }
async create() { async create() {
const [features, feature_geometries] = await Promise.all([ const [
c3nav_api.get(`mapdata/dataoverlayfeatures/?overlay=${this.id}`), {data: features, etag},
feature_geometries
] = await Promise.all([
c3nav_api.get_with_etag(`mapdata/dataoverlayfeatures/?overlay=${this.id}`, null),
c3nav_api.get(`mapdata/dataoverlayfeaturegeometries/?overlay=${this.id}`) c3nav_api.get(`mapdata/dataoverlayfeaturegeometries/?overlay=${this.id}`)
]); ]);
this.etag = etag;
this.feature_geometries = Object.fromEntries(feature_geometries.map(f => [f.id, f.geometry])); this.feature_geometries = Object.fromEntries(feature_geometries.map(f => [f.id, f.geometry]));
@ -3207,9 +3212,12 @@ class DataOverlay {
window.clearTimeout(this.fetch_timeout); window.clearTimeout(this.fetch_timeout);
this.fetch_timeout = null; this.fetch_timeout = null;
} }
const features= await c3nav_api.get(`mapdata/dataoverlayfeatures/?overlay=${this.id}`); const {data: features, etag} = await c3nav_api.get_with_etag(`mapdata/dataoverlayfeatures/?overlay=${this.id}`, this.etag);
if (features !== null) {
this.update_features(features); this.update_features(features);
this.etag = etag;
}
if (this.update_interval !== null && this.fetch_timeout === null) { if (this.update_interval !== null && this.fetch_timeout === null) {
this.fetch_timeout = window.setTimeout(() => { this.fetch_timeout = window.setTimeout(() => {

View file

@ -49,20 +49,34 @@
if (typeof body !== 'undefined') { if (typeof body !== 'undefined') {
init.body = JSON.stringify(body); init.body = JSON.stringify(body);
} }
const res = await fetch(this.make_url(path), init); return await fetch(this.make_url(path), init);
return await res.json();
} }
get(path) { get(path) {
return this.req('GET', path); return this.req('GET', path).then(r => r.json());
}
async get_with_etag(path, etag) {
const res = await this.req('GET', path);
const res_etag = res.headers.get('etag');
if (etag !== null && res_etag === etag) {
return {
etag: res_etag,
data: null,
};
}
return {
etag: res_etag,
data: await res.json(),
};
} }
post(path, data) { post(path, data) {
return this.req('POST', path, data); return this.req('POST', path, data).then(r => r.json());
} }
put(path, data) { put(path, data) {
return this.req('PUT', path, data); return this.req('PUT', path, data).then(r => r.json());
} }
} }