fix: 修复数据更新方法

This commit is contained in:
thinkinggis 2020-01-16 18:22:38 +08:00
parent 63b7bb844a
commit 052f306faf
23 changed files with 76 additions and 23 deletions

View File

@ -16,6 +16,7 @@ module.exports = api => {
plugins: [ plugins: [
'@babel/plugin-proposal-optional-chaining', '@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator', '@babel/plugin-proposal-nullish-coalescing-operator',
'transform-inline-environment-variables',
[ [
'@babel/plugin-proposal-decorators', '@babel/plugin-proposal-decorators',
{ {

View File

@ -77,6 +77,7 @@ Promise.all([
.color('#ff6b34') .color('#ff6b34')
.shape('arc3d') .shape('arc3d')
.size(2) .size(2)
.active(true)
.animate({ .animate({
interval: 2, interval: 2,
trailLength: 2, trailLength: 2,

View File

@ -80,6 +80,7 @@ Promise.all([
.color('#b97feb') .color('#b97feb')
.shape('arc3d') .shape('arc3d')
.size(2) .size(2)
.active(true)
.animate({ .animate({
interval: 2, interval: 2,
trailLength: 2, trailLength: 2,

View File

@ -43,6 +43,7 @@
"babel-plugin-inline-webgl-constants": "^1.0.1", "babel-plugin-inline-webgl-constants": "^1.0.1",
"babel-plugin-lodash": "^3.3.4", "babel-plugin-lodash": "^3.3.4",
"babel-plugin-transform-import-styles": "^0.0.11", "babel-plugin-transform-import-styles": "^0.0.11",
"babel-plugin-transform-inline-environment-variables": "^0.4.3",
"babel-plugin-transform-postcss": "^0.3.0", "babel-plugin-transform-postcss": "^0.3.0",
"babel-preset-gatsby": "^0.2.22", "babel-preset-gatsby": "^0.2.22",
"babel-template": "^6.26.0", "babel-template": "^6.26.0",
@ -144,7 +145,7 @@
"test-live": "cross-env BABEL_ENV=test DEBUG_MODE=1 jest --watch packages/scene/__tests__/index.spec.ts ", "test-live": "cross-env BABEL_ENV=test DEBUG_MODE=1 jest --watch packages/scene/__tests__/index.spec.ts ",
"coveralls": "jest --coverage && cat ./tests/coverage/lcov.info | coveralls", "coveralls": "jest --coverage && cat ./tests/coverage/lcov.info | coveralls",
"tsc": "tsc", "tsc": "tsc",
"watch": "yarn clean && lerna exec --parallel -- cross-env BABEL_ENV=cjs babel --watch src --root-mode upward --out-dir lib --source-maps --extensions .ts,.tsx --delete-dir-on-start --no-comments", "watch": "yarn clean && lerna exec --parallel -- cross-env BABEL_ENV=cjs NODE_ENV=production babel --watch src --root-mode upward --out-dir lib --source-maps --extensions .ts,.tsx --delete-dir-on-start --no-comments",
"bundle": "cross-env BABEL_ENV=bundle node_modules/.bin/rollup -c ./build/rollup.config.js", "bundle": "cross-env BABEL_ENV=bundle node_modules/.bin/rollup -c ./build/rollup.config.js",
"bundle:watch": "cross-env BABEL_ENV=bundle node_modules/.bin/rollup -c ./build/rollup.config.js --watch", "bundle:watch": "cross-env BABEL_ENV=bundle node_modules/.bin/rollup -c ./build/rollup.config.js --watch",
"glsl-minify": "node_modules/.bin/glsl-minifier -i ./build/example.frag -o ./build/example.min.frag", "glsl-minify": "node_modules/.bin/glsl-minifier -i ./build/example.frag -o ./build/example.min.frag",

View File

@ -16,7 +16,7 @@ export interface IMarkerOption {
export default class Marker extends EventEmitter { export default class Marker extends EventEmitter {
private markerOption: IMarkerOption; private markerOption: IMarkerOption;
private defaultMarker: boolean; private defaultMarker: boolean;
private popup: IPopup; // TODO: POPup private popup: IPopup;
private mapsService: IMapService<unknown>; private mapsService: IMapService<unknown>;
private lngLat: ILngLat; private lngLat: ILngLat;
private scene: Container; private scene: Container;

View File

@ -4,7 +4,7 @@ import { IMapWrapper } from '../map/IMapService';
import { IRenderConfig } from '../renderer/IRendererService'; import { IRenderConfig } from '../renderer/IRendererService';
export interface ISceneConfig extends IRenderConfig { export interface ISceneConfig extends IRenderConfig {
id: string; id: string | HTMLDivElement;
map: IMapWrapper; map: IMapWrapper;
} }

View File

@ -14,6 +14,8 @@ export default class LayerService implements ILayerService {
private layerRenderID: number; private layerRenderID: number;
private sceneInited: boolean = false;
private animateInstanceCount: number = 0; private animateInstanceCount: number = 0;
private alreadyInRendering: boolean = false; private alreadyInRendering: boolean = false;
@ -25,10 +27,14 @@ export default class LayerService implements ILayerService {
private readonly configService: IGlobalConfigService; private readonly configService: IGlobalConfigService;
public add(layer: ILayer) { public add(layer: ILayer) {
// if (this.sceneInited) {
// layer.init();
// }
this.layers.push(layer); this.layers.push(layer);
} }
public initLayers() { public initLayers() {
this.sceneInited = true;
this.layers.forEach((layer) => { this.layers.forEach((layer) => {
if (!layer.inited) { if (!layer.inited) {
layer.init(); layer.init();

View File

@ -3,7 +3,6 @@ import Probe, { Log } from 'probe.gl';
import { ILogService } from './ILogService'; import { ILogService } from './ILogService';
const Logger = new Log({ id: 'L7' }).enable(false); const Logger = new Log({ id: 'L7' }).enable(false);
// // 只输出 debug 级别以上的日志信息 // // 只输出 debug 级别以上的日志信息
Logger.priority = 5; Logger.priority = 5;

View File

@ -13,7 +13,7 @@ export interface IPoint {
export type MapStyle = string | { [key: string]: any }; export type MapStyle = string | { [key: string]: any };
export interface IMapWrapper { export interface IMapWrapper {
setContainer(container: Container, id: string): void; setContainer(container: Container, id: string | HTMLDivElement): void;
} }
export interface IMapService<RawMap = {}> { export interface IMapService<RawMap = {}> {

View File

@ -200,6 +200,7 @@ export default class Scene extends EventEmitter implements ISceneService {
// FIXME: 初始化 marker 容器,可以放到 map 初始化方法中? // FIXME: 初始化 marker 容器,可以放到 map 初始化方法中?
this.logger.info(' render inited'); this.logger.info(' render inited');
this.controlService.addControls();
this.emit('loaded'); this.emit('loaded');
this.inited = true; this.inited = true;
} }
@ -208,7 +209,7 @@ export default class Scene extends EventEmitter implements ISceneService {
this.layerService.initLayers(); this.layerService.initLayers();
this.layerService.renderLayers(); this.layerService.renderLayers();
// 组件需要等待layer 初始化完成之后添加 // 组件需要等待layer 初始化完成之后添加
this.controlService.addControls();
this.logger.debug(`scene ${this.id} render`); this.logger.debug(`scene ${this.id} render`);
this.rendering = false; this.rendering = false;

View File

@ -61,8 +61,10 @@ export interface ISource {
data: IParserData; data: IParserData;
cluster: boolean; cluster: boolean;
clusterOptions: Partial<IClusterOptions>; clusterOptions: Partial<IClusterOptions>;
setData(data: any): void;
updateClusterData(zoom: number): void; updateClusterData(zoom: number): void;
getFeatureById(id: number): unknown; getFeatureById(id: number): unknown;
getFeatureId(field: string, value: any): number | undefined;
getClusters(zoom: number): any; getClusters(zoom: number): any;
getClustersLeaves(id: number): any; getClustersLeaves(id: number): any;
} }

View File

@ -419,10 +419,14 @@ export default class BaseLayer<ChildLayerStyleOptions = {}> extends EventEmitter
} }
public setData(data: any, options?: ISourceCFG) { public setData(data: any, options?: ISourceCFG) {
this.sourceOption.data = data; if (this.inited) {
this.sourceOption.options = options; this.layerSource.setData(data);
this.hooks.init.call(); } else {
this.buildModels(); this.on('inited', () => {
this.layerSource.setData(data);
});
}
return this; return this;
} }
public style( public style(
@ -641,6 +645,8 @@ export default class BaseLayer<ChildLayerStyleOptions = {}> extends EventEmitter
public destroy() { public destroy() {
this.hooks.beforeDestroy.call(); this.hooks.beforeDestroy.call();
// 清除sources事件
this.layerSource.off('update', this.sourceEvent);
// 清除所有属性以及关联的 vao // 清除所有属性以及关联的 vao
this.styleAttributeService.clearAllAttributes(); this.styleAttributeService.clearAllAttributes();
@ -673,11 +679,12 @@ export default class BaseLayer<ChildLayerStyleOptions = {}> extends EventEmitter
public setSource(source: Source) { public setSource(source: Source) {
this.layerSource = source; this.layerSource = source;
const bounds = this.mapService.getBounds();
const zoom = this.mapService.getZoom(); const zoom = this.mapService.getZoom();
if (this.layerSource.cluster) { if (this.layerSource.cluster) {
this.layerSource.updateClusterData(zoom); this.layerSource.updateClusterData(zoom);
} }
// source 可能会复用会在其它layer被修改
this.layerSource.on('update', this.sourceEvent);
} }
public getSource() { public getSource() {
return this.layerSource; return this.layerSource;
@ -793,6 +800,12 @@ export default class BaseLayer<ChildLayerStyleOptions = {}> extends EventEmitter
protected getDefaultConfig() { protected getDefaultConfig() {
return {}; return {};
} }
private sourceEvent = () => {
this.dataState.dataSourceNeedUpdate = true;
this.reRender();
};
private reRender() { private reRender() {
if (this.inited) { if (this.inited) {
this.layerService.renderLayers(); this.layerService.renderLayers();

View File

@ -13,9 +13,12 @@ export default class DataSourcePlugin implements ILayerPlugin {
this.updateClusterData(layer); this.updateClusterData(layer);
}); });
// 检测数据不否需要更新 // 检测数据不否需要更新
layer.hooks.beforeRenderData.tap('DataSourcePlugin', (flag) => { layer.hooks.beforeRenderData.tap('DataSourcePlugin', (flag) => {
return this.updateClusterData(layer); const neeUpdate1 = this.updateClusterData(layer);
const neeUpdate2 = layer.dataState.dataSourceNeedUpdate;
layer.dataState.dataSourceNeedUpdate = false;
return neeUpdate1 || neeUpdate2;
}); });
} }

View File

@ -8,7 +8,6 @@ import {
TYPES, TYPES,
} from '@antv/l7-core'; } from '@antv/l7-core';
import { Container } from 'inversify'; import { Container } from 'inversify';
export default class BaseMapWrapper<RawMap> implements IMapWrapper { export default class BaseMapWrapper<RawMap> implements IMapWrapper {
@lazyInject(TYPES.ILogService) @lazyInject(TYPES.ILogService)
protected readonly logger: ILogService; protected readonly logger: ILogService;
@ -23,7 +22,7 @@ export default class BaseMapWrapper<RawMap> implements IMapWrapper {
} }
public setContainer(sceneContainer: Container, id: string) { public setContainer(sceneContainer: Container, id: string) {
// 首先使用全局配置服务校验地图参数 // // 首先使用全局配置服务校验地图参数
const { valid, errorText } = this.configService.validateMapConfig( const { valid, errorText } = this.configService.validateMapConfig(
this.config, this.config,
); );
@ -32,7 +31,6 @@ export default class BaseMapWrapper<RawMap> implements IMapWrapper {
this.logger.error(errorText || ''); this.logger.error(errorText || '');
return; return;
} }
// 绑定用户传入的原始地图参数 // 绑定用户传入的原始地图参数
sceneContainer.bind<Partial<IMapConfig>>(TYPES.MapConfig).toConstantValue({ sceneContainer.bind<Partial<IMapConfig>>(TYPES.MapConfig).toConstantValue({
...this.config, ...this.config,

View File

@ -260,7 +260,7 @@ export default class AMapService
resolve(); resolve();
} }
}; };
if (!document.getElementById(AMAP_SCRIPT_ID)) { if (!document.getElementById(AMAP_SCRIPT_ID) || !mapInstance) {
// 异步加载高德地图 // 异步加载高德地图
// @see https://lbs.amap.com/api/javascript-api/guide/abc/load // @see https://lbs.amap.com/api/javascript-api/guide/abc/load
// @ts-ignore // @ts-ignore

View File

@ -61,7 +61,6 @@ class Scene
// 创建场景容器 // 创建场景容器
const sceneContainer = createSceneContainer(); const sceneContainer = createSceneContainer();
this.container = sceneContainer; this.container = sceneContainer;
// 绑定地图服务 // 绑定地图服务
map.setContainer(sceneContainer, id); map.setContainer(sceneContainer, id);

View File

@ -1,6 +1,7 @@
import { import {
IClusterOptions, IClusterOptions,
IMapService, IMapService,
IParseDataItem,
IParserCfg, IParserCfg,
IParserData, IParserData,
ISourceCFG, ISourceCFG,
@ -84,6 +85,13 @@ export default class Source extends EventEmitter {
}); });
this.init(); this.init();
} }
public setData(data: any) {
this.rawData = data;
this.originData = cloneDeep(data);
this.init();
this.emit('update');
}
public getClusters(zoom: number): any { public getClusters(zoom: number): any {
return this.clusterIndex.getClusters(this.extent, zoom); return this.clusterIndex.getClusters(this.extent, zoom);
} }
@ -128,7 +136,7 @@ export default class Source extends EventEmitter {
} }
public getFeatureById(id: number): unknown { public getFeatureById(id: number): unknown {
const { type = 'geojson' } = this.parser; const { type = 'geojson' } = this.parser;
if (type === 'geojson') { if (type === 'geojson' && !this.cluster) {
return id < this.rawData.features.length return id < this.rawData.features.length
? this.rawData.features[id] ? this.rawData.features[id]
: 'null'; : 'null';
@ -137,6 +145,13 @@ export default class Source extends EventEmitter {
} }
} }
public getFeatureId(field: string, value: any): number | undefined {
const feature = this.data.dataArray.find((dataItem: IParseDataItem) => {
return dataItem[field] === name;
});
return feature?._id;
}
private excuteParser(): void { private excuteParser(): void {
const parser = this.parser; const parser = this.parser;
const type: string = parser.type || 'geojson'; const type: string = parser.type || 'geojson';

View File

@ -23,6 +23,7 @@ export default class ChartComponent extends React.Component {
}); });
addChart(); addChart();
scene.render(); scene.render();
this.scene = scene;
function addChart() { function addChart() {
Promise.all([ Promise.all([
fetch( fetch(

View File

@ -35,16 +35,22 @@ export default class Point3D extends React.Component {
// .scale('point_count', { // .scale('point_count', {
// type: 'quantile', // type: 'quantile',
// }) // })
// .size('point_count', [5, 10, 15, 20, 25]) .size('mag', [5, 10, 15, 20, 25])
.size(5)
.animate(false) .animate(false)
.active(true) .active(true)
.color('yellow') .color('yellow')
.style({ .style({
opacity: 1, opacity: 0.5,
strokeWidth: 1, strokeWidth: 1,
}); });
scene.addLayer(pointLayer); scene.addLayer(pointLayer);
scene.on('loaded', () => {
const newData = {
type: 'FeatureCollection',
features: pointsData.features.slice(0, 100),
};
pointLayer.setData(newData);
});
this.scene = scene; this.scene = scene;
// }); // });
} }

View File

@ -25,6 +25,7 @@ export default class GaodeMapComponent extends React.Component {
center: [116.397428, 39.90923], // 初始化地图中心点 center: [116.397428, 39.90923], // 初始化地图中心点
}); });
const scene = new Scene({ const scene = new Scene({
// @ts-ignore
id: 'map', id: 'map',
map: new GaodeMap({ map: new GaodeMap({
mapInstance: map, mapInstance: map,

View File

@ -17,7 +17,7 @@ export default class MapboxComponent extends React.Component {
const scene = new Scene({ const scene = new Scene({
id: 'map', id: 'map',
map: new Mapbox({ map: new Mapbox({
style: 'mapbox://styles/mapbox/streets-v9', style: 'dark',
center: [110.19382669582967, 30.258134], center: [110.19382669582967, 30.258134],
pitch: 0, pitch: 0,
zoom: 3, zoom: 3,

View File

@ -5335,6 +5335,11 @@ babel-plugin-transform-inline-consecutive-adds@^0.4.3:
resolved "https://registry.npm.taobao.org/babel-plugin-transform-inline-consecutive-adds/download/babel-plugin-transform-inline-consecutive-adds-0.4.3.tgz#323d47a3ea63a83a7ac3c811ae8e6941faf2b0d1" resolved "https://registry.npm.taobao.org/babel-plugin-transform-inline-consecutive-adds/download/babel-plugin-transform-inline-consecutive-adds-0.4.3.tgz#323d47a3ea63a83a7ac3c811ae8e6941faf2b0d1"
integrity sha1-Mj1Ho+pjqDp6w8gRro5pQfrysNE= integrity sha1-Mj1Ho+pjqDp6w8gRro5pQfrysNE=
babel-plugin-transform-inline-environment-variables@^0.4.3:
version "0.4.3"
resolved "https://registry.npmjs.org/babel-plugin-transform-inline-environment-variables/-/babel-plugin-transform-inline-environment-variables-0.4.3.tgz#a3b09883353be8b5e2336e3ff1ef8a5d93f9c489"
integrity sha1-o7CYgzU76LXiM24/8e+KXZP5xIk=
babel-plugin-transform-member-expression-literals@^6.9.4: babel-plugin-transform-member-expression-literals@^6.9.4:
version "6.9.4" version "6.9.4"
resolved "https://registry.npm.taobao.org/babel-plugin-transform-member-expression-literals/download/babel-plugin-transform-member-expression-literals-6.9.4.tgz#37039c9a0c3313a39495faac2ff3a6b5b9d038bf" resolved "https://registry.npm.taobao.org/babel-plugin-transform-member-expression-literals/download/babel-plugin-transform-member-expression-literals-6.9.4.tgz#37039c9a0c3313a39495faac2ff3a6b5b9d038bf"