尝试在 Unity Tiny 中保护我的自定义事件系统
Trying to typeguard my custom event system in UnityTiny
我正在 Unity Tiny 中创建一个事件系统,因为框架本身的事件系统非常有限。我让它工作了,但现在我想让它对我的同事更友好。所以我试图对其进行类型保护以防止用户使用不同的类型,但它似乎对我不起作用。有什么建议吗?
我已经看过这些:
- https://medium.com/ovrsea/checking-the-type-of-an-object-in-typescript-the-type-guards-24d98d9119b0
- https://dev.to/krumpet/generic-type-guard-in-typescript-258l
问题是unity tiny用的是es5。所以你无法通过 object.construtor.name 来获取类型的名称。因此使用 "instanceof" 是不可能的。
此外,(object as T) 不起作用,我也无法获得 (object as T).type。
export class Event<T> implements IEvent<T>{
private handlers: {(data?: T): void}[] = [];
public type : T;
constructor(value: T){
this.type = value;
}
public On(handler: { (data?: T): void }) : void {
this.handlers.push(handler);
}
public Off(handler: { (data?: T): void }) : void {
this.handlers = this.handlers.filter(h => h !== handler);
}
public Trigger(data?: T) {
this.handlers.slice(0).forEach(h => h(data));
}
public Expose() : IEvent<T> {
return this;
}
}
export class EventUtils {
public static events = new Object();
public static CreateEvent<T>(eventEntity: ut.Entity, nameOfEvent: string) : void{
this.events[nameOfEvent] = new game.Event<T>();
}
public static Subscribe<T>(nameOfEvent: string, handeler: {(data? : T): void}) : void{
//Loop through events object, look for nameOfEvent, use checkEventType() to check if it is the same type as given generic, then subscribe if true.
}
public static Trigger<T>(nameOfEvent: string, parameter?: T) : void{
//Loop through events object, look for nameOfEvent, use checkEventType() to check if it is the same type as given generic, then trigger if true.
}
private static checkEventType<T>(object: any) : object is Event<T>{
let temp : T;
let temp2 = {temp};
if (object.type instanceof temp2.constructor.name) {
return true;
}
return false;
}
}
}
private static checkEventType<T>(object: any) : object is Event<T>{
let temp : T;
if (object.type as typeof temp) {
return true;
//always returns true even if the type is different
}
return false;
}
}
和
let temp : T;
if ((object.type as typeof temp).type) {
return true;
//property 'type' does not exist in 'T'
}
return false;
我正在 Unity Tiny 中创建一个事件系统,因为框架本身的事件系统非常有限。我让它工作了,但现在我想让它对我的同事更友好。所以我试图对其进行类型保护以防止用户使用不同的类型,但它似乎对我不起作用。有什么建议吗?
我已经看过这些: - https://medium.com/ovrsea/checking-the-type-of-an-object-in-typescript-the-type-guards-24d98d9119b0 - https://dev.to/krumpet/generic-type-guard-in-typescript-258l
问题是unity tiny用的是es5。所以你无法通过 object.construtor.name 来获取类型的名称。因此使用 "instanceof" 是不可能的。
此外,(object as T) 不起作用,我也无法获得 (object as T).type。
export class Event<T> implements IEvent<T>{
private handlers: {(data?: T): void}[] = [];
public type : T;
constructor(value: T){
this.type = value;
}
public On(handler: { (data?: T): void }) : void {
this.handlers.push(handler);
}
public Off(handler: { (data?: T): void }) : void {
this.handlers = this.handlers.filter(h => h !== handler);
}
public Trigger(data?: T) {
this.handlers.slice(0).forEach(h => h(data));
}
public Expose() : IEvent<T> {
return this;
}
}
export class EventUtils {
public static events = new Object();
public static CreateEvent<T>(eventEntity: ut.Entity, nameOfEvent: string) : void{
this.events[nameOfEvent] = new game.Event<T>();
}
public static Subscribe<T>(nameOfEvent: string, handeler: {(data? : T): void}) : void{
//Loop through events object, look for nameOfEvent, use checkEventType() to check if it is the same type as given generic, then subscribe if true.
}
public static Trigger<T>(nameOfEvent: string, parameter?: T) : void{
//Loop through events object, look for nameOfEvent, use checkEventType() to check if it is the same type as given generic, then trigger if true.
}
private static checkEventType<T>(object: any) : object is Event<T>{
let temp : T;
let temp2 = {temp};
if (object.type instanceof temp2.constructor.name) {
return true;
}
return false;
}
}
}
private static checkEventType<T>(object: any) : object is Event<T>{
let temp : T;
if (object.type as typeof temp) {
return true;
//always returns true even if the type is different
}
return false;
}
}
和
let temp : T;
if ((object.type as typeof temp).type) {
return true;
//property 'type' does not exist in 'T'
}
return false;