拆分事件模块,之后使用npm install kunpoocc-event

This commit is contained in:
gongxh 2025-06-07 15:43:46 +08:00
parent c25c1554f3
commit 6953f738b0
9 changed files with 9 additions and 347 deletions

View File

@ -37,4 +37,8 @@
* 仓库地址: https://github.com/Gongxh0901/kunpo-quadtree
- 拆分行为树模块,使用 `npm install kunpocc-behaviortree` 安装
* 仓库地址: https://github.com/Gongxh0901/kunpocc-behaviortree
* 仓库地址: https://github.com/Gongxh0901/kunpocc-behaviortree
## 1.1.4 事件模块拆分
- 拆分事件模块,使用 `npm install kunpocc-event` 安装
* 仓库地址: https://github.com/Gongxh0901/kunpocc-event

View File

@ -1,6 +1,6 @@
{
"name": "kunpocc",
"version": "1.1.3",
"version": "1.1.4",
"description": "基于creator3.0+的kunpocc库",
"main": "./dist/kunpocc.cjs",
"module": "./dist/kunpocc.mjs",
@ -43,7 +43,8 @@
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"fairygui-cc": "^1.2.2"
"fairygui-cc": "^1.2.2",
"kunpocc-event": "^0.0.2"
},
"devDependencies": {
"@cocos/creator-types": "^3.8.0",

View File

@ -5,7 +5,6 @@
*/
import { _decorator, Component, director, game, JsonAsset, macro, sys } from "cc";
import { GlobalEvent } from "../global/GlobalEvent";
import { GlobalTimer } from "../global/GlobalTimer";
import { enableDebugMode, FrameConfig } from "../global/header";
import { InnerTimer } from "../global/InnerTimer";
@ -47,7 +46,6 @@ export abstract class CocosEntry extends Component {
this.node.setSiblingIndex(this.node.children.length - 1);
PropsHelper.setConfig(this.uiConfig?.json);
this.initPlatform();
this.initEvent();
this.initTime();
this.initAdapter();
this.initModule();
@ -108,14 +106,6 @@ export abstract class CocosEntry extends Component {
debug(`platform: ${PlatformType[Platform.platform]}`);
}
/**
*
* @internal
*/
private initEvent(): void {
GlobalEvent._initGlobalEvent();
}
/**
*
* @internal

View File

@ -1,21 +0,0 @@
/**
* @Author: Gongxh
* @Date: 2024-12-21
* @Description:
*/
/** @internal */
export class Event {
public id: number;
public name: string;
public target: any;
public once: boolean = false;
public callback: (...arg: any[]) => void;
public _destroy: boolean = false;
public _reset(): void {
this._destroy = false;
}
public _recycle(): void {
this._destroy = true;
}
}

View File

@ -1,44 +0,0 @@
/**
* @Author: Gongxh
* @Date: 2024-12-21
* @Description:
*/
import { Event } from "./Event";
/** @internal */
export class EventFactory {
private _id: number = 0;
private _stack: Event[] = [];
private _maxCapacity: number = 64;
private _msgClass: new () => Event;
get id(): number {
return this._id++;
}
constructor(capacity: number, objectClass: new () => Event) {
this._maxCapacity = capacity;
this._msgClass = objectClass;
}
public allocate<T extends Event>(): T {
if (this._stack.length == 0) {
const ret = new this._msgClass() as T;
ret.id = this.id;
return ret;
}
const ret = this._stack.pop() as T;
ret._reset();
return ret;
}
public recycle(ret: Event): boolean {
if (this._maxCapacity > 0 && this._stack.length < this._maxCapacity) {
ret._recycle();
this._stack.push(ret);
return true;
}
return false;
}
}

View File

@ -1,219 +0,0 @@
/**
* @Author: Gongxh
* @Date: 2024-12-21
* @Description:
*/
import { Event } from "./Event";
import { EventFactory } from "./EventFactory";
export class EventManager {
/** @internal */
private _idToEvent: Map<number, Event> = new Map<number, Event>();
/** @internal */
private _nameToIds: Map<string, Set<number>> = new Map<string, Set<number>>();
/** @internal */
private _targetToIds: Map<any, Set<number>> = new Map<any, Set<number>>();
/** @internal */
private _factroy: EventFactory = new EventFactory(64, Event);
/**
*
* @param name -
* @param callback -
* @param target -
* 便
*/
public addEvent(name: string, callback: (...args: any[]) => void, target?: any): void {
this._addEvent(name, callback, false, target);
}
/**
*
* @param name -
* @param callback -
* @param target -
*/
public addEventOnce(name: string, callback: (...args: any[]) => void, target?: any): void {
this._addEvent(name, callback, true, target);
}
/**
*
* @param name -
* @param target - ( )
* @param args -
*/
public send(name: string, target?: any, ...args: any[]): void {
let nameToIds = this._nameToIds;
if (!nameToIds.has(name)) {
return;
}
let ids = nameToIds.get(name);
let listenerMap = this._idToEvent;
let needRemoveIds: number[] = [];
let triggerList: Event[] = [];
for (const id of ids.values()) {
if (!listenerMap.has(id)) {
throw new Error(`消息ID:【${id}】不存在`);
}
let listener = listenerMap.get(id);
if (!listener._destroy && (!target || target == listener.target)) {
triggerList.push(listener);
if (listener.once) {
listener._destroy = true;
needRemoveIds.push(listener.id);
}
}
}
for (const listener of triggerList) {
listener.callback(...args);
}
if (needRemoveIds.length > 0) {
for (const id of needRemoveIds) {
this._remove(id);
}
}
}
/**
*
* @param name -
* @param callback -
* @param target -
* ID
* ID添加到待移除列表中
*/
public remove(name: string, callback: () => void, target: any): void {
let nameToIds = this._nameToIds;
if (!nameToIds.has(name)) {
return;
}
let ids = nameToIds.get(name);
if (ids.size == 0) {
return;
}
let needRemoveIds: number[] = [];
for (const id of ids.values()) {
let listener = this._idToEvent.get(id);
let needRemove = true;
if (callback && listener.callback != callback) {
needRemove = false;
}
if (target && listener.target != target) {
needRemove = false;
}
needRemove && needRemoveIds.push(id);
}
if (needRemoveIds.length > 0) {
for (const id of needRemoveIds) {
this._remove(id);
}
}
}
public removeByNameAndTarget(name: string, target: any): void {
this.remove(name, null, target);
}
public removeByNameAndCallback(name: string, callback: () => void): void {
this.remove(name, callback, null);
}
/**
*
* ID集合为空
* ID_idToEvent映射中删除监听器
* _nameToIds映射ID集合的映射保持最新
* @param target -
*/
public removeList(target: any): void {
let targetToIds = this._targetToIds;
if (!targetToIds.has(target)) {
return;
}
let ids = targetToIds.get(target);
if (ids.size == 0) {
return;
}
for (const id of ids.values()) {
let listener = this._idToEvent.get(id);
let name = listener.name;
this._idToEvent.delete(id);
this._factroy.recycle(listener);
let nameToIds = this._nameToIds;
if (nameToIds.has(name)) {
nameToIds.get(name).delete(id);
}
}
ids.clear();
}
public destroyAll(): void {
let listeners = this._idToEvent;
for (const listener of listeners.values()) {
this._factroy.recycle(listener);
}
this._idToEvent.clear();
this._nameToIds.clear();
this._targetToIds.clear();
}
/** @internal */
public _addEvent(name: string, callback: (...arg: any[]) => void, once: boolean, target: any): void {
let listener = this._factroy.allocate<Event>();
listener.name = name;
listener.target = target;
listener.once = once;
listener.callback = callback;
this._idToEvent.set(listener.id, listener);
let nameToIds = this._nameToIds;
let ids: Set<number>;
if (nameToIds.has(name)) {
ids = nameToIds.get(name);
} else {
ids = new Set<number>();
nameToIds.set(name, ids);
}
ids.add(listener.id);
if (target) {
let targetToIds = this._targetToIds;
if (!targetToIds.has(target)) {
let ids = new Set<number>();
ids.add(listener.id);
targetToIds.set(target, ids);
} else {
let ids = targetToIds.get(target);
ids.add(listener.id);
}
}
}
/** @internal */
private _remove(id: number): void {
if (!this._idToEvent.has(id)) {
return;
}
let ids = this._idToEvent.get(id);
let name = ids.name;
let target = ids.target;
this._idToEvent.delete(id);
this._factroy.recycle(ids);
let nameToIds = this._nameToIds;
if (nameToIds.has(name)) {
nameToIds.get(name).delete(id);
}
if (target) {
let targetToIds = this._targetToIds;
if (targetToIds.has(target)) {
targetToIds.get(target).delete(id);
}
}
}
}

View File

@ -5,9 +5,9 @@
*/
import { ResolutionPolicy, view } from "cc";
import { GlobalEvent } from "kunpocc-event";
import { debug } from "../tool/log";
import { WindowManager } from "../ui/WindowManager";
import { GlobalEvent } from "./GlobalEvent";
import { Screen } from "./Screen";
import { size } from "./header";

View File

@ -1,46 +0,0 @@
/**
* @Author: Gongxh
* @Date: 2024-12-22
* @Description:
*/
import { EventManager } from "../event/EventManager";
export class GlobalEvent {
/** @internal */
private static _globalEvent: EventManager = null;
public static add(eventName: string, callback: (...args: any[]) => void, target: any): void {
this._globalEvent.addEvent(eventName, callback, target);
}
public static addOnce(eventName: string, callback: (...args: any[]) => void, target: any): void {
this._globalEvent.addEventOnce(eventName, callback, target);
}
public static send(eventName: string, ...args: any[]): void {
this._globalEvent.send(eventName, null, ...args);
}
public static sendToTarget(eventName: string, target: any, ...args: any[]) {
this._globalEvent.send(eventName, target, ...args);
}
public static remove(eventName: string, callback: (...args: any[]) => void, target?: any): void {
this._globalEvent.remove(eventName, callback, target);
}
public static removeByNameAndTarget(eventName: string, target: any) {
this._globalEvent.removeByNameAndTarget(eventName, target);
}
public static removeByTarget(target: any): void {
this._globalEvent.removeList(target);
}
/** @internal */
public static _initGlobalEvent(): void {
if (!this._globalEvent) {
this._globalEvent = new EventManager();
}
}
}

View File

@ -1,5 +1,4 @@
/** 一些全局工具 */
export { GlobalEvent } from "./global/GlobalEvent";
export { GlobalTimer } from "./global/GlobalTimer";
export { enableDebugMode, FrameConfig, KUNPO_DEBUG } from "./global/header";
export { Platform, PlatformType } from "./global/Platform";
@ -12,8 +11,6 @@ export * from "./tool/log";
export { MathTool } from "./tool/Math";
export { md5 } from "./tool/MD5";
export { Time } from "./tool/Time";
/** 消息监听 */
export { EventManager } from "./event/EventManager";
/** Http */
export * from "./net/http/HttpManager";