Skip to content

Commit

Permalink
[MS] Fixed incompatible server modal not being displayed properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-7 committed Jan 15, 2025
1 parent 4cc6285 commit 2469cca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
6 changes: 3 additions & 3 deletions client/src/parsec/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export async function login(
if (connInfo) {
connInfo.shouldAcceptTos = true;
}
distributor.dispatchEvent(Events.TOSAcceptRequired, undefined, 2000);
distributor.dispatchEvent(Events.TOSAcceptRequired, undefined, { delay: 2000 });
break;
case ClientEventTag.InvitationChanged:
distributor.dispatchEvent(Events.InvitationUpdated, {
Expand All @@ -200,7 +200,7 @@ export async function login(
break;
case ClientEventTag.IncompatibleServer:
window.electronAPI.log('warn', `IncompatibleServerEvent: ${JSON.stringify(event)}`);
distributor.dispatchEvent(Events.IncompatibleServer, { reason: event.detail });
distributor.dispatchEvent(Events.IncompatibleServer, { reason: event.detail }, { delay: 5000 });
break;
case ClientEventTag.RevokedSelfUser:
eventDistributor.dispatchEvent(Events.ClientRevoked);
Expand All @@ -218,7 +218,7 @@ export async function login(
eventDistributor.dispatchEvent(Events.WorkspaceUpdated);
break;
case ClientEventTag.WorkspaceWatchedEntryChanged:
eventDistributor.dispatchEvent(Events.EntryUpdated, undefined, 1000);
eventDistributor.dispatchEvent(Events.EntryUpdated, undefined, { aggregateTime: 1000 });
break;
case ClientEventTag.WorkspaceOpsInboundSyncDone:
eventDistributor.dispatchEvent(Events.EntrySynced, { workspaceId: event.realmId, entryId: event.entryId, way: 'inbound' });
Expand Down
22 changes: 19 additions & 3 deletions client/src/services/eventDistributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ class EventDistributor {
this.timeouts = new Map<number, Events>();
}

async dispatchEvent(event: Events, data?: EventData, aggregateTime?: number): Promise<void> {
async dispatchEvent(
event: Events,
data?: EventData,
options: { aggregateTime?: number; delay?: number } = { aggregateTime: undefined, delay: undefined },
): Promise<void> {
async function sendToAll(callbacks: Array<Callback>, event: Events, data?: EventData): Promise<void> {
for (const cb of callbacks) {
if (event & cb.events) {
Expand All @@ -72,10 +76,14 @@ class EventDistributor {
}
}

if (options.aggregateTime !== undefined && options.delay !== undefined) {
console.warn('Cannot have both aggregateTime and delay set, ignoring this event.');
return;
}
// In some cases, events can occur very close to each other, leading to some heavy operations.
// We can aggregate those cases in order to distribute only one event if multiple occur in a short
// time lapse.
if (aggregateTime !== undefined) {
if (options.aggregateTime !== undefined) {
if (data) {
// Can't have data with an aggregateTime, we wouldn't know what data to use
console.warn('Cannot have an aggregate time with data, ignoring this event.');
Expand All @@ -90,9 +98,17 @@ class EventDistributor {
// Create a new timeout
const interval = window.setTimeout(async () => {
await sendToAll(this.callbacks, event, undefined);
}, aggregateTime);
}, options.aggregateTime);
// Add it to the list
this.timeouts.set(event, interval);
} else if (options.delay !== undefined) {
window.setTimeout(
async (d?: EventData) => {
await sendToAll(this.callbacks, event, d);
},
options.delay,
data,
);
} else {
await sendToAll(this.callbacks, event, data);
}
Expand Down

0 comments on commit 2469cca

Please sign in to comment.