Skip to content

Commit 606c73f

Browse files
committed
feat: make applyDocumentActions more similar to the rest
`applyDocumentActions` was the only function in the `core` package which interpreted the `SanityInstance` as a tree, automatically traversing the tree to find a matching instance. This is inconsistent with the other stores (e.g. query store) which assumes that it's being called with the right instance. In addition, this changes `applyDocumentActions` to only accept actions as an array (so we don't have to check if it's an array or not) and also it places the `actions` as part of option. This is consistent with e.g. `getQueryState` which has a required `query` field in its options. This now means that all functions which are wrapped in `bindActionByDataset` are now consistently getting passed an object as their first argument. This is going to be key so that we later can modify this function to consider the options (instead of the instance). There should be no breaking changes in the React package.
1 parent 60996a7 commit 606c73f

11 files changed

+334
-193
lines changed

packages/core/src/document/applyDocumentActions.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ describe('applyDocumentActions', () => {
7272
}
7373

7474
// Call applyDocumentActions with a fixed transactionId for reproducibility.
75-
const applyPromise = applyDocumentActions(instance, action, {
75+
const applyPromise = applyDocumentActions(instance, {
76+
actions: [action],
7677
transactionId: 'txn-success',
7778
})
7879

@@ -128,7 +129,8 @@ describe('applyDocumentActions', () => {
128129
}
129130

130131
// Call applyDocumentActions with a fixed transactionId.
131-
const applyPromise = applyDocumentActions(instance, action, {
132+
const applyPromise = applyDocumentActions(instance, {
133+
actions: [action],
132134
transactionId: 'txn-error',
133135
})
134136

@@ -160,7 +162,8 @@ describe('applyDocumentActions', () => {
160162
dataset: 'd',
161163
}
162164
// Call applyDocumentActions with the context using childInstance, but with action requiring parent's config
163-
const applyPromise = applyDocumentActions(childInstance, action, {
165+
const applyPromise = applyDocumentActions(childInstance, {
166+
actions: [action],
164167
transactionId: 'txn-child-match',
165168
})
166169

packages/core/src/document/applyDocumentActions.ts

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export interface ActionsResult<TDocument extends SanityDocument = SanityDocument
2424

2525
/** @beta */
2626
export interface ApplyDocumentActionsOptions {
27+
/**
28+
* List of actions to apply.
29+
*/
30+
actions: DocumentAction[]
31+
2732
/**
2833
* Optionally provide an ID to be used as this transaction ID
2934
*/
@@ -41,16 +46,12 @@ export function applyDocumentActions<
4146
TProjectId extends string = string,
4247
>(
4348
instance: SanityInstance,
44-
action:
45-
| DocumentAction<TDocumentType, TDataset, TProjectId>
46-
| DocumentAction<TDocumentType, TDataset, TProjectId>[],
47-
options?: ApplyDocumentActionsOptions,
49+
options: ApplyDocumentActionsOptions,
4850
): Promise<ActionsResult<SanityDocument<TDocumentType, `${TProjectId}.${TDataset}`>>>
4951
/** @beta */
5052
export function applyDocumentActions(
5153
instance: SanityInstance,
52-
action: DocumentAction | DocumentAction[],
53-
options?: ApplyDocumentActionsOptions,
54+
options: ApplyDocumentActionsOptions,
5455
): Promise<ActionsResult>
5556

5657
/** @beta */
@@ -64,50 +65,9 @@ const boundApplyDocumentActions = bindActionByDataset(documentStore, _applyDocum
6465

6566
/** @internal */
6667
async function _applyDocumentActions(
67-
{instance, state}: StoreContext<DocumentStoreState>,
68-
actionOrActions: DocumentAction | DocumentAction[],
69-
{transactionId = crypto.randomUUID(), disableBatching}: ApplyDocumentActionsOptions = {},
68+
{state}: StoreContext<DocumentStoreState>,
69+
{actions, transactionId = crypto.randomUUID(), disableBatching}: ApplyDocumentActionsOptions,
7070
): Promise<ActionsResult> {
71-
const actions = Array.isArray(actionOrActions) ? actionOrActions : [actionOrActions]
72-
73-
let projectId
74-
let dataset
75-
for (const action of actions) {
76-
if (action.projectId) {
77-
if (!projectId) projectId = action.projectId
78-
if (action.projectId !== projectId) {
79-
throw new Error(
80-
`Mismatched project IDs found in actions. All actions must belong to the same project. Found "${action.projectId}" but expected "${projectId}".`,
81-
)
82-
}
83-
84-
if (action.dataset) {
85-
if (!dataset) dataset = action.dataset
86-
if (action.dataset !== dataset) {
87-
throw new Error(
88-
`Mismatched datasets found in actions. All actions must belong to the same dataset. Found "${action.dataset}" but expected "${dataset}".`,
89-
)
90-
}
91-
}
92-
}
93-
}
94-
95-
if (
96-
(projectId && projectId !== instance.config.projectId) ||
97-
(dataset && dataset !== instance.config.dataset)
98-
) {
99-
const matchedInstance = instance.match({projectId, dataset})
100-
if (!matchedInstance) {
101-
throw new Error(
102-
`Could not find a matching instance for projectId: "${projectId}" and dataset: "${dataset}"`,
103-
)
104-
}
105-
return boundApplyDocumentActions(matchedInstance, actionOrActions, {
106-
disableBatching,
107-
transactionId,
108-
})
109-
}
110-
11171
const {events} = state.get()
11272

11373
const transaction: QueuedTransaction = {

0 commit comments

Comments
 (0)