this should make one file only

This commit is contained in:
jonny 2021-05-28 13:10:30 +02:00
parent 8a1376e966
commit ed39043bbe
2 changed files with 28 additions and 33 deletions

View File

@ -1,7 +1,7 @@
import type { WorkadventureImport } from './iframe_api'; import type { WorkadventureImport } from './iframe_api';
type PromiseReturnType<P> = P extends Promise<infer T> ? T : P; type PromiseReturnType<P> = P extends Promise<infer T> ? T : P;
type WorkadventureCommandClasses = PromiseReturnType<WorkadventureImport>[number]["default"]; type WorkadventureCommandClasses = PromiseReturnType<WorkadventureImport>[number];
type KeysOfUnion<T> = T extends T ? keyof T : never; type KeysOfUnion<T> = T extends T ? keyof T : never;
type ObjectWithKeyOfUnion<Key, O = WorkadventureCommandClasses> = O extends O ? (Key extends keyof O ? O[Key] : never) : never; type ObjectWithKeyOfUnion<Key, O = WorkadventureCommandClasses> = O extends O ? (Key extends keyof O ? O[Key] : never) : never;
type ApiKeys = KeysOfUnion<WorkadventureCommandClasses>; type ApiKeys = KeysOfUnion<WorkadventureCommandClasses>;

View File

@ -1,22 +1,23 @@
import { IframeResponseEvent, IframeResponseEventMap, isIframeResponseEventWrapper, TypedMessageEvent } from "./Api/Events/IframeEvent"; 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 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'; import type { WorkAdventureApi } from './iframe_api.d';
export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: IframeCallback<K> } = {} export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: IframeCallback<K> } = {}
const importType = Promise.all([ const apis = [
import("./Api/iframe/popup"), popupApi, Navigation, Player, Bubble,
import("./Api/iframe/chatmessage"), chatmessage, Sound, zoneRvents, CoWebsite
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")
])
export type WorkadventureImport = typeof importType export type WorkadventureImport = typeof apis
declare global { declare global {
@ -26,30 +27,24 @@ declare global {
let WA: WorkAdventureApi let WA: WorkAdventureApi
} }
async function populateWa(): Promise<void> { const wa: Partial<WorkAdventureApi> = {}
const wa: Partial<WorkAdventureApi> = {} for (const apiImport of apis) {
for (const apiImport of await importType) { const commandPrototype = Object.getPrototypeOf(apiImport);
const classInstance = apiImport.default const commandClassPropertyNames = Object.getOwnPropertyNames(commandPrototype).filter(name => name !== "constructor");
const commandPrototype = Object.getPrototypeOf(classInstance); const importObject: Partial<WorkAdventureApi> = {}
const commandClassPropertyNames = Object.getOwnPropertyNames(commandPrototype).filter(name => name !== "constructor"); for (const prop of commandClassPropertyNames) {
const importObject: Partial<WorkAdventureApi> = {} const apiImportKey = prop as keyof typeof apiImport;
for (const prop of commandClassPropertyNames) { if (typeof apiImport[apiImportKey] === "function") {
const apiImportKey = prop as keyof typeof classInstance; importObject[apiImportKey as keyof WorkAdventureApi] = commandPrototype[apiImportKey] as never
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)
} }
} }
wa[apiImport.subObjectIdentifier] = importObject as never
window.WA = Object.assign({}, wa) as WorkAdventureApi if (apiImport.addMethodsAtRoot) {
Object.assign(wa, importObject)
}
} }
window.WA = Object.assign({}, wa) as WorkAdventureApi
populateWa()
window.addEventListener('message', <T extends keyof IframeResponseEventMap>(message: TypedMessageEvent<IframeResponseEvent<T>>) => { window.addEventListener('message', <T extends keyof IframeResponseEventMap>(message: TypedMessageEvent<IframeResponseEvent<T>>) => {
if (message.source !== window.parent) { if (message.source !== window.parent) {