Migrating some promises to Deferred objects to simplify the code.

This commit is contained in:
David Négrier
2022-03-15 11:32:04 +01:00
parent 153bffd521
commit b959ce7a6d
+12 -19
View File
@@ -99,6 +99,7 @@ import { SimpleCoWebsite } from "../../WebRtc/CoWebsite/SimpleCoWebsite";
import type { CoWebsite } from "../../WebRtc/CoWebsite/CoWesbite"; import type { CoWebsite } from "../../WebRtc/CoWebsite/CoWesbite";
import { BodyResourceDescriptionInterface } from "../Entity/PlayerTextures"; import { BodyResourceDescriptionInterface } from "../Entity/PlayerTextures";
import CancelablePromise from "cancelable-promise"; import CancelablePromise from "cancelable-promise";
import { Deferred } from "ts-deferred";
export interface GameSceneInitInterface { export interface GameSceneInitInterface {
initPosition: PointInterface | null; initPosition: PointInterface | null;
reconnecting: boolean; reconnecting: boolean;
@@ -164,13 +165,9 @@ export class GameScene extends DirtyScene {
private playersPositionInterpolator = new PlayersPositionInterpolator(); private playersPositionInterpolator = new PlayersPositionInterpolator();
public connection: RoomConnection | undefined; public connection: RoomConnection | undefined;
private simplePeer!: SimplePeer; private simplePeer!: SimplePeer;
private connectionAnswerPromise: Promise<RoomJoinedMessageInterface>; private connectionAnswerPromiseDeferred: Deferred<RoomJoinedMessageInterface>;
private connectionAnswerPromiseResolve!: (
value: RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface>
) => void;
// A promise that will resolve when the "create" method is called (signaling loading is ended) // A promise that will resolve when the "create" method is called (signaling loading is ended)
private createPromise: Promise<void>; private createPromiseDeferred: Deferred<void>;
private createPromiseResolve!: (value?: void | PromiseLike<void>) => void;
private iframeSubscriptionList!: Array<Subscription>; private iframeSubscriptionList!: Array<Subscription>;
private peerStoreUnsubscribe!: Unsubscriber; private peerStoreUnsubscribe!: Unsubscriber;
private emoteUnsubscribe!: Unsubscriber; private emoteUnsubscribe!: Unsubscriber;
@@ -232,12 +229,8 @@ export class GameScene extends DirtyScene {
this.MapUrlFile = MapUrlFile; this.MapUrlFile = MapUrlFile;
this.roomUrl = room.key; this.roomUrl = room.key;
this.createPromise = new Promise<void>((resolve, reject): void => { this.createPromiseDeferred = new Deferred<void>();
this.createPromiseResolve = resolve; this.connectionAnswerPromiseDeferred = new Deferred<RoomJoinedMessageInterface>();
});
this.connectionAnswerPromise = new Promise<RoomJoinedMessageInterface>((resolve, reject): void => {
this.connectionAnswerPromiseResolve = resolve;
});
this.loader = new Loader(this); this.loader = new Loader(this);
} }
@@ -408,11 +401,11 @@ export class GameScene extends DirtyScene {
this.load.on("complete", () => { this.load.on("complete", () => {
// FIXME: the factory might fail because the resources might not be loaded yet... // FIXME: the factory might fail because the resources might not be loaded yet...
// We would need to add a loader ended event in addition to the createPromise // We would need to add a loader ended event in addition to the createPromise
this.createPromise this.createPromiseDeferred.promise
.then(async () => { .then(async () => {
itemFactory.create(this); itemFactory.create(this);
const roomJoinedAnswer = await this.connectionAnswerPromise; const roomJoinedAnswer = await this.connectionAnswerPromiseDeferred.promise;
for (const object of objectsOfType) { for (const object of objectsOfType) {
// TODO: we should pass here a factory to create sprites (maybe?) // TODO: we should pass here a factory to create sprites (maybe?)
@@ -609,7 +602,7 @@ export class GameScene extends DirtyScene {
} }
} }
this.createPromiseResolve(); this.createPromiseDeferred.resolve();
// Now, let's load the script, if any // Now, let's load the script, if any
const scripts = this.getScriptUrls(this.mapFile); const scripts = this.getScriptUrls(this.mapFile);
const disableModuleMode = this.getProperty(this.mapFile, GameMapProperties.SCRIPT_DISABLE_MODULE_SUPPORT) as const disableModuleMode = this.getProperty(this.mapFile, GameMapProperties.SCRIPT_DISABLE_MODULE_SUPPORT) as
@@ -703,7 +696,7 @@ export class GameScene extends DirtyScene {
}); });
Promise.all([ Promise.all([
this.connectionAnswerPromise as Promise<unknown>, this.connectionAnswerPromiseDeferred.promise as Promise<unknown>,
...scriptPromises, ...scriptPromises,
this.CurrentPlayer.getTextureLoadedPromise() as Promise<unknown>, this.CurrentPlayer.getTextureLoadedPromise() as Promise<unknown>,
]) ])
@@ -872,7 +865,7 @@ export class GameScene extends DirtyScene {
); );
//this.initUsersPosition(roomJoinedMessage.users); //this.initUsersPosition(roomJoinedMessage.users);
this.connectionAnswerPromiseResolve(onConnect.room); this.connectionAnswerPromiseDeferred.resolve(onConnect.room);
// Analyze tags to find if we are admin. If yes, show console. // Analyze tags to find if we are admin. If yes, show console.
if (this.scene.isSleeping()) { if (this.scene.isSleeping()) {
@@ -1287,7 +1280,7 @@ ${escapedMessage}
iframeListener.registerAnswerer("getState", async () => { iframeListener.registerAnswerer("getState", async () => {
// The sharedVariablesManager is not instantiated before the connection is established. So we need to wait // The sharedVariablesManager is not instantiated before the connection is established. So we need to wait
// for the connection to send back the answer. // for the connection to send back the answer.
await this.connectionAnswerPromise; await this.connectionAnswerPromiseDeferred.promise;
return { return {
mapUrl: this.MapUrlFile, mapUrl: this.MapUrlFile,
startLayerName: this.startPositionCalculator.startLayerName, startLayerName: this.startPositionCalculator.startLayerName,
@@ -1310,7 +1303,7 @@ ${escapedMessage}
}) })
); );
iframeListener.registerAnswerer("loadTileset", (eventTileset) => { iframeListener.registerAnswerer("loadTileset", (eventTileset) => {
return this.connectionAnswerPromise.then(() => { return this.connectionAnswerPromiseDeferred.promise.then(() => {
const jsonTilesetDir = eventTileset.url.substr(0, eventTileset.url.lastIndexOf("/")); const jsonTilesetDir = eventTileset.url.substr(0, eventTileset.url.lastIndexOf("/"));
//Initialise the firstgid to 1 because if there is no tileset in the tilemap, the firstgid will be 1 //Initialise the firstgid to 1 because if there is no tileset in the tilemap, the firstgid will be 1
let newFirstgid = 1; let newFirstgid = 1;