refactored to key based types

This commit is contained in:
jonny 2021-05-28 02:28:11 +02:00
parent dbb35d102f
commit 8a1376e966
2 changed files with 22 additions and 18 deletions

View file

@ -9,21 +9,28 @@ export function sendToWorkadventure(content: IframeEvent<keyof IframeEventMap>)
}
type GuardedType<Guard extends tg.TypeGuard<unknown>> = Guard extends tg.TypeGuard<infer T> ? T : never
export function apiCallback<T extends tg.TypeGuard<unknown>>(callbackData: IframeCallbackContribution<T>) {
registeredCallbacks[callbackData.type] = {
export function apiCallback<T extends keyof IframeResponseEventMap>(callbackData: IframeCallbackContribution<T>): IframeCallbackContribution<keyof IframeResponseEventMap> {
const iframeCallback = {
typeChecker: callbackData.typeChecker,
callback: callbackData.callback
}
return callbackData
} as IframeCallback<T>;
const newCallback = { [callbackData.type]: iframeCallback };
Object.assign(registeredCallbacks, newCallback)
return callbackData as unknown as IframeCallbackContribution<keyof IframeResponseEventMap>;
}
export interface IframeCallbackContribution<Guard extends tg.TypeGuard<unknown>, T = GuardedType<Guard>> {
export interface IframeCallback<Key extends keyof IframeResponseEventMap, T = IframeResponseEventMap[Key], Guard = tg.TypeGuard<T>> {
type: keyof IframeResponseEventMap,
typeChecker: Guard,
callback: (payloadData: T) => void
}
export interface IframeCallbackContribution<Key extends keyof IframeResponseEventMap> extends IframeCallback<Key> {
type: Key
}
export type PossibleSubobjects = "zone" | "chat" | "ui" | "nav" | "sound" | "cowebsite" | "player" | "bubble"
/**
* !! be aware that the implemented attributes (addMethodsAtRoot and subObjectIdentifier) must be readonly
@ -32,9 +39,7 @@ export type PossibleSubobjects = "zone" | "chat" | "ui" | "nav" | "sound" | "cow
*/
export abstract class IframeApiContribution<T extends {
// i think this is specific enough
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callbacks: Array<IframeCallbackContribution<tg.TypeGuard<any>>>,
callbacks: Array<IframeCallbackContribution<keyof IframeResponseEventMap>>,
readonly subObjectIdentifier: PossibleSubobjects,
readonly addMethodsAtRoot: boolean | undefined
}> {