Skip to content

Commit

Permalink
Correlate events only on Mutation ID
Browse files Browse the repository at this point in the history
Update the comparator to be uuid only.
  • Loading branch information
zknill committed Oct 18, 2023
1 parent f35897f commit d4e4698
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Logger } from 'pino';
import { Subject, Subscription } from 'rxjs';

import { toError } from './Errors.js';
import MutationsRegistry, { defaultComparator } from './MutationsRegistry.js';
import MutationsRegistry, { uuidComparator } from './MutationsRegistry.js';
import PendingConfirmationRegistry from './PendingConfirmationRegistry.js';
import { IStream } from './stream/Stream.js';
import StreamFactory, { IStreamFactory as IStreamFactory } from './stream/StreamFactory.js';
Expand Down Expand Up @@ -57,7 +57,7 @@ export default class Model<T> extends EventEmitter<Record<ModelState, ModelState
private readonly mutationsRegistry: MutationsRegistry;

private optimisticEvents: OptimisticEventWithParams[] = [];
private pendingConfirmationRegistry: PendingConfirmationRegistry = new PendingConfirmationRegistry(defaultComparator);
private pendingConfirmationRegistry: PendingConfirmationRegistry = new PendingConfirmationRegistry(uuidComparator);

private readonly subscriptions = new Subject<{ confirmed: boolean; data: T }>();
private subscriptionMap: WeakMap<StandardCallback<T>, Subscription> = new WeakMap();
Expand Down Expand Up @@ -432,7 +432,7 @@ export default class Model<T> extends EventEmitter<Record<ModelState, ModelState
// additional data on the confirmed event in the updated data.
for (let i = 0; i < this.optimisticEvents.length; i++) {
let e = this.optimisticEvents[i];
if (defaultComparator(e, event)) {
if (uuidComparator(e, event)) {
this.optimisticEvents.splice(i, 1);
await this.applyWithRebase(event, this.optimisticEvents);
return;
Expand Down Expand Up @@ -469,7 +469,7 @@ export default class Model<T> extends EventEmitter<Record<ModelState, ModelState
// remove any matching events from the optimisticEvents and re-apply the remaining events
// on top of the latest confirmed state
for (let event of events) {
this.optimisticEvents = this.optimisticEvents.filter((e) => !defaultComparator(e, event));
this.optimisticEvents = this.optimisticEvents.filter((e) => !uuidComparator(e, event));
}
let nextData = this.confirmedData;
for (const e of this.optimisticEvents) {
Expand Down
28 changes: 0 additions & 28 deletions src/MutationsRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
import isEqual from 'lodash/isEqual.js';

import { toError } from './Errors.js';
import type { Event, OptimisticEventWithParams } from './types/model.js';
import type { EventComparator, OptimisticEventOptions } from './types/optimistic.js';

/**
* This comparator compares events by equality of channel, event name and deep equality on the event data.
*
* @param optimistic - The optimistic event to compare.
* @param confirmed - The confirmed event to compare.
* @returns {boolean} Whether the two events are equal.
*/
export const equalityComparator: EventComparator = (optimistic: Event, confirmed: Event) => {
return optimistic.name === confirmed.name && isEqual(optimistic.data, confirmed.data);
};

/**
* This comparator compares events by their `uuid` property.
*
Expand All @@ -26,21 +13,6 @@ export const uuidComparator: EventComparator = (optimistic: Event, confirmed: Ev
return !!optimistic.mutationId && !!confirmed.mutationId && optimistic.mutationId === confirmed.mutationId;
};

/**
* The default event comparator. Compares events by uuid, if provided on both the optimistic and confirmed events.
* Otherwise, compares events by equality of channel, event name and deep equality on the event data.
*
* @param optimistic - The optimistic event to compare.
* @param confirmed - The confirmed event to compare.
* @returns {boolean} Whether the two events are equal.
*/
export const defaultComparator: EventComparator = (optimistic: Event, confirmed: Event) => {
if (optimistic.mutationId && confirmed.mutationId) {
return uuidComparator(optimistic, confirmed);
}
return equalityComparator(optimistic, confirmed);
};

/**
* Default options applied to all optimistic events.
*/
Expand Down

0 comments on commit d4e4698

Please sign in to comment.