Skip to content

Commit

Permalink
[Chore]: Technical: Translate deckgl-layers/layer-utils typesfix (#1791)
Browse files Browse the repository at this point in the history
* Let typescript check layer-utils files

Signed-off-by: Maksim Suslov <maksim.suslov@actionengine.com>

* Fixed most ts errors

Signed-off-by: Maksim Suslov <maksim.suslov@actionengine.com>

* Typed updaters and functions

Signed-off-by: Maksim Suslov <maksim.suslov@actionengine.com>

* Better types in constructor

Signed-off-by: Maksim Suslov <maksim.suslov@actionengine.com>

* Applied suggested changes

Signed-off-by: Maksim Suslov <maksim.suslov@actionengine.com>
  • Loading branch information
HeimEndyd committed May 6, 2022
1 parent 6a7d44b commit ae1173e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 41 deletions.
125 changes: 88 additions & 37 deletions src/deckgl-layers/layer-utils/cpu-aggregator.ts
Expand Up @@ -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;
};
};
Expand All @@ -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,
Expand Down Expand Up @@ -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;

Expand All @@ -94,18 +119,18 @@ 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
});
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}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -173,7 +204,7 @@ export function getAggregatedData(step, props, aggregation, aggregationParams) {
});
}

export const defaultAggregation = {
export const defaultAggregation: AggregationType = {
key: 'position',
updateSteps: [
{
Expand Down Expand Up @@ -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: {
Expand All @@ -383,19 +413,19 @@ 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() {
return defaultDimensions;
}

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]);
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -481,21 +516,37 @@ 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
);
}
}

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)
);
Expand All @@ -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) {
Expand Down
12 changes: 8 additions & 4 deletions tsconfig.json
Expand Up @@ -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"
]
}

0 comments on commit ae1173e

Please sign in to comment.