@pansy/react-mapbox-gl
Version:
🌍 基于 Mapbox GL 封装的 React 组件库
65 lines (63 loc) • 2.32 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/hooks/useEvents.ts
var useEvents_exports = {};
__export(useEvents_exports, {
useEvents: () => useEvents
});
module.exports = __toCommonJS(useEvents_exports);
var import_core = require("@rcuse/core");
var import_useTrackedEffect = require("./useTrackedEffect");
var useEvents = (ins, props, options) => {
const { eventMap = {}, eventList = [] } = options;
(0, import_useTrackedEffect.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])]
);
(0, import_core.useUnmount)(() => {
if (!ins) {
return;
}
eventList.forEach((key) => {
const eventName = eventMap[key];
const callback = props[key];
if (eventName && callback) {
ins.off(eventName, callback);
}
});
});
};