Skip to content

Commit

Permalink
refactor: 优化 useLayerEvent 代码及注释
Browse files Browse the repository at this point in the history
  • Loading branch information
heiyexing committed Sep 27, 2023
1 parent 98dcbab commit a9dc571
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/components/Layers/hooks/use-layer-event/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ export const useLayerEvent = (
// LarkMap 事件名列表
const layerEventList = useMemo(() => Object.keys(layerEventMap), [layerEventMap]);

const operateLayerEvents = (on: boolean) => {
// 绑定或解绑所有事件的回调函数
const handleLayerEvents = (type: 'on' | 'off') => {
layerEventList.forEach((callbackName) => {
const eventName = layerEventMap[callbackName];
const callback = props[callbackName];

if (callbackName && callback) {
console.log(on ? 'on' : 'off', eventName, callback);
layer[on ? 'on' : 'off'](eventName, callback);
layer[type](eventName, callback);
}
});
};

const bindLayerEvents = () => operateLayerEvents(true);
const bindLayerEvents = () => handleLayerEvents('on');

const unbindLayerEvents = () => operateLayerEvents(false);
const unbindLayerEvents = () => handleLayerEvents('off');

const isFirstRef = useRef(true);

// 保证图层初始化后同步执行事件绑定,而不是在 useEffect 中异步绑定事件
if (isFirstRef.current) {
bindLayerEvents();
}
Expand Down

0 comments on commit a9dc571

Please sign in to comment.