From 4de20c81dac3c43b8068778d80af6c3c3a3db383 Mon Sep 17 00:00:00 2001 From: zak Date: Wed, 18 Oct 2023 14:10:22 +0100 Subject: [PATCH] Correlate events only on Mutation ID Update the comparator to be uuid only. --- src/Model.ts | 10 ++++++---- src/MutationsRegistry.ts | 32 ++------------------------------ 2 files changed, 8 insertions(+), 34 deletions(-) diff --git a/src/Model.ts b/src/Model.ts index f0f54a8d..07e2e77e 100644 --- a/src/Model.ts +++ b/src/Model.ts @@ -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'; @@ -57,7 +57,9 @@ export default class Model extends EventEmitter(); private subscriptionMap: WeakMap, Subscription> = new WeakMap(); @@ -432,7 +434,7 @@ export default class Model extends EventEmitter extends EventEmitter !defaultComparator(e, event)); + this.optimisticEvents = this.optimisticEvents.filter((e) => !mutationIdComparator(e, event)); } let nextData = this.confirmedData; for (const e of this.optimisticEvents) { diff --git a/src/MutationsRegistry.ts b/src/MutationsRegistry.ts index b76e3f96..34d93b3e 100644 --- a/src/MutationsRegistry.ts +++ b/src/MutationsRegistry.ts @@ -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. */