Skip to content

Commit

Permalink
Revert "wip: allow pairs to include multiple drafts"
Browse files Browse the repository at this point in the history
This reverts commit 92a4c43.
  • Loading branch information
juice49 committed Aug 12, 2024
1 parent 7257f58 commit 973c6f8
Show file tree
Hide file tree
Showing 26 changed files with 54 additions and 118 deletions.
6 changes: 2 additions & 4 deletions packages/sanity/src/core/form/studio/formBuilderValueStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ function wrap(document: DocumentVersion) {
}

let hasWarned = false
// TODO: Rename -> `checkoutBundle`
export function checkoutPair(documentStore: DocumentStore, idPair: IdPair) {
if (!hasWarned) {
// eslint-disable-next-line no-console
Expand All @@ -91,11 +90,10 @@ export function checkoutPair(documentStore: DocumentStore, idPair: IdPair) {

hasWarned = true
}
const {drafts, published} = documentStore.checkoutPair(idPair)
const {draft, published} = documentStore.checkoutPair(idPair)

return {
drafts: drafts.map(wrap),
// TODO: Rename -> `public`
draft: wrap(draft),
published: wrap(published),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@ export interface DocumentVersion {
/**
* @hidden
* @beta */
// TODO: Rename -> `Bundle`
export interface Pair {
/** @internal */
transactionsPendingEvents$: Observable<PendingMutationsEvent>
// TODO: Rename -> `public`
published: DocumentVersion
drafts: DocumentVersion[]
draft: DocumentVersion
complete: () => void
}

Expand Down Expand Up @@ -116,12 +114,9 @@ function toActions(idPair: IdPair, mutationParams: Mutation['params']): Action[]
}
}
if (mutations.patch) {
// TODO: Should be dynamic
const draftIndex = 0
return {
actionType: 'sanity.action.document.edit',
draftId: idPair.draftIds[draftIndex],
// TODO: Rename -> `publicId`
draftId: idPair.draftId,
publishedId: idPair.publishedId,
patch: omit(mutations.patch, 'id'),
}
Expand Down Expand Up @@ -183,13 +178,12 @@ function submitCommitRequest(
}

/** @internal */
// TODO: Rename -> `checkoutBundle`
export function checkoutPair(
client: SanityClient,
idPair: IdPair,
serverActionsEnabled: Observable<boolean>,
): Pair {
const {publishedId, draftIds} = idPair
const {publishedId, draftId} = idPair

const listenerEventsConnector = new Subject<ListenerEvent>()
const listenerEvents$ = getPairListener(client, idPair).pipe(
Expand All @@ -200,11 +194,11 @@ export function checkoutPair(
filter((ev) => ev.type === 'reconnect'),
) as Observable<ReconnectEvent>

const drafts = draftIds.map((draftId) =>
createBufferedDocument(draftId, listenerEvents$.pipe(filter(isMutationEventForDocId(draftId)))),
const draft = createBufferedDocument(
draftId,
listenerEvents$.pipe(filter(isMutationEventForDocId(draftId))),
)

// TODO: Rename -> `public`
const published = createBufferedDocument(
publishedId,
listenerEvents$.pipe(filter(isMutationEventForDocId(publishedId))),
Expand All @@ -215,10 +209,7 @@ export function checkoutPair(
filter((ev): ev is PendingMutationsEvent => ev.type === 'pending'),
)

const commits$ = merge(
...drafts.map((draft) => draft.commitRequest$),
published.commitRequest$,
).pipe(
const commits$ = merge(draft.commitRequest$, published.commitRequest$).pipe(
mergeMap((commitRequest) =>
serverActionsEnabled.pipe(
take(1),
Expand All @@ -233,13 +224,12 @@ export function checkoutPair(

return {
transactionsPendingEvents$,
drafts: drafts.map<DocumentVersion>((draft) => ({
draft: {
...draft,
events: merge(commits$, reconnect$, draft.events).pipe(map(setVersion('draft'))),
consistency$: draft.consistency$,
remoteSnapshot$: draft.remoteSnapshot$.pipe(map(setVersion('draft'))),
})),
// TODO: Rename -> `public`
},
published: {
...published,
events: merge(commits$, reconnect$, published.events).pipe(map(setVersion('published'))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ export const consistencyStatus: (
typeName: string,
serverActionsEnabled: Observable<boolean>,
) => {
// TODO: Should be dynamic
const draftIndex = 0
// TODO: Rename -> `memoizedBundle`
return memoizedPair(client, idPair, typeName, serverActionsEnabled).pipe(
switchMap(({drafts, published}) =>
combineLatest([drafts[draftIndex].consistency$, published.consistency$]),
switchMap(({draft, published}) =>
combineLatest([draft.consistency$, published.consistency$]),
),
map(
([draftIsConsistent, publishedIsConsistent]) => draftIsConsistent && publishedIsConsistent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ export const documentEvents = memoize(
typeName: string,
serverActionsEnabled: Observable<boolean>,
): Observable<DocumentVersionEvent> => {
// TODO: Should be dynamic
const draftIndex = 0
return memoizedPair(client, idPair, typeName, serverActionsEnabled).pipe(
switchMap(({drafts, published}) => merge(drafts[draftIndex].events, published.events)),
switchMap(({draft, published}) => merge(draft.events, published.events)),
)
},
memoizeKeyGen,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export interface EditStateFor {
type: string
transactionSyncLock: TransactionSyncLockState | null
draft: SanityDocument | null
// TODO: Rename -> `public`
published: SanityDocument | null
liveEdit: boolean
ready: boolean
Expand All @@ -41,12 +40,10 @@ export const editState = memoize(
typeName: string,
): Observable<EditStateFor> => {
const liveEdit = isLiveEditEnabled(ctx.schema, typeName)
// TODO: Should be dynamic
const draftIndex = 0
return snapshotPair(ctx.client, idPair, typeName, ctx.serverActionsEnabled).pipe(
switchMap((versions) =>
combineLatest([
versions.drafts[draftIndex].snapshots$,
versions.draft.snapshots$,
versions.published.snapshots$,
versions.transactionsPendingEvents$.pipe(
map((ev: PendingMutationsEvent) => (ev.phase === 'begin' ? LOCKED : NOT_LOCKED)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ export const operationArgs = memoize(
idPair: IdPair,
typeName: string,
): Observable<OperationArgs> => {
// TODO: Should be dynamic
const draftIndex = 0
return snapshotPair(ctx.client, idPair, typeName, ctx.serverActionsEnabled).pipe(
switchMap((versions) =>
combineLatest([
versions.drafts[draftIndex].snapshots$,
versions.draft.snapshots$,
versions.published.snapshots$,
ctx.serverActionsEnabled,
]).pipe(
Expand All @@ -40,7 +38,7 @@ export const operationArgs = memoize(
idPair,
typeName,
snapshots: {draft, published},
draft: versions.drafts[draftIndex],
draft: versions.draft,
published: versions.published,
}),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export const del: OperationImpl<[], 'NOTHING_TO_DELETE'> = {
return tx.commit({tag: 'document.delete'})
}

idPair.draftIds.forEach((draftId) => tx.delete(draftId))

return tx.commit({
return tx.delete(idPair.draftId).commit({
tag: 'document.delete',
// this disables referential integrity for cross-dataset references. we
// have this set because we warn against deletes in the `ConfirmDeleteDialog`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ export const discardChanges: OperationImpl<[], DisabledReason> = {
return false
},
execute: ({client, idPair}) => {
// TODO: Should be dynamic
const draftIndex = 0
return client.observable
.transaction()
.delete(idPair.draftIds[draftIndex])
.delete(idPair.draftId)
.commit({tag: 'document.discard-changes'})
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ export const patch: OperationImpl<[patches: any[], initialDocument?: Record<stri
...published.patch(patches),
])
} else {
// TODO: Should be dynamic
const draftIndex = 0
draft.mutate([
draft.createIfNotExists({
...initialDocument,
...snapshots.published,
_id: idPair.draftIds[draftIndex],
_id: idPair.draftId,
_type: typeName,
}),
...draft.patch(patches),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ export const publish: OperationImpl<[], DisabledReason> = {
})
}

// TODO: Should be dynamic
const draftIndex = 0
tx.delete(idPair.draftIds[draftIndex])
tx.delete(idPair.draftId)

return tx.commit({tag: 'document.publish', visibility: 'async'})
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import {type OperationImpl} from './types'
export const restore: OperationImpl<[fromRevision: string]> = {
disabled: (): false => false,
execute: ({historyStore, schema, idPair, typeName}, fromRevision: string) => {
// TODO: Should be dynamic
const draftIndex = 0
const targetId = isLiveEditEnabled(schema, typeName)
? idPair.publishedId
: idPair.draftIds[draftIndex]
const targetId = isLiveEditEnabled(schema, typeName) ? idPair.publishedId : idPair.draftId
return historyStore.restore(idPair.publishedId, targetId, fromRevision)
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ export const unpublish: OperationImpl<[], DisabledReason> = {
let tx = client.observable.transaction().delete(idPair.publishedId)

if (snapshots.published) {
// TODO: Should be dynamic
const draftIndex = 0
tx = tx.createIfNotExists({
...omit(snapshots.published, '_updatedAt'),
_id: idPair.draftIds[draftIndex],
_id: idPair.draftId,
_type: snapshots.published._type,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ export const remoteSnapshots = memoize(
serverActionsEnabled: Observable<boolean>,
): Observable<RemoteSnapshotVersionEvent> => {
return memoizedPair(client, idPair, typeName, serverActionsEnabled).pipe(
switchMap(({published, drafts}) =>
merge(published.remoteSnapshot$, ...drafts.map((draft) => draft.remoteSnapshot$)),
),
switchMap(({published, draft}) => merge(published.remoteSnapshot$, draft.remoteSnapshot$)),
)
},
memoizeKeyGen,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ export const del: OperationImpl<[], 'NOTHING_TO_DELETE'> = {
//the delete action requires a published doc -- discard if not present
if (!snapshots.published) {
return actionsApiClient(client).observable.action(
idPair.draftIds.map((draftId) => ({
{
actionType: 'sanity.action.document.discard',
draftId,
})),
draftId: idPair.draftId,
},
{tag: 'document.delete'},
)
}

return actionsApiClient(client).observable.action(
{
actionType: 'sanity.action.document.delete',
includeDrafts: idPair.draftIds,
includeDrafts: snapshots.draft ? [idPair.draftId] : [],
publishedId: idPair.publishedId,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export const discardChanges: OperationImpl<[], DisabledReason> = {
},
execute: ({client, idPair}) => {
return actionsApiClient(client).observable.action(
idPair.draftIds.map((draftId) => ({
{
actionType: 'sanity.action.document.discard',
draftId,
})),
draftId: idPair.draftId,
},
{tag: 'document.discard-changes'},
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,25 @@ export const patch: OperationImpl<[patches: any[], initialDocument?: Record<stri
const patchMutation = draft.patch(patches)

if (snapshots.published) {
// TODO: Should be dynamic
const draftIndex = 0
draft.mutate([
// If there's no draft, the user's edits will be based on the published document in the form in front of them
// so before patching it we need to make sure it's created based on the current published version first.
draft.createIfNotExists({
...initialDocument,
...snapshots.published,
_id: idPair.draftIds[draftIndex],
_id: idPair.draftId,
_type: typeName,
}),
...patchMutation,
])
return
}

// TODO: Should be dynamic
const draftIndex = 0
const ensureDraft = snapshots.draft
? []
: [
draft.create({
...initialDocument,
_id: idPair.draftIds[draftIndex],
_id: idPair.draftId,
_type: typeName,
}),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ export const publish: OperationImpl<[], DisabledReason> = {
throw new Error('cannot execute "publish" when draft is missing')
}

// TODO: Should be dynamic
const draftIndex = 0

return actionsApiClient(client).observable.action(
{
actionType: 'sanity.action.document.publish',
draftId: idPair.draftIds[draftIndex],
draftId: idPair.draftId,
publishedId: idPair.publishedId,
// Optimistic locking using `ifPublishedRevisionId` ensures that concurrent publish action
// invocations do not override each other.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import {isLiveEditEnabled} from '../utils/isLiveEditEnabled'
export const restore: OperationImpl<[fromRevision: string]> = {
disabled: (): false => false,
execute: ({snapshots, historyStore, schema, idPair, typeName}, fromRevision: string) => {
// TODO: Should be dynamic
const draftIndex = 0

const targetId = isLiveEditEnabled(schema, typeName)
? idPair.publishedId
: idPair.draftIds[draftIndex]

const targetId = isLiveEditEnabled(schema, typeName) ? idPair.publishedId : idPair.draftId
return historyStore.restore(idPair.publishedId, targetId, fromRevision, {
fromDeleted: !snapshots.draft && !snapshots.published,
useServerDocumentActions: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ export const unpublish: OperationImpl<[], DisabledReason> = {
}
return snapshots.published ? false : 'NOT_PUBLISHED'
},
execute: ({client, idPair}) => {
// TODO: Should be dynamic
const draftIndex = 0

return actionsApiClient(client).observable.action(
execute: ({client, idPair}) =>
actionsApiClient(client).observable.action(
{
// This operation is run when "unpublish anyway" is clicked
actionType: 'sanity.action.document.unpublish',
draftId: idPair.draftIds[draftIndex],
draftId: idPair.draftId,
publishedId: idPair.publishedId,
},
{
Expand All @@ -29,6 +26,5 @@ export const unpublish: OperationImpl<[], DisabledReason> = {
// UI.
skipCrossDatasetReferenceValidation: true,
},
)
},
),
}
Loading

0 comments on commit 973c6f8

Please sign in to comment.