From a47ca719177b9b33b71cbc33df03aa37ec971f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rge=20N=C3=A6ss?= Date: Mon, 7 Oct 2024 16:28:01 +0200 Subject: [PATCH] fixup! poc fix missed listener events recovery --- .../__test__/discardInitialEvents.test.ts | 73 --------------- .../document/utils/discardInitialEvents.ts | 92 ------------------- 2 files changed, 165 deletions(-) delete mode 100644 packages/sanity/src/core/store/_legacy/document/utils/__test__/discardInitialEvents.test.ts delete mode 100644 packages/sanity/src/core/store/_legacy/document/utils/discardInitialEvents.ts diff --git a/packages/sanity/src/core/store/_legacy/document/utils/__test__/discardInitialEvents.test.ts b/packages/sanity/src/core/store/_legacy/document/utils/__test__/discardInitialEvents.test.ts deleted file mode 100644 index 045703b7296..00000000000 --- a/packages/sanity/src/core/store/_legacy/document/utils/__test__/discardInitialEvents.test.ts +++ /dev/null @@ -1,73 +0,0 @@ -/* eslint-disable no-nested-ternary */ -import {expect, it, test} from '@jest/globals' -import {from, lastValueFrom} from 'rxjs' -import {toArray} from 'rxjs/operators' - -import {type ListenerEvent} from '../../getPairListener' -import {discardInitialMutationEvents} from '../discardInitialEvents' -import {mutationEvent} from './test-utils' - -it('it disregards a chain of mutation events that leads up to the initial revision', async () => { - const events = from([ - { - type: 'snapshot', - documentId: 'test', - initialRevision: 'one', - document: { - _rev: 'one', - _id: 'test', - _type: 'test', - name: 'initial', - _createdAt: '2024-10-02T06:40:16.414Z', - _updatedAt: '2024-10-02T06:40:16.414Z', - }, - }, - // this is already applied to the snapshot emitted above and should be ignored - mutationEvent({ - previousRev: 'minus-one', - resultRev: 'zero', - mutations: [{patch: {set: {name: 'SHOULD BE IGNORED'}}}], - }), - // this is already applied to the snapshot emitted above and should be ignored - mutationEvent({ - previousRev: 'zero', - resultRev: 'one', - mutations: [{patch: {set: {name: 'SHOULD ALSO BE IGNORED'}}}], - }), - // this has the snapshot revision as it's previous and should be applied - mutationEvent({ - previousRev: 'one', - resultRev: 'two', - mutations: [{patch: {set: {name: 'SHOULD BE APPLIED'}}}], - }), - ] satisfies ListenerEvent[]) - - expect( - (await lastValueFrom(events.pipe(discardInitialMutationEvents(), toArray()))).map((event) => { - return event?.type === 'mutation' ? event.mutations : event?.type - }), - ).toEqual(['snapshot', [{patch: {set: {name: 'SHOULD BE APPLIED'}}}]]) -}) - -test('it handles an initial missing document', async () => { - const events = from([ - { - type: 'snapshot', - documentId: 'test', - initialRevision: undefined, - document: null, - }, - // this has the snapshot revision as it's previous and should be applied - mutationEvent({ - previousRev: 'one', - resultRev: 'two', - mutations: [{patch: {set: {name: 'SHOULD BE APPLIED'}}}], - }), - ] satisfies ListenerEvent[]) - - expect( - (await lastValueFrom(events.pipe(discardInitialMutationEvents(), toArray()))).map((event) => { - return event?.type === 'mutation' ? event.mutations : event?.type - }), - ).toEqual(['snapshot', [{patch: {set: {name: 'SHOULD BE APPLIED'}}}]]) -}) diff --git a/packages/sanity/src/core/store/_legacy/document/utils/discardInitialEvents.ts b/packages/sanity/src/core/store/_legacy/document/utils/discardInitialEvents.ts deleted file mode 100644 index aa46eb38cd1..00000000000 --- a/packages/sanity/src/core/store/_legacy/document/utils/discardInitialEvents.ts +++ /dev/null @@ -1,92 +0,0 @@ -/* eslint-disable no-console */ -import {type Observable} from 'rxjs' -import {filter, map, scan} from 'rxjs/operators' - -import {type ListenerEvent} from '../getPairListener' -import {type MutationEvent} from '../types' -import {discardChainTo} from './eventChainUtils' - -type State = { - consistent: boolean - event: ListenerEvent | undefined - initialSnapshot: {revision: string | undefined} | undefined - pending: MutationEvent[] -} - -/** - * Assumes events are received in sequence - * TODO: DELETE NOT IN USE - */ -export function discardInitialMutationEvents() { - return (input$: Observable): Observable => { - return input$.pipe( - scan( - (state: State, event: ListenerEvent): State => { - if (event.type === 'mutation' && !state.initialSnapshot) { - throw new Error( - 'Invalid state. Initial snapshot must always be set before accepting any mutation event', - ) - } - if (event.type === 'snapshot') { - // When receiving a new snapshot, we can safely discard the current orphaned and chainable buffers - return { - consistent: false, - initialSnapshot: { - // note: the document may not exist - revision: event.document?._rev, - }, - pending: [], - event, - } - } - - if (!state.consistent && event.type === 'mutation') { - // Common case: the first mutation we receive builds upon the revision of the document as we received it - if (event.previousRev === state.initialSnapshot?.revision) { - console.log('All good') - return { - ...state, - consistent: true, - event: event, - pending: [], - } - } - // if it doesn't build on the current document snapshot revision - if ( - !state.consistent && - state.initialSnapshot?.revision && - event.previousRev !== state.initialSnapshot?.revision - ) { - // If we get here we are dealing with events that happened before we fetched the snapshot. These mutations are already applied to the current snapshot - const [discarded, pendingWithDiscarded] = discardChainTo( - state.pending.concat(event), - state.initialSnapshot.revision, - ) - - if (discarded.length > 0) { - console.log('DISCARDED MUTATIONS THAT HAPPENED BEFORE INITIAL SNAPSHOT') - } - - return { - ...state, - consistent: pendingWithDiscarded.length === 0, - // signal that the event should not be passed on - event: undefined, - pending: pendingWithDiscarded, - } - } - } - return {...state, event} - }, - { - event: undefined, - initialSnapshot: undefined, - pending: [], - consistent: false, - } satisfies State, - ), - map((state) => state?.event), - filter((event) => event !== undefined), - ) - } -}