Simplifying promises
This commit is contained in:
parent
6d4c2cfd39
commit
ae5617f3a0
@ -268,31 +268,29 @@ export class SocketManager {
|
|||||||
//check and create new room
|
//check and create new room
|
||||||
let roomPromise = this.roomsPromises.get(roomId);
|
let roomPromise = this.roomsPromises.get(roomId);
|
||||||
if (roomPromise === undefined) {
|
if (roomPromise === undefined) {
|
||||||
roomPromise = new Promise<GameRoom>((resolve, reject) => {
|
roomPromise = GameRoom.create(
|
||||||
GameRoom.create(
|
roomId,
|
||||||
roomId,
|
(user: User, group: Group) => this.joinWebRtcRoom(user, group),
|
||||||
(user: User, group: Group) => this.joinWebRtcRoom(user, group),
|
(user: User, group: Group) => this.disConnectedUser(user, group),
|
||||||
(user: User, group: Group) => this.disConnectedUser(user, group),
|
MINIMUM_DISTANCE,
|
||||||
MINIMUM_DISTANCE,
|
GROUP_RADIUS,
|
||||||
GROUP_RADIUS,
|
(thing: Movable, fromZone: Zone | null, listener: ZoneSocket) =>
|
||||||
(thing: Movable, fromZone: Zone | null, listener: ZoneSocket) =>
|
this.onZoneEnter(thing, fromZone, listener),
|
||||||
this.onZoneEnter(thing, fromZone, listener),
|
(thing: Movable, position: PositionInterface, listener: ZoneSocket) =>
|
||||||
(thing: Movable, position: PositionInterface, listener: ZoneSocket) =>
|
this.onClientMove(thing, position, listener),
|
||||||
this.onClientMove(thing, position, listener),
|
(thing: Movable, newZone: Zone | null, listener: ZoneSocket) =>
|
||||||
(thing: Movable, newZone: Zone | null, listener: ZoneSocket) =>
|
this.onClientLeave(thing, newZone, listener),
|
||||||
this.onClientLeave(thing, newZone, listener),
|
(emoteEventMessage: EmoteEventMessage, listener: ZoneSocket) =>
|
||||||
(emoteEventMessage: EmoteEventMessage, listener: ZoneSocket) =>
|
this.onEmote(emoteEventMessage, listener)
|
||||||
this.onEmote(emoteEventMessage, listener)
|
)
|
||||||
)
|
.then((gameRoom) => {
|
||||||
.then((gameRoom) => {
|
gaugeManager.incNbRoomGauge();
|
||||||
gaugeManager.incNbRoomGauge();
|
return gameRoom;
|
||||||
resolve(gameRoom);
|
})
|
||||||
})
|
.catch((e) => {
|
||||||
.catch((e) => {
|
this.roomsPromises.delete(roomId);
|
||||||
this.roomsPromises.delete(roomId);
|
throw e;
|
||||||
reject(e);
|
});
|
||||||
});
|
|
||||||
});
|
|
||||||
this.roomsPromises.set(roomId, roomPromise);
|
this.roomsPromises.set(roomId, roomPromise);
|
||||||
}
|
}
|
||||||
return roomPromise;
|
return roomPromise;
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
import { registeredCallbacks } from "./Api/iframe/registeredCallbacks";
|
import { registeredCallbacks } from "./Api/iframe/registeredCallbacks";
|
||||||
import {
|
import {
|
||||||
IframeResponseEvent,
|
IframeResponseEvent,
|
||||||
IframeResponseEventMap, isIframeAnswerEvent, isIframeErrorAnswerEvent,
|
IframeResponseEventMap,
|
||||||
|
isIframeAnswerEvent,
|
||||||
|
isIframeErrorAnswerEvent,
|
||||||
isIframeResponseEventWrapper,
|
isIframeResponseEventWrapper,
|
||||||
TypedMessageEvent,
|
TypedMessageEvent,
|
||||||
} from "./Api/Events/IframeEvent";
|
} from "./Api/Events/IframeEvent";
|
||||||
@ -11,28 +13,25 @@ import nav from "./Api/iframe/nav";
|
|||||||
import controls from "./Api/iframe/controls";
|
import controls from "./Api/iframe/controls";
|
||||||
import ui from "./Api/iframe/ui";
|
import ui from "./Api/iframe/ui";
|
||||||
import sound from "./Api/iframe/sound";
|
import sound from "./Api/iframe/sound";
|
||||||
import room, {setMapURL, setRoomId} from "./Api/iframe/room";
|
import room, { setMapURL, setRoomId } from "./Api/iframe/room";
|
||||||
import state, {initVariables} from "./Api/iframe/state";
|
import state, { initVariables } from "./Api/iframe/state";
|
||||||
import player, {setPlayerName, setTags, setUuid} from "./Api/iframe/player";
|
import player, { setPlayerName, setTags, setUuid } from "./Api/iframe/player";
|
||||||
import type { ButtonDescriptor } from "./Api/iframe/Ui/ButtonDescriptor";
|
import type { ButtonDescriptor } from "./Api/iframe/Ui/ButtonDescriptor";
|
||||||
import type { Popup } from "./Api/iframe/Ui/Popup";
|
import type { Popup } from "./Api/iframe/Ui/Popup";
|
||||||
import type { Sound } from "./Api/iframe/Sound/Sound";
|
import type { Sound } from "./Api/iframe/Sound/Sound";
|
||||||
import {answerPromises, queryWorkadventure, sendToWorkadventure} from "./Api/iframe/IframeApiContribution";
|
import { answerPromises, queryWorkadventure, sendToWorkadventure } from "./Api/iframe/IframeApiContribution";
|
||||||
|
|
||||||
const initPromise = new Promise<void>((resolve) => {
|
|
||||||
// Notify WorkAdventure that we are ready to receive data
|
// Notify WorkAdventure that we are ready to receive data
|
||||||
queryWorkadventure({
|
const initPromise = queryWorkadventure({
|
||||||
type: 'getState',
|
type: "getState",
|
||||||
data: undefined
|
data: undefined,
|
||||||
}).then((state => {
|
}).then((state) => {
|
||||||
setPlayerName(state.nickname);
|
setPlayerName(state.nickname);
|
||||||
setRoomId(state.roomId);
|
setRoomId(state.roomId);
|
||||||
setMapURL(state.mapUrl);
|
setMapURL(state.mapUrl);
|
||||||
setTags(state.tags);
|
setTags(state.tags);
|
||||||
setUuid(state.uuid);
|
setUuid(state.uuid);
|
||||||
initVariables(state.variables as Map<string, unknown>);
|
initVariables(state.variables as Map<string, unknown>);
|
||||||
resolve();
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const wa = {
|
const wa = {
|
||||||
@ -186,38 +185,39 @@ declare global {
|
|||||||
window.WA = wa;
|
window.WA = wa;
|
||||||
|
|
||||||
window.addEventListener(
|
window.addEventListener(
|
||||||
"message", <T extends keyof IframeResponseEventMap>(message: TypedMessageEvent<IframeResponseEvent<T>>) => {
|
"message",
|
||||||
if (message.source !== window.parent) {
|
<T extends keyof IframeResponseEventMap>(message: TypedMessageEvent<IframeResponseEvent<T>>) => {
|
||||||
return; // Skip message in this event listener
|
if (message.source !== window.parent) {
|
||||||
}
|
return; // Skip message in this event listener
|
||||||
const payload = message.data;
|
|
||||||
|
|
||||||
//console.debug(payload);
|
|
||||||
|
|
||||||
if (isIframeErrorAnswerEvent(payload)) {
|
|
||||||
const queryId = payload.id;
|
|
||||||
const payloadError = payload.error;
|
|
||||||
|
|
||||||
const resolver = answerPromises.get(queryId);
|
|
||||||
if (resolver === undefined) {
|
|
||||||
throw new Error('In Iframe API, got an error answer for a question that we have no track of.');
|
|
||||||
}
|
}
|
||||||
resolver.reject(new Error(payloadError));
|
const payload = message.data;
|
||||||
|
|
||||||
answerPromises.delete(queryId);
|
//console.debug(payload);
|
||||||
} else if (isIframeAnswerEvent(payload)) {
|
|
||||||
const queryId = payload.id;
|
|
||||||
const payloadData = payload.data;
|
|
||||||
|
|
||||||
const resolver = answerPromises.get(queryId);
|
if (isIframeErrorAnswerEvent(payload)) {
|
||||||
if (resolver === undefined) {
|
const queryId = payload.id;
|
||||||
throw new Error('In Iframe API, got an answer for a question that we have no track of.');
|
const payloadError = payload.error;
|
||||||
}
|
|
||||||
resolver.resolve(payloadData);
|
|
||||||
|
|
||||||
answerPromises.delete(queryId);
|
const resolver = answerPromises.get(queryId);
|
||||||
} else if (isIframeResponseEventWrapper(payload)) {
|
if (resolver === undefined) {
|
||||||
const payloadData = payload.data;
|
throw new Error("In Iframe API, got an error answer for a question that we have no track of.");
|
||||||
|
}
|
||||||
|
resolver.reject(new Error(payloadError));
|
||||||
|
|
||||||
|
answerPromises.delete(queryId);
|
||||||
|
} else if (isIframeAnswerEvent(payload)) {
|
||||||
|
const queryId = payload.id;
|
||||||
|
const payloadData = payload.data;
|
||||||
|
|
||||||
|
const resolver = answerPromises.get(queryId);
|
||||||
|
if (resolver === undefined) {
|
||||||
|
throw new Error("In Iframe API, got an answer for a question that we have no track of.");
|
||||||
|
}
|
||||||
|
resolver.resolve(payloadData);
|
||||||
|
|
||||||
|
answerPromises.delete(queryId);
|
||||||
|
} else if (isIframeResponseEventWrapper(payload)) {
|
||||||
|
const payloadData = payload.data;
|
||||||
|
|
||||||
const callback = registeredCallbacks[payload.type] as IframeCallback<T> | undefined;
|
const callback = registeredCallbacks[payload.type] as IframeCallback<T> | undefined;
|
||||||
if (callback?.typeChecker(payloadData)) {
|
if (callback?.typeChecker(payloadData)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user