diff --git a/front/src/Api/iframe/IframeApiContribution.ts b/front/src/Api/iframe/IframeApiContribution.ts index dc261762..77739f7e 100644 --- a/front/src/Api/iframe/IframeApiContribution.ts +++ b/front/src/Api/iframe/IframeApiContribution.ts @@ -9,21 +9,28 @@ export function sendToWorkadventure(content: IframeEvent) } type GuardedType> = Guard extends tg.TypeGuard ? T : never -export function apiCallback>(callbackData: IframeCallbackContribution) { - registeredCallbacks[callbackData.type] = { +export function apiCallback(callbackData: IframeCallbackContribution): IframeCallbackContribution { + const iframeCallback = { typeChecker: callbackData.typeChecker, callback: callbackData.callback - } - return callbackData + } as IframeCallback; + + const newCallback = { [callbackData.type]: iframeCallback }; + Object.assign(registeredCallbacks, newCallback) + return callbackData as unknown as IframeCallbackContribution; } -export interface IframeCallbackContribution, T = GuardedType> { +export interface IframeCallback> { - type: keyof IframeResponseEventMap, typeChecker: Guard, callback: (payloadData: T) => void } +export interface IframeCallbackContribution extends IframeCallback { + + 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>>, + callbacks: Array>, readonly subObjectIdentifier: PossibleSubobjects, readonly addMethodsAtRoot: boolean | undefined }> { diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index 136edb8e..60080303 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -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 } = {} const importType = Promise.all([ import("./Api/iframe/popup"), @@ -52,7 +51,7 @@ async function populateWa(): Promise { populateWa() -window.addEventListener('message', message => { +window.addEventListener('message', (message: TypedMessageEvent>) => { 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 | undefined + if (callback?.typeChecker(payloadData)) { + callback?.callback(payloadData) } }