Skip to content

Commit

Permalink
refactor(cesium): use classes for converters
Browse files Browse the repository at this point in the history
This differs more from the original implementation but it helps work
around a forward-referenced type bug in the compiler when declaring the
@type of a function as a @typedef.
  • Loading branch information
wallw-teal committed Feb 26, 2020
1 parent b55cc8c commit 7527875
Show file tree
Hide file tree
Showing 26 changed files with 617 additions and 634 deletions.
43 changes: 37 additions & 6 deletions src/plugin/cesium/primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@ const styleUtils = goog.require('plugin.cesium.sync.style');

const Feature = goog.requireType('ol.Feature');
const Geometry = goog.requireType('ol.geom.Geometry');
const IConverter = goog.requireType('plugin.cesium.sync.IConverter');
const Style = goog.requireType('ol.style.Style');
const {RetrieveFunction, UpdateFunction} = goog.requireType('plugin.cesium.sync.ConverterTypes');
const VectorContext = goog.requireType('plugin.cesium.VectorContext');

/**
* @type {plugin.cesium.sync.ConverterTypes.RetrieveFunction}
* @param {!Feature} feature
* @param {!Geometry} geometry
* @param {!Style} style
* @param {!VectorContext} context
* @return {!Array<!Cesium.PrimitiveLike>|!Cesium.PrimitiveLike|null|undefined}
* @this {IConverter}
*/
const getPrimitive = (feature, geometry, style, context) => {
return context.getPrimitiveForGeometry(geometry);
};


