Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[Chore]: Technical: Translate deckgl-layers/layer-utils typesfix (#1791)

Maksim Suslovae1173ec28b9
diff --git a/src/deckgl-layers/layer-utils/cpu-aggregator.ts b/src/deckgl-layers/layer-utils/cpu-aggregator.ts
index c4c603eb8..212a5e1a4 100644
--- a/src/deckgl-layers/layer-utils/cpu-aggregator.ts
+++ b/src/deckgl-layers/layer-utils/cpu-aggregator.ts
@@ -26,13 +26,22 @@ import {aggregate} from '../../utils/aggregate-utils';
import {AGGREGATION_TYPES, SCALE_FUNC} from '../../constants/default-settings';
import {RGBAColor} from '../../reducers';
-export type UpdaterType = (step, props, dimensionUpdater) => void;
+export type UpdaterType = (this: CPUAggregator, step, props, dimensionUpdater) => void;
+export type BindedUpdaterType = () => void;
+export type AggregatedUpdaterType = (
+ this: CPUAggregator,
+ step,
+ props,
+ aggregation,
+ aggregationParams
+) => void;
+export type BindedAggregatedUpdaterType = (aggregationParams) => void;
export type UpdateStepsType = {
key: string;
triggers: {
[key: string]: {
- prop?: string;
+ prop: string;
updateTrigger?: string;
};
};
@@ -42,15 +51,31 @@ export type UpdateStepsType = {
updater: UpdaterType;
};
-export type DimensionType<ValueType> = {
+export type DimensionType<ValueType = any> = {
key: string;
accessor: string;
- getPickingInfo: (dimensionState, cell) => any;
+ getPickingInfo: (dimensionState, cell, layerProps?) => any;
nullValue: ValueType;
updateSteps: UpdateStepsType[];
getSubLayerAccessor;
};
+export type AggregationUpdateStepsType = {
+ key: string;
+ triggers: {
+ [key: string]: {
+ prop: string;
+ updateTrigger?: string;
+ };
+ };
+ updater: AggregatedUpdaterType;
+};
+
+export type AggregationType = {
+ key: string;
+ updateSteps: AggregationUpdateStepsType[];
+};
+
export const DECK_AGGREGATION_MAP = {
[AGGREGATION_OPERATION.SUM]: AGGREGATION_TYPES.sum,
[AGGREGATION_OPERATION.MEAN]: AGGREGATION_TYPES.average,
@@ -78,7 +103,7 @@ export function getScaleFunctor(scaleType) {
function nop() {}
-export function getGetValue(step, props, dimensionUpdater) {
+export function getGetValue(this: CPUAggregator, step, props, dimensionUpdater) {
const {key} = dimensionUpdater;
const {value, weight, aggregation} = step.triggers;
@@ -94,10 +119,10 @@ export function getGetValue(step, props, dimensionUpdater) {
}
}
-export function getDimensionSortedBins(step, props, dimensionUpdater) {
+export function getDimensionSortedBins(this: CPUAggregator, step, props, dimensionUpdater) {
const {key} = dimensionUpdater;
const {getValue} = this.state.dimensions[key];
-
+ // @ts-expect-error
const sortedBins = new BinSorter(this.state.layerData.data || [], {
getValue,
filterData: props._filterData
@@ -105,7 +130,7 @@ export function getDimensionSortedBins(step, props, dimensionUpdater) {
this._setDimensionState(key, {sortedBins});
}
-export function getDimensionValueDomain(step, props, dimensionUpdater) {
+export function getDimensionValueDomain(this: CPUAggregator, step, props, dimensionUpdater) {
const {key} = dimensionUpdater;
const {
triggers: {lowerPercentile, upperPercentile, scaleType}
@@ -126,7 +151,7 @@ export function getDimensionValueDomain(step, props, dimensionUpdater) {
this._setDimensionState(key, {valueDomain});
}
-export function getDimensionScale(step, props, dimensionUpdater) {
+export function getDimensionScale(this: CPUAggregator, step, props, dimensionUpdater) {
const {key} = dimensionUpdater;
const {domain, range, scaleType} = step.triggers;
const {onSet} = step;
@@ -159,7 +184,13 @@ function normalizeResult(result: {hexagons?; layerData?} = {}) {
return result;
}
-export function getAggregatedData(step, props, aggregation, aggregationParams) {
+export function getAggregatedData(
+ this: CPUAggregator,
+ step,
+ props,
+ aggregation,
+ aggregationParams
+) {
const {
triggers: {aggregator: aggr}
} = step;
@@ -173,7 +204,7 @@ export function getAggregatedData(step, props, aggregation, aggregationParams) {
});
}
-export const defaultAggregation = {
+export const defaultAggregation: AggregationType = {
key: 'position',
updateSteps: [
{
@@ -349,22 +380,21 @@ export const defaultElevationDimension: DimensionType<number> = {
export const defaultDimensions = [defaultColorDimension, defaultElevationDimension];
-export type CPUAggregatorState = {layerData: {data}; dimensions: {}};
+export type CPUAggregatorState = {layerData: {data?}; dimensions: {}};
export default class CPUAggregator {
static getDimensionScale: any;
state: CPUAggregatorState;
- dimensionUpdaters: {
- [key: string]: {
- updateSteps: UpdateStepsType[];
- accessor: string;
- getPickingInfo;
- getSubLayerAccessor;
- };
- };
- aggregationUpdater: {};
-
- constructor(opts: {initialState?: CPUAggregatorState; dimensions?; aggregation?} = {}) {
+ dimensionUpdaters: {[key: string]: DimensionType};
+ aggregationUpdater: AggregationType;
+
+ constructor(
+ opts: {
+ initialState?: CPUAggregatorState;
+ dimensions?: DimensionType[];
+ aggregation?: AggregationType;
+ } = {}
+ ) {
this.state = {
layerData: {},
dimensions: {
@@ -383,11 +413,11 @@ export default class CPUAggregator {
},
...opts.initialState
};
+
this.dimensionUpdaters = {};
- this.aggregationUpdater = {};
+ this.aggregationUpdater = opts.aggregation || defaultAggregation;
this._addDimension(opts.dimensions || defaultDimensions);
- this._addAggregation(opts.aggregation || defaultAggregation);
}
static defaultDimensions() {
@@ -395,7 +425,7 @@ export default class CPUAggregator {
}
updateAllDimensions(props) {
- let dimensionChanges = [];
+ let dimensionChanges: BindedUpdaterType[] = [];
// update all dimensions
for (const dim in this.dimensionUpdaters) {
const updaters = this._accumulateUpdaters(0, props, this.dimensionUpdaters[dim]);
@@ -412,7 +442,7 @@ export default class CPUAggregator {
updateState(opts, aggregationParams) {
const {oldProps, props, changeFlags} = opts;
- let dimensionChanges = [];
+ let dimensionChanges: BindedUpdaterType[] = [];
if (changeFlags.dataChanged) {
// if data changed update everything
@@ -451,18 +481,23 @@ export default class CPUAggregator {
});
}
- _addAggregation(aggregation) {
+ _addAggregation(aggregation: AggregationType) {
this.aggregationUpdater = aggregation;
}
- _addDimension(dimensions = []) {
+ _addDimension(dimensions: DimensionType[] = []) {
dimensions.forEach(dimension => {
const {key} = dimension;
this.dimensionUpdaters[key] = dimension;
});
}
- _needUpdateStep(dimensionStep: UpdateStepsType, oldProps, props, changeFlags) {
+ _needUpdateStep(
+ dimensionStep: UpdateStepsType | AggregationUpdateStepsType,
+ oldProps,
+ props,
+ changeFlags
+ ) {
// whether need to update current dimension step
// dimension step is the value, domain, scaleFunction of each dimension
// each step is an object with properties links to layer prop and whether the prop is
@@ -481,12 +516,20 @@ export default class CPUAggregator {
});
}
- _accumulateUpdaters(step, props, dimension) {
- const updaters = [];
+ _accumulateUpdaters<UpdaterObjectType extends DimensionType | AggregationType>(
+ step,
+ props,
+ dimension: UpdaterObjectType
+ ) {
+ type UpdaterType = UpdaterObjectType extends DimensionType
+ ? BindedUpdaterType
+ : BindedAggregatedUpdaterType;
+ const updaters: UpdaterType[] = [];
for (let i = step; i < dimension.updateSteps.length; i++) {
- if (typeof dimension.updateSteps[i].updater === 'function') {
+ const updater = dimension.updateSteps[i].updater;
+ if (typeof updater === 'function') {
updaters.push(
- dimension.updateSteps[i].updater.bind(this, dimension.updateSteps[i], props, dimension)
+ updater.bind(this, dimension.updateSteps[i], props, dimension) as UpdaterType
);
}
}
@@ -494,8 +537,16 @@ export default class CPUAggregator {
return updaters;
}
- _getAllUpdaters(dimension, oldProps, props, changeFlags) {
- let updaters = [];
+ _getAllUpdaters<UpdaterObjectType extends DimensionType | AggregationType>(
+ dimension: UpdaterObjectType,
+ oldProps,
+ props,
+ changeFlags
+ ) {
+ type UpdaterType = UpdaterObjectType extends DimensionType
+ ? BindedUpdaterType
+ : BindedAggregatedUpdaterType;
+ let updaters: UpdaterType[] = [];
const needUpdateStep = dimension.updateSteps.findIndex(step =>
this._needUpdateStep(step, oldProps, props, changeFlags)
);
@@ -513,7 +564,7 @@ export default class CPUAggregator {
}
_getDimensionChanges(oldProps, props, changeFlags) {
- let updaters = [];
+ let updaters: BindedUpdaterType[] = [];
// get dimension to be updated
for (const key in this.dimensionUpdaters) {
diff --git a/tsconfig.json b/tsconfig.json
index ae36ecdf8..10140bfc0 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -26,11 +26,15 @@
"*": ["*"]
}
},
- "include": [
- "src"
- ],
+ "include": ["src"],
"exclude": [
- "src/deckgl-layers",
+ "src/deckgl-layers/3d-building-layer",
+ "src/deckgl-layers/cluster-layer",
+ "src/deckgl-layers/column-layer",
+ "src/deckgl-layers/grid-layer",
+ "src/deckgl-layers/hexagon-layer",
+ "src/deckgl-layers/line-layer",
+ "src/deckgl-layers/svg-icon-layer",
"src/layers"
]
}