UNPKG

@pansy/react-mapbox-gl

Version:

🌍 基于 Mapbox GL 封装的 React 组件库

45 lines (44 loc) 1.37 kB
// src/hooks/useEvents.ts import { useUnmount } from "@rcuse/core"; import { useTrackedEffect } from "./useTrackedEffect"; var useEvents = (ins, props, options) => { const { eventMap = {}, eventList = [] } = options; useTrackedEffect( (changeIndexList = [], previousDeps, currentDeps) => { if (!ins) { return; } let eventIndexList = changeIndexList.filter((index) => !!index).map((index) => index - 1); if (changeIndexList.includes(0)) { eventIndexList = eventList.map((_, index) => index); } eventIndexList.forEach((index) => { const eventName = eventMap[eventList[index]]; const previousCallback = previousDeps == null ? void 0 : previousDeps[index + 1]; const currentCallback = currentDeps == null ? void 0 : currentDeps[index + 1]; if (previousCallback) { ins.off(eventName, previousCallback); } if (currentCallback) { ins.on(eventName, currentCallback); } }); }, [ins, ...eventList.map((eventName) => props[eventName])] ); useUnmount(() => { if (!ins) { return; } eventList.forEach((key) => { const eventName = eventMap[key]; const callback = props[key]; if (eventName && callback) { ins.off(eventName, callback); } }); }); }; export { useEvents };