Simplifying promises

This commit is contained in:
David Négrier 2021-07-21 18:21:12 +02:00
parent 3cfb74be54
commit 1bb6d893e0
2 changed files with 27 additions and 42 deletions

View file

@ -436,48 +436,32 @@ export class GameRoom {
private getVariableManager(): Promise<VariablesManager> { private getVariableManager(): Promise<VariablesManager> {
if (!this.variableManagerPromise) { if (!this.variableManagerPromise) {
this.variableManagerPromise = new Promise<VariablesManager>((resolve, reject) => { this.variableManagerPromise = this.getMap()
this.getMap() .then((map) => {
.then((map) => { const variablesManager = new VariablesManager(this.roomUrl, map);
const variablesManager = new VariablesManager(this.roomUrl, map); return variablesManager.init();
variablesManager })
.init() .catch((e) => {
.then(() => { if (e instanceof LocalUrlError) {
resolve(variablesManager); // If we are trying to load a local URL, we are probably in test mode.
}) // In this case, let's bypass the server-side checks completely.
.catch((e) => {
reject(e);
});
})
.catch((e) => {
if (e instanceof LocalUrlError) {
// If we are trying to load a local URL, we are probably in test mode.
// In this case, let's bypass the server-side checks completely.
// Note: we run this message inside a setTimeout so that the room listeners can have time to connect. // Note: we run this message inside a setTimeout so that the room listeners can have time to connect.
setTimeout(() => { setTimeout(() => {
for (const roomListener of this.roomListeners) { for (const roomListener of this.roomListeners) {
emitErrorOnRoomSocket( emitErrorOnRoomSocket(
roomListener, roomListener,
"You are loading a local map. If you use the scripting API in this map, please be aware that server-side checks and variable persistence is disabled." "You are loading a local map. If you use the scripting API in this map, please be aware that server-side checks and variable persistence is disabled."
); );
} }
}, 1000); }, 1000);
const variablesManager = new VariablesManager(this.roomUrl, null); const variablesManager = new VariablesManager(this.roomUrl, null);
variablesManager return variablesManager.init();
.init() } else {
.then(() => { throw e;
resolve(variablesManager); }
}) });
.catch((e) => {
reject(e);
});
} else {
reject(e);
}
});
});
} }
return this.variableManagerPromise; return this.variableManagerPromise;
} }

View file

@ -45,14 +45,15 @@ export class VariablesManager {
/** /**
* Let's load data from the Redis backend. * Let's load data from the Redis backend.
*/ */
public async init(): Promise<void> { public async init(): Promise<VariablesManager> {
if (!this.shouldPersist()) { if (!this.shouldPersist()) {
return; return this;
} }
const variables = await variablesRepository.loadVariables(this.roomUrl); const variables = await variablesRepository.loadVariables(this.roomUrl);
for (const key in variables) { for (const key in variables) {
this._variables.set(key, variables[key]); this._variables.set(key, variables[key]);
} }
return this;
} }
/** /**