Refactoring WorkAdventureAPI.
Simplifying a lot what was done (even if the new code is a bit less automated, it allows to list exactly the methods we want to see deprecated and to add a notification message)
This commit is contained in:
parent
97e6ad6700
commit
34dc5a0bc6
@ -1,20 +0,0 @@
|
||||
import { IframeApiContribution } from './IframeApiContribution';
|
||||
|
||||
class WorkadventureNavigationCommands extends IframeApiContribution<WorkadventureNavigationCommands> {
|
||||
|
||||
readonly subObjectIdentifier = "bubble"
|
||||
|
||||
readonly addMethodsAtRoot = true
|
||||
callbacks = []
|
||||
displayBubble(): void {
|
||||
window.parent.postMessage({ 'type': 'displayBubble' }, '*');
|
||||
}
|
||||
|
||||
removeBubble(): void {
|
||||
window.parent.postMessage({ 'type': 'removeBubble' }, '*');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export default new WorkadventureNavigationCommands();
|
@ -1,29 +0,0 @@
|
||||
import type { OpenCoWebSiteEvent } from '../Events/OpenCoWebSiteEvent';
|
||||
import { IframeApiContribution, sendToWorkadventure } from './IframeApiContribution';
|
||||
|
||||
class WorkadventureCoWebsiteCommands extends IframeApiContribution<WorkadventureCoWebsiteCommands> {
|
||||
|
||||
readonly subObjectIdentifier = "cowebsite"
|
||||
|
||||
readonly addMethodsAtRoot = true
|
||||
callbacks = []
|
||||
|
||||
openCoWebSite(url: string): void {
|
||||
sendToWorkadventure({
|
||||
"type": 'openCoWebSite',
|
||||
"data": {
|
||||
url
|
||||
} as OpenCoWebSiteEvent
|
||||
});
|
||||
}
|
||||
|
||||
closeCoWebSite(): void {
|
||||
sendToWorkadventure({
|
||||
"type": 'closeCoWebSite',
|
||||
data: null
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default new WorkadventureCoWebsiteCommands();
|
@ -1,6 +1,6 @@
|
||||
import type * as tg from "generic-type-guard";
|
||||
import { registeredCallbacks } from '../../iframe_api';
|
||||
import type { IframeEvent, IframeEventMap, IframeResponseEventMap } from '../Events/IframeEvent';
|
||||
import {registeredCallbacks} from "../../registered_callbacks";
|
||||
|
||||
|
||||
|
||||
@ -31,26 +31,15 @@ export interface IframeCallbackContribution<Key extends keyof IframeResponseEven
|
||||
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
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
export abstract class IframeApiContribution<T extends {
|
||||
callbacks: Array<IframeCallbackContribution<keyof IframeResponseEventMap>>,
|
||||
readonly subObjectIdentifier: PossibleSubobjects,
|
||||
readonly addMethodsAtRoot: boolean | undefined
|
||||
}> {
|
||||
|
||||
abstract callbacks: T["callbacks"]
|
||||
|
||||
/**
|
||||
* @deprecated this is only there for backwards compatibility on new apis this should be set to false or ignored
|
||||
*/
|
||||
addMethodsAtRoot = false
|
||||
|
||||
abstract readonly subObjectIdentifier: T["subObjectIdentifier"]
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,7 @@ import { isUserInputChatEvent, UserInputChatEvent } from '../Events/UserInputCha
|
||||
import { apiCallback, IframeApiContribution, sendToWorkadventure } from './IframeApiContribution'
|
||||
|
||||
|
||||
class WorkadvntureChatCommands extends IframeApiContribution<WorkadvntureChatCommands> {
|
||||
readonly subObjectIdentifier = 'chat'
|
||||
|
||||
readonly addMethodsAtRoot = true
|
||||
|
||||
class WorkadventureChatCommands extends IframeApiContribution<WorkadventureChatCommands> {
|
||||
chatMessageCallback?: (event: string) => void
|
||||
|
||||
callbacks = [apiCallback({
|
||||
@ -37,4 +33,4 @@ class WorkadvntureChatCommands extends IframeApiContribution<WorkadvntureChatCom
|
||||
}
|
||||
}
|
||||
|
||||
export default new WorkadvntureChatCommands()
|
||||
export default new WorkadventureChatCommands()
|
@ -1,10 +1,6 @@
|
||||
import { IframeApiContribution, sendToWorkadventure } from './IframeApiContribution';
|
||||
|
||||
class WorkadventureNavigationCommands extends IframeApiContribution<WorkadventureNavigationCommands> {
|
||||
|
||||
readonly subObjectIdentifier = "player"
|
||||
|
||||
readonly addMethodsAtRoot = true
|
||||
class WorkadventureControlsCommands extends IframeApiContribution<WorkadventureControlsCommands> {
|
||||
callbacks = []
|
||||
|
||||
disablePlayerControls(): void {
|
||||
@ -14,9 +10,7 @@ class WorkadventureNavigationCommands extends IframeApiContribution<Workadventur
|
||||
restorePlayerControls(): void {
|
||||
sendToWorkadventure({ 'type': 'restorePlayerControls', data: null });
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
export default new WorkadventureNavigationCommands();
|
||||
export default new WorkadventureControlsCommands();
|
@ -1,13 +1,10 @@
|
||||
import type { GoToPageEvent } from '../Events/GoToPageEvent';
|
||||
import type { OpenTabEvent } from '../Events/OpenTabEvent';
|
||||
import { IframeApiContribution, sendToWorkadventure } from './IframeApiContribution';
|
||||
import type {OpenCoWebSiteEvent} from "../Events/OpenCoWebSiteEvent";
|
||||
|
||||
|
||||
class WorkadventureNavigationCommands extends IframeApiContribution<WorkadventureNavigationCommands> {
|
||||
|
||||
readonly subObjectIdentifier = "nav"
|
||||
|
||||
readonly addMethodsAtRoot = true
|
||||
callbacks = []
|
||||
|
||||
|
||||
@ -37,6 +34,22 @@ class WorkadventureNavigationCommands extends IframeApiContribution<Workadventur
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
openCoWebSite(url: string): void {
|
||||
sendToWorkadventure({
|
||||
"type": 'openCoWebSite',
|
||||
"data": {
|
||||
url
|
||||
} as OpenCoWebSiteEvent
|
||||
});
|
||||
}
|
||||
|
||||
closeCoWebSite(): void {
|
||||
sendToWorkadventure({
|
||||
"type": 'closeCoWebSite',
|
||||
data: null
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
0
front/src/Api/iframe/registeredCallbacks.ts
Normal file
0
front/src/Api/iframe/registeredCallbacks.ts
Normal file
@ -6,11 +6,7 @@ import { apiCallback as apiCallback, IframeApiContribution } from './IframeApiCo
|
||||
const enterStreams: Map<string, Subject<EnterLeaveEvent>> = new Map<string, Subject<EnterLeaveEvent>>();
|
||||
const leaveStreams: Map<string, Subject<EnterLeaveEvent>> = new Map<string, Subject<EnterLeaveEvent>>();
|
||||
|
||||
class WorkadventureZoneCommands extends IframeApiContribution<WorkadventureZoneCommands> {
|
||||
|
||||
readonly subObjectIdentifier = "zone"
|
||||
|
||||
readonly addMethodsAtRoot = true
|
||||
class WorkadventureRoomCommands extends IframeApiContribution<WorkadventureRoomCommands> {
|
||||
callbacks = [
|
||||
apiCallback({
|
||||
callback: (payloadData: EnterLeaveEvent) => {
|
||||
@ -51,4 +47,4 @@ class WorkadventureZoneCommands extends IframeApiContribution<WorkadventureZoneC
|
||||
}
|
||||
|
||||
|
||||
export default new WorkadventureZoneCommands();
|
||||
export default new WorkadventureRoomCommands();
|
@ -1,7 +1,7 @@
|
||||
import { isButtonClickedEvent } from '../Events/ButtonClickedEvent';
|
||||
import type { ClosePopupEvent } from '../Events/ClosePopupEvent';
|
||||
import { apiCallback, IframeApiContribution, sendToWorkadventure } from './IframeApiContribution';
|
||||
class Popup {
|
||||
export class Popup {
|
||||
constructor(private id: number) {
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ class Popup {
|
||||
}
|
||||
|
||||
type ButtonClickedCallback = (popup: Popup) => void;
|
||||
interface ButtonDescriptor {
|
||||
export interface ButtonDescriptor {
|
||||
/**
|
||||
* The label of the button
|
||||
*/
|
||||
@ -46,7 +46,7 @@ interface ZonedPopupOptions {
|
||||
}
|
||||
|
||||
|
||||
class PopupApiContribution extends IframeApiContribution<PopupApiContribution> {
|
||||
class WorkAdventureUiCommands extends IframeApiContribution<WorkAdventureUiCommands> {
|
||||
|
||||
readonly subObjectIdentifier = "ui"
|
||||
|
||||
@ -101,6 +101,14 @@ class PopupApiContribution extends IframeApiContribution<PopupApiContribution> {
|
||||
popups.set(popupId, popup)
|
||||
return popup;
|
||||
}
|
||||
|
||||
displayBubble(): void {
|
||||
window.parent.postMessage({ 'type': 'displayBubble' }, '*');
|
||||
}
|
||||
|
||||
removeBubble(): void {
|
||||
window.parent.postMessage({ 'type': 'removeBubble' }, '*');
|
||||
}
|
||||
}
|
||||
|
||||
export default new PopupApiContribution()
|
||||
export default new WorkAdventureUiCommands()
|
25
front/src/iframe_api.d.ts
vendored
25
front/src/iframe_api.d.ts
vendored
@ -1,25 +0,0 @@
|
||||
import type { WorkadventureImport } from './iframe_api';
|
||||
|
||||
type PromiseReturnType<P> = P extends Promise<infer T> ? T : P;
|
||||
type WorkadventureCommandClasses = PromiseReturnType<WorkadventureImport>[number];
|
||||
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 ApiKeys = KeysOfUnion<WorkadventureCommandClasses>;
|
||||
type ObjectOfKey<Key extends ApiKeys, O = WorkadventureCommandClasses> = O extends O ? (Key extends keyof O ? O : never) : never;
|
||||
type ShouldAddAttribute<Key extends ApiKeys> = ObjectWithKeyOfUnion<Key>;
|
||||
type WorkadventureFunctions = {
|
||||
[K in ApiKeys]: ObjectWithKeyOfUnion<K> extends Function ? K : never;
|
||||
}[ApiKeys];
|
||||
type WorkadventureFunctionsFilteredByRoot = {
|
||||
[K in WorkadventureFunctions]: ObjectOfKey<K>["addMethodsAtRoot"] extends true ? K : never;
|
||||
}[WorkadventureFunctions];
|
||||
type JustMethodKeys<T> = ({
|
||||
[P in keyof T]: T[P] extends Function ? P : never;
|
||||
})[keyof T];
|
||||
type JustMethods<T> = Pick<T, JustMethodKeys<T>>;
|
||||
type SubObjectTypes = {
|
||||
[importCl in WorkadventureCommandClasses as importCl["subObjectIdentifier"]]: JustMethods<importCl>;
|
||||
};
|
||||
export type WorkAdventureApi = {
|
||||
[Key in WorkadventureFunctionsFilteredByRoot]: ShouldAddAttribute<Key>;
|
||||
} & SubObjectTypes;
|
@ -1,24 +1,148 @@
|
||||
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';
|
||||
import type { LoadPageEvent } from './Api/Events/LoadPageEvent';
|
||||
import {registeredCallbacks} from "./registered_callbacks";
|
||||
import {
|
||||
IframeResponseEvent,
|
||||
IframeResponseEventMap,
|
||||
isIframeResponseEventWrapper,
|
||||
TypedMessageEvent
|
||||
} from "./Api/Events/IframeEvent";
|
||||
import chat from "./Api/iframe/chat";
|
||||
import type {IframeCallback} from './Api/iframe/IframeApiContribution';
|
||||
import nav from "./Api/iframe/nav";
|
||||
import controls from "./Api/iframe/controls";
|
||||
import ui, {ButtonDescriptor, Popup} from "./Api/iframe/ui";
|
||||
import sound, {Sound} from "./Api/iframe/sound";
|
||||
import room from "./Api/iframe/room";
|
||||
|
||||
export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: IframeCallback<K> } = {}
|
||||
|
||||
const apis = [
|
||||
popupApi, Navigation, Player, Bubble,
|
||||
chatmessage, Sound, zoneRvents, CoWebsite
|
||||
]
|
||||
const wa = {
|
||||
ui,
|
||||
nav,
|
||||
controls,
|
||||
chat,
|
||||
sound,
|
||||
room,
|
||||
|
||||
export type WorkadventureImport = typeof apis
|
||||
// All methods below are deprecated and should not be used anymore.
|
||||
// They are kept here for backward compatibility.
|
||||
|
||||
/**
|
||||
* @deprecated Use WA.chat.sendChatMessage instead
|
||||
*/
|
||||
sendChatMessage(message: string, author: string): void {
|
||||
console.log('Method WA.sendChatMessage is deprecated. Please use WA.chat.sendChatMessage instead');
|
||||
chat.sendChatMessage(message, author);
|
||||
},
|
||||
/**
|
||||
* @deprecated Use WA.chat.disablePlayerControls instead
|
||||
*/
|
||||
disablePlayerControls(): void {
|
||||
console.log('Method WA.disablePlayerControls is deprecated. Please use WA.controls.disablePlayerControls instead');
|
||||
controls.disablePlayerControls();
|
||||
},
|
||||
|
||||
/**
|
||||
* @deprecated Use WA.controls.restorePlayerControls instead
|
||||
*/
|
||||
restorePlayerControls(): void {
|
||||
console.log('Method WA.restorePlayerControls is deprecated. Please use WA.controls.restorePlayerControls instead');
|
||||
controls.restorePlayerControls();
|
||||
},
|
||||
|
||||
/**
|
||||
* @deprecated Use WA.ui.displayBubble instead
|
||||
*/
|
||||
displayBubble(): void {
|
||||
console.log('Method WA.displayBubble is deprecated. Please use WA.ui.displayBubble instead');
|
||||
ui.displayBubble();
|
||||
},
|
||||
|
||||
/**
|
||||
* @deprecated Use WA.ui.removeBubble instead
|
||||
*/
|
||||
removeBubble(): void {
|
||||
console.log('Method WA.removeBubble is deprecated. Please use WA.ui.removeBubble instead');
|
||||
ui.removeBubble();
|
||||
},
|
||||
|
||||
/**
|
||||
* @deprecated Use WA.nav.openTab instead
|
||||
*/
|
||||
openTab(url: string): void {
|
||||
console.log('Method WA.openTab is deprecated. Please use WA.nav.openTab instead');
|
||||
nav.openTab(url);
|
||||
},
|
||||
|
||||
/**
|
||||
* @deprecated Use WA.sound.loadSound instead
|
||||
*/
|
||||
loadSound(url: string) : Sound {
|
||||
console.log('Method WA.loadSound is deprecated. Please use WA.sound.loadSound instead');
|
||||
return sound.loadSound(url);
|
||||
},
|
||||
|
||||
/**
|
||||
* @deprecated Use WA.nav.goToPage instead
|
||||
*/
|
||||
goToPage(url : string) : void {
|
||||
console.log('Method WA.goToPage is deprecated. Please use WA.nav.goToPage instead');
|
||||
nav.goToPage(url);
|
||||
},
|
||||
|
||||
/**
|
||||
* @deprecated Use WA.nav.goToRoom instead
|
||||
*/
|
||||
goToRoom(url: string): void {
|
||||
console.log('Method WA.goToRoom is deprecated. Please use WA.nav.goToRoom instead');
|
||||
nav.goToRoom(url);
|
||||
},
|
||||
|
||||
/**
|
||||
* @deprecated Use WA.nav.openCoWebSite instead
|
||||
*/
|
||||
openCoWebSite(url : string) : void{
|
||||
console.log('Method WA.openCoWebSite is deprecated. Please use WA.nav.openCoWebSite instead');
|
||||
nav.openCoWebSite(url);
|
||||
},
|
||||
|
||||
/**
|
||||
* @deprecated Use WA.nav.closeCoWebSite instead
|
||||
*/
|
||||
closeCoWebSite(): void {
|
||||
console.log('Method WA.closeCoWebSite is deprecated. Please use WA.nav.closeCoWebSite instead');
|
||||
nav.closeCoWebSite();
|
||||
},
|
||||
|
||||
/**
|
||||
* @deprecated Use WA.controls.restorePlayerControls instead
|
||||
*/
|
||||
openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): Popup {
|
||||
console.log('Method WA.openPopup is deprecated. Please use WA.ui.openPopup instead');
|
||||
return ui.openPopup(targetObject, message, buttons);
|
||||
},
|
||||
/**
|
||||
* @deprecated Use WA.chat.onChatMessage instead
|
||||
*/
|
||||
onChatMessage(callback: (message: string) => void): void {
|
||||
console.log('Method WA.onChatMessage is deprecated. Please use WA.chat.onChatMessage instead');
|
||||
chat.onChatMessage(callback);
|
||||
},
|
||||
/**
|
||||
* @deprecated Use WA.room.onEnterZone instead
|
||||
*/
|
||||
onEnterZone(name: string, callback: () => void): void {
|
||||
console.log('Method WA.onEnterZone is deprecated. Please use WA.room.onEnterZone instead');
|
||||
room.onEnterZone(name, callback);
|
||||
},
|
||||
/**
|
||||
* @deprecated Use WA.room.onLeaveZone instead
|
||||
*/
|
||||
onLeaveZone(name: string, callback: () => void): void {
|
||||
console.log('Method WA.onLeaveZone is deprecated. Please use WA.room.onLeaveZone instead');
|
||||
room.onLeaveZone(name, callback);
|
||||
},
|
||||
};
|
||||
|
||||
export type WorkAdventureApi = typeof wa;
|
||||
|
||||
declare global {
|
||||
|
||||
@ -28,24 +152,7 @@ declare global {
|
||||
let WA: WorkAdventureApi
|
||||
}
|
||||
|
||||
const wa: Partial<WorkAdventureApi> = {}
|
||||
for (const apiImport of apis) {
|
||||
const commandPrototype = Object.getPrototypeOf(apiImport);
|
||||
const commandClassPropertyNames = Object.getOwnPropertyNames(commandPrototype).filter(name => name !== "constructor");
|
||||
const importObject: Partial<WorkAdventureApi> = {}
|
||||
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
|
||||
}
|
||||
}
|
||||
wa[apiImport.subObjectIdentifier] = importObject as never
|
||||
if (apiImport.addMethodsAtRoot) {
|
||||
Object.assign(wa, importObject)
|
||||
}
|
||||
}
|
||||
|
||||
window.WA = Object.assign({}, wa) as WorkAdventureApi
|
||||
window.WA = wa;
|
||||
|
||||
window.addEventListener('message', <T extends keyof IframeResponseEventMap>(message: TypedMessageEvent<IframeResponseEvent<T>>) => {
|
||||
if (message.source !== window.parent) {
|
||||
|
4
front/src/registered_callbacks.ts
Normal file
4
front/src/registered_callbacks.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import type {IframeResponseEventMap} from "./Api/Events/IframeEvent";
|
||||
import type {IframeCallback} from "./Api/iframe/IframeApiContribution";
|
||||
|
||||
export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: IframeCallback<K> } = {}
|
Loading…
Reference in New Issue
Block a user