diff --git a/packages/common/src/Expressions/Expression.ts b/packages/common/src/Expressions/Expression.ts index dd7687d71..a0f16d9d9 100644 --- a/packages/common/src/Expressions/Expression.ts +++ b/packages/common/src/Expressions/Expression.ts @@ -49,7 +49,7 @@ export abstract class Expression implements IExpression { json: JSONExpression; - supportsPartialEval: boolean = false; + supportsPartialEval: boolean; constructor(json: JSONExpression, env: ExpressionParser) { // this.id = expId++; this.json = json; @@ -83,9 +83,6 @@ export abstract class Expression implements IExpression { resolve(context=this.env.context, mode?: ExpressionMode) { const {env} = this; - env.setMode(mode); - const result = env.evaluate(this, context); - env.setMode(ExpressionMode.static); - return result; + return env.evaluate(this, context, mode); } } diff --git a/packages/common/src/Expressions/ExpressionParser.ts b/packages/common/src/Expressions/ExpressionParser.ts index 8d64a9732..ac636a1d7 100644 --- a/packages/common/src/Expressions/ExpressionParser.ts +++ b/packages/common/src/Expressions/ExpressionParser.ts @@ -17,10 +17,9 @@ * License-Filename: LICENSE */ -import {Expression, ExpressionMode, IExpression, JSONExpression} from './Expression'; +import {Expression, ExpressionMode, JSONExpression} from './Expression'; import * as Expressions from './Expressions'; import * as InterpolateExpressions from './InterpolateExpression'; -import {JSUtils} from '@here/xyz-maps-common'; type ResultCache = Map & { hits?: number }; @@ -69,7 +68,15 @@ export class ExpressionParser { // this.cache.get = this.cache.set =()=>undefined; // this.defaultResultCache.get = this.defaultResultCache.set =()=>undefined; - // this.dynamicResultCache.get = this.defaultResultCache.set =()=>undefined; + // this.dynamicResultCache.get = this.dynamicResultCache.set =()=>undefined; + // this.dynamicResultCache.get = (exp)=>{ + // console.log('dynamicResultCache get'); + // }; + // this.dynamicResultCache._clear = this.dynamicResultCache.clear; + // this.dynamicResultCache.clear =()=>{ + // console.log('dynamicResultCache cleared'); + // this.dynamicResultCache._clear(); + // }; } init(def, mapContext) { @@ -87,10 +94,11 @@ export class ExpressionParser { this.cache.clear(); } - evaluate(exp, context) { - exp = this.parseJSON(exp); + evaluate(exp, context, mode: ExpressionMode = ExpressionMode.static) { let result; + exp = this.parseJSON(exp); try { + this.setMode(mode); result = this.evaluateParsed(exp, context); } catch (e) { if (e.message === 'DynamicExpressionInterrupt') { @@ -189,6 +197,9 @@ export class ExpressionParser { clearResultCache() { this.defaultResultCache.clear(); + } + + clearDynamicResultCache() { this.dynamicResultCache.clear(); } diff --git a/packages/core/src/styles/XYZLayerStyle.ts b/packages/core/src/styles/XYZLayerStyle.ts index f8e56c0b0..4e404d9ce 100644 --- a/packages/core/src/styles/XYZLayerStyle.ts +++ b/packages/core/src/styles/XYZLayerStyle.ts @@ -19,9 +19,8 @@ import {Feature} from '../features/Feature'; import {Color, LayerStyle, Style, StyleGroup, StyleGroupMap, StyleValueFunction, StyleZoomRange} from '../styles/LayerStyle'; -import {ExpressionParser, JSONExpression} from '@here/xyz-maps-common'; +import {Expression, ExpressionParser, JSONExpression} from '@here/xyz-maps-common'; import {TileLayer} from '../layers/TileLayer'; -// import {JsonExpression} from '@here/xyz-maps-common'; const isTypedArray = (() => { const TypedArray = Object.getPrototypeOf(Uint8Array); @@ -94,9 +93,8 @@ export class XYZLayerStyle implements LayerStyle { const property = styleJSON[p]; this[p] = p == 'styleGroups' ? deepCopy(property) : property; } - // layerStyle._l = layer; - this.definitions ||= {}; + this.definitions ||= {}; this.expParser = new ExpressionParser(this.definitions, this.exprContext); if (!styleJSON.assign) { @@ -107,14 +105,10 @@ export class XYZLayerStyle implements LayerStyle { styleGrp = [styleGrp]; } for (let style of styleGrp) { - // for (let prop in style) { - // if ( ExpressionParser.isJSONExp(style[prop])) { - // style[prop] = this.createExpEvaluator(style[prop]); - // } - // } if (style.filter) { if (ExpressionParser.isJSONExp(style.filter)) { - style.filter = this.createExpEvaluator(style.filter); + // style.filter = this.createExpEvaluator(style.filter); + style.filter = this.expParser.parseJSON(style.filter); } flatStyles.push(style); } @@ -128,18 +122,6 @@ export class XYZLayerStyle implements LayerStyle { this._style = styleJSON; } - private createExpEvaluator(expr: JSONExpression) { - return (feature) => { - return this.expParser.evaluate(expr, feature.properties); - // window._measure.count('filter-calls'); - // window._measure.start('filter-eval'); - // let filtered = this.expParser.evaluate(expr, feature.properties); - // window._measure.stop('filter-eval'); - // return filtered; - }; - }; - - // default: simple assignment based on geometryType. assign(feature: Feature, level?: number): string | null | undefined { // return this.flatStyles; // let filteredStyleGroups = this.flatStyles.filter((style) => { @@ -156,9 +138,10 @@ export class XYZLayerStyle implements LayerStyle { for (let style of this.flatStyles) { const {filter} = style; - const filtered = - typeof filter === 'function' ? (style.filter as StyleValueFunction)(feature, level) - // : ExpressionParser.isJSONExp(filter) ? this.expParser.evaluate(filter, feature.properties) + const filtered = filter instanceof Expression + ? filter.resolve(feature.properties) + : typeof filter === 'function' + ? (style.filter as StyleValueFunction)(feature, level) : filter; if (filtered) { @@ -168,19 +151,11 @@ export class XYZLayerStyle implements LayerStyle { if (i > 0) { filteredStyleGroups.length = i; - // filteredStyleGroups.__p = false; return filteredStyleGroups; } // return feature.geometry.type; }; - // get : function( feature, level ) - // { - // return this.styleGroups[ - // this.assign( feature, level ) - // ]; - // }, - getDefinitions() { return this.definitions; } @@ -256,11 +231,6 @@ export class XYZLayerStyle implements LayerStyle { } return group; - - // if( layer = this._l ) - // { - // layer._l.trigger( 'style', [ feature, group, layer ], true ); - // } }; merge(grp1: readonly Style[], grp2: StyleGroup | false) { diff --git a/packages/display/src/displays/Layers.ts b/packages/display/src/displays/Layers.ts index 00bce01f6..f38502528 100644 --- a/packages/display/src/displays/Layers.ts +++ b/packages/display/src/displays/Layers.ts @@ -222,7 +222,7 @@ class Layers extends Array { if (expContext && expContext.zoom != zoomlevel) { expContext.zoom = zoomlevel; expContext.$zoom = zoomlevel^0; - // expParser.dynamicResultCache.clear(); + expParser.clearDynamicResultCache(); } } }