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: 修复 Layer 初始化后异步事件监听改为同步监听 #189

Merged
merged 5 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/larkmap",
"version": "1.4.4",
"version": "1.4.5",
heiyexing marked this conversation as resolved.
Show resolved Hide resolved
"description": "A React toolkit for geospatial visualization based on L7",
"license": "MIT",
"main": "lib/index.js",
Expand Down
63 changes: 37 additions & 26 deletions src/components/Layers/hooks/use-layer-event/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useTrackedEffect, useUnmount } from 'ahooks';
import { useMemo } from 'react';
import type { Layer, LayerEventCallback, LayerEventProps } from '../../../../types';
import { useUnmount } from 'ahooks';
import { useEffect, useMemo, useRef } from 'react';
import type { Layer, LayerEventProps } from '../../../../types';
import { LayerEventMap } from './constant';

export const useLayerEvent = (
Expand All @@ -11,31 +11,42 @@ export const useLayerEvent = (
// LarkMap 事件名列表
const layerEventList = useMemo(() => Object.keys(layerEventMap), [layerEventMap]);

useTrackedEffect(
(changeIndexList: number[], previousDeps: LayerEventCallback[] = [], currentDeps: LayerEventCallback[] = []) => {
changeIndexList.forEach((index) => {
const eventName = layerEventMap[layerEventList[index]] as string;
const previousCallback = previousDeps[index];
const currentCallback = currentDeps[index];
// 分别注销旧的事件回调并绑定新的事件
if (previousCallback) {
layer.off(eventName, previousCallback);
}
if (currentCallback) {
layer.on(eventName, currentCallback);
}
});
},
layerEventList.map((eventName) => props[eventName]),
);
// 绑定或解绑所有事件的回调函数
const handleLayerEvents = (type: 'on' | 'off') => {
layerEventList.forEach((callbackName) => {
const eventName = layerEventMap[callbackName];
const callback = props[callbackName];

useUnmount(() => {
layerEventList.forEach((key) => {
const eventName = layerEventMap[key];
const callback = props[key];
if (eventName && callback) {
layer.off(eventName, callback);
if (callbackName && callback) {
layer[type](eventName, callback);
}
});
};

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

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

const isFirstRef = useRef(true);

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

useEffect(() => {
if (isFirstRef.current) {
isFirstRef.current = false;
} else {
bindLayerEvents();
}
return () => {
unbindLayerEvents();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, layerEventList.map((eventName) => props[eventName]));

useUnmount(() => {
unbindLayerEvents();
});
};
Loading