diff --git a/src/Model.ts b/src/Model.ts index f0f54a8d..dd0c807c 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, { uuidComparator } 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,7 @@ export default class Model extends EventEmitter(); private subscriptionMap: WeakMap, Subscription> = new WeakMap(); @@ -432,7 +432,7 @@ export default class Model extends EventEmitter extends EventEmitter !defaultComparator(e, event)); + this.optimisticEvents = this.optimisticEvents.filter((e) => !uuidComparator(e, event)); } let nextData = this.confirmedData; for (const e of this.optimisticEvents) { diff --git a/src/MutationsRegistry.ts b/src/MutationsRegistry.ts index b76e3f96..52d2b442 100644 --- a/src/MutationsRegistry.ts +++ b/src/MutationsRegistry.ts @@ -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. * @@ -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. */