@@ -73,13 +73,15 @@ export class StyleExpression {
7373 _defaultValue : Value ;
7474 _warningHistory : { [ key : string ] : boolean } ;
7575 _enumValues : { [ _ : string ] : any } ;
76+ readonly _globalState : Record < string , any > ;
7677
77- constructor ( expression : Expression , propertySpec ?: StylePropertySpecification | null ) {
78+ constructor ( expression : Expression , propertySpec ?: StylePropertySpecification | null , globalState ?: Record < string , any > ) {
7879 this . expression = expression ;
7980 this . _warningHistory = { } ;
8081 this . _evaluator = new EvaluationContext ( ) ;
8182 this . _defaultValue = propertySpec ? getDefaultValue ( propertySpec ) : null ;
8283 this . _enumValues = propertySpec && propertySpec . type === 'enum' ? propertySpec . values : null ;
84+ this . _globalState = globalState ;
8385 }
8486
8587 evaluateWithoutErrorHandling (
@@ -90,6 +92,9 @@ export class StyleExpression {
9092 availableImages ?: Array < string > ,
9193 formattedSection ?: FormattedSection
9294 ) : any {
95+ if ( this . _globalState ) {
96+ globals = { ...globals , globalState : this . _globalState } ;
97+ }
9398 this . _evaluator . globals = globals ;
9499 this . _evaluator . feature = feature ;
95100 this . _evaluator . featureState = featureState ;
@@ -108,6 +113,9 @@ export class StyleExpression {
108113 availableImages ?: Array < string > ,
109114 formattedSection ?: FormattedSection
110115 ) : any {
116+ if ( this . _globalState ) {
117+ globals = { ...globals , globalState : this . _globalState } ;
118+ }
111119 this . _evaluator . globals = globals ;
112120 this . _evaluator . feature = feature || null ;
113121 this . _evaluator . featureState = featureState || null ;
@@ -150,7 +158,7 @@ export function isExpression(expression: unknown) {
150158 *
151159 * @private
152160 */
153- export function createExpression ( expression : unknown , propertySpec ?: StylePropertySpecification | null ) : Result < StyleExpression , Array < ExpressionParsingError > > {
161+ export function createExpression ( expression : unknown , propertySpec ?: StylePropertySpecification | null , globalState ?: Record < string , any > ) : Result < StyleExpression , Array < ExpressionParsingError > > {
154162 const parser = new ParsingContext ( expressions , isExpressionConstant , [ ] , propertySpec ? getExpectedType ( propertySpec ) : undefined ) ;
155163
156164 // For string-valued properties, coerce to string at the top level rather than asserting.
@@ -161,20 +169,22 @@ export function createExpression(expression: unknown, propertySpec?: StyleProper
161169 return error ( parser . errors ) ;
162170 }
163171
164- return success ( new StyleExpression ( parsed , propertySpec ) ) ;
172+ return success ( new StyleExpression ( parsed , propertySpec , globalState ) ) ;
165173}
166174
167175export class ZoomConstantExpression < Kind extends EvaluationKind > {
168176 kind : Kind ;
169177 isStateDependent : boolean ;
170178 globalStateRefs : Set < string > ;
171179 _styleExpression : StyleExpression ;
180+ readonly _globalState : Record < string , any > ;
172181
173- constructor ( kind : Kind , expression : StyleExpression ) {
182+ constructor ( kind : Kind , expression : StyleExpression , globalState ?: Record < string , any > ) {
174183 this . kind = kind ;
175184 this . _styleExpression = expression ;
176185 this . isStateDependent = kind !== ( 'constant' as EvaluationKind ) && ! isStateConstant ( expression . expression ) ;
177186 this . globalStateRefs = findGlobalStateRefs ( expression . expression ) ;
187+ this . _globalState = globalState ;
178188 }
179189
180190 evaluateWithoutErrorHandling (
@@ -185,6 +195,9 @@ export class ZoomConstantExpression<Kind extends EvaluationKind> {
185195 availableImages ?: Array < string > ,
186196 formattedSection ?: FormattedSection
187197 ) : any {
198+ if ( this . _globalState ) {
199+ globals = { ...globals , globalState : this . _globalState } ;
200+ }
188201 return this . _styleExpression . evaluateWithoutErrorHandling ( globals , feature , featureState , canonical , availableImages , formattedSection ) ;
189202 }
190203
@@ -196,6 +209,9 @@ export class ZoomConstantExpression<Kind extends EvaluationKind> {
196209 availableImages ?: Array < string > ,
197210 formattedSection ?: FormattedSection
198211 ) : any {
212+ if ( this . _globalState ) {
213+ globals = { ...globals , globalState : this . _globalState } ;
214+ }
199215 return this . _styleExpression . evaluate ( globals , feature , featureState , canonical , availableImages , formattedSection ) ;
200216 }
201217}
@@ -207,14 +223,16 @@ export class ZoomDependentExpression<Kind extends EvaluationKind> {
207223 globalStateRefs : Set < string > ;
208224 _styleExpression : StyleExpression ;
209225 interpolationType : InterpolationType ;
226+ readonly _globalState : Record < string , any > ;
210227
211- constructor ( kind : Kind , expression : StyleExpression , zoomStops : Array < number > , interpolationType ?: InterpolationType ) {
228+ constructor ( kind : Kind , expression : StyleExpression , zoomStops : Array < number > , interpolationType ?: InterpolationType , globalState ?: Record < string , any > ) {
212229 this . kind = kind ;
213230 this . zoomStops = zoomStops ;
214231 this . _styleExpression = expression ;
215232 this . isStateDependent = kind !== ( 'camera' as EvaluationKind ) && ! isStateConstant ( expression . expression ) ;
216233 this . globalStateRefs = findGlobalStateRefs ( expression . expression ) ;
217234 this . interpolationType = interpolationType ;
235+ this . _globalState = globalState ;
218236 }
219237
220238 evaluateWithoutErrorHandling (
@@ -225,6 +243,9 @@ export class ZoomDependentExpression<Kind extends EvaluationKind> {
225243 availableImages ?: Array < string > ,
226244 formattedSection ?: FormattedSection
227245 ) : any {
246+ if ( this . _globalState ) {
247+ globals = { ...globals , globalState : this . _globalState } ;
248+ }
228249 return this . _styleExpression . evaluateWithoutErrorHandling ( globals , feature , featureState , canonical , availableImages , formattedSection ) ;
229250 }
230251
@@ -236,6 +257,9 @@ export class ZoomDependentExpression<Kind extends EvaluationKind> {
236257 availableImages ?: Array < string > ,
237258 formattedSection ?: FormattedSection
238259 ) : any {
260+ if ( this . _globalState ) {
261+ globals = { ...globals , globalState : this . _globalState } ;
262+ }
239263 return this . _styleExpression . evaluate ( globals , feature , featureState , canonical , availableImages , formattedSection ) ;
240264 }
241265
@@ -255,6 +279,7 @@ export function isZoomExpression(expression: any): expression is ZoomConstantExp
255279export type ConstantExpression = {
256280 kind : 'constant' ;
257281 globalStateRefs : Set < string > ;
282+ readonly _globalState : Record < string , any > ;
258283 readonly evaluate : (
259284 globals : GlobalProperties ,
260285 feature ?: Feature ,
@@ -268,6 +293,7 @@ export type SourceExpression = {
268293 kind : 'source' ;
269294 isStateDependent : boolean ;
270295 globalStateRefs : Set < string > ;
296+ readonly _globalState : Record < string , any > ;
271297 readonly evaluate : (
272298 globals : GlobalProperties ,
273299 feature ?: Feature ,
@@ -281,6 +307,7 @@ export type SourceExpression = {
281307export type CameraExpression = {
282308 kind : 'camera' ;
283309 globalStateRefs : Set < string > ;
310+ readonly _globalState : Record < string , any > ;
284311 readonly evaluate : (
285312 globals : GlobalProperties ,
286313 feature ?: Feature ,
@@ -297,6 +324,7 @@ export type CompositeExpression = {
297324 kind : 'composite' ;
298325 isStateDependent : boolean ;
299326 globalStateRefs : Set < string > ;
327+ readonly _globalState : Record < string , any > ;
300328 readonly evaluate : (
301329 globals : GlobalProperties ,
302330 feature ?: Feature ,
@@ -312,8 +340,8 @@ export type CompositeExpression = {
312340
313341export type StylePropertyExpression = ConstantExpression | SourceExpression | CameraExpression | CompositeExpression ;
314342
315- export function createPropertyExpression ( expressionInput : unknown , propertySpec : StylePropertySpecification ) : Result < StylePropertyExpression , Array < ExpressionParsingError > > {
316- const expression = createExpression ( expressionInput , propertySpec ) ;
343+ export function createPropertyExpression ( expressionInput : unknown , propertySpec : StylePropertySpecification , globalState ?: Record < string , any > ) : Result < StylePropertyExpression , Array < ExpressionParsingError > > {
344+ const expression = createExpression ( expressionInput , propertySpec , globalState ) ;
317345 if ( expression . result === 'error' ) {
318346 return expression ;
319347 }
@@ -341,15 +369,15 @@ export function createPropertyExpression(expressionInput: unknown, propertySpec:
341369
342370 if ( ! zoomCurve ) {
343371 return success ( isFeatureConstantResult ?
344- ( new ZoomConstantExpression ( 'constant' , expression . value ) as ConstantExpression ) :
345- ( new ZoomConstantExpression ( 'source' , expression . value ) as SourceExpression ) ) ;
372+ ( new ZoomConstantExpression ( 'constant' , expression . value , globalState ) as ConstantExpression ) :
373+ ( new ZoomConstantExpression ( 'source' , expression . value , globalState ) as SourceExpression ) ) ;
346374 }
347375
348376 const interpolationType = zoomCurve instanceof Interpolate ? zoomCurve . interpolation : undefined ;
349377
350378 return success ( isFeatureConstantResult ?
351- ( new ZoomDependentExpression ( 'camera' , expression . value , zoomCurve . labels , interpolationType ) as CameraExpression ) :
352- ( new ZoomDependentExpression ( 'composite' , expression . value , zoomCurve . labels , interpolationType ) as CompositeExpression ) ) ;
379+ ( new ZoomDependentExpression ( 'camera' , expression . value , zoomCurve . labels , interpolationType , globalState ) as CameraExpression ) :
380+ ( new ZoomDependentExpression ( 'composite' , expression . value , zoomCurve . labels , interpolationType , globalState ) as CompositeExpression ) ) ;
353381}
354382
355383// serialization wrapper for old-style stop functions normalized to the
@@ -386,13 +414,14 @@ export class StylePropertyFunction<T> {
386414
387415export function normalizePropertyExpression < T > (
388416 value : PropertyValueSpecification < T > ,
389- specification : StylePropertySpecification
417+ specification : StylePropertySpecification ,
418+ globalState ?: Record < string , any >
390419) : StylePropertyExpression {
391420 if ( isFunction ( value ) ) {
392421 return new StylePropertyFunction ( value , specification ) as any ;
393422
394423 } else if ( isExpression ( value ) ) {
395- const expression = createPropertyExpression ( value , specification ) ;
424+ const expression = createPropertyExpression ( value , specification , globalState ) ;
396425 if ( expression . result === 'error' ) {
397426 // this should have been caught in validation
398427 throw new Error ( expression . value . map ( err => `${ err . key } : ${ err . message } ` ) . join ( ', ' ) ) ;
@@ -416,6 +445,7 @@ export function normalizePropertyExpression<T>(
416445 }
417446 return {
418447 globalStateRefs : new Set < string > ( ) ,
448+ _globalState : null ,
419449 kind : 'constant' ,
420450 evaluate : ( ) => constant
421451 } ;
0 commit comments