Deal with race in a better way

This commit is contained in:
RMidhunSuresh 2023-02-02 18:39:19 +05:30
parent de57e07982
commit 09e67ec21c
No known key found for this signature in database

View File

@ -132,7 +132,6 @@ export class Session {
this._createRoomEncryption = this._createRoomEncryption.bind(this); this._createRoomEncryption = this._createRoomEncryption.bind(this);
this._forgetArchivedRoom = this._forgetArchivedRoom.bind(this); this._forgetArchivedRoom = this._forgetArchivedRoom.bind(this);
this.needsKeyBackup = new ObservableValue(false); this.needsKeyBackup = new ObservableValue(false);
this._pendingObserveCalls = [];
} }
get fingerprintKey() { get fingerprintKey() {
@ -797,8 +796,6 @@ export class Session {
// now all the collections are updated, update the room status // now all the collections are updated, update the room status
// so any listeners to the status will find the collections // so any listeners to the status will find the collections
// completely up to date // completely up to date
await Promise.all(this._pendingObserveCalls);
this._pendingObserveCalls = [];
if (this._observedRoomStatus.size !== 0) { if (this._observedRoomStatus.size !== 0) {
for (const ars of archivedRoomStates) { for (const ars of archivedRoomStates) {
if (ars.shouldAdd) { if (ars.shouldAdd) {
@ -941,21 +938,24 @@ export class Session {
} }
async observeRoomStatus(roomId) { async observeRoomStatus(roomId) {
const op = async () => { let observable = this._observedRoomStatus.get(roomId);
let observable = this._observedRoomStatus.get(roomId); if (!observable) {
if (!observable) { let status = undefined;
const status = await this.getRoomStatus(roomId); // Create and set the observable with value = undefined, so that
observable = new RetainedObservableValue(status, () => { // we don't loose any sync changes that come in while we are busy
this._observedRoomStatus.delete(roomId); // calculating the current room status.
}); observable = new RetainedObservableValue(status, () => {
this._observedRoomStatus.delete(roomId);
this._observedRoomStatus.set(roomId, observable); });
this._observedRoomStatus.set(roomId, observable);
status = await this.getRoomStatus(roomId);
// If observable.value is not undefined anymore, then some
// change has come through the sync.
if (observable.get() === undefined) {
observable.set(status);
} }
return observable;
} }
const promise = op(); return observable;
this._pendingObserveCalls.push(promise);
return await promise;
} }
observeRoomState(roomStateHandler) { observeRoomState(roomStateHandler) {