Skip to content

Commit

Permalink
fix(sparql): Remove duplicate loadCounter
Browse files Browse the repository at this point in the history
Possibly breaking change in logic when the 'featuresloadend' event is dispatched for SparqlJson source.
  • Loading branch information
jmacura committed Feb 23, 2024
1 parent 1edcc29 commit aac00d6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 59 deletions.
10 changes: 4 additions & 6 deletions projects/hslayers/common/layers/hs.source.SparqlJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type SparqlOptions = {
/**
* When set to 'virtuoso', the query will use Virtuoso's optimised "bif" functions instead of standardised GeoSPARQL functions.
* When set to 'wikibase', the query will use Blazegraph & Wikibase SERVICE instead of standardised GeoSPARQL functions.
* When unset, the query will use standardised GeoSPARQL functions.
* @default undefined
*/
optimization?: 'virtuoso' | 'wikibase';
Expand Down Expand Up @@ -72,7 +73,6 @@ export class SparqlJson extends Vector {
category_id = 0;
legend_categories;
loadCounter: number;
loadTotal: number;
occupied_xy: {[coord: string]: boolean} = {};
//What is it good for?
styleAble: boolean;
Expand Down Expand Up @@ -120,6 +120,8 @@ export class SparqlJson extends Vector {
geomAttribute = geomAttribute.slice(1);
}
this.set('loaded', false);
this.loadCounter += 1;
// No need to manually dispatch 'featuresloadstart' event as it is casted automatically when the loader is executed
if (typeof clear_on_move !== 'undefined' && clear_on_move) {
this.clear();
}
Expand All @@ -134,8 +136,6 @@ export class SparqlJson extends Vector {
if (console && typeof this.get('geoname') !== 'undefined') {
console.log('Get ', this.get('geoname'));
}
this.loadCounter += 1;
this.loadTotal += 1;
const response = await fetch(url, {
headers: {
'Accept':
Expand Down Expand Up @@ -173,7 +173,6 @@ export class SparqlJson extends Vector {
data.results.bindings.length
);
}*/
this.loadCounter -= 1;
const objects = {};
for (const item of data.results.bindings) {
const id = item[idAttribute]?.value;
Expand Down Expand Up @@ -206,9 +205,9 @@ export class SparqlJson extends Vector {
this.hasPoint = true;
this.loadCounter -= 1;
this.set('last_feature_count', Object.keys(objects).length);
this.dispatchEvent('featuresloadend');
if (this.loadCounter == 0) {
this.set('loaded', true);
this.dispatchEvent('featuresloadend');
}
},
strategy:
Expand All @@ -225,7 +224,6 @@ export class SparqlJson extends Vector {
},
});
this.loadCounter = 0;
this.loadTotal = 0;
this.legend_categories = this.category_map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,59 @@ export class HsLayerManagerLoadingProgressService {
private hsEventBusService: HsEventBusService,
) {}

private determineLayerType(olLayer) {
/**
* Create events for checking whether the layer is being loaded or is loaded
* @param layer - Layer which is being added
*/
loadingEvents(layer: HsLayerDescriptor): void {
const olLayer = layer.layer;
const showInLM = getShowInLayerManager(olLayer);
if (
(getBase(olLayer) && this.hsConfig.componentsEnabled.basemapGallery) ||
!(showInLM === undefined || showInLM == true)
) {
return;
}
const source: any = olLayer.get('cluster')
? (olLayer.getSource() as Cluster).getSource()
: olLayer.getSource();
if (!source) {
this.hsLog.error(`Layer ${getTitle(olLayer)} has no source`);
return;
}
const loadProgress: HsLayerLoadProgress = {
pending: 0,
total: 0,
loadError: 0,
loaded: true,
error: undefined,
percents: 0,
};
layer.loadProgress = loadProgress;

const layerType = this.determineLayerType(olLayer);

if (!layerType) {
return;
}

this.createLoadingProgressTimer(loadProgress, olLayer);
const loadStart = this.subscribeToEventSubject(1, loadProgress, olLayer);
const loadEnd = this.subscribeToEventSubject(-1, loadProgress, olLayer);

source.on(`${layerType}loadstart`, (e) => {
loadStart.next(true);
});
source.on(`${layerType}loadend`, (e) => {
loadEnd.next(true);
loadProgress.error = false;
});
source.on(`${layerType}loaderror`, (e) => {
this.loadError(loadProgress, olLayer, this[`${layerType}LoadFailed`]);
});
}

private determineLayerType(olLayer: Layer) {
if (this.hsUtilsService.instOf(olLayer, VectorLayer)) {
return 'features';
} else if (this.hsUtilsService.instOf(olLayer, ImageLayer)) {
Expand Down Expand Up @@ -98,58 +150,6 @@ export class HsLayerManagerLoadingProgressService {
}
}

/**
* Create events for checking whether the layer is being loaded or is loaded
* @param layer - Layer which is being added
*/
loadingEvents(layer: HsLayerDescriptor): void {
const olLayer = layer.layer;
const showInLM = getShowInLayerManager(olLayer);
if (
(getBase(olLayer) && this.hsConfig.componentsEnabled.basemapGallery) ||
!(showInLM === undefined || showInLM == true)
) {
return;
}
const source: any = olLayer.get('cluster')
? (olLayer.getSource() as Cluster).getSource()
: olLayer.getSource();
if (!source) {
this.hsLog.error(`Layer ${getTitle(olLayer)} has no source`);
return;
}
const loadProgress: HsLayerLoadProgress = {
pending: 0,
total: 0,
loadError: 0,
loaded: true,
error: undefined,
percents: 0,
};
layer.loadProgress = loadProgress;

const layerType = this.determineLayerType(olLayer);

if (!layerType) {
return;
}

this.createLoadingProgressTimer(loadProgress, olLayer);
const loadStart = this.subscribeToEventSubject(1, loadProgress, olLayer);
const loadEnd = this.subscribeToEventSubject(-1, loadProgress, olLayer);

source.on(`${layerType}loadstart`, (e) => {
loadStart.next(true);
});
source.on(`${layerType}loadend`, (e) => {
loadEnd.next(true);
loadProgress.error = false;
});
source.on(`${layerType}loaderror`, (e) => {
this.loadError(loadProgress, olLayer, this[`${layerType}LoadFailed`]);
});
}

/**
* Creates loading progress timer which controls the executions of load events callbacks
* and tries to reset progress once the loading has finished (no execution in 2000ms)
Expand Down

0 comments on commit aac00d6

Please sign in to comment.