Skip to content

Commit

Permalink
properly expose struct arrays as factories rather than classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Nov 2, 2016
1 parent b098c72 commit 2ada7d7
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 60 deletions.
8 changes: 4 additions & 4 deletions js/data/bucket/circle_bucket.js
@@ -1,18 +1,18 @@
'use strict';

const Bucket = require('../bucket');
const VertexArrayType = require('../vertex_array_type');
const ElementArrayType = require('../element_array_type');
const createVertexArrayType = require('../vertex_array_type');
const createElementArrayType = require('../element_array_type');
const loadGeometry = require('../load_geometry');
const EXTENT = require('../extent');

const circleInterface = {
layoutVertexArrayType: new VertexArrayType([{
layoutVertexArrayType: createVertexArrayType([{
name: 'a_pos',
components: 2,
type: 'Int16'
}]),
elementArrayType: new ElementArrayType(),
elementArrayType: createElementArrayType(),

paintAttributes: [{
name: 'a_color',
Expand Down
10 changes: 5 additions & 5 deletions js/data/bucket/fill_bucket.js
@@ -1,22 +1,22 @@
'use strict';

const Bucket = require('../bucket');
const VertexArrayType = require('../vertex_array_type');
const ElementArrayType = require('../element_array_type');
const createVertexArrayType = require('../vertex_array_type');
const createElementArrayType = require('../element_array_type');
const loadGeometry = require('../load_geometry');
const earcut = require('earcut');
const classifyRings = require('../../util/classify_rings');
const assert = require('assert');
const EARCUT_MAX_RINGS = 500;

const fillInterface = {
layoutVertexArrayType: new VertexArrayType([{
layoutVertexArrayType: createVertexArrayType([{
name: 'a_pos',
components: 2,
type: 'Int16'
}]),
elementArrayType: new ElementArrayType(3),
elementArrayType2: new ElementArrayType(2),
elementArrayType: createElementArrayType(3),
elementArrayType2: createElementArrayType(2),

paintAttributes: [{
name: 'a_color',
Expand Down
8 changes: 4 additions & 4 deletions js/data/bucket/fill_extrusion_bucket.js
@@ -1,8 +1,8 @@
'use strict';

const Bucket = require('../bucket');
const VertexArrayType = require('../vertex_array_type');
const ElementArrayType = require('../element_array_type');
const createVertexArrayType = require('../vertex_array_type');
const createElementArrayType = require('../element_array_type');
const loadGeometry = require('../load_geometry');
const EXTENT = require('../extent');
const earcut = require('earcut');
Expand All @@ -11,7 +11,7 @@ const assert = require('assert');
const EARCUT_MAX_RINGS = 500;

const fillExtrusionInterface = {
layoutVertexArrayType: new VertexArrayType([{
layoutVertexArrayType: createVertexArrayType([{
name: 'a_pos',
components: 2,
type: 'Int16'
Expand All @@ -24,7 +24,7 @@ const fillExtrusionInterface = {
components: 1,
type: 'Int16'
}]),
elementArrayType: new ElementArrayType(3),
elementArrayType: createElementArrayType(3),

paintAttributes: [{
name: 'a_minH',
Expand Down
8 changes: 4 additions & 4 deletions js/data/bucket/line_bucket.js
@@ -1,8 +1,8 @@
'use strict';

const Bucket = require('../bucket');
const VertexArrayType = require('../vertex_array_type');
const ElementArrayType = require('../element_array_type');
const createVertexArrayType = require('../vertex_array_type');
const createElementArrayType = require('../element_array_type');
const loadGeometry = require('../load_geometry');
const EXTENT = require('../extent');

Expand Down Expand Up @@ -40,7 +40,7 @@ const LINE_DISTANCE_SCALE = 1 / 2;
const MAX_LINE_DISTANCE = Math.pow(2, LINE_DISTANCE_BUFFER_BITS - 1) / LINE_DISTANCE_SCALE;

const lineInterface = {
layoutVertexArrayType: new VertexArrayType([{
layoutVertexArrayType: createVertexArrayType([{
name: 'a_pos',
components: 2,
type: 'Int16'
Expand All @@ -59,7 +59,7 @@ const lineInterface = {
multiplier: 255,
paintProperty: 'line-color'
}],
elementArrayType: new ElementArrayType()
elementArrayType: createElementArrayType()
};

function addLineVertex(layoutVertexBuffer, point, extrude, tx, ty, dir, linesofar) {
Expand Down
13 changes: 7 additions & 6 deletions js/data/bucket/symbol_bucket.js
Expand Up @@ -3,8 +3,8 @@
const Point = require('point-geometry');
const ArrayGroup = require('../array_group');
const BufferGroup = require('../buffer_group');
const VertexArrayType = require('../vertex_array_type');
const ElementArrayType = require('../element_array_type');
const createVertexArrayType = require('../vertex_array_type');
const createElementArrayType = require('../element_array_type');
const EXTENT = require('../extent');
const Anchor = require('../../symbol/anchor');
const getAnchors = require('../../symbol/get_anchors');
Expand All @@ -25,8 +25,9 @@ const shapeIcon = Shaping.shapeIcon;
const getGlyphQuads = Quads.getGlyphQuads;
const getIconQuads = Quads.getIconQuads;

const elementArrayType = new ElementArrayType();
const layoutVertexArrayType = new VertexArrayType([{
const elementArrayType = createElementArrayType();

const layoutVertexArrayType = createVertexArrayType([{
name: 'a_pos',
components: 2,
type: 'Int16'
Expand Down Expand Up @@ -54,7 +55,7 @@ const symbolInterfaces = {
elementArrayType: elementArrayType
},
collisionBox: {
layoutVertexArrayType: new VertexArrayType([{
layoutVertexArrayType: createVertexArrayType([{
name: 'a_pos',
components: 2,
type: 'Int16'
Expand All @@ -67,7 +68,7 @@ const symbolInterfaces = {
components: 2,
type: 'Uint8'
}]),
elementArrayType: new ElementArrayType(2)
elementArrayType: createElementArrayType(2)
}
};

Expand Down
10 changes: 6 additions & 4 deletions js/data/element_array_type.js
@@ -1,18 +1,20 @@
'use strict';

const StructArrayType = require('../util/struct_array');
const createStructArrayType = require('../util/struct_array');

module.exports = createElementArrayType;

/**
* An element array stores Uint16 indicies of vertexes in a corresponding vertex array. With no
* arguments, it defaults to three components per element, forming triangles.
* @private
*/
module.exports = function ElementArrayType(components) {
return new StructArrayType({
function createElementArrayType(components) {
return createStructArrayType({
members: [{
type: 'Uint16',
name: 'vertices',
components: components || 3
}]
});
};
}
7 changes: 4 additions & 3 deletions js/data/feature_index.js
Expand Up @@ -4,7 +4,7 @@ const Point = require('point-geometry');
const loadGeometry = require('./load_geometry');
const EXTENT = require('./extent');
const featureFilter = require('feature-filter');
const StructArrayType = require('../util/struct_array');
const createStructArrayType = require('../util/struct_array');
const Grid = require('grid-index');
const DictionaryCoder = require('../util/dictionary_coder');
const vt = require('vector-tile');
Expand All @@ -17,15 +17,16 @@ const multiPolygonIntersectsBufferedMultiPoint = intersection.multiPolygonInters
const multiPolygonIntersectsMultiPolygon = intersection.multiPolygonIntersectsMultiPolygon;
const multiPolygonIntersectsBufferedMultiLine = intersection.multiPolygonIntersectsBufferedMultiLine;

const FeatureIndexArray = new StructArrayType({
const FeatureIndexArray = createStructArrayType({
members: [
// the index of the feature in the original vectortile
{ type: 'Uint32', name: 'featureIndex' },
// the source layer the feature appears in
{ type: 'Uint16', name: 'sourceLayerIndex' },
// the bucket the feature appears in
{ type: 'Uint16', name: 'bucketIndex' }
]});
]
});

class FeatureIndex {
constructor(coord, overscaling, collisionTile) {
Expand Down
4 changes: 2 additions & 2 deletions js/data/pos_array.js
@@ -1,8 +1,8 @@
'use strict';

const StructArrayType = require('../util/struct_array');
const createStructArrayType = require('../util/struct_array');

const PosArray = new StructArrayType({
const PosArray = createStructArrayType({
members: [{ name: 'a_pos', type: 'Int16', components: 2 }]
});

Expand Down
4 changes: 2 additions & 2 deletions js/data/program_configuration.js
@@ -1,6 +1,6 @@
'use strict';

const VertexArrayType = require('./vertex_array_type');
const createVertexArrayType = require('./vertex_array_type');
const util = require('../util/util');
const shaders = require('mapbox-gl-shaders');
const assert = require('assert');
Expand Down Expand Up @@ -154,7 +154,7 @@ class ProgramConfiguration {
}

paintVertexArrayType() {
return new VertexArrayType(this.attributes);
return createVertexArrayType(this.attributes);
}

createProgram(name, showOverdraw, gl) {
Expand Down
4 changes: 2 additions & 2 deletions js/data/raster_bounds_array.js
@@ -1,8 +1,8 @@
'use strict';

const StructArrayType = require('../util/struct_array');
const createStructArrayType = require('../util/struct_array');

const RasterBoundsArray = new StructArrayType({
const RasterBoundsArray = createStructArrayType({
members: [
{ name: 'a_pos', type: 'Int16', components: 2 },
{ name: 'a_texture_pos', type: 'Int16', components: 2 }
Expand Down
10 changes: 6 additions & 4 deletions js/data/vertex_array_type.js
@@ -1,15 +1,17 @@
'use strict';

const StructArrayType = require('../util/struct_array');
const createStructArrayType = require('../util/struct_array');

module.exports = createVertexArrayType;

/**
* A vertex array stores data for each vertex in a geometry. Elements are aligned to 4 byte
* boundaries for best performance in WebGL.
* @private
*/
module.exports = function VertexArrayType(members) {
return new StructArrayType({
function createVertexArrayType(members) {
return createStructArrayType({
members: members,
alignment: 4
});
};
}
9 changes: 6 additions & 3 deletions js/symbol/collision_box.js
@@ -1,6 +1,6 @@
'use strict';

const StructArrayType = require('../util/struct_array');
const createStructArrayType = require('../util/struct_array');
const Point = require('point-geometry');

/**
Expand Down Expand Up @@ -39,7 +39,7 @@ const Point = require('point-geometry');
* @private
*/

const CollisionBoxArray = module.exports = new StructArrayType({
const CollisionBoxArray = createStructArrayType({
members: [
// the box is centered around the anchor point
{ type: 'Int16', name: 'anchorPointX' },
Expand Down Expand Up @@ -69,8 +69,11 @@ const CollisionBoxArray = module.exports = new StructArrayType({
{ type: 'Int16', name: 'bbox3' },

{ type: 'Float32', name: 'placementScale' }
]});
]
});

Object.defineProperty(CollisionBoxArray.prototype.StructType.prototype, 'anchorPoint', {
get() { return new Point(this.anchorPointX, this.anchorPointY); }
});

module.exports = CollisionBoxArray;
6 changes: 3 additions & 3 deletions js/symbol/symbol_instances.js
@@ -1,6 +1,6 @@
'use strict';

const StructArrayType = require('../util/struct_array');
const createStructArrayType = require('../util/struct_array');
const Point = require('point-geometry');

/*
Expand All @@ -12,7 +12,7 @@ const Point = require('point-geometry');
* @private
*/

const SymbolInstancesArray = module.exports = new StructArrayType({
const SymbolInstancesArray = createStructArrayType({
members: [

{ type: 'Uint16', name: 'textBoxStartIndex' },
Expand All @@ -37,4 +37,4 @@ Object.defineProperty(SymbolInstancesArray.prototype.StructType.prototype, 'anch
get() { return new Point(this.anchorPointX, this.anchorPointY); }
});


module.exports = SymbolInstancesArray;
6 changes: 4 additions & 2 deletions js/symbol/symbol_quads.js
@@ -1,6 +1,6 @@
'use strict';

const StructArrayType = require('../util/struct_array');
const createStructArrayType = require('../util/struct_array');
const Point = require('point-geometry');
const SymbolQuad = require('./quads').SymbolQuad;

Expand All @@ -17,7 +17,7 @@ const SymbolQuad = require('./quads').SymbolQuad;
* @private
*/

const SymbolQuadsArray = module.exports = new StructArrayType({
const SymbolQuadsArray = createStructArrayType({
members: [
// the quad is centered around the anchor point
{ type: 'Int16', name: 'anchorPointX' },
Expand Down Expand Up @@ -69,3 +69,5 @@ Object.defineProperty(SymbolQuadsArray.prototype.StructType.prototype, 'SymbolQu
this.maxScale);
}
});

module.exports = SymbolQuadsArray;
10 changes: 5 additions & 5 deletions js/util/struct_array.js
Expand Up @@ -4,7 +4,7 @@

const assert = require('assert');

module.exports = StructArrayType;
module.exports = createStructArrayType;

const viewTypes = {
'Int8': Int8Array,
Expand Down Expand Up @@ -49,7 +49,7 @@ const RESIZE_MULTIPLIER = 5;

/**
* The StructArray class is inherited by the custom StructArrayType classes created with
* `new StructArrayType(members, options)`.
* `createStructArrayType(members, options)`.
* @private
*/
class StructArray {
Expand Down Expand Up @@ -159,7 +159,7 @@ class StructArray {
const structArrayTypeCache = {};

/**
* `StructArrayType` is used to create new `StructArray` types.
* `createStructArrayType` is used to create new `StructArray` types.
*
* `StructArray` provides an abstraction over `ArrayBuffer` and `TypedArray` making it behave like
* an array of typed structs. A StructArray is comprised of elements. Each element has a set of
Expand All @@ -177,7 +177,7 @@ const structArrayTypeCache = {};
* @param {Array<StructMember>} options.members
* @example
*
* var PointArrayType = new StructArrayType({
* var PointArrayType = createStructArrayType({
* members: [
* { type: 'Int16', name: 'x' },
* { type: 'Int16', name: 'y' }
Expand All @@ -193,7 +193,7 @@ const structArrayTypeCache = {};
*
* @private
*/
function StructArrayType(options) {
function createStructArrayType(options) {

const key = JSON.stringify(options);

Expand Down

0 comments on commit 2ada7d7

Please sign in to comment.