From 9129ceede1391b2d3e8ef73d298a889daf9adb8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Mon, 21 Jun 2021 11:48:39 +0200 Subject: [PATCH] Improving refactoring of API following @jonnytest1 feedback --- front/src/Api/Events/IframeEvent.ts | 2 +- front/src/Api/iframe/IframeApiContribution.ts | 14 -------- front/src/Api/iframe/chat.ts | 4 +-- front/src/Api/iframe/registeredCallbacks.ts | 16 ++++++++++ front/src/Api/iframe/room.ts | 4 +-- front/src/Api/iframe/ui.ts | 14 ++++---- front/src/iframe_api.ts | 32 +++++++++---------- front/src/registered_callbacks.ts | 4 --- 8 files changed, 45 insertions(+), 45 deletions(-) delete mode 100644 front/src/registered_callbacks.ts diff --git a/front/src/Api/Events/IframeEvent.ts b/front/src/Api/Events/IframeEvent.ts index 33de9675..4da8ea96 100644 --- a/front/src/Api/Events/IframeEvent.ts +++ b/front/src/Api/Events/IframeEvent.ts @@ -35,7 +35,7 @@ export type IframeEventMap = { removeBubble: null loadSound: LoadSoundEvent playSound: PlaySoundEvent - stopSound: null + stopSound: null, } export interface IframeEvent { type: T; diff --git a/front/src/Api/iframe/IframeApiContribution.ts b/front/src/Api/iframe/IframeApiContribution.ts index 9b5aba33..f3b25999 100644 --- a/front/src/Api/iframe/IframeApiContribution.ts +++ b/front/src/Api/iframe/IframeApiContribution.ts @@ -1,25 +1,11 @@ import type * as tg from "generic-type-guard"; import type { IframeEvent, IframeEventMap, IframeResponseEventMap } from '../Events/IframeEvent'; -import {registeredCallbacks} from "../../registered_callbacks"; - - export function sendToWorkadventure(content: IframeEvent) { window.parent.postMessage(content, "*") } type GuardedType> = Guard extends tg.TypeGuard ? T : never -export function apiCallback(callbackData: IframeCallbackContribution): IframeCallbackContribution { - const iframeCallback = { - typeChecker: callbackData.typeChecker, - callback: callbackData.callback - } as IframeCallback; - - const newCallback = { [callbackData.type]: iframeCallback }; - Object.assign(registeredCallbacks, newCallback) - return callbackData as unknown as IframeCallbackContribution; -} - export interface IframeCallback> { typeChecker: Guard, diff --git a/front/src/Api/iframe/chat.ts b/front/src/Api/iframe/chat.ts index c6fec2ad..0d467c2c 100644 --- a/front/src/Api/iframe/chat.ts +++ b/front/src/Api/iframe/chat.ts @@ -1,7 +1,7 @@ import type { ChatEvent } from '../Events/ChatEvent' import { isUserInputChatEvent, UserInputChatEvent } from '../Events/UserInputChatEvent' -import { apiCallback, IframeApiContribution, sendToWorkadventure } from './IframeApiContribution' - +import { IframeApiContribution, sendToWorkadventure } from './IframeApiContribution' +import { apiCallback } from "./registeredCallbacks"; class WorkadventureChatCommands extends IframeApiContribution { chatMessageCallback?: (event: string) => void diff --git a/front/src/Api/iframe/registeredCallbacks.ts b/front/src/Api/iframe/registeredCallbacks.ts index e69de29b..5d6f784d 100644 --- a/front/src/Api/iframe/registeredCallbacks.ts +++ b/front/src/Api/iframe/registeredCallbacks.ts @@ -0,0 +1,16 @@ +import type {IframeResponseEventMap} from "../../Api/Events/IframeEvent"; +import type {IframeCallback} from "../../Api/iframe/IframeApiContribution"; +import type {IframeCallbackContribution} from "../../Api/iframe/IframeApiContribution"; + +export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: IframeCallback } = {} + +export function apiCallback(callbackData: IframeCallbackContribution): IframeCallbackContribution { + const iframeCallback = { + typeChecker: callbackData.typeChecker, + callback: callbackData.callback + } as IframeCallback; + + const newCallback = { [callbackData.type]: iframeCallback }; + Object.assign(registeredCallbacks, newCallback) + return callbackData as unknown as IframeCallbackContribution; +} diff --git a/front/src/Api/iframe/room.ts b/front/src/Api/iframe/room.ts index 90dd6f1d..ed412166 100644 --- a/front/src/Api/iframe/room.ts +++ b/front/src/Api/iframe/room.ts @@ -1,7 +1,7 @@ import { Subject } from "rxjs"; import { EnterLeaveEvent, isEnterLeaveEvent } from '../Events/EnterLeaveEvent'; -import { apiCallback as apiCallback, IframeApiContribution } from './IframeApiContribution'; - +import { IframeApiContribution } from './IframeApiContribution'; +import { apiCallback } from "./registeredCallbacks"; const enterStreams: Map> = new Map>(); const leaveStreams: Map> = new Map>(); diff --git a/front/src/Api/iframe/ui.ts b/front/src/Api/iframe/ui.ts index 763e704d..d786bb93 100644 --- a/front/src/Api/iframe/ui.ts +++ b/front/src/Api/iframe/ui.ts @@ -1,6 +1,8 @@ import { isButtonClickedEvent } from '../Events/ButtonClickedEvent'; import type { ClosePopupEvent } from '../Events/ClosePopupEvent'; -import { apiCallback, IframeApiContribution, sendToWorkadventure } from './IframeApiContribution'; +import { IframeApiContribution, sendToWorkadventure } from './IframeApiContribution'; +import { apiCallback } from "./registeredCallbacks"; + export class Popup { constructor(private id: number) { } @@ -9,12 +11,12 @@ export class Popup { * Closes the popup */ public close(): void { - window.parent.postMessage({ + sendToWorkadventure({ 'type': 'closePopup', 'data': { 'popupId': this.id, } as ClosePopupEvent - }, '*'); + }); } } @@ -103,12 +105,12 @@ class WorkAdventureUiCommands extends IframeApiContribution void): void { - console.log('Method WA.onChatMessage is deprecated. Please use WA.chat.onChatMessage instead'); + console.warn('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'); + console.warn('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'); + console.warn('Method WA.onLeaveZone is deprecated. Please use WA.room.onLeaveZone instead'); room.onLeaveZone(name, callback); }, }; diff --git a/front/src/registered_callbacks.ts b/front/src/registered_callbacks.ts deleted file mode 100644 index cf941a13..00000000 --- a/front/src/registered_callbacks.ts +++ /dev/null @@ -1,4 +0,0 @@ -import type {IframeResponseEventMap} from "./Api/Events/IframeEvent"; -import type {IframeCallback} from "./Api/iframe/IframeApiContribution"; - -export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: IframeCallback } = {}