forked from MapComplete/MapComplete
Performance hacks
This commit is contained in:
parent
686fb29ed3
commit
7090a5ceb8
15 changed files with 167 additions and 93 deletions
|
@ -39,20 +39,6 @@ export default class SelectedFeatureHandler {
|
|||
hash.addCallback(() => self.setSelectedElementFromHash())
|
||||
|
||||
|
||||
// IF the selected element changes, set the hash correctly
|
||||
state.selectedElement.addCallback(feature => {
|
||||
if (feature === undefined) {
|
||||
if (!SelectedFeatureHandler._no_trigger_on.has(hash.data)) {
|
||||
hash.setData("")
|
||||
}
|
||||
}
|
||||
|
||||
const h = feature?.properties?.id;
|
||||
if (h !== undefined) {
|
||||
hash.setData(h)
|
||||
}
|
||||
})
|
||||
|
||||
state.featurePipeline?.newDataLoadedSignal?.addCallbackAndRunD(_ => {
|
||||
// New data was loaded. In initial startup, the hash might be set (via the URL) but might not be selected yet
|
||||
if (hash.data === undefined || SelectedFeatureHandler._no_trigger_on.has(hash.data)) {
|
||||
|
|
|
@ -201,9 +201,10 @@ export interface TiledFeatureSourceOptions {
|
|||
readonly minZoomLevel?: number,
|
||||
/**
|
||||
* IF minZoomLevel is set, and if a feature runs through a tile boundary, it would normally be duplicated.
|
||||
* Setting 'dontEnforceMinZoomLevel' will still allow bigger zoom levels for those features
|
||||
* Setting 'dontEnforceMinZoomLevel' will still allow bigger zoom levels for those features.
|
||||
* If 'pick_first' is set, the feature will not be duplicated but set to some tile
|
||||
*/
|
||||
readonly dontEnforceMinZoom?: boolean,
|
||||
readonly dontEnforceMinZoom?: boolean | "pick_first",
|
||||
readonly registerTile?: (tile: TiledFeatureSource & FeatureSourceForLayer & Tiled) => void,
|
||||
readonly layer?: FilteredLayer
|
||||
}
|
|
@ -56,6 +56,7 @@ export default class MetaTagging {
|
|||
const feature = ff.feature
|
||||
const freshness = ff.freshness
|
||||
let somethingChanged = false
|
||||
let definedTags = new Set(Object.getOwnPropertyNames( feature.properties ))
|
||||
for (const metatag of metatagsToApply) {
|
||||
try {
|
||||
if (!metatag.keys.some(key => feature.properties[key] === undefined)) {
|
||||
|
@ -64,8 +65,11 @@ export default class MetaTagging {
|
|||
}
|
||||
|
||||
if (metatag.isLazy) {
|
||||
if(!metatag.keys.some(key => !definedTags.has(key))) {
|
||||
// All keys are defined - lets skip!
|
||||
continue
|
||||
}
|
||||
somethingChanged = true;
|
||||
|
||||
metatag.applyMetaTagsOnFeature(feature, freshness, layer, state)
|
||||
} else {
|
||||
const newValueAdded = metatag.applyMetaTagsOnFeature(feature, freshness, layer, state)
|
||||
|
@ -84,12 +88,13 @@ export default class MetaTagging {
|
|||
}
|
||||
|
||||
if (layerFuncs !== undefined) {
|
||||
let retaggingChanged = false;
|
||||
try {
|
||||
layerFuncs(params, feature)
|
||||
retaggingChanged = layerFuncs(params, feature)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
somethingChanged = true
|
||||
somethingChanged = somethingChanged || retaggingChanged
|
||||
}
|
||||
|
||||
if (somethingChanged) {
|
||||
|
@ -99,8 +104,8 @@ export default class MetaTagging {
|
|||
}
|
||||
return atLeastOneFeatureChanged
|
||||
}
|
||||
public static createFunctionsForFeature(layerId: string, calculatedTags: [string, string, boolean][]): ((feature: any) => void)[] {
|
||||
const functions: ((feature: any) => void)[] = [];
|
||||
public static createFunctionsForFeature(layerId: string, calculatedTags: [string, string, boolean][]): ((feature: any) => boolean)[] {
|
||||
const functions: ((feature: any) => boolean)[] = [];
|
||||
|
||||
for (const entry of calculatedTags) {
|
||||
const key = entry[0]
|
||||
|
@ -110,10 +115,9 @@ export default class MetaTagging {
|
|||
continue;
|
||||
}
|
||||
|
||||
const calculateAndAssign = (feat) => {
|
||||
|
||||
|
||||
const calculateAndAssign: ((feat: any) => boolean) = (feat) => {
|
||||
try {
|
||||
let oldValue = isStrict ? feat.properties[key] : undefined
|
||||
let result = new Function("feat", "return " + code + ";")(feat);
|
||||
if (result === "") {
|
||||
result === undefined
|
||||
|
@ -124,7 +128,7 @@ export default class MetaTagging {
|
|||
}
|
||||
delete feat.properties[key]
|
||||
feat.properties[key] = result;
|
||||
return result;
|
||||
return result === oldValue;
|
||||
}catch(e){
|
||||
if (MetaTagging.errorPrintCount < MetaTagging.stopErrorOutputAt) {
|
||||
console.warn("Could not calculate a " + (isStrict ? "strict " : "") + " calculated tag for key " + key + " defined by " + code + " (in layer" + layerId + ") due to \n" + e + "\n. Are you the theme creator? Doublecheck your code. Note that the metatags might not be stable on new features", e, e.stack)
|
||||
|
@ -133,6 +137,7 @@ export default class MetaTagging {
|
|||
console.error("Got ", MetaTagging.stopErrorOutputAt, " errors calculating this metatagging - stopping output now")
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -149,9 +154,11 @@ export default class MetaTagging {
|
|||
configurable: true,
|
||||
enumerable: false, // By setting this as not enumerable, the localTileSaver will _not_ calculate this
|
||||
get: function () {
|
||||
return calculateAndAssign(feature)
|
||||
calculateAndAssign(feature)
|
||||
return feature.properties[key]
|
||||
}
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
|
@ -160,17 +167,23 @@ export default class MetaTagging {
|
|||
return functions;
|
||||
}
|
||||
|
||||
private static retaggingFuncCache = new Map<string, ((feature: any) => void)[]>()
|
||||
private static retaggingFuncCache = new Map<string, ((feature: any) => boolean)[]>()
|
||||
|
||||
/**
|
||||
* Creates the function which adds all the calculated tags to a feature. Called once per layer
|
||||
* @param layer
|
||||
* @param state
|
||||
* @private
|
||||
*/
|
||||
private static createRetaggingFunc(layer: LayerConfig, state):
|
||||
((params: ExtraFuncParams, feature: any) => void) {
|
||||
((params: ExtraFuncParams, feature: any) => boolean) {
|
||||
|
||||
const calculatedTags: [string, string, boolean][] = layer.calculatedTags;
|
||||
if (calculatedTags === undefined || calculatedTags.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let functions = MetaTagging.retaggingFuncCache.get(layer.id);
|
||||
let functions :((feature: any) => boolean)[] = MetaTagging.retaggingFuncCache.get(layer.id);
|
||||
if (functions === undefined) {
|
||||
functions = MetaTagging.createFunctionsForFeature(layer.id, calculatedTags)
|
||||
MetaTagging.retaggingFuncCache.set(layer.id, functions)
|
||||
|
@ -192,6 +205,7 @@ export default class MetaTagging {
|
|||
} catch (e) {
|
||||
console.error("Invalid syntax in calculated tags or some other error: ", e)
|
||||
}
|
||||
return true; // Something changed
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -272,7 +272,7 @@ export default class SimpleMetaTaggers {
|
|||
public static country = new CountryTagger()
|
||||
private static isOpen = new SimpleMetaTagger(
|
||||
{
|
||||
keys: ["_isOpen", "_isOpen:description"],
|
||||
keys: ["_isOpen"],
|
||||
doc: "If 'opening_hours' is present, it will add the current state of the feature (being 'yes' or 'no')",
|
||||
includesDates: true,
|
||||
isLazy: true
|
||||
|
@ -283,7 +283,7 @@ export default class SimpleMetaTaggers {
|
|||
// isOpen is irrelevant
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
Object.defineProperty(feature.properties, "_isOpen", {
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
|
@ -291,7 +291,8 @@ export default class SimpleMetaTaggers {
|
|||
delete feature.properties._isOpen
|
||||
feature.properties._isOpen = undefined
|
||||
const tagsSource = state.allElements.getEventSourceById(feature.properties.id);
|
||||
tagsSource.addCallbackAndRunD(tags => {
|
||||
tagsSource
|
||||
.addCallbackAndRunD(tags => {
|
||||
if (tags.opening_hours === undefined || tags._country === undefined) {
|
||||
return;
|
||||
}
|
||||
|
@ -341,7 +342,6 @@ export default class SimpleMetaTaggers {
|
|||
}
|
||||
}
|
||||
updateTags();
|
||||
return true; // Our job is done, lets unregister!
|
||||
} catch (e) {
|
||||
console.warn("Error while parsing opening hours of ", tags.id, e);
|
||||
delete tags._isOpen
|
||||
|
@ -352,6 +352,7 @@ export default class SimpleMetaTaggers {
|
|||
return undefined
|
||||
}
|
||||
})
|
||||
return true;
|
||||
|
||||
})
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue