Skip to content

Commit 9a63f22

Browse files
committed
Add types for AbstractSynchronizer and core
1 parent 14b1bb6 commit 9a63f22

15 files changed

+194
-355
lines changed

.eslintrc-es6.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@
150150

151151
# enforce spacing around the * in yield* expressions
152152
# http://eslint.org/docs/rules/yield-star-spacing
153-
'yield-star-spacing': ['error', 'after']
153+
'yield-star-spacing': ['error', 'after'],
154+
155+
# Enforce the consistent use of single quotes
156+
# https://eslint.org/docs/latest/rules/quotes
157+
quotes: [ error, single, { avoidEscape: true } ]
154158
}
155159
}

buildtools/webpack.commons.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ const config = {
8585
https: false,
8686
zlib: false,
8787
url: false,
88-
}
88+
},
89+
extensions: ['.ts', '.js']
8990
}
9091
};
9192

examples/icon-position.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import olStyleFill from 'ol/style/Fill.js';
1717
import olMap from 'ol/Map.js';
1818
import olSourceVector from 'ol/source/Vector.js';
1919
import olLayerVector from 'ol/layer/Vector.js';
20-
import {rotateAroundAxis, pickBottomPoint} from 'olcs/core.js';
20+
import {rotateAroundAxis, pickBottomPoint} from 'olcs/core.ts';
2121
import {OLCS_ION_TOKEN} from './_common.js';
2222

2323

examples/rotate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @module examples.rotate
33
*/
4-
import {computeSignedTiltAngleOnGlobe, pickBottomPoint, computeAngleToZenith, setHeadingUsingBottomCenter, rotateAroundAxis} from 'olcs/core.js';
4+
import {computeSignedTiltAngleOnGlobe, pickBottomPoint, computeAngleToZenith, setHeadingUsingBottomCenter, rotateAroundAxis} from 'olcs/core.ts';
55
import OLCesium from 'olcs/OLCesium.ts';
66
import olView from 'ol/View.js';
77
import {defaults as olControlDefaults} from 'ol/control.js';

examples/vectors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import olInteractionDragAndDrop from 'ol/interaction/DragAndDrop.js';
2828
import olGeomMultiPolygon from 'ol/geom/MultiPolygon.js';
2929
import olLayerVector from 'ol/layer/Vector.js';
3030
import {transform} from 'ol/proj.js';
31-
import {createMatrixAtCoordinates} from 'olcs/core.js';
31+
import {createMatrixAtCoordinates} from 'olcs/core.ts';
3232
import {OLCS_ION_TOKEN} from './_common.js';
3333

3434

src/index.library.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import OLCesium from './olcs/OLCesium.ts';
22

3-
import AbstractSynchronizer from './olcs/AbstractSynchronizer.js';
3+
import AbstractSynchronizer from './olcs/AbstractSynchronizer.ts';
44
import RasterSynchronizer from './olcs/RasterSynchronizer.js';
55
import VectorSynchronizer from './olcs/VectorSynchronizer.js';
66

7-
import * as core from './olcs/core.js';
7+
import * as core from './olcs/core.ts';
88
import OLImageryProvider from './olcs/core/OLImageryProvider.js';
99
import VectorLayerCounterpart from './olcs/core/VectorLayerCounterpart.js';
1010

src/olcs/AbstractSynchronizer.js src/olcs/AbstractSynchronizer.ts

