ly-utils-lib 2.6.0 → 2.8.2
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.
- package/README.md +130 -0
- package/dist/event.cjs +234 -0
- package/dist/event.cjs.map +1 -0
- package/dist/event.d.cts +2 -0
- package/dist/event.d.ts +2 -0
- package/dist/event.js +220 -0
- package/dist/event.js.map +1 -0
- package/dist/index-g6UFPtxf.d.cts +260 -0
- package/dist/index-g6UFPtxf.d.ts +260 -0
- package/dist/index.cjs +228 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +227 -1
- package/dist/index.js.map +1 -1
- package/package.json +12 -1
package/dist/index.d.cts
CHANGED
|
@@ -9,6 +9,7 @@ export { i as storage } from './index-Ba1rjTzj.cjs';
|
|
|
9
9
|
export { i as utils } from './index-B80SEVzM.cjs';
|
|
10
10
|
export { i as crypto } from './index-Bg1ise7y.cjs';
|
|
11
11
|
export { i as websocket } from './index-Cq1GhjpY.cjs';
|
|
12
|
+
export { i as event } from './index-g6UFPtxf.cjs';
|
|
12
13
|
export { Dayjs, default as dayjs } from 'dayjs';
|
|
13
14
|
import 'es-toolkit';
|
|
14
15
|
import 'xlsx';
|
|
@@ -20,3 +21,4 @@ import 'ol/layer/Vector';
|
|
|
20
21
|
import 'ol/source/Vector';
|
|
21
22
|
import 'ol/style';
|
|
22
23
|
import 'crypto-js';
|
|
24
|
+
import 'mitt';
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export { i as storage } from './index-Ba1rjTzj.js';
|
|
|
9
9
|
export { i as utils } from './index-B80SEVzM.js';
|
|
10
10
|
export { i as crypto } from './index-Bg1ise7y.js';
|
|
11
11
|
export { i as websocket } from './index-Cq1GhjpY.js';
|
|
12
|
+
export { i as event } from './index-g6UFPtxf.js';
|
|
12
13
|
export { Dayjs, default as dayjs } from 'dayjs';
|
|
13
14
|
import 'es-toolkit';
|
|
14
15
|
import 'xlsx';
|
|
@@ -20,3 +21,4 @@ import 'ol/layer/Vector';
|
|
|
20
21
|
import 'ol/source/Vector';
|
|
21
22
|
import 'ol/style';
|
|
22
23
|
import 'crypto-js';
|
|
24
|
+
import 'mitt';
|
package/dist/index.js
CHANGED
|
@@ -35,6 +35,7 @@ import { Style, Icon, Circle, Stroke, Fill } from 'ol/style';
|
|
|
35
35
|
import { boundingExtent } from 'ol/extent';
|
|
36
36
|
import Cookies from 'js-cookie';
|
|
37
37
|
import * as CryptoJS from 'crypto-js';
|
|
38
|
+
import mitt from 'mitt';
|
|
38
39
|
|
|
39
40
|
var __defProp = Object.defineProperty;
|
|
40
41
|
var __export = (target, all) => {
|
|
@@ -3616,6 +3617,231 @@ var websocket_default = {
|
|
|
3616
3617
|
quickConnect,
|
|
3617
3618
|
WebSocketState
|
|
3618
3619
|
};
|
|
3620
|
+
|
|
3621
|
+
// src/modules/event/index.ts
|
|
3622
|
+
var event_exports = {};
|
|
3623
|
+
__export(event_exports, {
|
|
3624
|
+
EventBus: () => EventBus,
|
|
3625
|
+
batchSubscribe: () => batchSubscribe,
|
|
3626
|
+
createEventBus: () => createEventBus,
|
|
3627
|
+
createNamespacedEventBus: () => createNamespacedEventBus,
|
|
3628
|
+
default: () => event_default,
|
|
3629
|
+
getGlobalEventBus: () => getGlobalEventBus,
|
|
3630
|
+
waitForEvent: () => waitForEvent
|
|
3631
|
+
});
|
|
3632
|
+
var EventBus = class {
|
|
3633
|
+
constructor(options = {}) {
|
|
3634
|
+
this.listeners = /* @__PURE__ */ new Map();
|
|
3635
|
+
this.options = {
|
|
3636
|
+
debug: options.debug ?? false,
|
|
3637
|
+
prefix: options.prefix ?? ""
|
|
3638
|
+
};
|
|
3639
|
+
this.emitter = mitt();
|
|
3640
|
+
this.log("EventBus created");
|
|
3641
|
+
}
|
|
3642
|
+
on(event, handler) {
|
|
3643
|
+
const events = Array.isArray(event) ? event : [event];
|
|
3644
|
+
const unsubscribers = [];
|
|
3645
|
+
events.forEach((eventName) => {
|
|
3646
|
+
const fullName = this.getFullName(eventName);
|
|
3647
|
+
const wrappedHandler = (data) => {
|
|
3648
|
+
this.log(`Event triggered: ${fullName}`, data);
|
|
3649
|
+
return handler(data);
|
|
3650
|
+
};
|
|
3651
|
+
this.emitter.on(fullName, wrappedHandler);
|
|
3652
|
+
const meta = {
|
|
3653
|
+
event: fullName,
|
|
3654
|
+
handler: wrappedHandler,
|
|
3655
|
+
once: false,
|
|
3656
|
+
timestamp: Date.now()
|
|
3657
|
+
};
|
|
3658
|
+
this.addListener(fullName, meta);
|
|
3659
|
+
unsubscribers.push(() => {
|
|
3660
|
+
this.emitter.off(fullName, wrappedHandler);
|
|
3661
|
+
this.removeListener(fullName, wrappedHandler);
|
|
3662
|
+
this.log(`Unsubscribed from: ${fullName}`);
|
|
3663
|
+
});
|
|
3664
|
+
});
|
|
3665
|
+
return () => {
|
|
3666
|
+
unsubscribers.forEach((unsub) => unsub());
|
|
3667
|
+
};
|
|
3668
|
+
}
|
|
3669
|
+
once(event, handler) {
|
|
3670
|
+
const events = Array.isArray(event) ? event : [event];
|
|
3671
|
+
const unsubscribers = [];
|
|
3672
|
+
events.forEach((eventName) => {
|
|
3673
|
+
const fullName = this.getFullName(eventName);
|
|
3674
|
+
const wrappedHandler = (data) => {
|
|
3675
|
+
this.log(`One-time event triggered: ${fullName}`, data);
|
|
3676
|
+
this.emitter.off(fullName, wrappedHandler);
|
|
3677
|
+
this.removeListener(fullName, wrappedHandler);
|
|
3678
|
+
return handler(data);
|
|
3679
|
+
};
|
|
3680
|
+
this.emitter.on(fullName, wrappedHandler);
|
|
3681
|
+
const meta = {
|
|
3682
|
+
event: fullName,
|
|
3683
|
+
handler: wrappedHandler,
|
|
3684
|
+
once: true,
|
|
3685
|
+
timestamp: Date.now()
|
|
3686
|
+
};
|
|
3687
|
+
this.addListener(fullName, meta);
|
|
3688
|
+
unsubscribers.push(() => {
|
|
3689
|
+
this.emitter.off(fullName, wrappedHandler);
|
|
3690
|
+
this.removeListener(fullName, wrappedHandler);
|
|
3691
|
+
});
|
|
3692
|
+
});
|
|
3693
|
+
return () => {
|
|
3694
|
+
unsubscribers.forEach((unsub) => unsub());
|
|
3695
|
+
};
|
|
3696
|
+
}
|
|
3697
|
+
off(event, handler) {
|
|
3698
|
+
const events = Array.isArray(event) ? event : [event];
|
|
3699
|
+
events.forEach((eventName) => {
|
|
3700
|
+
const fullName = this.getFullName(eventName);
|
|
3701
|
+
const listeners = this.listeners.get(fullName) || [];
|
|
3702
|
+
if (handler) {
|
|
3703
|
+
const index = listeners.findIndex((meta) => meta.handler === handler);
|
|
3704
|
+
if (index !== -1) {
|
|
3705
|
+
this.emitter.off(fullName, listeners[index].handler);
|
|
3706
|
+
listeners.splice(index, 1);
|
|
3707
|
+
this.log(`Removed handler from: ${fullName}`);
|
|
3708
|
+
}
|
|
3709
|
+
} else {
|
|
3710
|
+
listeners.forEach((meta) => {
|
|
3711
|
+
this.emitter.off(fullName, meta.handler);
|
|
3712
|
+
});
|
|
3713
|
+
this.listeners.delete(fullName);
|
|
3714
|
+
this.log(`Cleared all listeners for: ${fullName}`);
|
|
3715
|
+
}
|
|
3716
|
+
});
|
|
3717
|
+
}
|
|
3718
|
+
emit(event, data) {
|
|
3719
|
+
const fullName = this.getFullName(event);
|
|
3720
|
+
this.log(`Emitting event: ${fullName}`, data);
|
|
3721
|
+
this.emitter.emit(fullName, data);
|
|
3722
|
+
}
|
|
3723
|
+
/**
|
|
3724
|
+
* 清空所有事件监听器
|
|
3725
|
+
*/
|
|
3726
|
+
clear() {
|
|
3727
|
+
this.emitter.all.clear();
|
|
3728
|
+
this.listeners.clear();
|
|
3729
|
+
this.log("All event listeners cleared");
|
|
3730
|
+
}
|
|
3731
|
+
/**
|
|
3732
|
+
* 获取所有事件
|
|
3733
|
+
*/
|
|
3734
|
+
get all() {
|
|
3735
|
+
return this.emitter.all;
|
|
3736
|
+
}
|
|
3737
|
+
/**
|
|
3738
|
+
* 获取事件监听器数量
|
|
3739
|
+
*/
|
|
3740
|
+
getListenerCount(event) {
|
|
3741
|
+
if (event) {
|
|
3742
|
+
const fullName = this.getFullName(event);
|
|
3743
|
+
return this.listeners.get(fullName)?.length || 0;
|
|
3744
|
+
}
|
|
3745
|
+
return Array.from(this.listeners.values()).reduce((total, list) => total + list.length, 0);
|
|
3746
|
+
}
|
|
3747
|
+
/**
|
|
3748
|
+
* 获取所有事件名称
|
|
3749
|
+
*/
|
|
3750
|
+
getEventNames() {
|
|
3751
|
+
return Array.from(this.listeners.keys());
|
|
3752
|
+
}
|
|
3753
|
+
/**
|
|
3754
|
+
* 获取事件监听器列表
|
|
3755
|
+
*/
|
|
3756
|
+
getListeners(event) {
|
|
3757
|
+
const fullName = this.getFullName(event);
|
|
3758
|
+
return this.listeners.get(fullName) || [];
|
|
3759
|
+
}
|
|
3760
|
+
/**
|
|
3761
|
+
* 添加监听器记录
|
|
3762
|
+
*/
|
|
3763
|
+
addListener(event, meta) {
|
|
3764
|
+
if (!this.listeners.has(event)) {
|
|
3765
|
+
this.listeners.set(event, []);
|
|
3766
|
+
}
|
|
3767
|
+
this.listeners.get(event).push(meta);
|
|
3768
|
+
}
|
|
3769
|
+
/**
|
|
3770
|
+
* 移除监听器记录
|
|
3771
|
+
*/
|
|
3772
|
+
removeListener(event, handler) {
|
|
3773
|
+
const listeners = this.listeners.get(event);
|
|
3774
|
+
if (listeners) {
|
|
3775
|
+
const index = listeners.findIndex((meta) => meta.handler === handler);
|
|
3776
|
+
if (index !== -1) {
|
|
3777
|
+
listeners.splice(index, 1);
|
|
3778
|
+
}
|
|
3779
|
+
if (listeners.length === 0) {
|
|
3780
|
+
this.listeners.delete(event);
|
|
3781
|
+
}
|
|
3782
|
+
}
|
|
3783
|
+
}
|
|
3784
|
+
/**
|
|
3785
|
+
* 获取完整的事件名称(带前缀)
|
|
3786
|
+
*/
|
|
3787
|
+
getFullName(event) {
|
|
3788
|
+
return this.options.prefix ? `${this.options.prefix}:${event}` : event;
|
|
3789
|
+
}
|
|
3790
|
+
/**
|
|
3791
|
+
* 日志输出
|
|
3792
|
+
*/
|
|
3793
|
+
log(...args) {
|
|
3794
|
+
if (this.options.debug) {
|
|
3795
|
+
console.log("[EventBus]", ...args);
|
|
3796
|
+
}
|
|
3797
|
+
}
|
|
3798
|
+
};
|
|
3799
|
+
function createEventBus(options) {
|
|
3800
|
+
return new EventBus(options);
|
|
3801
|
+
}
|
|
3802
|
+
var globalEventBus = null;
|
|
3803
|
+
function getGlobalEventBus(options) {
|
|
3804
|
+
if (!globalEventBus) {
|
|
3805
|
+
globalEventBus = createEventBus(options);
|
|
3806
|
+
}
|
|
3807
|
+
return globalEventBus;
|
|
3808
|
+
}
|
|
3809
|
+
function waitForEvent(bus, event, timeout = 5e3) {
|
|
3810
|
+
return new Promise((resolve, reject) => {
|
|
3811
|
+
const timer = setTimeout(() => {
|
|
3812
|
+
unsubscribe();
|
|
3813
|
+
reject(new Error(`Timeout waiting for event: ${event}`));
|
|
3814
|
+
}, timeout);
|
|
3815
|
+
const unsubscribe = bus.on(event, (data) => {
|
|
3816
|
+
clearTimeout(timer);
|
|
3817
|
+
unsubscribe();
|
|
3818
|
+
resolve(data);
|
|
3819
|
+
});
|
|
3820
|
+
});
|
|
3821
|
+
}
|
|
3822
|
+
function batchSubscribe(bus, events) {
|
|
3823
|
+
const unsubscribers = [];
|
|
3824
|
+
Object.entries(events).forEach(([event, handler]) => {
|
|
3825
|
+
unsubscribers.push(bus.on(event, handler));
|
|
3826
|
+
});
|
|
3827
|
+
return () => {
|
|
3828
|
+
unsubscribers.forEach((unsub) => unsub());
|
|
3829
|
+
};
|
|
3830
|
+
}
|
|
3831
|
+
function createNamespacedEventBus(namespace, options) {
|
|
3832
|
+
return createEventBus({
|
|
3833
|
+
...options,
|
|
3834
|
+
prefix: namespace
|
|
3835
|
+
});
|
|
3836
|
+
}
|
|
3837
|
+
var event_default = {
|
|
3838
|
+
EventBus,
|
|
3839
|
+
createEventBus,
|
|
3840
|
+
getGlobalEventBus,
|
|
3841
|
+
waitForEvent,
|
|
3842
|
+
batchSubscribe,
|
|
3843
|
+
createNamespacedEventBus
|
|
3844
|
+
};
|
|
3619
3845
|
/**
|
|
3620
3846
|
* Utils Toolkit
|
|
3621
3847
|
* 一个功能强大的 JavaScript/TypeScript 工具函数库
|
|
@@ -3624,6 +3850,6 @@ var websocket_default = {
|
|
|
3624
3850
|
* @license MIT
|
|
3625
3851
|
*/
|
|
3626
3852
|
|
|
3627
|
-
export { array_exports as array, crypto_exports as crypto, date_exports as date, excel_exports as excel, map_exports as map, object_exports as object, pdf_exports as pdf, storage_exports as storage, string_exports as string, utils_exports as utils, websocket_exports as websocket };
|
|
3853
|
+
export { array_exports as array, crypto_exports as crypto, date_exports as date, event_exports as event, excel_exports as excel, map_exports as map, object_exports as object, pdf_exports as pdf, storage_exports as storage, string_exports as string, utils_exports as utils, websocket_exports as websocket };
|
|
3628
3854
|
//# sourceMappingURL=index.js.map
|
|
3629
3855
|
//# sourceMappingURL=index.js.map
|