@blocklet/pages-kit 0.5.17 → 0.5.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.useEventBus = exports.eventBus = void 0;
7
+ const mitt_1 = __importDefault(require("mitt"));
8
+ const react_1 = require("react");
9
+ exports.eventBus = (0, mitt_1.default)();
10
+ const useEventBus = (eventName, eventCallBack, deps = []) => {
11
+ // 使用 useCallback 包装 emit 函数避免重渲染
12
+ const emit = (0, react_1.useCallback)((name, data) => {
13
+ exports.eventBus.emit(name, data);
14
+ }, []);
15
+ (0, react_1.useEffect)(() => {
16
+ if (eventName && eventCallBack) {
17
+ // 包装回调以添加错误处理
18
+ const wrappedCallback = (data) => {
19
+ try {
20
+ eventCallBack(data);
21
+ }
22
+ catch (error) {
23
+ console.error(`Error in event handler for "${eventName}":`, error);
24
+ }
25
+ };
26
+ exports.eventBus.on(eventName, wrappedCallback);
27
+ return () => {
28
+ exports.eventBus.off(eventName, wrappedCallback);
29
+ };
30
+ }
31
+ return () => { };
32
+ }, [eventName, eventCallBack, ...deps]);
33
+ return {
34
+ emit,
35
+ // 提供更多控制功能
36
+ off: exports.eventBus.off,
37
+ on: exports.eventBus.on,
38
+ all: exports.eventBus.all,
39
+ };
40
+ };
41
+ exports.useEventBus = useEventBus;
42
+ exports.default = exports.useEventBus;