修复emitter

This commit is contained in:
yhh
2020-12-07 12:16:36 +08:00
parent bddae046a0
commit cf492daa93
5 changed files with 63 additions and 19 deletions

View File

@@ -725,7 +725,8 @@ declare module es {
}
declare module es {
/**
* 请注意,这不是一个完整的、多迭代的物理系统!它可以用于简单的、街机风格的物理。这可以用于简单的,街机风格的物理学
* 请注意,这不是一个完整的、多迭代的物理系统!它可以用于简单的、街机风格的物理。
* 这可以用于简单的,街机风格的物理学
*/
class ArcadeRigidbody extends Component implements IUpdatable {
/** 这个刚体的质量。质量为0则是一个不可移动的物体 */
@@ -3076,6 +3077,16 @@ declare module es {
}
}
declare module es {
/**
* 用于包装事件的一个小类
*/
class FuncPack {
/** 函数 */
func: Function;
/** 上下文 */
context: any;
constructor(func: Function, context: any);
}
/**
* 用于事件管理
*/

View File

@@ -1,3 +1,4 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -1592,7 +1593,8 @@ var es;
var es;
(function (es) {
/**
* 请注意,这不是一个完整的、多迭代的物理系统!它可以用于简单的、街机风格的物理。这可以用于简单的,街机风格的物理学
* 请注意,这不是一个完整的、多迭代的物理系统!它可以用于简单的、街机风格的物理。
* 这可以用于简单的,街机风格的物理学
*/
var ArcadeRigidbody = /** @class */ (function (_super) {
__extends(ArcadeRigidbody, _super);
@@ -7838,6 +7840,17 @@ var es;
})(es || (es = {}));
var es;
(function (es) {
/**
* 用于包装事件的一个小类
*/
var FuncPack = /** @class */ (function () {
function FuncPack(func, context) {
this.func = func;
this.context = context;
}
return FuncPack;
}());
es.FuncPack = FuncPack;
/**
* 用于事件管理
*/
@@ -7852,15 +7865,14 @@ var es;
* @param context 监听上下文
*/
Emitter.prototype.addObserver = function (eventType, handler, context) {
handler.bind(context);
var list = this._messageTable.get(eventType);
if (!list) {
list = [];
this._messageTable.set(eventType, list);
}
if (new linq.List(list).contains(handler))
if (list.findIndex(function (funcPack) { return funcPack.func == handler; }) != -1)
console.warn("您试图添加相同的观察者两次");
list.push(handler);
list.push(new FuncPack(handler, context));
};
/**
* 移除监听项
@@ -7869,7 +7881,9 @@ var es;
*/
Emitter.prototype.removeObserver = function (eventType, handler) {
var messageData = this._messageTable.get(eventType);
new linq.List(messageData).remove(handler);
var index = messageData.findIndex(function (data) { return data.func == handler; });
if (index != -1)
new linq.List(messageData).removeAt(index);
};
/**
* 触发该事件
@@ -7880,7 +7894,7 @@ var es;
var list = this._messageTable.get(eventType);
if (list) {
for (var i = list.length - 1; i >= 0; i--)
list[i](data);
list[i].func.call(list[i].context, data);
}
};
return Emitter;

File diff suppressed because one or more lines are too long

View File

@@ -1,12 +1,27 @@
module es {
/**
* 用于包装事件的一个小类
*/
export class FuncPack {
/** 函数 */
public func: Function;
/** 上下文 */
public context: any;
constructor(func: Function, context: any) {
this.func = func;
this.context = context;
}
}
/**
* 用于事件管理
*/
export class Emitter<T> {
private _messageTable: Map<T, Function[]>;
private _messageTable: Map<T, FuncPack[]>;
constructor() {
this._messageTable = new Map<T, Function[]>();
this._messageTable = new Map<T, FuncPack[]>();
}
/**
@@ -16,17 +31,15 @@ module es {
* @param context 监听上下文
*/
public addObserver(eventType: T, handler: Function, context: any) {
handler.bind(context);
let list: Function[] = this._messageTable.get(eventType);
let list: FuncPack[] = this._messageTable.get(eventType);
if (!list) {
list = [];
this._messageTable.set(eventType, list);
}
if (new linq.List(list).contains(handler))
if (list.findIndex(funcPack => funcPack.func == handler) != -1)
console.warn("您试图添加相同的观察者两次");
list.push(handler);
list.push(new FuncPack(handler, context));
}
/**
@@ -36,7 +49,9 @@ module es {
*/
public removeObserver(eventType: T, handler: Function) {
let messageData = this._messageTable.get(eventType);
new linq.List(messageData).remove(handler);
let index = messageData.findIndex(data => data.func == handler);
if (index != -1)
new linq.List(messageData).removeAt(index);
}
/**
@@ -45,10 +60,10 @@ module es {
* @param data 事件数据
*/
public emit(eventType: T, data?: any) {
let list: Function[] = this._messageTable.get(eventType);
let list: FuncPack[] = this._messageTable.get(eventType);
if (list) {
for (let i = list.length - 1; i >= 0; i--)
list[i](data);
list[i].func.call(list[i].context, data);
}
}
}

View File

@@ -1,7 +1,11 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"experimentalDecorators": false,
"downlevelIteration": true,
"importHelpers": true,
"pretty": true,
"preserveConstEnums": true,
"alwaysStrict": true,
"module": "system",
"target": "es5",
"declaration": true,