Skip to content

Commit

Permalink
fix: issue a warning on event.mutation access if listener event didn'…
Browse files Browse the repository at this point in the history
…t include them
  • Loading branch information
bjoerge committed Jul 31, 2024
1 parent d937f29 commit 024a4af
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
45 changes: 41 additions & 4 deletions src/store/contentLakeStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {type ReconnectEvent} from '@sanity/client'
import {
defer,
EMPTY,
Expand Down Expand Up @@ -28,6 +29,7 @@ import {
type OptimisticDocumentEvent,
type RemoteDocumentEvent,
type RemoteListenerEvent,
type RemoteMutationEvent,
type SubmitResult,
type TransactionalMutationGroup,
} from './types'
Expand All @@ -45,6 +47,24 @@ export interface StoreBackend {
submit: (mutationGroups: Transaction[]) => Observable<SubmitResult>
}

let didEmitMutationsAccessWarning = false
// certain components, like the portable text editor, rely on mutations to be present in the event
// i.e. it's not enough to just have the mendoza-patches.
// If the listener event did not include mutations (e.g. if excludeMutations was set to true),
// this warning will be issued if a downstream consumers attempts to access event.mutations
function warnNoMutationsReceived() {
if (!didEmitMutationsAccessWarning) {
console.warn(
new Error(
'No mutation received from backend. The listener is likely set up with `excludeMutations: true`. If your app need to now about mutations, make sure the listener is set up to include mutations',
),
)
didEmitMutationsAccessWarning = true
}
}

const EMPTY_ARRAY: any[] = []

export function createContentLakeStore(
backend: StoreBackend,
): ContentLakeStore {
Expand All @@ -69,6 +89,10 @@ export function createContentLakeStore(

function getRemoteEvents(id: string) {
return backend.observe(id).pipe(
filter(
(event): event is Exclude<RemoteListenerEvent, ReconnectEvent> =>
event.type !== 'reconnect',
),
mergeMap((event): Observable<RemoteDocumentEvent> => {
const oldLocal = local.get(id)
const oldRemote = remote.get(id)
Expand Down Expand Up @@ -108,17 +132,30 @@ export function createContentLakeStore(
if (newLocal) {
newLocal._rev = event.transactionId
}

return of({
const emittedEvent: RemoteMutationEvent = {
type: 'mutation',
id,
rebasedStage,
before: {remote: oldRemote, local: oldLocal},
after: {remote: newRemote, local: newLocal},
effects: event.effects,
mutations: decodeAll(event.mutations as SanityMutation[]),
})
// overwritten below
mutations: EMPTY_ARRAY,
}
if (event.mutations) {
emittedEvent.mutations = decodeAll(
event.mutations as SanityMutation[],
)
} else {
Object.defineProperty(
emittedEvent,
'mutations',
warnNoMutationsReceived,
)
}
return of(emittedEvent)
} else {
// @ts-expect-error should have covered all cases
throw new Error(`Unknown event type: ${event.type}`)
}
}),
Expand Down
7 changes: 3 additions & 4 deletions src/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ export interface ListenerMutationEvent {
effects: Required<SanityMutationEvent>['effects']['apply']
mutations: Required<SanityMutationEvent>['mutations']
}
export interface ListenerErrorEvent {
type: 'error'
error: Error
export interface ListenerReconnectEvent {
type: 'reconnect'
}

export type RemoteListenerEvent =
| ListenerSyncEvent
| ListenerMutationEvent
| ListenerErrorEvent
| ListenerReconnectEvent

export interface OptimisticDocumentEvent {
type: 'optimistic'
Expand Down

0 comments on commit 024a4af

Please sign in to comment.