Keep the isReady value for multiple calls

This commit is contained in:
Sosthene 2025-06-11 15:13:51 +02:00
parent 1e6065ec7c
commit d8105fa3cf

View File

@ -10,6 +10,8 @@ export default class MessageBus {
private readonly origin: string; private readonly origin: string;
private messageListener: ((event: MessageEvent) => void) | null = null; private messageListener: ((event: MessageEvent) => void) | null = null;
private errors: { [key: string]: string } = {}; private errors: { [key: string]: string } = {};
private readyPromise: Promise<void> | null = null;
private isReadyFlag = false;
private constructor(origin: string) { private constructor(origin: string) {
this.origin = origin; this.origin = origin;
@ -23,19 +25,28 @@ export default class MessageBus {
} }
public isReady(): Promise<void> { public isReady(): Promise<void> {
return new Promise<void>((resolve: () => void) => { if (this.isReadyFlag) {
return Promise.resolve();
}
if (this.readyPromise) {
return this.readyPromise;
}
this.readyPromise = new Promise<void>((resolve) => {
const correlationId = uuidv4(); const correlationId = uuidv4();
this.initMessageListener(correlationId); this.initMessageListener(correlationId);
const unsubscribe = EventBus.getInstance().on('IS_READY', (responseId: string) => { const unsubscribe = EventBus.getInstance().on('IS_READY', (responseId: string) => {
if (responseId !== correlationId) { if (responseId !== correlationId) return;
return;
}
unsubscribe(); unsubscribe();
this.destroyMessageListener(); this.destroyMessageListener();
this.isReadyFlag = true;
resolve(); resolve();
}); });
}); });
return this.readyPromise;
} }
public requestLink(): Promise<void> { public requestLink(): Promise<void> {
@ -68,6 +79,41 @@ export default class MessageBus {
}); });
} }
public getUserPairingId(): Promise<string> {
return new Promise<string>((resolve: (userPairingId: string) => void, reject: (error: string) => void) => {
this.checkToken().then(() => {
const userStore = UserStore.getInstance();
const accessToken = userStore.getAccessToken()!;
const correlationId = uuidv4();
this.initMessageListener(correlationId);
const unsubscribe = EventBus.getInstance().on('PAIRING_ID', (responseId: string, userPairingId: string) => {
if (responseId !== correlationId) {
return;
}
unsubscribe();
this.destroyMessageListener();
resolve(userPairingId);
});
const unsubscribeError = EventBus.getInstance().on('ERROR_PAIRING_ID', (responseId: string, error: string) => {
if (responseId !== correlationId) {
return;
}
unsubscribeError();
this.destroyMessageListener();
reject(error);
});
this.sendMessage({
type: 'GET_PAIRING_ID',
accessToken,
});
}).catch(console.error);
});
}
public validateToken(): Promise<boolean> { public validateToken(): Promise<boolean> {
return new Promise<boolean>((resolve: (isValid: boolean) => void, reject: (error: string) => void) => { return new Promise<boolean>((resolve: (isValid: boolean) => void, reject: (error: string) => void) => {
const userStore = UserStore.getInstance(); const userStore = UserStore.getInstance();
@ -148,10 +194,15 @@ export default class MessageBus {
public getProcesses(): Promise<any> { public getProcesses(): Promise<any> {
return new Promise<any>((resolve: (processes: any) => void, reject: (error: string) => void) => { return new Promise<any>((resolve: (processes: any) => void, reject: (error: string) => void) => {
this.checkToken().then(() => { this.checkToken().then(() => {
const userStore = UserStore.getInstance();
const accessToken = userStore.getAccessToken()!;
const correlationId = uuidv4(); const correlationId = uuidv4();
console.log(correlationId);
this.initMessageListener(correlationId); this.initMessageListener(correlationId);
const unsubscribe = EventBus.getInstance().on('PROCESSES_RETRIEVED', (responseId: string, processes: any) => { const unsubscribe = EventBus.getInstance().on('PROCESSES_RETRIEVED', (responseId: string, processes: any) => {
console.log(responseId);
if (responseId !== correlationId) { if (responseId !== correlationId) {
return; return;
} }
@ -170,7 +221,8 @@ export default class MessageBus {
}); });
this.sendMessage({ this.sendMessage({
type: 'GET_PROCESSES' type: 'GET_PROCESSES',
accessToken,
}); });
}).catch(console.error); }).catch(console.error);
}); });
@ -371,6 +423,17 @@ export default class MessageBus {
EventBus.getInstance().emit('TOKEN_RENEWED', correlationId, message.accessToken, message.refreshToken); EventBus.getInstance().emit('TOKEN_RENEWED', correlationId, message.accessToken, message.refreshToken);
break; break;
case 'GET_PAIRING_ID':
if (this.errors[correlationId]) {
const error = this.errors[correlationId];
delete this.errors[correlationId];
EventBus.getInstance().emit('ERROR_PAIRING_ID', correlationId, error);
return;
}
EventBus.getInstance().emit('MESSAGE_RECEIVED', message);
EventBus.getInstance().emit('PAIRING_ID', correlationId, message.userPairingId);
break;
case 'PROCESSES_RETRIEVED': // GET_PROCESSES case 'PROCESSES_RETRIEVED': // GET_PROCESSES
if (this.errors[correlationId]) { if (this.errors[correlationId]) {
const error = this.errors[correlationId]; const error = this.errors[correlationId];