refactored to key based types
This commit is contained in:
parent
dbb35d102f
commit
8a1376e966
@ -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
|
||||
}> {
|
||||
|
@ -1,10 +1,9 @@
|
||||
import { IframeResponseEventMap, isIframeResponseEventWrapper } from "./Api/Events/IframeEvent";
|
||||
import { IframeResponseEvent, IframeResponseEventMap, isIframeResponseEventWrapper, TypedMessageEvent } from "./Api/Events/IframeEvent";
|
||||
import type { IframeCallback } from './Api/iframe/IframeApiContribution';
|
||||
import type { WorkAdventureApi } from './iframe_api.d';
|
||||
|
||||
export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: {
|
||||
typeChecker: Function
|
||||
callback: Function
|
||||
} } = {}
|
||||
|
||||
export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: IframeCallback<K> } = {}
|
||||
|
||||
const importType = Promise.all([
|
||||
import("./Api/iframe/popup"),
|
||||
@ -52,7 +51,7 @@ async function populateWa(): Promise<void> {
|
||||
|
||||
populateWa()
|
||||
|
||||
window.addEventListener('message', message => {
|
||||
window.addEventListener('message', <T extends keyof IframeResponseEventMap>(message: TypedMessageEvent<IframeResponseEvent<T>>) => {
|
||||
if (message.source !== window.parent) {
|
||||
return; // Skip message in this event listener
|
||||
}
|
||||
@ -62,9 +61,9 @@ window.addEventListener('message', message => {
|
||||
if (isIframeResponseEventWrapper(payload)) {
|
||||
const payloadData = payload.data;
|
||||
|
||||
if (registeredCallbacks[payload.type] && registeredCallbacks[payload.type]?.typeChecker(payloadData)) {
|
||||
registeredCallbacks[payload.type]?.callback(payloadData)
|
||||
return
|
||||
const callback = registeredCallbacks[payload.type] as IframeCallback<T> | undefined
|
||||
if (callback?.typeChecker(payloadData)) {
|
||||
callback?.callback(payloadData)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user