/**
* @type {UpdateFunction}
* @param {!Feature} feature
* @param {!Geometry} geometry
* @param {!Style} style
* @param {!VectorContext} context
* @param {!Cesium.PrimitiveLike} primitive
* @return {boolean}
*/
const shouldUpdatePrimitive = (feature, geometry, style, context, primitive) => {
const heightReference = getHeightReference(context.layer, feature, geometry);
Expand All @@ -30,7 +40,13 @@ const shouldUpdatePrimitive = (feature, geometry, style, context, primitive) =>


/**
* @type {UpdateFunction}
* @param {!Feature} feature
* @param {!Geometry} geometry
* @param {!Style} style
* @param {!VectorContext} context
* @param {!Cesium.PrimitiveLike} primitive
* @return {boolean}
* @this {IConverter}
*/
const updatePrimitive = (feature, geometry, style, context, primitive) => {
if (!shouldUpdatePrimitive(feature, geometry, style, context, primitive)) {
Expand Down Expand Up @@ -106,9 +122,22 @@ const updatePrimitiveGeomInstances = (style, context, primitive) => {


/**
* @type {UpdateFunction}
* @param {!Feature} feature
* @param {!Geometry} geometry
* @param {!Style} style
* @param {!VectorContext} context
* @param {!Array<!Cesium.PrimitiveLike>|!Cesium.PrimitiveLike} primitive
* @return {boolean}
* @this {IConverter}
*/
const deletePrimitive = (feature, geometry, style, context, primitive) => {
if (Array.isArray(primitive)) {
for (let i = 0, n = primitive.length; i < n; i++) {
deletePrimitive(feature, geometry, style, context, primitive[i]);
}
return true;
}

context.removePrimitive(primitive);
return true;
};
Expand Down Expand Up @@ -206,12 +235,14 @@ const isGroundPrimitive = function(primitive) {


/**
* @param {?(Array<Cesium.PrimitiveLike>|Cesium.PrimitiveLike)} primitive The primitive
* @param {?(Array<Cesium.PrimitiveLike>|Cesium.PrimitiveLike|Cesium.CollectionLike)} primitive The primitive
* @return {!boolean}
*/
const isPrimitiveShown = function(primitive) {
if (Array.isArray(primitive)) {
return primitive.length > 0 ? isPrimitiveShown(primitive[0]) : true;
} else if (primitive.length != null) {
return primitive.length > 0 ? isPrimitiveShown(primitive.get(0)) : true;
}

return !!primitive.show;
Expand Down
25 changes: 25 additions & 0 deletions src/plugin/cesium/sync/baseconverter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
goog.module('plugin.cesium.sync.BaseConverter');

const {getPrimitive, deletePrimitive} = goog.require('plugin.cesium.primitive');

const IConverter = goog.requireType('plugin.cesium.sync.IConverter');


/**
* @abstract
* @implements {IConverter}
*/
class BaseConverter {}


/**
* @inheritDoc
*/
BaseConverter.prototype.retrieve = getPrimitive;

/**
* @inheritDoc
*/
BaseConverter.prototype.delete = deletePrimitive;

exports = BaseConverter;
32 changes: 17 additions & 15 deletions src/plugin/cesium/sync/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const PointConverter = goog.require('plugin.cesium.sync.PointConverter');
const PolygonConverter = goog.require('plugin.cesium.sync.PolygonConverter');
const {runConverter} = goog.require('plugin.cesium.sync.runConverter');

const IConverter = goog.requireType('plugin.cesium.sync.IConverter');
const Feature = goog.requireType('ol.Feature');
const Geometry = goog.requireType('ol.geom.Geometry');
const Style = goog.requireType('ol.style.Style');
const {Converter} = goog.requireType('plugin.cesium.sync.ConverterTypes');
const VectorContext = goog.requireType('plugin.cesium.VectorContext');

/**
Expand All @@ -40,29 +40,31 @@ const convertGeometry = (feature, geometry, style, context) => {


/**
* @type {Object<string, Converter>}
* @type {Object<string, !IConverter>}
*/
const converters = {
[GeometryType.GEOMETRY_COLLECTION]: GeometryCollectionConverter,
[GeometryType.LINE_STRING]: LineStringConverter,
[GeometryType.MULTI_LINE_STRING]: MultiLineStringConverter,
[GeometryType.MULTI_POINT]: MultiPointConverter,
[GeometryType.MULTI_POLYGON]: MultiPolygonConverter,
[GeometryType.POINT]: PointConverter,
[GeometryType.POLYGON]: PolygonConverter
[GeometryType.GEOMETRY_COLLECTION]: new GeometryCollectionConverter,
[GeometryType.LINE_STRING]: new LineStringConverter,
[GeometryType.MULTI_LINE_STRING]: new MultiLineStringConverter,
[GeometryType.MULTI_POINT]: new MultiPointConverter,
[GeometryType.MULTI_POLYGON]: new MultiPolygonConverter,
[GeometryType.POINT]: new PointConverter,
[GeometryType.POLYGON]: new PolygonConverter,
label: new LabelConverter,
ellipse: new EllipseConverter
};


GeometryCollectionConverter.setConvertFunction(convertGeometry);


/**
* @type {Object<string, Converter>}
* @type {Object<string, !IConverter>}
*/
const dynamicConverters = {
...converters,
[GeometryType.LINE_STRING]: DynamicLineStringConverter,
[GeometryType.MULTI_LINE_STRING]: MultiDynamicLineStringConverter
[GeometryType.LINE_STRING]: new DynamicLineStringConverter,
[GeometryType.MULTI_LINE_STRING]: new MultiDynamicLineStringConverter
};


Expand All @@ -71,21 +73,21 @@ const dynamicConverters = {
* @param {!Geometry} geometry
* @param {!Style} style
* @param {!VectorContext} context
* @return {Converter|undefined}
* @return {IConverter|undefined}
*/
const getConverter = (feature, geometry, style, context) => {
const geometryType = geometry.getType();

if (style && style.getText()) {
return LabelConverter;
return converters.label;
}

if (feature instanceof DynamicFeature) {
return dynamicConverters[geometryType];
}

if (geometry instanceof Ellipse) {
return EllipseConverter;
return converters.ellipse;
}

return converters[geometryType];
Expand Down
30 changes: 0 additions & 30 deletions src/plugin/cesium/sync/convertertypes.js

This file was deleted.

51 changes: 23 additions & 28 deletions src/plugin/cesium/sync/dynamiclinestringconverter.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
goog.module('plugin.cesium.sync.DynamicLineStringConverter');

const {deletePrimitive, getPrimitive, shouldUpdatePrimitive} = goog.require('plugin.cesium.primitive');
const BaseConverter = goog.require('plugin.cesium.sync.BaseConverter');
const {shouldUpdatePrimitive} = goog.require('plugin.cesium.primitive');
const {createPolyline, updatePolyline} = goog.require('plugin.cesium.sync.DynamicLineString');

const {CreateFunction, UpdateFunction, Converter} = goog.requireType('plugin.cesium.sync.ConverterTypes');

/**
* @type {CreateFunction}
* Converter for DynamicFeature lines
*/
const create = (feature, geometry, style, context) => {
const polylineOptions = createPolyline(feature, geometry, style, context);
context.addPolyline(polylineOptions, feature, geometry);
return true;
};
class DynamicLineStringConverter extends BaseConverter {
/**
* @inheritDoc
*/
create(feature, geometry, style, context) {
const polylineOptions = createPolyline(feature, geometry, style, context);
context.addPolyline(polylineOptions, feature, geometry);
return true;
}

/**
* @inheritDoc
*/
update(feature, geometry, style, context, primitive) {
if (!shouldUpdatePrimitive(feature, geometry, style, context, primitive)) {
return false;
}

/**
* @type {UpdateFunction}
*/
const update = (feature, geometry, style, context, primitive) => {
if (!shouldUpdatePrimitive(feature, geometry, style, context, primitive)) {
return false;
updatePolyline(feature, geometry, style, context, primitive);
return true;
}
}

updatePolyline(feature, geometry, style, context, primitive);
return true;
};


/**
* @type {Converter}
*/
exports = {
create,
retrieve: getPrimitive,
update,
delete: deletePrimitive
};
exports = DynamicLineStringConverter;
61 changes: 32 additions & 29 deletions src/plugin/cesium/sync/ellipseconverter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
goog.module('plugin.cesium.sync.EllipseConverter');

const BaseConverter = goog.require('plugin.cesium.sync.BaseConverter');
const DynamicLineStringConverter = goog.require('plugin.cesium.sync.DynamicLineStringConverter');
const ILayer = goog.require('os.layer.ILayer');
const LineString = goog.require('ol.geom.LineString');
Expand All @@ -14,41 +15,49 @@ const Feature = goog.requireType('ol.Feature');
const Ellipse = goog.requireType('os.geom.Ellipse');
const Style = goog.requireType('ol.style.Style');
const VectorContext = goog.requireType('plugin.cesium.VectorContext');
const {CreateFunction, UpdateFunction, Converter} = goog.requireType('plugin.cesium.sync.ConverterTypes');
const IConverter = goog.requireType('plugin.cesium.sync.IConverter');

/**
* @type {CreateFunction}
* Converter for Ellipses
*/
const create = (feature, geometry, style, context) => {
const returnVal = getConverter(context).create(feature, geometry, style, context);
class EllipseConverter extends BaseConverter {
/**
* @inheritDoc
*/
create(feature, geometry, style, context) {
const returnVal = getConverter(context).create(feature, geometry, style, context);

if (returnVal) {
createOrUpdateGroundReference(feature, geometry, style, context);
}

if (returnVal) {
createOrUpdateGroundReference(feature, geometry, style, context);
return returnVal;
}

return returnVal;
};

/**
* @inheritDoc
*/
update(feature, geometry, style, context, primitive) {
const returnVal = getConverter(context).update(feature, geometry, style, context, primitive);

/**
* @type {UpdateFunction}
*/
const update = (feature, geometry, style, context, primitive) => {
const returnVal = getConverter(context).update(feature, geometry, style, context, primitive);
if (returnVal) {
createOrUpdateGroundReference(feature, geometry, style, context);
}

if (returnVal) {
createOrUpdateGroundReference(feature, geometry, style, context);
return returnVal;
}
}

return returnVal;
};

const ellipsoidConverter = new EllipsoidConverter();
const polygonConverter = new PolygonConverter();

/**
* @param {VectorContext} context
* @return {Converter}
* @return {IConverter}
*/
const getConverter = (context) => isEllipsoid(context) ? EllipsoidConverter : PolygonConverter;
const getConverter = (context) => isEllipsoid(context) ? ellipsoidConverter : polygonConverter;


/**
Expand All @@ -62,6 +71,8 @@ const isEllipsoid = (context) => {
};


const dynamicConverter = new DynamicLineStringConverter();

/**
* Get a ground reference line from a coordinate to the surface of the globe.
*
Expand Down Expand Up @@ -106,17 +117,9 @@ const createOrUpdateGroundReference = (feature, geometry, style, context) => {
}

if (groundRef) {
runConverter(DynamicLineStringConverter, feature, groundRef, style, context);
runConverter(dynamicConverter, feature, groundRef, style, context);
}
};


/**
* @type {Converter}
*/
exports = {
create,
retrieve: PolygonConverter.retrieve,
update,
delete: PolygonConverter.delete
};
exports = EllipseConverter;
Loading

0 comments on commit 7527875

Please sign in to comment.