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:
David Négrier 2021-06-18 17:22:56 +02:00
parent 97e6ad6700
commit 34dc5a0bc6
13 changed files with 186 additions and 153 deletions

View file

@ -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();

View file

@ -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();

View file

@ -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"]
}
}

View file

@ -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()

View file

@ -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();

View file

@ -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
});
}
}

View 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();

View file

@ -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()