immutableData was never assigned

This commit is contained in:
jonny 2021-06-23 20:19:38 +02:00
parent 3cf0a9ee84
commit d1178b1a01

View File

@ -1,53 +1,51 @@
import { Subject } from "rxjs"; import { Subject } from "rxjs";
import { EnterLeaveEvent, isEnterLeaveEvent } from '../Events/EnterLeaveEvent';
import {IframeApiContribution, sendToWorkadventure} from './IframeApiContribution'; import { isDataLayerEvent } from "../Events/DataLayerEvent";
import { EnterLeaveEvent, isEnterLeaveEvent } from "../Events/EnterLeaveEvent";
import { isGameStateEvent } from "../Events/GameStateEvent";
import { IframeApiContribution, sendToWorkadventure } from "./IframeApiContribution";
import { apiCallback } from "./registeredCallbacks"; import { apiCallback } from "./registeredCallbacks";
import type {LayerEvent} from "../Events/LayerEvent";
import type {SetPropertyEvent} from "../Events/setPropertyEvent"; import type { ITiledMap } from "../../Phaser/Map/ITiledMap";
import type {GameStateEvent} from "../Events/GameStateEvent"; import type { DataLayerEvent } from "../Events/DataLayerEvent";
import type {ITiledMap} from "../../Phaser/Map/ITiledMap"; import type { GameStateEvent } from "../Events/GameStateEvent";
import type {DataLayerEvent} from "../Events/DataLayerEvent";
import {isGameStateEvent} from "../Events/GameStateEvent";
import {isDataLayerEvent} from "../Events/DataLayerEvent";
const enterStreams: Map<string, Subject<EnterLeaveEvent>> = new Map<string, Subject<EnterLeaveEvent>>(); const enterStreams: Map<string, Subject<EnterLeaveEvent>> = new Map<string, Subject<EnterLeaveEvent>>();
const leaveStreams: Map<string, Subject<EnterLeaveEvent>> = new Map<string, Subject<EnterLeaveEvent>>(); const leaveStreams: Map<string, Subject<EnterLeaveEvent>> = new Map<string, Subject<EnterLeaveEvent>>();
const dataLayerResolver = new Subject<DataLayerEvent>(); const dataLayerResolver = new Subject<DataLayerEvent>();
const stateResolvers = new Subject<GameStateEvent>(); const stateResolvers = new Subject<GameStateEvent>();
let immutableData: GameStateEvent; let immutableDataPromise: Promise<GameStateEvent> | undefined = undefined;
interface Room { interface Room {
id: string, id: string;
mapUrl: string, mapUrl: string;
map: ITiledMap, map: ITiledMap;
startLayer: string | null startLayer: string | null;
} }
interface User { interface User {
id: string | undefined, id: string | undefined;
nickName: string | null, nickName: string | null;
tags: string[] tags: string[];
} }
function getGameState(): Promise<GameStateEvent> { function getGameState(): Promise<GameStateEvent> {
if (immutableData) { if (immutableDataPromise === undefined) {
return Promise.resolve(immutableData); immutableDataPromise = new Promise<GameStateEvent>((resolver, thrower) => {
}
else {
return new Promise<GameStateEvent>((resolver, thrower) => {
stateResolvers.subscribe(resolver); stateResolvers.subscribe(resolver);
sendToWorkadventure({type: "getState", data: null}); sendToWorkadventure({ type: "getState", data: null });
}) });
} }
return immutableDataPromise;
} }
function getDataLayer(): Promise<DataLayerEvent> { function getDataLayer(): Promise<DataLayerEvent> {
return new Promise<DataLayerEvent>((resolver, thrower) => { return new Promise<DataLayerEvent>((resolver, thrower) => {
dataLayerResolver.subscribe(resolver); dataLayerResolver.subscribe(resolver);
sendToWorkadventure({type: "getDataLayer", data: null}) sendToWorkadventure({ type: "getDataLayer", data: null });
}) });
} }
class WorkadventureRoomCommands extends IframeApiContribution<WorkadventureRoomCommands> { class WorkadventureRoomCommands extends IframeApiContribution<WorkadventureRoomCommands> {
@ -57,31 +55,30 @@ class WorkadventureRoomCommands extends IframeApiContribution<WorkadventureRoomC
enterStreams.get(payloadData.name)?.next(); enterStreams.get(payloadData.name)?.next();
}, },
type: "enterEvent", type: "enterEvent",
typeChecker: isEnterLeaveEvent typeChecker: isEnterLeaveEvent,
}), }),
apiCallback({ apiCallback({
type: "leaveEvent", type: "leaveEvent",
typeChecker: isEnterLeaveEvent, typeChecker: isEnterLeaveEvent,
callback: (payloadData) => { callback: (payloadData) => {
leaveStreams.get(payloadData.name)?.next(); leaveStreams.get(payloadData.name)?.next();
} },
}), }),
apiCallback({ apiCallback({
type: "gameState", type: "gameState",
typeChecker: isGameStateEvent, typeChecker: isGameStateEvent,
callback: (payloadData) => { callback: (payloadData) => {
stateResolvers.next(payloadData); stateResolvers.next(payloadData);
} },
}), }),
apiCallback({ apiCallback({
type: "dataLayer", type: "dataLayer",
typeChecker: isDataLayerEvent, typeChecker: isDataLayerEvent,
callback: (payloadData) => { callback: (payloadData) => {
dataLayerResolver.next(payloadData); dataLayerResolver.next(payloadData);
} },
}), }),
] ];
onEnterZone(name: string, callback: () => void): void { onEnterZone(name: string, callback: () => void): void {
let subject = enterStreams.get(name); let subject = enterStreams.get(name);
@ -90,7 +87,6 @@ class WorkadventureRoomCommands extends IframeApiContribution<WorkadventureRoomC
enterStreams.set(name, subject); enterStreams.set(name, subject);
} }
subject.subscribe(callback); subject.subscribe(callback);
} }
onLeaveZone(name: string, callback: () => void): void { onLeaveZone(name: string, callback: () => void): void {
let subject = leaveStreams.get(name); let subject = leaveStreams.get(name);
@ -101,35 +97,38 @@ class WorkadventureRoomCommands extends IframeApiContribution<WorkadventureRoomC
subject.subscribe(callback); subject.subscribe(callback);
} }
showLayer(layerName: string): void { showLayer(layerName: string): void {
sendToWorkadventure({type: 'showLayer', data: {'name': layerName}}); sendToWorkadventure({ type: "showLayer", data: { name: layerName } });
} }
hideLayer(layerName: string): void { hideLayer(layerName: string): void {
sendToWorkadventure({type: 'hideLayer', data: {'name': layerName}}); sendToWorkadventure({ type: "hideLayer", data: { name: layerName } });
} }
setProperty(layerName: string, propertyName: string, propertyValue: string | number | boolean | undefined): void { setProperty(layerName: string, propertyName: string, propertyValue: string | number | boolean | undefined): void {
sendToWorkadventure({ sendToWorkadventure({
type: 'setProperty', type: "setProperty",
data: { data: {
'layerName': layerName, layerName: layerName,
'propertyName': propertyName, propertyName: propertyName,
'propertyValue': propertyValue, propertyValue: propertyValue,
} },
}) });
} }
getCurrentRoom(): Promise<Room> { getCurrentRoom(): Promise<Room> {
return getGameState().then((gameState) => { return getGameState().then((gameState) => {
return getDataLayer().then((mapJson) => { return getDataLayer().then((mapJson) => {
return {id: gameState.roomId, map: mapJson.data as ITiledMap, mapUrl: gameState.mapUrl, startLayer: gameState.startLayerName}; return {
}) id: gameState.roomId,
}) map: mapJson.data as ITiledMap,
mapUrl: gameState.mapUrl,
startLayer: gameState.startLayerName,
};
});
});
} }
getCurrentUser(): Promise<User> { getCurrentUser(): Promise<User> {
return getGameState().then((gameState) => { return getGameState().then((gameState) => {
return {id: gameState.uuid, nickName: gameState.nickname, tags: gameState.tags}; return { id: gameState.uuid, nickName: gameState.nickname, tags: gameState.tags };
}) });
} }
} }
export default new WorkadventureRoomCommands(); export default new WorkadventureRoomCommands();