Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复 useLayer 动态加载图层情况 #25

Merged
merged 3 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
"version": "node scripts/sync-version"
},
"dependencies": {
"@antv/event-emitter": "^0.1.3",
"@antv/l7-composite-layers": "^0.1.0-alpha.0",
"@antv/l7-draw": "^3.0.4",
"@antv/l7-draw": "^3.0.6",
"@types/geojson": "^7946.0.8",
"ahooks": "^3.3.13",
"classnames": "^2.3.1",
Expand Down
2 changes: 1 addition & 1 deletion src/components/LarkMap/hooks/use-layer/demos/default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const MyComponent = () => {

useEffect(() => {
myBubbleLayer?.fitBounds();
}, []);
}, [myBubbleLayer]);

return null;
};
Expand Down
35 changes: 32 additions & 3 deletions src/components/LarkMap/hooks/use-layer/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
import { useEffect, useState } from 'react';
import type { Layer } from '../../../../utils';
import { useLayerManager } from '../use-layer-manager';

export const useLayer = <T extends Layer = Layer>(id: string): T => {
export const useLayer = <T extends Layer = Layer>(id: string): T | undefined => {
const layerManager = useLayerManager();
const layer = layerManager.getLayer(id);
const [layer, setLayer] = useState<T | undefined>(layerManager.getLayer(id) as T);

return layer as T;
useEffect(() => {
const onLayerAdd = (_layer: T) => {
if (id === _layer.id) {
setLayer(_layer);
}
};
const onLayerRemove = (layerId: string) => {
if (id === layerId) {
setLayer(undefined);
}
};
const _layer = layerManager.getLayer(id);

if (_layer) {
setLayer(_layer as T);
} else {
layerManager.on('add', onLayerAdd);
layerManager.on('remove', onLayerRemove);
}

return () => {
if (_layer === undefined) {
layerManager.off('add', onLayerAdd);
layerManager.off('remove', onLayerRemove);
}
};
}, [id]);

return layer;
};
6 changes: 5 additions & 1 deletion src/utils/layer-manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import EventEmitter from '@antv/event-emitter';
import type { ICompositeLayer, ICoreLayer, Scene } from '@antv/l7-composite-layers';

export type LayerManagerOptions = {
Expand All @@ -10,7 +11,7 @@ let layerCounter = 0;
/**
* 图层管理器
*/
export class LayerManager {
export class LayerManager extends EventEmitter {
/**
* 地图容器
*/
Expand All @@ -25,6 +26,7 @@ export class LayerManager {
private layerMap = new Map<string, Layer>();

constructor(options: LayerManagerOptions) {
super();
this.scene = options.scene;
this.options = options;
}
Expand All @@ -45,6 +47,7 @@ export class LayerManager {
this.layerMap.set(layerId, layer);

layer.addTo(this.scene);
this.emit('add', layer);
}

/**
Expand All @@ -68,6 +71,7 @@ export class LayerManager {
this.layerMap.delete(layerId);

findLayer.remove();
this.emit('remove', layerId);

return true;
}
Expand Down