+51-117
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,48 @@
22
* @module olcs.AbstractSynchronizer
33
*/
44
import {unByKey as olObservableUnByKey} from 'ol/Observable.js';
5-
import olLayerGroup from 'ol/layer/Group.js';
6-
import {olcsListen, getUid} from './util.js';
7-
8-
9-
class AbstractSynchronizer {
5+
import LayerGroup from 'ol/layer/Group.js';
6+
import {getUid, olcsListen} from './util.js';
7+
import Map from 'ol/Map'
8+
import type {Scene, ImageryLayer} from 'cesium';
9+
import View from 'ol/View';
10+
import Collection from 'ol/Collection';
11+
import BaseLayer from 'ol/layer/Base';
12+
import {EventsKey} from 'ol/events';
13+
import {LayerWithParents} from './core';
14+
import VectorLayerCounterpart from './core/VectorLayerCounterpart';
15+
16+
17+
abstract class AbstractSynchronizer<T extends ImageryLayer | VectorLayerCounterpart> {
18+
protected map: Map;
19+
protected view: View;
20+
protected scene: Scene;
21+
protected olLayers: Collection<BaseLayer>;
22+
mapLayerGroup: LayerGroup;
1023
/**
11-
* @param {!ol.Map} map
12-
* @param {!Cesium.Scene} scene
13-
* @template T
14-
* @abstract
15-
* @api
24+
* Map of OpenLayers layer ids (from getUid) to the Cesium ImageryLayers.
25+
* Null value means, that we are unable to create equivalent layers.
1626
*/
17-
constructor(map, scene) {
18-
/**
19-
* @type {!ol.Map}
20-
* @protected
21-
*/
22-
this.map = map;
27+
protected layerMap: Record<string, Array<T>> = {};
28+
/**
29+
* Map of listen keys for OpenLayers layer layers ids (from getUid).
30+
*/
31+
protected olLayerListenKeys: Record<string, Array<EventsKey>> = {};
32+
/**
33+
* Map of listen keys for OpenLayers layer groups ids (from getUid).
34+
*/
35+
private olGroupListenKeys_: Record<string, Array<EventsKey>> = {};
2336

24-
/**
25-
* @type {ol.View}
26-
* @protected
27-
*/
37+
protected constructor(map: Map, scene: Scene) {
38+
this.map = map;
2839
this.view = map.getView();
29-
30-
/**
31-
* @type {!Cesium.Scene}
32-
* @protected
33-
*/
3440
this.scene = scene;
35-
36-
/**
37-
* @type {ol.Collection.<ol.layer.Base>}
38-
* @protected
39-
*/
4041
this.olLayers = map.getLayerGroup().getLayers();
41-
42-
/**
43-
* @type {ol.layer.Group}
44-
*/
4542
this.mapLayerGroup = map.getLayerGroup();
46-
47-
/**
48-
* Map of OpenLayers layer ids (from getUid) to the Cesium ImageryLayers.
49-
* Null value means, that we are unable to create equivalent layers.
50-
* @type {Object.<string, ?Array.<T>>}
51-
* @protected
52-
*/
53-
this.layerMap = {};
54-
55-
/**
56-
* Map of listen keys for OpenLayers layer layers ids (from getUid).
57-
* @type {!Object.<string, Array<ol.EventsKey>>}
58-
* @protected
59-
*/
60-
this.olLayerListenKeys = {};
61-
62-
/**
63-
* Map of listen keys for OpenLayers layer groups ids (from getUid).
64-
* @type {!Object.<string, !Array.<ol.EventsKey>>}
65-
* @private
66-
*/
67-
this.olGroupListenKeys_ = {};
6843
}
6944

7045
/**
7146
* Destroy all and perform complete synchronization of the layers.
72-
* @api
7347
*/
7448
synchronize() {
7549
this.destroyAll();
@@ -79,20 +53,16 @@ class AbstractSynchronizer {
7953
/**
8054
* Order counterparts using the same algorithm as the Openlayers renderer:
8155
* z-index then original sequence order.
82-
* @protected
8356
*/
84-
orderLayers() {
57+
protected orderLayers() {
8558
// Ordering logics is handled in subclasses.
8659
}
8760

8861
/**
8962
* Add a layer hierarchy.
90-
* @param {ol.layer.Base} root
91-
* @private
9263
*/
93-
addLayers_(root) {
94-
/** @type {Array<import('olsc/core.js').LayerWithParents>} */
95-
const fifo = [{
64+
private addLayers_(root: BaseLayer) {
65+
const fifo: LayerWithParents[] = [{
9666
layer: root,
9767
parents: []
9868
}];
@@ -104,15 +74,15 @@ class AbstractSynchronizer {
10474
console.assert(!this.layerMap[olLayerId]);
10575

10676
let cesiumObjects = null;
107-
if (olLayer instanceof olLayerGroup) {
77+
if (olLayer instanceof LayerGroup) {
10878
this.listenForGroupChanges_(olLayer);
10979
if (olLayer !== this.mapLayerGroup) {
11080
cesiumObjects = this.createSingleLayerCounterparts(olLayerWithParents);
11181
}
11282
if (!cesiumObjects) {
11383
olLayer.getLayers().forEach((l) => {
11484
if (l) {
115-
const newOlLayerWithParents = {
85+
const newOlLayerWithParents: LayerWithParents = {
11686
layer: l,
11787
parents: olLayer === this.mapLayerGroup ?
11888
[] :
@@ -129,7 +99,7 @@ class AbstractSynchronizer {
12999
// for example when a source is set after the layer is added to the map
130100
const layerId = olLayerId;
131101
const layerWithParents = olLayerWithParents;
132-
const onLayerChange = (e) => {
102+
const onLayerChange = () => {
133103
const cesiumObjs = this.createSingleLayerCounterparts(layerWithParents);
134104
if (cesiumObjs) {
135105
// unsubscribe event listener
@@ -152,12 +122,8 @@ class AbstractSynchronizer {
152122

153123
/**
154124
* Add Cesium objects.
155-
* @param {Array.<T>} cesiumObjects
156-
* @param {string} layerId
157-
* @param {ol.layer.Base} layer
158-
* @private
159125
*/
160-
addCesiumObjects_(cesiumObjects, layerId, layer) {
126+
private addCesiumObjects_(cesiumObjects: Array<T>, layerId: string, layer: BaseLayer) {
161127
this.layerMap[layerId] = cesiumObjects;
162128
this.olLayerListenKeys[layerId].push(olcsListen(layer, 'change:zIndex', () => this.orderLayers()));
163129
cesiumObjects.forEach((cesiumObject) => {
@@ -169,9 +135,8 @@ class AbstractSynchronizer {
169135
* Remove and destroy a single layer.
170136
* @param {ol.layer.Layer} layer
171137
* @return {boolean} counterpart destroyed
172-
* @private
173138
*/
174-
removeAndDestroySingleLayer_(layer) {
139+
private removeAndDestroySingleLayer_(layer: BaseLayer): boolean {
175140
const uid = getUid(layer).toString();
176141
const counterparts = this.layerMap[uid];
177142
if (!!counterparts) {
@@ -188,10 +153,8 @@ class AbstractSynchronizer {
188153

189154
/**
190155
* Unlisten a single layer group.
191-
* @param {ol.layer.Group} group
192-
* @private
193156
*/
194-
unlistenSingleGroup_(group) {
157+
private unlistenSingleGroup_(group: LayerGroup) {
195158
if (group === this.mapLayerGroup) {
196159
return;
197160
}
@@ -206,16 +169,14 @@ class AbstractSynchronizer {
206169

207170
/**
208171
* Remove layer hierarchy.
209-
* @param {ol.layer.Base} root
210-
* @private
211172
*/
212-
removeLayer_(root) {
173+
private removeLayer_(root: BaseLayer) {
213174
if (!!root) {
214175
const fifo = [root];
215176
while (fifo.length > 0) {
216177
const olLayer = fifo.splice(0, 1)[0];
217178
const done = this.removeAndDestroySingleLayer_(olLayer);
218-
if (olLayer instanceof olLayerGroup) {
179+
if (olLayer instanceof LayerGroup) {
219180
this.unlistenSingleGroup_(olLayer);
220181
if (!done) {
221182
// No counterpart for the group itself so removing
@@ -231,19 +192,17 @@ class AbstractSynchronizer {
231192

232193
/**
233194
* Register listeners for single layer group change.
234-
* @param {ol.layer.Group} group
235-
* @private
236195
*/
237-
listenForGroupChanges_(group) {
196+
private listenForGroupChanges_(group: LayerGroup) {
238197
const uuid = getUid(group).toString();
239198

240199
console.assert(this.olGroupListenKeys_[uuid] === undefined);
241200

242-
const listenKeyArray = [];
201+
const listenKeyArray: EventsKey[] = [];
243202
this.olGroupListenKeys_[uuid] = listenKeyArray;
244203

245204
// only the keys that need to be relistened when collection changes
246-
let contentKeys = [];
205+
let contentKeys: EventsKey[] = [];
247206
const listenAddRemove = (function() {
248207
const collection = group.getLayers();
249208
if (collection) {
@@ -275,9 +234,8 @@ class AbstractSynchronizer {
275234

276235
/**
277236
* Destroys all the created Cesium objects.
278-
* @protected
279237
*/
280-
destroyAll() {
238+
protected destroyAll() {
281239
this.removeAllCesiumObjects(true); // destroy
282240
let objKey;
283241
for (objKey in this.olGroupListenKeys_) {
@@ -294,43 +252,19 @@ class AbstractSynchronizer {
294252

295253
/**
296254
* Adds a single Cesium object to the collection.
297-
* @param {!T} object
298-
* @abstract
299-
* @protected
300255
*/
301-
addCesiumObject(object) {}
256+
protected abstract addCesiumObject(object: T): void;
302257

303-
/**
304-
* @param {!T} object
305-
* @abstract
306-
* @protected
307-
*/
308-
destroyCesiumObject(object) {}
258+
protected abstract destroyCesiumObject(object: T): void;
309259

310260
/**
311261
* Remove single Cesium object from the collection.
312-
* @param {!T} object
313-
* @param {boolean} destroy
314-
* @abstract
315-
* @protected
316262
*/
317-
removeSingleCesiumObject(object, destroy) {}
263+
protected abstract removeSingleCesiumObject(object: T, destroy: boolean): void;
318264

319-
/**
320-
* Remove all Cesium objects from the collection.
321-
* @param {boolean} destroy
322-
* @abstract
323-
* @protected
324-
*/
325-
removeAllCesiumObjects(destroy) {}
265+
protected abstract removeAllCesiumObjects(destroy: boolean): void;
326266

327-
/**
328-
* @param {import('olsc/core.js').LayerWithParents} olLayerWithParents
329-
* @return {?Array.<T>}
330-
* @abstract
331-
* @protected
332-
*/
333-
createSingleLayerCounterparts(olLayerWithParents) {}
267+
protected abstract createSingleLayerCounterparts(olLayerWithParents: LayerWithParents): Array<T>;
334268
}
335269

336270

src/olcs/Camera.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import {unByKey as olObservableUnByKey} from 'ol/Observable.js';
66
import {toRadians, toDegrees} from './math.js';
77
import {getTransform} from 'ol/proj.js';
8-
import {pickCenterPoint, calcDistanceForResolution, calcResolutionForDistance} from './core.js';
8+
import {pickCenterPoint, calcDistanceForResolution, calcResolutionForDistance} from './core.ts';
99

1010
class Camera {
1111
/**

src/olcs/FeatureConverter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import olSourceCluster from 'ol/source/Cluster.js';
88
import {circular as olCreateCircularPolygon} from 'ol/geom/Polygon.js';
99
import {boundingExtent, getCenter} from 'ol/extent.js';
1010
import olGeomSimpleGeometry from 'ol/geom/SimpleGeometry.js';
11-
import {convertColorToCesium, olGeometryCloneTo4326, ol4326CoordinateToCesiumCartesian, ol4326CoordinateArrayToCsCartesians} from './core.js';
11+
import {convertColorToCesium, olGeometryCloneTo4326, ol4326CoordinateToCesiumCartesian, ol4326CoordinateArrayToCsCartesians} from './core.ts';
1212
import olcsCoreVectorLayerCounterpart from './core/VectorLayerCounterpart.js';
1313
import {obj, getUid, isGroundPolylinePrimitiveSupported} from './util.js';
1414

0 commit comments

Comments
 (0)