TeaSpeak-Client/modules/shared/proxy/Definitions.ts

35 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-08-23 15:26:27 -04:00
export type ProxiedEvents<EventObject> = {
[Q in keyof EventObject]: EventObject[Q] extends (...args: any) => void ? (...args: Parameters<EventObject[Q]>) => void : never
}
export type FunctionalInterface<ObjectType> = {
[P in keyof ObjectType]: ObjectType[P] extends (...args: any) => Promise<any> ? (...args: any) => Promise<any> :
P extends "events" ? ObjectType[P] extends ProxiedEvents<ObjectType[P]> ? ProxiedEvents<ObjectType[P]> : never : never
};
export type ProxiedClassProperties = { instanceId: string, ownerWindowId: number, events: any };
export type ProxyInterface<ObjectType> = FunctionalInterface<ObjectType>;
export type ProxyClass<ObjectType> = { new(props: ProxiedClassProperties): ProxyInterface<ObjectType> & ProxiedClass<ObjectType> };
export abstract class ProxiedClass<Interface extends { events?: ProxiedEvents<Interface["events"]> }> {
public readonly ownerWindowId: number;
public readonly instanceId: string;
public readonly events: ProxiedEvents<Interface["events"]>;
2020-12-02 12:08:49 -05:00
protected constructor(props: ProxiedClassProperties) {
2020-08-23 15:26:27 -04:00
this.ownerWindowId = props.ownerWindowId;
this.instanceId = props.instanceId;
this.events = props.events;
}
public destroy() {}
}
export function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}