Skip to content

Commit

Permalink
[Flow] Type exports for data/* (#11472)
Browse files Browse the repository at this point in the history
* type exports for data/bucket/*

* fix pos_attributes types

* add export types for the rest of data/*

* name return value type for LineVertexRange
  • Loading branch information
mourner authored Feb 4, 2022
1 parent 3c3fe28 commit 8dde786
Show file tree
Hide file tree
Showing 21 changed files with 88 additions and 62 deletions.
6 changes: 4 additions & 2 deletions src/data/bounds_attributes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @flow
import {createLayout} from '../util/struct_array.js';

export default createLayout([
import type {StructArrayLayout} from '../util/struct_array.js';

export default (createLayout([
{name: 'a_pos', type: 'Int16', components: 2},
{name: 'a_texture_pos', type: 'Int16', components: 2}
]);
]): StructArrayLayout);
6 changes: 4 additions & 2 deletions src/data/bucket/circle_attributes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// @flow
import {createLayout} from '../../util/struct_array.js';

export const circleAttributes = createLayout([
import type {StructArrayLayout} from '../../util/struct_array.js';

export const circleAttributes: StructArrayLayout = createLayout([
{name: 'a_pos', components: 2, type: 'Int16'}
], 4);

export const circleGlobeAttributesExt = createLayout([
export const circleGlobeAttributesExt: StructArrayLayout = createLayout([
{name: 'a_pos_3', components: 3, type: 'Int16'},
{name: 'a_pos_normal_3', components: 3, type: 'Int16'},
{name: 'a_scale', components: 1, type: 'Float32'},
Expand Down
4 changes: 2 additions & 2 deletions src/data/bucket/circle_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ class CircleBucket<Layer: CircleStyleLayer | HeatmapStyleLayer> implements Bucke
this.programConfigurations.updatePaintArrays(states, vtLayer, this.stateDependentLayers, availableImages, imagePositions);
}

isEmpty() {
isEmpty(): boolean {
return this.layoutVertexArray.length === 0;
}

uploadPending() {
uploadPending(): boolean {
return !this.uploaded || this.programConfigurations.needsUpload;
}

Expand Down
4 changes: 3 additions & 1 deletion src/data/bucket/dash_attributes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @flow
import {createLayout} from '../../util/struct_array.js';

const dashAttributes = createLayout([
import type {StructArrayLayout} from '../../util/struct_array.js';

const dashAttributes: StructArrayLayout = createLayout([
{name: 'a_dash_to', components: 4, type: 'Uint16'}, // [x, y, width, unused]
{name: 'a_dash_from', components: 4, type: 'Uint16'}
]);
Expand Down
4 changes: 3 additions & 1 deletion src/data/bucket/fill_attributes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @flow
import {createLayout} from '../../util/struct_array.js';

const layout = createLayout([
import type {StructArrayLayout} from '../../util/struct_array.js';

const layout: StructArrayLayout = createLayout([
{name: 'a_pos', components: 2, type: 'Int16'}
], 4);

Expand Down
2 changes: 1 addition & 1 deletion src/data/bucket/fill_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class FillBucket implements Bucket {
}
}

isEmpty() {
isEmpty(): boolean {
return this.layoutVertexArray.length === 0;
}

Expand Down
6 changes: 4 additions & 2 deletions src/data/bucket/fill_extrusion_attributes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// @flow
import {createLayout} from '../../util/struct_array.js';

export const fillExtrusionAttributes = createLayout([
import type {StructArrayLayout} from '../../util/struct_array.js';

export const fillExtrusionAttributes: StructArrayLayout = createLayout([
{name: 'a_pos_normal_ed', components: 4, type: 'Int16'}
]);

export const centroidAttributes = createLayout([
export const centroidAttributes: StructArrayLayout = createLayout([
{name: 'a_centroid_pos', components: 2, type: 'Uint16'}
]);

Expand Down
4 changes: 2 additions & 2 deletions src/data/bucket/fill_extrusion_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ class FillExtrusionBucket implements Bucket {
this.programConfigurations.updatePaintArrays(states, vtLayer, this.stateDependentLayers, availableImages, imagePositions);
}

isEmpty() {
isEmpty(): boolean {
return this.layoutVertexArray.length === 0;
}

uploadPending() {
uploadPending(): boolean {
return !this.uploaded || this.programConfigurations.needsUpload;
}

Expand Down
4 changes: 3 additions & 1 deletion src/data/bucket/line_attributes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @flow
import {createLayout} from '../../util/struct_array.js';

const lineLayoutAttributes = createLayout([
import type {StructArrayLayout} from '../../util/struct_array.js';

const lineLayoutAttributes: StructArrayLayout = createLayout([
{name: 'a_pos_normal', components: 2, type: 'Int16'},
{name: 'a_data', components: 4, type: 'Uint8'},
{name: 'a_linesofar', components: 1, type: 'Float32'}
Expand Down
4 changes: 3 additions & 1 deletion src/data/bucket/line_attributes_ext.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @flow
import {createLayout} from '../../util/struct_array.js';

const lineLayoutAttributesExt = createLayout([
import type {StructArrayLayout} from '../../util/struct_array.js';

const lineLayoutAttributesExt: StructArrayLayout = createLayout([
{name: 'a_packed', components: 3, type: 'Float32'}
]);

Expand Down
6 changes: 3 additions & 3 deletions src/data/bucket/line_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class LineBucket implements Bucket {
}
}

addConstantDashes(lineAtlas: LineAtlas) {
addConstantDashes(lineAtlas: LineAtlas): boolean {
let hasFeatureDashes = false;

for (const layer of this.layers) {
Expand Down Expand Up @@ -278,11 +278,11 @@ class LineBucket implements Bucket {
}
}

isEmpty() {
isEmpty(): boolean {
return this.layoutVertexArray.length === 0;
}

uploadPending() {
uploadPending(): boolean {
return !this.uploaded || this.programConfigurations.needsUpload;
}

Expand Down
4 changes: 3 additions & 1 deletion src/data/bucket/pattern_attributes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @flow
import {createLayout} from '../../util/struct_array.js';

const patternAttributes = createLayout([
import type {StructArrayLayout} from '../../util/struct_array.js';

const patternAttributes: StructArrayLayout = createLayout([
// [tl.x, tl.y, br.x, br.y]
{name: 'a_pattern_to', components: 4, type: 'Uint16'},
{name: 'a_pattern_from', components: 4, type: 'Uint16'},
Expand Down
4 changes: 2 additions & 2 deletions src/data/bucket/pattern_bucket_features.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type PatternStyleLayers =
Array<FillStyleLayer> |
Array<FillExtrusionStyleLayer>;

export function hasPattern(type: string, layers: PatternStyleLayers, options: PopulateParameters) {
export function hasPattern(type: string, layers: PatternStyleLayers, options: PopulateParameters): boolean {
const patterns = options.patternDependencies;
let hasPattern = false;

Expand All @@ -34,7 +34,7 @@ export function hasPattern(type: string, layers: PatternStyleLayers, options: Po
return hasPattern;
}

export function addPatternDependencies(type: string, layers: PatternStyleLayers, patternFeature: BucketFeature, zoom: number, options: PopulateParameters) {
export function addPatternDependencies(type: string, layers: PatternStyleLayers, patternFeature: BucketFeature, zoom: number, options: PopulateParameters): BucketFeature {
const patterns = options.patternDependencies;
for (const layer of layers) {
const patternProperty = layer.paint.get(`${type}-pattern`);
Expand Down
29 changes: 15 additions & 14 deletions src/data/bucket/symbol_attributes.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
// @flow

import {createLayout} from '../../util/struct_array.js';

export const symbolLayoutAttributes = createLayout([
import type {StructArrayLayout} from '../../util/struct_array.js';

export const symbolLayoutAttributes: StructArrayLayout = createLayout([
{name: 'a_pos_offset', components: 4, type: 'Int16'},
{name: 'a_tex_size', components: 4, type: 'Uint16'},
{name: 'a_pixeloffset', components: 4, type: 'Int16'},
{name: 'a_z_tile_anchor', components: 4, type: 'Int16'}
], 4);

export const dynamicLayoutAttributes = createLayout([
export const dynamicLayoutAttributes: StructArrayLayout = createLayout([
{name: 'a_projected_pos', components: 3, type: 'Float32'}
], 4);

export const placementOpacityAttributes = createLayout([
export const placementOpacityAttributes: StructArrayLayout = createLayout([
{name: 'a_fade_opacity', components: 1, type: 'Uint32'}
], 4);

export const collisionVertexAttributes = createLayout([
export const collisionVertexAttributes: StructArrayLayout = createLayout([
{name: 'a_placed', components: 2, type: 'Uint8'},
{name: 'a_shift', components: 2, type: 'Float32'},
]);

export const collisionVertexAttributesExt = createLayout([
export const collisionVertexAttributesExt: StructArrayLayout = createLayout([
{name: 'a_size_scale', components: 1, type: 'Float32'},
{name: 'a_padding', components: 2, type: 'Float32'},
]);

export const collisionBox = createLayout([
export const collisionBox: StructArrayLayout = createLayout([
// the box is centered around the anchor point
{type: 'Int16', name: 'projectedAnchorX'},
{type: 'Int16', name: 'projectedAnchorY'},
Expand All @@ -52,23 +53,23 @@ export const collisionBox = createLayout([
{type: 'Uint16', name: 'bucketIndex'},
]);

export const collisionBoxLayout = createLayout([ // used to render collision boxes for debugging purposes
export const collisionBoxLayout: StructArrayLayout = createLayout([ // used to render collision boxes for debugging purposes
{name: 'a_pos', components: 3, type: 'Int16'},
{name: 'a_anchor_pos', components: 2, type: 'Int16'},
{name: 'a_extrude', components: 2, type: 'Int16'}
], 4);

export const collisionCircleLayout = createLayout([ // used to render collision circles for debugging purposes
export const collisionCircleLayout: StructArrayLayout = createLayout([ // used to render collision circles for debugging purposes
{name: 'a_pos_2f', components: 2, type: 'Float32'},
{name: 'a_radius', components: 1, type: 'Float32'},
{name: 'a_flags', components: 2, type: 'Int16'}
], 4);

export const quadTriangle = createLayout([
export const quadTriangle: StructArrayLayout = createLayout([
{name: 'triangle', components: 3, type: 'Uint16'},
]);

export const placement = createLayout([
export const placement: StructArrayLayout = createLayout([
{type: 'Int16', name: 'projectedAnchorX'},
{type: 'Int16', name: 'projectedAnchorY'},
{type: 'Int16', name: 'projectedAnchorZ'},
Expand All @@ -92,7 +93,7 @@ export const placement = createLayout([
{type: 'Uint8', name: 'flipState'}
]);

export const symbolInstance = createLayout([
export const symbolInstance: StructArrayLayout = createLayout([
{type: 'Int16', name: 'projectedAnchorX'},
{type: 'Int16', name: 'projectedAnchorY'},
{type: 'Int16', name: 'projectedAnchorZ'},
Expand Down Expand Up @@ -124,11 +125,11 @@ export const symbolInstance = createLayout([
{type: 'Float32', name: 'collisionCircleDiameter'},
]);

export const glyphOffset = createLayout([
export const glyphOffset: StructArrayLayout = createLayout([
{type: 'Float32', name: 'offsetX'}
]);

export const lineVertex = createLayout([
export const lineVertex: StructArrayLayout = createLayout([
{type: 'Int16', name: 'x'},
{type: 'Int16', name: 'y'},
{type: 'Int16', name: 'tileUnitDistanceFromAnchor'}
Expand Down
29 changes: 17 additions & 12 deletions src/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export type SortKeyRange = {
symbolInstanceEnd: number
};

type LineVertexRange = {|
lineLength: number,
lineStartIndex: number
|};

// Opacity arrays are frequently updated but don't contain a lot of information, so we pack them
// tight. Each Uint32 is actually four duplicate Uint8s for the four corners of a glyph
// 7 bits are for the current opacity, and the lowest bit is the target opacity
Expand Down Expand Up @@ -192,7 +197,7 @@ export class SymbolBuffers {
this.placedSymbolArray = new PlacedSymbolArray();
}

isEmpty() {
isEmpty(): boolean {
return this.layoutVertexArray.length === 0 &&
this.indexArray.length === 0 &&
this.dynamicLayoutVertexArray.length === 0 &&
Expand Down Expand Up @@ -564,13 +569,13 @@ class SymbolBucket implements Bucket {
this.icon.programConfigurations.updatePaintArrays(states, vtLayer, this.layers, availableImages, imagePositions);
}

isEmpty() {
isEmpty(): boolean {
// When the bucket encounters only rtl-text but the plugin isn't loaded, no symbol instances will be created.
// In order for the bucket to be serialized, and not discarded as an empty bucket both checks are necessary.
return this.symbolInstances.length === 0 && !this.hasRTLText;
}

uploadPending() {
uploadPending(): boolean {
return !this.uploaded || this.text.programConfigurations.needsUpload || this.icon.programConfigurations.needsUpload;
}

Expand Down Expand Up @@ -598,7 +603,7 @@ class SymbolBucket implements Bucket {
}
}

addToLineVertexArray(anchor: Anchor, line: any) {
addToLineVertexArray(anchor: Anchor, line: any): LineVertexRange {
const lineStartIndex = this.lineVertexArray.length;
const segment = anchor.segment;
if (segment !== undefined) {
Expand Down Expand Up @@ -778,7 +783,7 @@ class SymbolBucket implements Bucket {
}
}

getSymbolInstanceTextSize(textSize: any, instance: SymbolInstance, zoom: number, boxIndex: number) {
getSymbolInstanceTextSize(textSize: any, instance: SymbolInstance, zoom: number, boxIndex: number): number {
const symbolIndex = instance.rightJustifiedTextSymbolIndex >= 0 ?
instance.rightJustifiedTextSymbolIndex : instance.centerJustifiedTextSymbolIndex >= 0 ?
instance.centerJustifiedTextSymbolIndex : instance.leftJustifiedTextSymbolIndex >= 0 ?
Expand All @@ -791,7 +796,7 @@ class SymbolBucket implements Bucket {
return this.tilePixelRatio * featureSize;
}

getSymbolInstanceIconSize(iconSize: any, zoom: number, index: number) {
getSymbolInstanceIconSize(iconSize: any, zoom: number, index: number): number {
const symbol: any = this.icon.placedSymbolArray.get(index);
const featureSize = symbolSize.evaluateSizeForFeature(this.iconSizeData, iconSize, symbol);

Expand Down Expand Up @@ -906,23 +911,23 @@ class SymbolBucket implements Bucket {
}
}

hasTextData() {
hasTextData(): boolean {
return this.text.segments.get().length > 0;
}

hasIconData() {
hasIconData(): boolean {
return this.icon.segments.get().length > 0;
}

hasDebugData() {
hasDebugData(): CollisionBuffers {
return this.textCollisionBox && this.iconCollisionBox;
}

hasTextCollisionBoxData() {
hasTextCollisionBoxData(): boolean {
return this.hasDebugData() && this.textCollisionBox.segments.get().length > 0;
}

hasIconCollisionBoxData() {
hasIconCollisionBoxData(): boolean {
return this.hasDebugData() && this.iconCollisionBox.segments.get().length > 0;
}

Expand All @@ -936,7 +941,7 @@ class SymbolBucket implements Bucket {
}
}

getSortedSymbolIndexes(angle: number) {
getSortedSymbolIndexes(angle: number): Array<number> {
if (this.sortedAngle === angle && this.symbolInstanceIndexes !== undefined) {
return this.symbolInstanceIndexes;
}
Expand Down
Loading

0 comments on commit 8dde786

Please sign in to comment.