partey_workadventure/front/src/iframe_api.ts

71 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-05-28 02:28:11 +02:00
import { IframeResponseEvent, IframeResponseEventMap, isIframeResponseEventWrapper, TypedMessageEvent } from "./Api/Events/IframeEvent";
import type { IframeCallback } from './Api/iframe/IframeApiContribution';
2021-05-28 01:59:43 +02:00
import type { WorkAdventureApi } from './iframe_api.d';
2021-05-28 02:28:11 +02:00
export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: IframeCallback<K> } = {}
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")
])
2021-05-28 01:59:43 +02:00
export type WorkadventureImport = typeof importType
declare global {
interface Window {
WA: WorkAdventureApi
}
let WA: WorkAdventureApi
}
2021-05-28 01:44:38 +02:00
async function populateWa(): Promise<void> {
const wa: Partial<WorkAdventureApi> = {}
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<WorkAdventureApi> = {}
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)
}
}
2021-05-28 01:44:38 +02:00
window.WA = Object.assign({}, wa) as WorkAdventureApi
}
2021-05-28 01:44:38 +02:00
populateWa()
2021-05-28 02:28:11 +02:00
window.addEventListener('message', <T extends keyof IframeResponseEventMap>(message: TypedMessageEvent<IframeResponseEvent<T>>) => {
if (message.source !== window.parent) {
return; // Skip message in this event listener
}
const payload = message.data;
console.debug(payload);
if (isIframeResponseEventWrapper(payload)) {
const payloadData = payload.data;
2021-05-28 02:28:11 +02:00
const callback = registeredCallbacks[payload.type] as IframeCallback<T> | undefined
if (callback?.typeChecker(payloadData)) {
callback?.callback(payloadData)
}
}
// ...
});