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 19, 2023
1 parent f35897f commit 4de20c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 34 deletions.
10 changes: 6 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, { mutationIdComparator } 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,9 @@ 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(
mutationIdComparator,
);

private readonly subscriptions = new Subject<{ confirmed: boolean; data: T }>();
private subscriptionMap: WeakMap<StandardCallback<T>, Subscription> = new WeakMap();
Expand Down Expand Up @@ -432,7 +434,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 (mutationIdComparator(e, event)) {
this.optimisticEvents.splice(i, 1);
await this.applyWithRebase(event, this.optimisticEvents);
return;
Expand Down Expand Up @@ -469,7 +471,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) => !mutationIdComparator(e, event));
}
let nextData = this.confirmedData;
for (const e of this.optimisticEvents) {
Expand Down
32 changes: 2 additions & 30 deletions src/MutationsRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,18 @@
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.
* This comparator compares events by their `mutationId` property.
*
* @param optimistic - The optimistic event to compare.
* @param confirmed - The confirmed event to compare.
* @returns {boolean} Whether the two events are equal.
*/
export const uuidComparator: EventComparator = (optimistic: Event, confirmed: Event) => {
export const mutationIdComparator: EventComparator = (optimistic: Event, confirmed: Event) => {
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 4de20c8

Please sign in to comment.