From ed39043bbe06acf36c4e6996e70108d810ae2372 Mon Sep 17 00:00:00 2001 From: jonny Date: Fri, 28 May 2021 13:10:30 +0200 Subject: [PATCH] this should make one file only --- front/src/iframe_api.d.ts | 2 +- front/src/iframe_api.ts | 59 ++++++++++++++++++--------------------- 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/front/src/iframe_api.d.ts b/front/src/iframe_api.d.ts index 953c92d2..944fdd6b 100644 --- a/front/src/iframe_api.d.ts +++ b/front/src/iframe_api.d.ts @@ -1,7 +1,7 @@ import type { WorkadventureImport } from './iframe_api'; type PromiseReturnType

= P extends Promise ? T : P; -type WorkadventureCommandClasses = PromiseReturnType[number]["default"]; +type WorkadventureCommandClasses = PromiseReturnType[number]; type KeysOfUnion = T extends T ? keyof T : never; type ObjectWithKeyOfUnion = O extends O ? (Key extends keyof O ? O[Key] : never) : never; type ApiKeys = KeysOfUnion; diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index 60080303..f4888ec4 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -1,22 +1,23 @@ import { IframeResponseEvent, IframeResponseEventMap, isIframeResponseEventWrapper, TypedMessageEvent } from "./Api/Events/IframeEvent"; +import Bubble from "./Api/iframe/Bubble"; +import chatmessage from "./Api/iframe/chatmessage"; +import CoWebsite from "./Api/iframe/CoWebsite"; import type { IframeCallback } from './Api/iframe/IframeApiContribution'; +import Navigation from "./Api/iframe/Navigation"; +import Player from "./Api/iframe/Player"; +import popupApi from "./Api/iframe/popup"; +import Sound from "./Api/iframe/Sound"; +import zoneRvents from "./Api/iframe/zone-events"; import type { WorkAdventureApi } from './iframe_api.d'; - export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: IframeCallback } = {} -const importType = Promise.all([ - import("./Api/iframe/popup"), - import("./Api/iframe/chatmessage"), - import("./Api/iframe/Sound"), - import("./Api/iframe/zone-events"), - import("./Api/iframe/Navigation"), - import("./Api/iframe/CoWebsite"), - import("./Api/iframe/Player"), - import("./Api/iframe/Bubble") -]) +const apis = [ + popupApi, Navigation, Player, Bubble, + chatmessage, Sound, zoneRvents, CoWebsite +] -export type WorkadventureImport = typeof importType +export type WorkadventureImport = typeof apis declare global { @@ -26,30 +27,24 @@ declare global { let WA: WorkAdventureApi } -async function populateWa(): Promise { - const wa: Partial = {} - for (const apiImport of await importType) { - const classInstance = apiImport.default - const commandPrototype = Object.getPrototypeOf(classInstance); - const commandClassPropertyNames = Object.getOwnPropertyNames(commandPrototype).filter(name => name !== "constructor"); - const importObject: Partial = {} - for (const prop of commandClassPropertyNames) { - const apiImportKey = prop as keyof typeof classInstance; - if (typeof classInstance[apiImportKey] === "function") { - importObject[apiImportKey as keyof WorkAdventureApi] = commandPrototype[apiImportKey] as never - } - } - wa[classInstance.subObjectIdentifier] = importObject as never - if (classInstance.addMethodsAtRoot) { - Object.assign(wa, importObject) +const wa: Partial = {} +for (const apiImport of apis) { + const commandPrototype = Object.getPrototypeOf(apiImport); + const commandClassPropertyNames = Object.getOwnPropertyNames(commandPrototype).filter(name => name !== "constructor"); + const importObject: Partial = {} + for (const prop of commandClassPropertyNames) { + const apiImportKey = prop as keyof typeof apiImport; + if (typeof apiImport[apiImportKey] === "function") { + importObject[apiImportKey as keyof WorkAdventureApi] = commandPrototype[apiImportKey] as never } } - - window.WA = Object.assign({}, wa) as WorkAdventureApi + wa[apiImport.subObjectIdentifier] = importObject as never + if (apiImport.addMethodsAtRoot) { + Object.assign(wa, importObject) + } } - -populateWa() +window.WA = Object.assign({}, wa) as WorkAdventureApi window.addEventListener('message', (message: TypedMessageEvent>) => { if (message.source !== window.parent) {