@contember/binding-legacy 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/dist/development/src/core/DataBinding.cjs +1 -2
  2. package/dist/development/src/core/DataBinding.cjs.map +1 -1
  3. package/dist/development/src/core/DataBinding.js +1 -2
  4. package/dist/development/src/core/DataBinding.js.map +1 -1
  5. package/dist/development/src/core/MutationGenerator.cjs +118 -107
  6. package/dist/development/src/core/MutationGenerator.cjs.map +1 -1
  7. package/dist/development/src/core/MutationGenerator.js +118 -107
  8. package/dist/development/src/core/MutationGenerator.js.map +1 -1
  9. package/dist/development/src/core/TreeAugmenter.cjs +1 -1
  10. package/dist/development/src/core/TreeAugmenter.cjs.map +1 -1
  11. package/dist/development/src/core/TreeAugmenter.js +1 -1
  12. package/dist/development/src/core/TreeAugmenter.js.map +1 -1
  13. package/dist/production/src/core/DataBinding.cjs +1 -2
  14. package/dist/production/src/core/DataBinding.cjs.map +1 -1
  15. package/dist/production/src/core/DataBinding.js +1 -2
  16. package/dist/production/src/core/DataBinding.js.map +1 -1
  17. package/dist/production/src/core/MutationGenerator.cjs +118 -107
  18. package/dist/production/src/core/MutationGenerator.cjs.map +1 -1
  19. package/dist/production/src/core/MutationGenerator.js +118 -107
  20. package/dist/production/src/core/MutationGenerator.js.map +1 -1
  21. package/dist/production/src/core/TreeAugmenter.cjs +1 -1
  22. package/dist/production/src/core/TreeAugmenter.cjs.map +1 -1
  23. package/dist/production/src/core/TreeAugmenter.js +1 -1
  24. package/dist/production/src/core/TreeAugmenter.js.map +1 -1
  25. package/dist/types/core/DataBinding.d.ts.map +1 -1
  26. package/dist/types/core/MutationGenerator.d.ts +5 -2
  27. package/dist/types/core/MutationGenerator.d.ts.map +1 -1
  28. package/dist/types/tsconfig.tsbuildinfo +1 -1
  29. package/package.json +4 -4
  30. package/src/core/DataBinding.ts +1 -2
  31. package/src/core/MutationGenerator.ts +117 -122
  32. package/src/core/TreeAugmenter.ts +1 -1
@@ -96,8 +96,7 @@ const _DataBinding = class _DataBinding2 {
96
96
  });
97
97
  await this.eventManager.triggerOnBeforePersist();
98
98
  await this.checkErrorsBeforePersist(onPersistError);
99
- const generator = new MutationGenerator(this.treeStore, this.queryBuilder);
100
- const mutationResult = generator.getPersistMutation();
99
+ const mutationResult = MutationGenerator.getPersistMutation(this.treeStore, this.queryBuilder);
101
100
  if (mutationResult.operations.length === 0) {
102
101
  return await this.processEmptyPersistMutation(onPersistSuccess);
103
102
  }
@@ -1 +1 @@
1
- {"version":3,"file":"DataBinding.js","sources":["../../../../../packages/binding-legacy/src/core/DataBinding.ts"],"sourcesContent":["import { ContentClient, ContentQueryBuilder, GraphQlClient, GraphQlClientError } from '@contember/client'\nimport type { DataBindingEventListenerMap, DataBindingTransactionResult, EntityId, Environment, MarkerTreeRoot, TreeRootId } from '@contember/binding-common'\nimport {\n\tassertNever,\n\tAsyncBatchUpdatesOptions,\n\tBatchUpdatesOptions,\n\tBindingOperations,\n\tErrorPersistResult,\n\tEventListenersStore,\n\tExtendTreeOptions,\n\tMarkerMerger,\n\tPersistOptions,\n\tPersistSuccessOptions,\n\tReceivedDataTree,\n\tSuccessfulPersistResult,\n\tTreeRootAccessor,\n} from '@contember/binding-common'\nimport { generateEnumerabilityPreventingEntropy } from '../utils'\nimport { AccessorErrorManager } from './AccessorErrorManager'\nimport { Config } from './Config'\nimport { DirtinessTracker } from './DirtinessTracker'\nimport { EventManager } from './EventManager'\nimport { createBatchUpdatesOptions } from './factories'\nimport { MutationGenerator, SubMutationOperation } from './MutationGenerator'\nimport { QueryGenerator } from './QueryGenerator'\nimport { StateIterator } from './state'\nimport { StateInitializer } from './StateInitializer'\nimport { TreeAugmenter } from './TreeAugmenter'\nimport { TreeStore } from './TreeStore'\nimport type { UpdateMetadata } from './UpdateMetadata'\nimport { getCombinedSignal } from './utils'\nimport { createQueryBuilder } from '@contember/binding-common'\nimport { DataBindingExtendAborted } from '@contember/binding-common'\n\n\nexport class DataBinding<Node> {\n\tprivate readonly accessorErrorManager: AccessorErrorManager\n\tprivate readonly batchUpdatesOptions: BatchUpdatesOptions\n\tprivate readonly asyncBatchUpdatesOptions: AsyncBatchUpdatesOptions\n\tprivate readonly bindingOperations: BindingOperations<Node>\n\tprivate readonly config: Config\n\tprivate readonly dirtinessTracker: DirtinessTracker\n\tprivate readonly eventManager: EventManager\n\tprivate readonly stateInitializer: StateInitializer\n\tprivate readonly treeAugmenter: TreeAugmenter\n\tprivate readonly queryBuilder: ContentQueryBuilder\n\tprivate readonly contentClient: ContentClient\n\tprivate readonly eventListenerStore = new EventListenersStore<DataBindingEventListenerMap>()\n\n\t// private treeRootListeners: {\n\t// \teventListeners: {}\n\t// } = {\n\t// \teventListeners: {},\n\t// }\n\n\tpublic constructor(\n\t\tprivate readonly contentApiClient: GraphQlClient,\n\t\tprivate readonly systemApiClient: GraphQlClient,\n\t\tprivate readonly tenantApiClient: GraphQlClient,\n\t\tprivate readonly treeStore: TreeStore,\n\t\tprivate readonly environment: Environment,\n\t\tprivate readonly createMarkerTree: (node: Node, environment: Environment) => MarkerTreeRoot,\n\t\tprivate readonly batchedUpdates: (callback: () => any) => void,\n\t\tprivate readonly onUpdate: (newData: TreeRootAccessor<Node>, binding: DataBinding<Node>) => void,\n\t\tprivate readonly onError: (error: GraphQlClientError, binding: DataBinding<Node>) => void,\n\t\tprivate readonly onPersistSuccess: (result: SuccessfulPersistResult, binding: DataBinding<Node>) => void,\n\t\tprivate readonly options: {\n\t\t\tskipStateUpdateAfterPersist: boolean\n\t\t},\n\t) {\n\t\tthis.config = new Config()\n\t\tthis.batchUpdatesOptions = createBatchUpdatesOptions(environment, this.treeStore)\n\t\tthis.asyncBatchUpdatesOptions = {\n\t\t\t...this.batchUpdatesOptions,\n\t\t\tcontentClient: contentApiClient,\n\t\t\tsystemClient: systemApiClient,\n\t\t\ttenantClient: tenantApiClient,\n\t\t}\n\t\tthis.dirtinessTracker = new DirtinessTracker()\n\t\tthis.eventManager = new EventManager(\n\t\t\tthis.asyncBatchUpdatesOptions,\n\t\t\tthis.batchUpdatesOptions,\n\t\t\tthis.config,\n\t\t\tthis.dirtinessTracker,\n\t\t\tthis.resolvedOnUpdate,\n\t\t\tthis.treeStore,\n\t\t\tthis.batchedUpdates,\n\t\t)\n\t\tthis.accessorErrorManager = new AccessorErrorManager(this.eventManager, this.treeStore)\n\t\tthis.stateInitializer = new StateInitializer(\n\t\t\tthis.accessorErrorManager,\n\t\t\tthis.batchUpdatesOptions,\n\t\t\tthis.eventManager,\n\t\t\tthis.treeStore,\n\t\t)\n\t\tthis.treeAugmenter = new TreeAugmenter(this.eventManager, this.stateInitializer, this.treeStore, options.skipStateUpdateAfterPersist)\n\n\t\t// TODO move this elsewhere\n\t\tthis.bindingOperations = Object.freeze<BindingOperations<Node>>({\n\t\t\t...this.asyncBatchUpdatesOptions,\n\t\t\taddEventListener: (type, listener) => this.eventListenerStore.add({ type }, listener),\n\t\t\tbatchDeferredUpdates: performUpdates => {\n\t\t\t\tthis.eventManager.syncTransaction(() => performUpdates(this.bindingOperations))\n\t\t\t},\n\t\t\textendTree: async (...args) => await this.extendTree(...args),\n\t\t\tpersist: async options => await this.persist(options),\n\t\t\tfetchData: async (...args) => await this.fetchData(...args),\n\t\t})\n\t\tthis.queryBuilder = createQueryBuilder(this.environment.getSchema())\n\t\tthis.contentClient = new ContentClient(contentApiClient)\n\t}\n\n\tprivate async persist({ onPersistError, onPersistSuccess, signal }: PersistOptions = {}) {\n\t\treturn await this.eventManager.persistOperation(async () => {\n\t\t\t// TODO if the tree is in an inconsistent state, wait for lock releases\n\n\t\t\tthis.eventManager.syncTransaction(() => {\n\t\t\t\tthis.accessorErrorManager.clearErrors()\n\t\t\t})\n\t\t\tawait this.eventManager.triggerOnBeforePersist()\n\n\t\t\tawait this.checkErrorsBeforePersist(onPersistError)\n\n\t\t\tconst generator = new MutationGenerator(this.treeStore, this.queryBuilder)\n\t\t\tconst mutationResult = generator.getPersistMutation()\n\n\t\t\tif (mutationResult.operations.length === 0) {\n\t\t\t\treturn await this.processEmptyPersistMutation(onPersistSuccess)\n\t\t\t}\n\n\t\t\tconst { mutations, operations } = mutationResult\n\n\t\t\tlet response: DataBindingTransactionResult\n\t\t\ttry {\n\t\t\t\tresponse = await this.contentClient.mutate(this.queryBuilder.transaction(mutations, {\n\t\t\t\t\tdeferForeignKeyConstraints: true,\n\t\t\t\t}), {\n\t\t\t\t\tsignal,\n\t\t\t\t\tonBeforeRequest: ({ query, variables }) => {\n\t\t\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\t\t\tconsole.debug(query, variables)\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t} catch (e) {\n\t\t\t\tif (e instanceof GraphQlClientError) {\n\t\t\t\t\tthis.onError(e, this)\n\t\t\t\t\tthrow await this.persistFail({\n\t\t\t\t\t\terrors: [e],\n\t\t\t\t\t\ttype: 'invalidResponse',\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tthrow e\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (response.ok && Object.values(response.data).every(it => it.ok)) {\n\t\t\t\treturn await this.processSuccessfulPersistResult(response, operations, onPersistSuccess)\n\t\t\t} else {\n\t\t\t\tif (response.errorMessage) {\n\t\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\t\tconsole.debug(response.errorMessage)\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.syncTransaction(() => this.accessorErrorManager.replaceErrors(response, operations))\n\n\t\t\t\tawait this.eventManager.triggerOnPersistError(this.bindingOperations)\n\t\t\t\tawait onPersistError?.(this.bindingOperations)\n\t\t\t\tthrow await this.persistFail({\n\t\t\t\t\terrors: this.accessorErrorManager.getErrors(),\n\t\t\t\t\ttype: 'invalidInput',\n\t\t\t\t\tresponse,\n\t\t\t\t})\n\n\t\t\t}\n\t\t})\n\t}\n\n\tprivate async checkErrorsBeforePersist(onPersistError: PersistOptions['onPersistError']) {\n\t\tif (this.accessorErrorManager.hasErrors()) {\n\t\t\tawait this.eventManager.triggerOnPersistError(this.bindingOperations)\n\t\t\tawait onPersistError?.(this.bindingOperations)\n\t\t\tthrow await this.persistFail({\n\t\t\t\terrors: this.accessorErrorManager.getErrors(),\n\t\t\t\ttype: 'invalidInput',\n\t\t\t})\n\t\t}\n\t}\n\n\tprivate async processEmptyPersistMutation(onPersistSuccess: PersistOptions['onPersistSuccess']): Promise<SuccessfulPersistResult> {\n\t\tthis.dirtinessTracker.reset() // TODO This ideally shouldn't be necessary but given the current limitations, this makes for better UX.\n\t\tconst persistSuccessOptions: PersistSuccessOptions = {\n\t\t\t...this.bindingOperations,\n\t\t\tsuccessType: 'nothingToPersist',\n\t\t}\n\t\tawait this.eventManager.triggerOnPersistSuccess(persistSuccessOptions)\n\t\tawait onPersistSuccess?.(persistSuccessOptions)\n\t\tconst result: SuccessfulPersistResult = { type: 'nothingToPersist' }\n\t\tawait this.eventListenerStore.invoke({ type: 'persistSuccess' }, result)\n\t\treturn result\n\t}\n\n\tprivate async processSuccessfulPersistResult(mutationData: DataBindingTransactionResult, operations: SubMutationOperation[], onPersistSuccess: PersistOptions['onPersistSuccess']) {\n\t\tconst persistedEntityIds = Object.values(mutationData.data).map(it => it.node?.id).filter((id): id is EntityId => id !== undefined)\n\t\tconst result: SuccessfulPersistResult = {\n\t\t\ttype: 'justSuccess',\n\t\t\tpersistedEntityIds,\n\t\t}\n\n\t\ttry {\n\t\t\tawait this.eventManager.asyncTransaction(async () => {\n\t\t\t\tthis.resetTreeAfterSuccessfulPersist()\n\t\t\t\tthis.treeAugmenter.updatePersistedData(\n\t\t\t\t\tObject.fromEntries(\n\t\t\t\t\t\tObject.entries(mutationData.data).map(([placeholderName, subTreeResponse]) => [\n\t\t\t\t\t\t\tplaceholderName,\n\t\t\t\t\t\t\tsubTreeResponse.node,\n\t\t\t\t\t\t]),\n\t\t\t\t\t),\n\t\t\t\t\toperations,\n\t\t\t\t)\n\t\t\t\tconst persistSuccessOptions: PersistSuccessOptions = {\n\t\t\t\t\t...this.bindingOperations,\n\t\t\t\t\tsuccessType: result.type,\n\t\t\t\t}\n\t\t\t\tawait this.eventManager.triggerOnPersistSuccess(persistSuccessOptions)\n\t\t\t\tawait onPersistSuccess?.(persistSuccessOptions)\n\t\t\t\tawait this.eventListenerStore.invoke({ type: 'persistSuccess' }, result)\n\n\t\t\t\tthis.treeAugmenter.resetCreatingSubTrees()\n\t\t\t})\n\t\t} catch (e) {\n\t\t\tconsole.error(e)\n\t\t\tconst resultWithError = {\n\t\t\t\t...result,\n\t\t\t\tafterPersistError: e,\n\t\t\t}\n\t\t\tthis.onPersistSuccess(resultWithError, this)\n\t\t\treturn resultWithError\n\t\t}\n\t\tthis.onPersistSuccess(result, this)\n\t\treturn result\n\t}\n\n\tprivate async persistFail(error: ErrorPersistResult): Promise<ErrorPersistResult> {\n\t\tawait this.eventListenerStore.invoke({ type: 'persistError' }, error)\n\t\treturn error\n\t}\n\n\tprivate resolvedOnUpdate = ({ isMutating }: UpdateMetadata) => {\n\t\tthis.onUpdate(new TreeRootAccessor<Node>(this.dirtinessTracker.hasChanges(), isMutating, this.bindingOperations), this)\n\t}\n\n\t// This is currently useless but potentially future-compatible\n\t// private readonly addTreeRootEventListener: TreeRootAccessor.AddTreeRootEventListener = this.getAddEventListener(\n\t// \tthis.treeRootListeners,\n\t// )\n\n\tprivate pendingExtensions: Set<{\n\t\tmarkerTreeRoot: MarkerTreeRoot\n\t\tnewFragment: Node\n\t\tnewTreeRootId: TreeRootId | undefined\n\t\toptions: ExtendTreeOptions\n\t\tonError?: (error: Error) => void\n\t}> = new Set()\n\n\tprivate flushResult: Promise<boolean> | undefined\n\n\tpublic async extendTree(newFragment: Node, options: ExtendTreeOptions = {}): Promise<TreeRootId | undefined> {\n\t\tif (options.signal?.aborted) {\n\t\t\treturn Promise.reject(DataBindingExtendAborted)\n\t\t}\n\t\tconst markerTreeRoot = this.createMarkerTree(newFragment, options.environment ?? this.environment)\n\n\t\tif (!options.force && this.treeStore.effectivelyHasTreeRoot(markerTreeRoot)) {\n\t\t\t// This isn't perfectly accurate as theoretically, we could already have all the data necessary but this\n\t\t\t// could still be false.\n\n\t\t\treturn this.eventManager.syncOperation(() => {\n\t\t\t\tconst newTreeRootId = this.getNewTreeRootId()\n\t\t\t\tthis.treeAugmenter.extendTreeStates(newTreeRootId, markerTreeRoot)\n\t\t\t\treturn newTreeRootId\n\t\t\t})\n\t\t}\n\n\t\treturn await new Promise(async (resolve, reject) => {\n\t\t\tconst treeRootId = this.getNewTreeRootId()\n\t\t\tthis.pendingExtensions.add({\n\t\t\t\tmarkerTreeRoot,\n\t\t\t\tnewFragment,\n\t\t\t\tnewTreeRootId: treeRootId,\n\t\t\t\toptions,\n\t\t\t\tonError: options.onError,\n\t\t\t})\n\t\t\tthis.flushResult ??= (async () => {\n\t\t\t\tawait Promise.resolve()\n\t\t\t\tthis.flushResult = undefined\n\t\t\t\treturn await this.flushBatchedTreeExtensions()\n\t\t\t})()\n\n\t\t\tif (!(await this.flushResult) || options.signal?.aborted) {\n\t\t\t\treturn reject(DataBindingExtendAborted)\n\t\t\t}\n\n\t\t\tresolve(treeRootId)\n\t\t})\n\t}\n\n\tpublic async fetchData(fragment: Node, options?: { signal?: AbortSignal; environment?: Environment }): Promise<{\n\t\tdata: ReceivedDataTree\n\t\tmarkerTreeRoot: MarkerTreeRoot\n\t}> {\n\t\tconst markerTreeRoot = this.createMarkerTree(fragment, options?.environment ?? this.environment)\n\t\tconst data = await this.fetchPersistedData(markerTreeRoot, options?.signal)\n\n\t\treturn {\n\t\t\tdata: data,\n\t\t\tmarkerTreeRoot,\n\t\t}\n\t}\n\n\tprivate async flushBatchedTreeExtensions(): Promise<boolean> {\n\t\t// This is an async operation so we just take whatever pendingExtensions there are now and let any new ones\n\t\t// accumulate in an empty set.\n\t\tconst pendingExtensions = Array.from(this.pendingExtensions).filter(extension => {\n\t\t\treturn !extension.options.signal?.aborted\n\t\t})\n\t\tthis.pendingExtensions.clear()\n\n\t\tconst aggregateMarkerTreeRoot = pendingExtensions.reduce<MarkerTreeRoot | undefined>(\n\t\t\t(previousValue, currentValue) => {\n\t\t\t\tif (previousValue === undefined) {\n\t\t\t\t\treturn currentValue.markerTreeRoot\n\t\t\t\t}\n\t\t\t\treturn MarkerMerger.mergeMarkerTreeRoots(previousValue, currentValue.markerTreeRoot)\n\t\t\t},\n\t\t\tundefined,\n\t\t)\n\n\t\tif (aggregateMarkerTreeRoot === undefined) {\n\t\t\treturn false\n\t\t}\n\n\t\tconst aggregateSignal = getCombinedSignal(pendingExtensions.map(extension => extension.options.signal))\n\n\t\tlet aggregatePersistedData: ReceivedDataTree\n\t\ttry {\n\t\t\taggregatePersistedData = await this.fetchPersistedData(aggregateMarkerTreeRoot, aggregateSignal)\n\t\t} catch (e) {\n\t\t\tif (e instanceof GraphQlClientError) {\n\t\t\t\tif (e.type === 'aborted') {\n\t\t\t\t\treturn false\n\t\t\t\t}\n\t\t\t\tfor (const extension of pendingExtensions) {\n\t\t\t\t\textension.onError?.(e)\n\t\t\t\t}\n\t\t\t\tif (!pendingExtensions.every(it => !!it.onError)) {\n\t\t\t\t\tthis.onError(e, this)\n\t\t\t\t}\n\t\t\t\treturn false\n\t\t\t} else {\n\t\t\t\tthrow e\n\t\t\t}\n\t\t}\n\n\t\tif (aggregateSignal?.aborted) {\n\t\t\treturn false\n\t\t}\n\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tthis.treeAugmenter.extendPersistedData(aggregatePersistedData, aggregateMarkerTreeRoot)\n\n\t\t\tfor (const extension of pendingExtensions) {\n\t\t\t\tthis.treeAugmenter.extendTreeStates(extension.newTreeRootId, extension.markerTreeRoot)\n\t\t\t}\n\t\t})\n\t\treturn true\n\t}\n\n\tprivate async fetchPersistedData(\n\t\ttree: MarkerTreeRoot,\n\t\tsignal?: AbortSignal,\n\t): Promise<ReceivedDataTree> {\n\t\tconst queryGenerator = new QueryGenerator(tree, this.queryBuilder)\n\t\tconst query = queryGenerator.getReadQuery()\n\n\t\treturn await this.contentClient.query(query, {\n\t\t\tsignal,\n\t\t\tonBeforeRequest: ({ query, variables }) => {\n\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\tconsole.debug(query, variables)\n\t\t\t},\n\t\t})\n\t}\n\n\tprivate resetTreeAfterSuccessfulPersist() {\n\t\tthis.eventManager.syncTransaction(() => {\n\t\t\tthis.accessorErrorManager.clearErrors()\n\t\t\tthis.dirtinessTracker.reset()\n\n\t\t\tfor (const [, rootState] of StateIterator.eachRootState(this.treeStore)) {\n\t\t\t\tfor (const state of StateIterator.depthFirstAllNodes(rootState)) {\n\t\t\t\t\tswitch (state.type) {\n\t\t\t\t\t\tcase 'field': {\n\t\t\t\t\t\t\tif (state.hasUnpersistedChanges || state.touchLog?.size) {\n\t\t\t\t\t\t\t\tstate.touchLog?.clear()\n\t\t\t\t\t\t\t\tstate.hasUnpersistedChanges = false\n\t\t\t\t\t\t\t\tthis.eventManager.registerJustUpdated(state, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcase 'entityList':\n\t\t\t\t\t\t\tif (state.unpersistedChangesCount > 0 || state.plannedRemovals?.size) {\n\t\t\t\t\t\t\t\tstate.unpersistedChangesCount = 0\n\t\t\t\t\t\t\t\tstate.plannedRemovals?.clear()\n\t\t\t\t\t\t\t\tthis.eventManager.registerJustUpdated(state, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\tcase 'entityRealm': {\n\t\t\t\t\t\t\tif (state.unpersistedChangesCount > 0 || state.plannedHasOneDeletions?.size) {\n\t\t\t\t\t\t\t\tstate.unpersistedChangesCount = 0\n\t\t\t\t\t\t\t\tstate.plannedHasOneDeletions?.clear()\n\t\t\t\t\t\t\t\tthis.eventManager.registerJustUpdated(state, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\t}\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tassertNever(state)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tprivate static getNextTreeRootIdSeed = (() => {\n\t\tlet seed = 1\n\t\treturn () => seed++\n\t})()\n\n\tprivate getNewTreeRootId() {\n\t\t// TODO this is an awful, awful hack.\n\t\t// TODO the particular shape of the alias is relied on in MutationAlias. If you change this, change it there too.\n\t\treturn this.treeStore.markerTrees.size === 0\n\t\t\t? undefined\n\t\t\t: `${generateEnumerabilityPreventingEntropy()}-${DataBinding.getNextTreeRootIdSeed()}`\n\t}\n}\n"],"names":["_DataBinding","options","query"],"mappings":";;;;;;;;;;;;;;;;;AAmCO,MAAM,eAAN,MAAMA,cAAkB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBvB,YACW,kBACA,iBACA,iBACA,WACA,aACA,kBACA,gBACA,UACA,SACA,kBACA,SAGhB;AAbgB,SAAA,mBAAA;AACA,SAAA,kBAAA;AACA,SAAA,kBAAA;AACA,SAAA,YAAA;AACA,SAAA,cAAA;AACA,SAAA,mBAAA;AACA,SAAA,iBAAA;AACA,SAAA,WAAA;AACA,SAAA,UAAA;AACA,SAAA,mBAAA;AACA,SAAA,UAAA;AA9BD,kBAAA,MAAA,sBAAA;AACA,kBAAA,MAAA,qBAAA;AACA,kBAAA,MAAA,0BAAA;AACA,kBAAA,MAAA,mBAAA;AACA,kBAAA,MAAA,QAAA;AACA,kBAAA,MAAA,kBAAA;AACA,kBAAA,MAAA,cAAA;AACA,kBAAA,MAAA,kBAAA;AACA,kBAAA,MAAA,eAAA;AACA,kBAAA,MAAA,cAAA;AACA,kBAAA,MAAA,eAAA;AACA,kBAAA,MAAA,sBAAqB,IAAI,oBAAiD,CAAA;AAyM3F,kBAAA,MAAQ,oBAAmB,CAAC,EAAE,WAAA,MAAiC;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SAAS;AACb,kBAAI,MAAM,yBAAyB,MAAM,UAAU,MAAM;AACxD,sBAAM,UAAU,MAAM;AACtB,sBAAM,wBAAwB;AAC9B,qBAAK,aAAa,oBAAoB,OAAO,aAAa,qBAAqB;AAAA,cAAA;AAEhF;AAAA,YAAA;AAAA,YAED,KAAK;AACJ,kBAAI,MAAM,0BAA0B,KAAK,MAAM,iBAAiB,MAAM;AACrE,sBAAM,0BAA0B;AAChC,sBAAM,iBAAiB,MAAM;AAC7B,qBAAK,aAAa,oBAAoB,OAAO,aAAa,qBAAqB;AAAA,cAAA;AAEhF;AAAA,YACD,KAAK,eAAe;AACnB,kBAAI,MAAM,0BAA0B,KAAK,MAAM,wBAAwB,MAAM;AAC5E,sBAAM,0BAA0B;AAChC,sBAAM,wBAAwB,MAAM;AACpC,qBAAK,aAAa,oBAAoB,OAAO,aAAa,qBAAqB;AAAA,cAAA;AAEhF;AAAA,YAAA;AAAA,YAED;AACC,0BAAY,KAAK;AAAA,UAAA;AAAA,QACnB;AAAA,MACD;AAAA,IACD,CACA;AAAA,EAAA;AAAA,EAQM,mBAAmB;AAG1B,WAAO,KAAK,UAAU,YAAY,SAAS,IACxC,SACA,GAAG,uCAAA,CAAwC,IAAIF,cAAY,sBAAuB,CAAA;AAAA,EAAA;AAEvF;AAZC,cA9YY,cA8YG,yBAA+B,uBAAA;AAC7C,MAAI,OAAO;AACX,SAAO,MAAM;AACd,IAAG;AAjZG,IAAM,cAAN;"}
1
+ {"version":3,"file":"DataBinding.js","sources":["../../../../../packages/binding-legacy/src/core/DataBinding.ts"],"sourcesContent":["import { ContentClient, ContentQueryBuilder, GraphQlClient, GraphQlClientError } from '@contember/client'\nimport type { DataBindingEventListenerMap, DataBindingTransactionResult, EntityId, Environment, MarkerTreeRoot, TreeRootId } from '@contember/binding-common'\nimport {\n\tassertNever,\n\tAsyncBatchUpdatesOptions,\n\tBatchUpdatesOptions,\n\tBindingOperations,\n\tErrorPersistResult,\n\tEventListenersStore,\n\tExtendTreeOptions,\n\tMarkerMerger,\n\tPersistOptions,\n\tPersistSuccessOptions,\n\tReceivedDataTree,\n\tSuccessfulPersistResult,\n\tTreeRootAccessor,\n} from '@contember/binding-common'\nimport { generateEnumerabilityPreventingEntropy } from '../utils'\nimport { AccessorErrorManager } from './AccessorErrorManager'\nimport { Config } from './Config'\nimport { DirtinessTracker } from './DirtinessTracker'\nimport { EventManager } from './EventManager'\nimport { createBatchUpdatesOptions } from './factories'\nimport { MutationGenerator, SubMutationOperation } from './MutationGenerator'\nimport { QueryGenerator } from './QueryGenerator'\nimport { StateIterator } from './state'\nimport { StateInitializer } from './StateInitializer'\nimport { TreeAugmenter } from './TreeAugmenter'\nimport { TreeStore } from './TreeStore'\nimport type { UpdateMetadata } from './UpdateMetadata'\nimport { getCombinedSignal } from './utils'\nimport { createQueryBuilder } from '@contember/binding-common'\nimport { DataBindingExtendAborted } from '@contember/binding-common'\n\n\nexport class DataBinding<Node> {\n\tprivate readonly accessorErrorManager: AccessorErrorManager\n\tprivate readonly batchUpdatesOptions: BatchUpdatesOptions\n\tprivate readonly asyncBatchUpdatesOptions: AsyncBatchUpdatesOptions\n\tprivate readonly bindingOperations: BindingOperations<Node>\n\tprivate readonly config: Config\n\tprivate readonly dirtinessTracker: DirtinessTracker\n\tprivate readonly eventManager: EventManager\n\tprivate readonly stateInitializer: StateInitializer\n\tprivate readonly treeAugmenter: TreeAugmenter\n\tprivate readonly queryBuilder: ContentQueryBuilder\n\tprivate readonly contentClient: ContentClient\n\tprivate readonly eventListenerStore = new EventListenersStore<DataBindingEventListenerMap>()\n\n\t// private treeRootListeners: {\n\t// \teventListeners: {}\n\t// } = {\n\t// \teventListeners: {},\n\t// }\n\n\tpublic constructor(\n\t\tprivate readonly contentApiClient: GraphQlClient,\n\t\tprivate readonly systemApiClient: GraphQlClient,\n\t\tprivate readonly tenantApiClient: GraphQlClient,\n\t\tprivate readonly treeStore: TreeStore,\n\t\tprivate readonly environment: Environment,\n\t\tprivate readonly createMarkerTree: (node: Node, environment: Environment) => MarkerTreeRoot,\n\t\tprivate readonly batchedUpdates: (callback: () => any) => void,\n\t\tprivate readonly onUpdate: (newData: TreeRootAccessor<Node>, binding: DataBinding<Node>) => void,\n\t\tprivate readonly onError: (error: GraphQlClientError, binding: DataBinding<Node>) => void,\n\t\tprivate readonly onPersistSuccess: (result: SuccessfulPersistResult, binding: DataBinding<Node>) => void,\n\t\tprivate readonly options: {\n\t\t\tskipStateUpdateAfterPersist: boolean\n\t\t},\n\t) {\n\t\tthis.config = new Config()\n\t\tthis.batchUpdatesOptions = createBatchUpdatesOptions(environment, this.treeStore)\n\t\tthis.asyncBatchUpdatesOptions = {\n\t\t\t...this.batchUpdatesOptions,\n\t\t\tcontentClient: contentApiClient,\n\t\t\tsystemClient: systemApiClient,\n\t\t\ttenantClient: tenantApiClient,\n\t\t}\n\t\tthis.dirtinessTracker = new DirtinessTracker()\n\t\tthis.eventManager = new EventManager(\n\t\t\tthis.asyncBatchUpdatesOptions,\n\t\t\tthis.batchUpdatesOptions,\n\t\t\tthis.config,\n\t\t\tthis.dirtinessTracker,\n\t\t\tthis.resolvedOnUpdate,\n\t\t\tthis.treeStore,\n\t\t\tthis.batchedUpdates,\n\t\t)\n\t\tthis.accessorErrorManager = new AccessorErrorManager(this.eventManager, this.treeStore)\n\t\tthis.stateInitializer = new StateInitializer(\n\t\t\tthis.accessorErrorManager,\n\t\t\tthis.batchUpdatesOptions,\n\t\t\tthis.eventManager,\n\t\t\tthis.treeStore,\n\t\t)\n\t\tthis.treeAugmenter = new TreeAugmenter(this.eventManager, this.stateInitializer, this.treeStore, options.skipStateUpdateAfterPersist)\n\n\t\t// TODO move this elsewhere\n\t\tthis.bindingOperations = Object.freeze<BindingOperations<Node>>({\n\t\t\t...this.asyncBatchUpdatesOptions,\n\t\t\taddEventListener: (type, listener) => this.eventListenerStore.add({ type }, listener),\n\t\t\tbatchDeferredUpdates: performUpdates => {\n\t\t\t\tthis.eventManager.syncTransaction(() => performUpdates(this.bindingOperations))\n\t\t\t},\n\t\t\textendTree: async (...args) => await this.extendTree(...args),\n\t\t\tpersist: async options => await this.persist(options),\n\t\t\tfetchData: async (...args) => await this.fetchData(...args),\n\t\t})\n\t\tthis.queryBuilder = createQueryBuilder(this.environment.getSchema())\n\t\tthis.contentClient = new ContentClient(contentApiClient)\n\t}\n\n\tprivate async persist({ onPersistError, onPersistSuccess, signal }: PersistOptions = {}) {\n\t\treturn await this.eventManager.persistOperation(async () => {\n\t\t\t// TODO if the tree is in an inconsistent state, wait for lock releases\n\n\t\t\tthis.eventManager.syncTransaction(() => {\n\t\t\t\tthis.accessorErrorManager.clearErrors()\n\t\t\t})\n\t\t\tawait this.eventManager.triggerOnBeforePersist()\n\n\t\t\tawait this.checkErrorsBeforePersist(onPersistError)\n\n\t\t\tconst mutationResult = MutationGenerator.getPersistMutation(this.treeStore, this.queryBuilder)\n\n\t\t\tif (mutationResult.operations.length === 0) {\n\t\t\t\treturn await this.processEmptyPersistMutation(onPersistSuccess)\n\t\t\t}\n\n\t\t\tconst { mutations, operations } = mutationResult\n\n\t\t\tlet response: DataBindingTransactionResult\n\t\t\ttry {\n\t\t\t\tresponse = await this.contentClient.mutate(this.queryBuilder.transaction(mutations, {\n\t\t\t\t\tdeferForeignKeyConstraints: true,\n\t\t\t\t}), {\n\t\t\t\t\tsignal,\n\t\t\t\t\tonBeforeRequest: ({ query, variables }) => {\n\t\t\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\t\t\tconsole.debug(query, variables)\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t} catch (e) {\n\t\t\t\tif (e instanceof GraphQlClientError) {\n\t\t\t\t\tthis.onError(e, this)\n\t\t\t\t\tthrow await this.persistFail({\n\t\t\t\t\t\terrors: [e],\n\t\t\t\t\t\ttype: 'invalidResponse',\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tthrow e\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (response.ok && Object.values(response.data).every(it => it.ok)) {\n\t\t\t\treturn await this.processSuccessfulPersistResult(response, operations, onPersistSuccess)\n\t\t\t} else {\n\t\t\t\tif (response.errorMessage) {\n\t\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\t\tconsole.debug(response.errorMessage)\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.syncTransaction(() => this.accessorErrorManager.replaceErrors(response, operations))\n\n\t\t\t\tawait this.eventManager.triggerOnPersistError(this.bindingOperations)\n\t\t\t\tawait onPersistError?.(this.bindingOperations)\n\t\t\t\tthrow await this.persistFail({\n\t\t\t\t\terrors: this.accessorErrorManager.getErrors(),\n\t\t\t\t\ttype: 'invalidInput',\n\t\t\t\t\tresponse,\n\t\t\t\t})\n\n\t\t\t}\n\t\t})\n\t}\n\n\tprivate async checkErrorsBeforePersist(onPersistError: PersistOptions['onPersistError']) {\n\t\tif (this.accessorErrorManager.hasErrors()) {\n\t\t\tawait this.eventManager.triggerOnPersistError(this.bindingOperations)\n\t\t\tawait onPersistError?.(this.bindingOperations)\n\t\t\tthrow await this.persistFail({\n\t\t\t\terrors: this.accessorErrorManager.getErrors(),\n\t\t\t\ttype: 'invalidInput',\n\t\t\t})\n\t\t}\n\t}\n\n\tprivate async processEmptyPersistMutation(onPersistSuccess: PersistOptions['onPersistSuccess']): Promise<SuccessfulPersistResult> {\n\t\tthis.dirtinessTracker.reset() // TODO This ideally shouldn't be necessary but given the current limitations, this makes for better UX.\n\t\tconst persistSuccessOptions: PersistSuccessOptions = {\n\t\t\t...this.bindingOperations,\n\t\t\tsuccessType: 'nothingToPersist',\n\t\t}\n\t\tawait this.eventManager.triggerOnPersistSuccess(persistSuccessOptions)\n\t\tawait onPersistSuccess?.(persistSuccessOptions)\n\t\tconst result: SuccessfulPersistResult = { type: 'nothingToPersist' }\n\t\tawait this.eventListenerStore.invoke({ type: 'persistSuccess' }, result)\n\t\treturn result\n\t}\n\n\tprivate async processSuccessfulPersistResult(mutationData: DataBindingTransactionResult, operations: SubMutationOperation[], onPersistSuccess: PersistOptions['onPersistSuccess']) {\n\t\tconst persistedEntityIds = Object.values(mutationData.data).map(it => it.node?.id).filter((id): id is EntityId => id !== undefined)\n\t\tconst result: SuccessfulPersistResult = {\n\t\t\ttype: 'justSuccess',\n\t\t\tpersistedEntityIds,\n\t\t}\n\n\t\ttry {\n\t\t\tawait this.eventManager.asyncTransaction(async () => {\n\t\t\t\tthis.resetTreeAfterSuccessfulPersist()\n\t\t\t\tthis.treeAugmenter.updatePersistedData(\n\t\t\t\t\tObject.fromEntries(\n\t\t\t\t\t\tObject.entries(mutationData.data).map(([placeholderName, subTreeResponse]) => [\n\t\t\t\t\t\t\tplaceholderName,\n\t\t\t\t\t\t\tsubTreeResponse.node,\n\t\t\t\t\t\t]),\n\t\t\t\t\t),\n\t\t\t\t\toperations,\n\t\t\t\t)\n\t\t\t\tconst persistSuccessOptions: PersistSuccessOptions = {\n\t\t\t\t\t...this.bindingOperations,\n\t\t\t\t\tsuccessType: result.type,\n\t\t\t\t}\n\t\t\t\tawait this.eventManager.triggerOnPersistSuccess(persistSuccessOptions)\n\t\t\t\tawait onPersistSuccess?.(persistSuccessOptions)\n\t\t\t\tawait this.eventListenerStore.invoke({ type: 'persistSuccess' }, result)\n\n\t\t\t\tthis.treeAugmenter.resetCreatingSubTrees()\n\t\t\t})\n\t\t} catch (e) {\n\t\t\tconsole.error(e)\n\t\t\tconst resultWithError = {\n\t\t\t\t...result,\n\t\t\t\tafterPersistError: e,\n\t\t\t}\n\t\t\tthis.onPersistSuccess(resultWithError, this)\n\t\t\treturn resultWithError\n\t\t}\n\t\tthis.onPersistSuccess(result, this)\n\t\treturn result\n\t}\n\n\tprivate async persistFail(error: ErrorPersistResult): Promise<ErrorPersistResult> {\n\t\tawait this.eventListenerStore.invoke({ type: 'persistError' }, error)\n\t\treturn error\n\t}\n\n\tprivate resolvedOnUpdate = ({ isMutating }: UpdateMetadata) => {\n\t\tthis.onUpdate(new TreeRootAccessor<Node>(this.dirtinessTracker.hasChanges(), isMutating, this.bindingOperations), this)\n\t}\n\n\t// This is currently useless but potentially future-compatible\n\t// private readonly addTreeRootEventListener: TreeRootAccessor.AddTreeRootEventListener = this.getAddEventListener(\n\t// \tthis.treeRootListeners,\n\t// )\n\n\tprivate pendingExtensions: Set<{\n\t\tmarkerTreeRoot: MarkerTreeRoot\n\t\tnewFragment: Node\n\t\tnewTreeRootId: TreeRootId | undefined\n\t\toptions: ExtendTreeOptions\n\t\tonError?: (error: Error) => void\n\t}> = new Set()\n\n\tprivate flushResult: Promise<boolean> | undefined\n\n\tpublic async extendTree(newFragment: Node, options: ExtendTreeOptions = {}): Promise<TreeRootId | undefined> {\n\t\tif (options.signal?.aborted) {\n\t\t\treturn Promise.reject(DataBindingExtendAborted)\n\t\t}\n\t\tconst markerTreeRoot = this.createMarkerTree(newFragment, options.environment ?? this.environment)\n\n\t\tif (!options.force && this.treeStore.effectivelyHasTreeRoot(markerTreeRoot)) {\n\t\t\t// This isn't perfectly accurate as theoretically, we could already have all the data necessary but this\n\t\t\t// could still be false.\n\n\t\t\treturn this.eventManager.syncOperation(() => {\n\t\t\t\tconst newTreeRootId = this.getNewTreeRootId()\n\t\t\t\tthis.treeAugmenter.extendTreeStates(newTreeRootId, markerTreeRoot)\n\t\t\t\treturn newTreeRootId\n\t\t\t})\n\t\t}\n\n\t\treturn await new Promise(async (resolve, reject) => {\n\t\t\tconst treeRootId = this.getNewTreeRootId()\n\t\t\tthis.pendingExtensions.add({\n\t\t\t\tmarkerTreeRoot,\n\t\t\t\tnewFragment,\n\t\t\t\tnewTreeRootId: treeRootId,\n\t\t\t\toptions,\n\t\t\t\tonError: options.onError,\n\t\t\t})\n\t\t\tthis.flushResult ??= (async () => {\n\t\t\t\tawait Promise.resolve()\n\t\t\t\tthis.flushResult = undefined\n\t\t\t\treturn await this.flushBatchedTreeExtensions()\n\t\t\t})()\n\n\t\t\tif (!(await this.flushResult) || options.signal?.aborted) {\n\t\t\t\treturn reject(DataBindingExtendAborted)\n\t\t\t}\n\n\t\t\tresolve(treeRootId)\n\t\t})\n\t}\n\n\tpublic async fetchData(fragment: Node, options?: { signal?: AbortSignal; environment?: Environment }): Promise<{\n\t\tdata: ReceivedDataTree\n\t\tmarkerTreeRoot: MarkerTreeRoot\n\t}> {\n\t\tconst markerTreeRoot = this.createMarkerTree(fragment, options?.environment ?? this.environment)\n\t\tconst data = await this.fetchPersistedData(markerTreeRoot, options?.signal)\n\n\t\treturn {\n\t\t\tdata: data,\n\t\t\tmarkerTreeRoot,\n\t\t}\n\t}\n\n\tprivate async flushBatchedTreeExtensions(): Promise<boolean> {\n\t\t// This is an async operation so we just take whatever pendingExtensions there are now and let any new ones\n\t\t// accumulate in an empty set.\n\t\tconst pendingExtensions = Array.from(this.pendingExtensions).filter(extension => {\n\t\t\treturn !extension.options.signal?.aborted\n\t\t})\n\t\tthis.pendingExtensions.clear()\n\n\t\tconst aggregateMarkerTreeRoot = pendingExtensions.reduce<MarkerTreeRoot | undefined>(\n\t\t\t(previousValue, currentValue) => {\n\t\t\t\tif (previousValue === undefined) {\n\t\t\t\t\treturn currentValue.markerTreeRoot\n\t\t\t\t}\n\t\t\t\treturn MarkerMerger.mergeMarkerTreeRoots(previousValue, currentValue.markerTreeRoot)\n\t\t\t},\n\t\t\tundefined,\n\t\t)\n\n\t\tif (aggregateMarkerTreeRoot === undefined) {\n\t\t\treturn false\n\t\t}\n\n\t\tconst aggregateSignal = getCombinedSignal(pendingExtensions.map(extension => extension.options.signal))\n\n\t\tlet aggregatePersistedData: ReceivedDataTree\n\t\ttry {\n\t\t\taggregatePersistedData = await this.fetchPersistedData(aggregateMarkerTreeRoot, aggregateSignal)\n\t\t} catch (e) {\n\t\t\tif (e instanceof GraphQlClientError) {\n\t\t\t\tif (e.type === 'aborted') {\n\t\t\t\t\treturn false\n\t\t\t\t}\n\t\t\t\tfor (const extension of pendingExtensions) {\n\t\t\t\t\textension.onError?.(e)\n\t\t\t\t}\n\t\t\t\tif (!pendingExtensions.every(it => !!it.onError)) {\n\t\t\t\t\tthis.onError(e, this)\n\t\t\t\t}\n\t\t\t\treturn false\n\t\t\t} else {\n\t\t\t\tthrow e\n\t\t\t}\n\t\t}\n\n\t\tif (aggregateSignal?.aborted) {\n\t\t\treturn false\n\t\t}\n\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tthis.treeAugmenter.extendPersistedData(aggregatePersistedData, aggregateMarkerTreeRoot)\n\n\t\t\tfor (const extension of pendingExtensions) {\n\t\t\t\tthis.treeAugmenter.extendTreeStates(extension.newTreeRootId, extension.markerTreeRoot)\n\t\t\t}\n\t\t})\n\t\treturn true\n\t}\n\n\tprivate async fetchPersistedData(\n\t\ttree: MarkerTreeRoot,\n\t\tsignal?: AbortSignal,\n\t): Promise<ReceivedDataTree> {\n\t\tconst queryGenerator = new QueryGenerator(tree, this.queryBuilder)\n\t\tconst query = queryGenerator.getReadQuery()\n\n\t\treturn await this.contentClient.query(query, {\n\t\t\tsignal,\n\t\t\tonBeforeRequest: ({ query, variables }) => {\n\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\tconsole.debug(query, variables)\n\t\t\t},\n\t\t})\n\t}\n\n\tprivate resetTreeAfterSuccessfulPersist() {\n\t\tthis.eventManager.syncTransaction(() => {\n\t\t\tthis.accessorErrorManager.clearErrors()\n\t\t\tthis.dirtinessTracker.reset()\n\n\t\t\tfor (const [, rootState] of StateIterator.eachRootState(this.treeStore)) {\n\t\t\t\tfor (const state of StateIterator.depthFirstAllNodes(rootState)) {\n\t\t\t\t\tswitch (state.type) {\n\t\t\t\t\t\tcase 'field': {\n\t\t\t\t\t\t\tif (state.hasUnpersistedChanges || state.touchLog?.size) {\n\t\t\t\t\t\t\t\tstate.touchLog?.clear()\n\t\t\t\t\t\t\t\tstate.hasUnpersistedChanges = false\n\t\t\t\t\t\t\t\tthis.eventManager.registerJustUpdated(state, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcase 'entityList':\n\t\t\t\t\t\t\tif (state.unpersistedChangesCount > 0 || state.plannedRemovals?.size) {\n\t\t\t\t\t\t\t\tstate.unpersistedChangesCount = 0\n\t\t\t\t\t\t\t\tstate.plannedRemovals?.clear()\n\t\t\t\t\t\t\t\tthis.eventManager.registerJustUpdated(state, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\tcase 'entityRealm': {\n\t\t\t\t\t\t\tif (state.unpersistedChangesCount > 0 || state.plannedHasOneDeletions?.size) {\n\t\t\t\t\t\t\t\tstate.unpersistedChangesCount = 0\n\t\t\t\t\t\t\t\tstate.plannedHasOneDeletions?.clear()\n\t\t\t\t\t\t\t\tthis.eventManager.registerJustUpdated(state, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\t}\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tassertNever(state)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tprivate static getNextTreeRootIdSeed = (() => {\n\t\tlet seed = 1\n\t\treturn () => seed++\n\t})()\n\n\tprivate getNewTreeRootId() {\n\t\t// TODO this is an awful, awful hack.\n\t\t// TODO the particular shape of the alias is relied on in MutationAlias. If you change this, change it there too.\n\t\treturn this.treeStore.markerTrees.size === 0\n\t\t\t? undefined\n\t\t\t: `${generateEnumerabilityPreventingEntropy()}-${DataBinding.getNextTreeRootIdSeed()}`\n\t}\n}\n"],"names":["_DataBinding","options","query"],"mappings":";;;;;;;;;;;;;;;;;AAmCO,MAAM,eAAN,MAAMA,cAAkB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBvB,YACW,kBACA,iBACA,iBACA,WACA,aACA,kBACA,gBACA,UACA,SACA,kBACA,SAGhB;AAbgB,SAAA,mBAAA;AACA,SAAA,kBAAA;A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SACA,GAAG,uCAAA,CAAwC,IAAIF,cAAY,sBAAuB,CAAA;AAAA,EAAA;AAEvF;AAZC,cA7YY,cA6YG,yBAA+B,uBAAA;AAC7C,MAAI,OAAO;AACX,SAAO,MAAM;AACd,IAAG;AAhZG,IAAM,cAAN;"}
@@ -8,18 +8,21 @@ const StateIterator = require("./state/StateIterator.cjs");
8
8
  const MutationAlias = require("./requestAliases/MutationAlias.cjs");
9
9
  var __defProp = Object.defineProperty;
10
10
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
- var __publicField = (obj, key, value) => __defNormalProp(obj, key + "", value);
11
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
12
12
  class MutationGenerator {
13
13
  constructor(treeStore, qb) {
14
14
  this.treeStore = treeStore;
15
15
  this.qb = qb;
16
16
  __publicField(this, "aliasCounter", 1);
17
+ __publicField(this, "processedPlaceholdersByEntity", /* @__PURE__ */ new Map());
18
+ __publicField(this, "processedDeletes", /* @__PURE__ */ new Set());
19
+ }
20
+ static getPersistMutation(treeStore, qb) {
21
+ return new MutationGenerator(treeStore, qb).getPersistMutation();
17
22
  }
18
23
  getPersistMutation() {
19
24
  const mutations = {};
20
25
  const operations = [];
21
- const processedPlaceholdersByEntity = /* @__PURE__ */ new Map();
22
- const processedDeletes = /* @__PURE__ */ new Set();
23
26
  for (const [treeRootId, rootStates] of this.treeStore.subTreeStatesByRoot.entries()) {
24
27
  for (const [placeholderName, subTreeState] of Array.from(rootStates.entries()).reverse()) {
25
28
  if (subTreeState.unpersistedChangesCount <= 0) {
@@ -30,7 +33,6 @@ class MutationGenerator {
30
33
  continue;
31
34
  }
32
35
  const subMutation = this.createMutation(
33
- processedPlaceholdersByEntity,
34
36
  placeholderName,
35
37
  "single",
36
38
  subTreeState.entity.id.value,
@@ -49,7 +51,6 @@ class MutationGenerator {
49
51
  continue;
50
52
  }
51
53
  const subMutation = this.createMutation(
52
- processedPlaceholdersByEntity,
53
54
  placeholderName,
54
55
  "list",
55
56
  childState.entity.id.value,
@@ -67,10 +68,10 @@ class MutationGenerator {
67
68
  }
68
69
  if (removalType === "delete") {
69
70
  const key = `${subTreeState.entityName}#${removedId}`;
70
- if (processedDeletes.has(key)) {
71
+ if (this.processedDeletes.has(key)) {
71
72
  continue;
72
73
  }
73
- processedDeletes.add(key);
74
+ this.processedDeletes.add(key);
74
75
  const alias = this.createAlias();
75
76
  mutations[alias] = this.createDeleteMutation(
76
77
  subTreeState.entityName,
@@ -89,7 +90,7 @@ class MutationGenerator {
89
90
  }
90
91
  return { mutations, operations };
91
92
  }
92
- createMutation(processedPlaceholdersByEntity, subTreePlaceholder, subTreeType, entityId, realmState) {
93
+ createMutation(subTreePlaceholder, subTreeType, entityId, realmState) {
93
94
  if (realmState.unpersistedChangesCount === 0) {
94
95
  return void 0;
95
96
  }
@@ -112,7 +113,6 @@ class MutationGenerator {
112
113
  ];
113
114
  } else if (!realmState.entity.id.existsOnServer) {
114
115
  const builder2 = this.createCreateMutation(
115
- processedPlaceholdersByEntity,
116
116
  realmState,
117
117
  fieldMarkers
118
118
  );
@@ -132,7 +132,6 @@ class MutationGenerator {
132
132
  ];
133
133
  }
134
134
  const builder = this.createUpdateMutation(
135
- processedPlaceholdersByEntity,
136
135
  realmState,
137
136
  fieldMarkers
138
137
  );
@@ -156,8 +155,8 @@ class MutationGenerator {
156
155
  by: { [bindingCommon.PRIMARY_KEY_NAME]: deletedId }
157
156
  }, (it) => it.$(bindingCommon.PRIMARY_KEY_NAME).$("__typename"));
158
157
  }
159
- createUpdateMutation(processedPlaceholdersByEntity, entityRealm, fieldMarkers) {
160
- const input = this.getUpdateDataInput(processedPlaceholdersByEntity, entityRealm);
158
+ createUpdateMutation(entityRealm, fieldMarkers) {
159
+ const input = this.getUpdateDataInput(entityRealm);
161
160
  if (input === void 0) {
162
161
  return void 0;
163
162
  }
@@ -172,11 +171,8 @@ class MutationGenerator {
172
171
  data: input
173
172
  }, (it) => QueryGenerator.QueryGenerator.registerQueryPart(fieldMarkers, it).$(bindingCommon.PRIMARY_KEY_NAME).$("__typename"));
174
173
  }
175
- createCreateMutation(processedPlaceholdersByEntity, entityRealm, fieldMarkers) {
176
- const input = this.getCreateDataInput(
177
- processedPlaceholdersByEntity,
178
- entityRealm
179
- );
174
+ createCreateMutation(entityRealm, fieldMarkers) {
175
+ const input = this.getCreateDataInput(entityRealm);
180
176
  if (input === void 0) {
181
177
  return void 0;
182
178
  }
@@ -184,10 +180,10 @@ class MutationGenerator {
184
180
  data: input
185
181
  }, (it) => QueryGenerator.QueryGenerator.registerQueryPart(fieldMarkers, it).$(bindingCommon.PRIMARY_KEY_NAME).$("__typename"));
186
182
  }
187
- getCreateDataInput(processedPlaceholdersByEntity, currentState) {
188
- let processedPlaceholders = processedPlaceholdersByEntity.get(currentState.entity);
183
+ getCreateDataInput(currentState) {
184
+ let processedPlaceholders = this.processedPlaceholdersByEntity.get(currentState.entity);
189
185
  if (processedPlaceholders === void 0) {
190
- processedPlaceholdersByEntity.set(currentState.entity, processedPlaceholders = /* @__PURE__ */ new Set());
186
+ this.processedPlaceholdersByEntity.set(currentState.entity, processedPlaceholders = /* @__PURE__ */ new Set());
191
187
  }
192
188
  if (currentState.type === "entityRealmStub") {
193
189
  return void 0;
@@ -240,7 +236,7 @@ class MutationGenerator {
240
236
  });
241
237
  continue;
242
238
  }
243
- const input = this.getCreateOneRelationInput(processedPlaceholdersByEntity, fieldState, marker);
239
+ const input = this.getCreateOneRelationInput(fieldState, marker);
244
240
  if (input !== void 0) {
245
241
  const current = result[marker.parameters.field];
246
242
  if (Array.isArray(input) && Array.isArray(current)) {
@@ -267,7 +263,7 @@ class MutationGenerator {
267
263
  });
268
264
  continue;
269
265
  }
270
- const input = this.getCreateManyRelationInput(processedPlaceholdersByEntity, fieldState);
266
+ const input = this.getCreateManyRelationInput(fieldState);
271
267
  if (input !== void 0) {
272
268
  const current = result[marker.parameters.field];
273
269
  if (Array.isArray(current)) {
@@ -296,7 +292,6 @@ class MutationGenerator {
296
292
  }
297
293
  case "hasOne": {
298
294
  const input = this.getCreateOneRelationInput(
299
- processedPlaceholdersByEntity,
300
295
  field.fieldState,
301
296
  field.marker
302
297
  );
@@ -312,7 +307,6 @@ class MutationGenerator {
312
307
  }
313
308
  case "hasMany": {
314
309
  const input = this.getCreateManyRelationInput(
315
- processedPlaceholdersByEntity,
316
310
  field.fieldState
317
311
  );
318
312
  if (input !== void 0) {
@@ -344,41 +338,41 @@ class MutationGenerator {
344
338
  }
345
339
  return isEmptyObject.isEmptyObject(result) ? void 0 : result;
346
340
  }
347
- getCreateOneRelationInput(processedPlaceholdersByEntity, fieldState, marker) {
341
+ getCreateOneRelationInput(fieldState, marker) {
348
342
  const reducedBy = marker.parameters.reducedBy;
349
343
  const runtimeId = fieldState.entity.id;
350
344
  if (reducedBy === void 0) {
351
- if (this.shouldConnectInsteadOfCreate(processedPlaceholdersByEntity, fieldState)) {
345
+ if (this.shouldConnectInsteadOfCreate(fieldState)) {
352
346
  return { connect: { [bindingCommon.PRIMARY_KEY_NAME]: runtimeId.value } };
353
347
  }
354
- const createData2 = this.getCreateDataInput(processedPlaceholdersByEntity, fieldState);
348
+ const createData2 = this.getCreateDataInput(fieldState);
355
349
  if (createData2 === void 0) {
356
350
  return void 0;
357
351
  }
358
352
  return { create: createData2 };
359
353
  }
360
354
  const alias = MutationAlias.MutationAlias.encodeEntityId(runtimeId);
361
- if (this.shouldConnectInsteadOfCreate(processedPlaceholdersByEntity, fieldState)) {
355
+ if (this.shouldConnectInsteadOfCreate(fieldState)) {
362
356
  return [{ connect: { [bindingCommon.PRIMARY_KEY_NAME]: runtimeId.value }, alias }];
363
357
  }
364
- const createData = this.getCreateDataInput(processedPlaceholdersByEntity, fieldState);
358
+ const createData = this.getCreateDataInput(fieldState);
365
359
  if (createData === void 0) {
366
360
  return void 0;
367
361
  }
368
362
  return [{ create: createData, alias }];
369
363
  }
370
- getCreateManyRelationInput(processedPlaceholdersByEntity, fieldState) {
364
+ getCreateManyRelationInput(fieldState) {
371
365
  const result = [];
372
366
  for (const entityRealm of fieldState.children.values()) {
373
367
  const runtimeId = entityRealm.entity.id;
374
368
  const alias = MutationAlias.MutationAlias.encodeEntityId(runtimeId);
375
- if (this.shouldConnectInsteadOfCreate(processedPlaceholdersByEntity, entityRealm)) {
369
+ if (this.shouldConnectInsteadOfCreate(entityRealm)) {
376
370
  result.push({
377
371
  alias,
378
372
  connect: { [bindingCommon.PRIMARY_KEY_NAME]: runtimeId.value }
379
373
  });
380
374
  } else {
381
- const createData = this.getCreateDataInput(processedPlaceholdersByEntity, entityRealm);
375
+ const createData = this.getCreateDataInput(entityRealm);
382
376
  if (createData === void 0) {
383
377
  continue;
384
378
  }
@@ -390,56 +384,81 @@ class MutationGenerator {
390
384
  }
391
385
  return result.length === 0 ? void 0 : result;
392
386
  }
393
- shouldConnectInsteadOfCreate(processedPlaceholdersByEntity, currentState) {
387
+ shouldConnectInsteadOfCreate(currentState) {
394
388
  const runtimeId = currentState.entity.id;
395
- const processedPlaceholders = processedPlaceholdersByEntity.get(currentState.entity);
389
+ const processedPlaceholders = this.processedPlaceholdersByEntity.get(currentState.entity);
396
390
  return runtimeId.existsOnServer || runtimeId instanceof bindingCommon.ClientGeneratedUuid && (processedPlaceholders?.has(bindingCommon.PRIMARY_KEY_NAME) ?? false);
397
391
  }
398
- getUpdateDataInput(processedPlaceholdersByEntity, currentState) {
399
- let processedPlaceholders = processedPlaceholdersByEntity.get(currentState.entity);
392
+ getUpdateDataInput(currentState) {
393
+ let processedPlaceholders = this.processedPlaceholdersByEntity.get(currentState.entity);
400
394
  if (processedPlaceholders === void 0) {
401
- processedPlaceholdersByEntity.set(currentState.entity, processedPlaceholders = /* @__PURE__ */ new Set());
395
+ this.processedPlaceholdersByEntity.set(currentState.entity, processedPlaceholders = /* @__PURE__ */ new Set());
402
396
  }
403
397
  const pathBack = this.treeStore.getPathBackToParent(currentState);
404
398
  const entityData = this.treeStore.persistedEntityData.get(currentState.entity.id.uniqueValue);
405
399
  const result = {};
406
- for (const [placeholderName, fieldState] of currentState.children) {
407
- if (placeholderName === bindingCommon.PRIMARY_KEY_NAME || placeholderName === bindingCommon.TYPENAME_KEY_NAME) {
408
- continue;
409
- }
410
- if (processedPlaceholders.has(placeholderName)) {
411
- continue;
412
- }
413
- processedPlaceholders.add(placeholderName);
414
- switch (fieldState.type) {
415
- case "field": {
416
- if (fieldState.persistedValue !== void 0) {
417
- const accessor = fieldState.getAccessor();
418
- const resolvedValue = accessor.value ?? accessor.defaultValue ?? null;
419
- if (fieldState.persistedValue !== resolvedValue) {
420
- result[placeholderName] = resolvedValue;
421
- }
422
- }
423
- break;
400
+ for (const siblingState of StateIterator.StateIterator.eachSiblingRealm(currentState)) {
401
+ for (const [placeholderName, fieldState] of siblingState.children) {
402
+ if (placeholderName === bindingCommon.PRIMARY_KEY_NAME || placeholderName === bindingCommon.TYPENAME_KEY_NAME) {
403
+ continue;
404
+ }
405
+ if (processedPlaceholders.has(placeholderName)) {
406
+ continue;
424
407
  }
425
- case "entityRealmStub":
426
- case "entityRealm": {
427
- const marker = getEntityMarker.getEntityMarker(fieldState);
428
- if (!(marker instanceof bindingCommon.HasOneRelationMarker)) {
429
- throw new bindingCommon.BindingError();
408
+ processedPlaceholders.add(placeholderName);
409
+ switch (fieldState.type) {
410
+ case "field": {
411
+ if (fieldState.persistedValue !== void 0) {
412
+ const accessor = fieldState.getAccessor();
413
+ const resolvedValue = accessor.value ?? accessor.defaultValue ?? null;
414
+ if (fieldState.persistedValue !== resolvedValue) {
415
+ result[placeholderName] = resolvedValue;
416
+ }
417
+ }
418
+ break;
430
419
  }
431
- if (pathBack?.fieldBackToParent === marker.parameters.field) {
432
- continue;
420
+ case "entityRealmStub":
421
+ case "entityRealm": {
422
+ const marker = getEntityMarker.getEntityMarker(fieldState);
423
+ if (!(marker instanceof bindingCommon.HasOneRelationMarker)) {
424
+ throw new bindingCommon.BindingError();
425
+ }
426
+ if (pathBack?.fieldBackToParent === marker.parameters.field) {
427
+ continue;
428
+ }
429
+ const reducedBy = marker.parameters.reducedBy;
430
+ const persistedValue = entityData?.get?.(placeholderName)?.value;
431
+ if (reducedBy === void 0) {
432
+ const relationData = this.getUpdateOneRelationInput(currentState, fieldState, persistedValue, placeholderName);
433
+ if (relationData !== void 0) {
434
+ result[marker.parameters.field] = relationData;
435
+ }
436
+ } else {
437
+ const relationData = this.getUpdateManyRelationForReducedInput(currentState, fieldState, persistedValue, placeholderName, reducedBy);
438
+ if (relationData !== void 0) {
439
+ const current = result[marker.parameters.field];
440
+ if (Array.isArray(current)) {
441
+ current.push(...relationData);
442
+ } else {
443
+ result[marker.parameters.field] = relationData;
444
+ }
445
+ }
446
+ }
447
+ break;
433
448
  }
434
- const reducedBy = marker.parameters.reducedBy;
435
- const persistedValue = entityData?.get?.(placeholderName)?.value;
436
- if (reducedBy === void 0) {
437
- const relationData = this.getUpdateOneRelationInput(currentState, fieldState, persistedValue, processedPlaceholdersByEntity, placeholderName);
438
- if (relationData !== void 0) {
439
- result[marker.parameters.field] = relationData;
449
+ case "entityList": {
450
+ const marker = fieldState.blueprint.marker;
451
+ if (!(marker instanceof bindingCommon.HasManyRelationMarker)) {
452
+ throw new bindingCommon.BindingError();
440
453
  }
441
- } else {
442
- const relationData = this.getUpdateManyRelationForReducedInput(currentState, fieldState, persistedValue, processedPlaceholdersByEntity, placeholderName, reducedBy);
454
+ if (pathBack?.fieldBackToParent === marker.parameters.field) {
455
+ continue;
456
+ }
457
+ const persistedEntityIds = entityData?.get?.(placeholderName)?.value ?? /* @__PURE__ */ new Set();
458
+ if (!(persistedEntityIds instanceof Set)) {
459
+ continue;
460
+ }
461
+ const relationData = this.getUpdateManyRelationInput(fieldState, persistedEntityIds);
443
462
  if (relationData !== void 0) {
444
463
  const current = result[marker.parameters.field];
445
464
  if (Array.isArray(current)) {
@@ -448,46 +467,23 @@ class MutationGenerator {
448
467
  result[marker.parameters.field] = relationData;
449
468
  }
450
469
  }
470
+ break;
451
471
  }
452
- break;
453
- }
454
- case "entityList": {
455
- const marker = fieldState.blueprint.marker;
456
- if (!(marker instanceof bindingCommon.HasManyRelationMarker)) {
457
- throw new bindingCommon.BindingError();
458
- }
459
- if (pathBack?.fieldBackToParent === marker.parameters.field) {
460
- continue;
461
- }
462
- const persistedEntityIds = entityData?.get?.(placeholderName)?.value ?? /* @__PURE__ */ new Set();
463
- if (!(persistedEntityIds instanceof Set)) {
464
- continue;
465
- }
466
- const relationData = this.getUpdateManyRelationInput(fieldState, persistedEntityIds, processedPlaceholdersByEntity);
467
- if (relationData !== void 0) {
468
- const current = result[marker.parameters.field];
469
- if (Array.isArray(current)) {
470
- current.push(...relationData);
471
- } else {
472
- result[marker.parameters.field] = relationData;
473
- }
474
- }
475
- break;
472
+ default:
473
+ return bindingCommon.assertNever(fieldState);
476
474
  }
477
- default:
478
- return bindingCommon.assertNever(fieldState);
479
475
  }
480
476
  }
481
477
  return isEmptyObject.isEmptyObject(result) ? void 0 : result;
482
478
  }
483
- getUpdateOneRelationInput(currentState, fieldState, persistedValue, processedPlaceholdersByEntity, placeholderName) {
479
+ getUpdateOneRelationInput(currentState, fieldState, persistedValue, placeholderName) {
484
480
  const runtimeId = fieldState.entity.id;
485
481
  if (persistedValue instanceof bindingCommon.ServerId) {
486
482
  if (persistedValue.value === runtimeId.value) {
487
483
  if (fieldState.type === "entityRealmStub") {
488
484
  return void 0;
489
485
  }
490
- const input = this.getUpdateDataInput(processedPlaceholdersByEntity, fieldState);
486
+ const input = this.getUpdateDataInput(fieldState);
491
487
  if (input === void 0) {
492
488
  return void 0;
493
489
  }
@@ -497,6 +493,11 @@ class MutationGenerator {
497
493
  }
498
494
  const plannedDeletion = currentState.plannedHasOneDeletions?.get(placeholderName);
499
495
  if (plannedDeletion !== void 0) {
496
+ const key = `${persistedValue.entityName}#${persistedValue.value}`;
497
+ if (this.processedDeletes.has(key)) {
498
+ return void 0;
499
+ }
500
+ this.processedDeletes.add(key);
500
501
  return {
501
502
  delete: true
502
503
  };
@@ -506,7 +507,7 @@ class MutationGenerator {
506
507
  connect: { [bindingCommon.PRIMARY_KEY_NAME]: runtimeId.value }
507
508
  };
508
509
  }
509
- const createInput = this.getCreateDataInput(processedPlaceholdersByEntity, fieldState);
510
+ const createInput = this.getCreateDataInput(fieldState);
510
511
  if (createInput !== void 0) {
511
512
  return {
512
513
  create: createInput
@@ -521,7 +522,7 @@ class MutationGenerator {
521
522
  connect: { [bindingCommon.PRIMARY_KEY_NAME]: runtimeId.value }
522
523
  };
523
524
  } else {
524
- const input = this.getCreateDataInput(processedPlaceholdersByEntity, fieldState);
525
+ const input = this.getCreateDataInput(fieldState);
525
526
  if (input === void 0) {
526
527
  return void 0;
527
528
  }
@@ -530,7 +531,7 @@ class MutationGenerator {
530
531
  };
531
532
  }
532
533
  }
533
- getUpdateManyRelationForReducedInput(currentState, fieldState, persistedValue, processedPlaceholdersByEntity, placeholderName, reducedBy) {
534
+ getUpdateManyRelationForReducedInput(currentState, fieldState, persistedValue, placeholderName, reducedBy) {
534
535
  const runtimeId = fieldState.entity.id;
535
536
  const alias = MutationAlias.MutationAlias.encodeEntityId(runtimeId);
536
537
  if (persistedValue instanceof bindingCommon.ServerId) {
@@ -538,7 +539,7 @@ class MutationGenerator {
538
539
  if (fieldState.type === "entityRealmStub") {
539
540
  return void 0;
540
541
  }
541
- const updateData = this.getUpdateDataInput(processedPlaceholdersByEntity, fieldState);
542
+ const updateData = this.getUpdateDataInput(fieldState);
542
543
  if (updateData === void 0) {
543
544
  return void 0;
544
545
  }
@@ -552,6 +553,11 @@ class MutationGenerator {
552
553
  }
553
554
  const plannedDeletion = currentState.plannedHasOneDeletions?.get(placeholderName);
554
555
  if (plannedDeletion !== void 0) {
556
+ const key = `${persistedValue.entityName}#${persistedValue.value}`;
557
+ if (this.processedDeletes.has(key)) {
558
+ return void 0;
559
+ }
560
+ this.processedDeletes.add(key);
555
561
  return [{
556
562
  alias,
557
563
  delete: reducedBy
@@ -569,7 +575,7 @@ class MutationGenerator {
569
575
  }
570
576
  ];
571
577
  }
572
- const createInput = this.getCreateDataInput(processedPlaceholdersByEntity, fieldState);
578
+ const createInput = this.getCreateDataInput(fieldState);
573
579
  if (createInput === void 0) {
574
580
  return [{
575
581
  alias,
@@ -586,7 +592,7 @@ class MutationGenerator {
586
592
  connect: { [bindingCommon.PRIMARY_KEY_NAME]: runtimeId.value }
587
593
  }];
588
594
  } else {
589
- const createInput = this.getCreateDataInput(processedPlaceholdersByEntity, fieldState);
595
+ const createInput = this.getCreateDataInput(fieldState);
590
596
  if (createInput === void 0) {
591
597
  return void 0;
592
598
  }
@@ -596,7 +602,7 @@ class MutationGenerator {
596
602
  }];
597
603
  }
598
604
  }
599
- getUpdateManyRelationInput(fieldState, persistedEntityIds, processedPlaceholdersByEntity) {
605
+ getUpdateManyRelationInput(fieldState, persistedEntityIds) {
600
606
  const input = [];
601
607
  for (const childState of fieldState.children.values()) {
602
608
  const runtimeId = childState.entity.id;
@@ -604,7 +610,7 @@ class MutationGenerator {
604
610
  if (runtimeId.existsOnServer) {
605
611
  if (persistedEntityIds.has(runtimeId.value)) {
606
612
  if (childState.type !== "entityRealmStub") {
607
- const dataInput = this.getUpdateDataInput(processedPlaceholdersByEntity, childState);
613
+ const dataInput = this.getUpdateDataInput(childState);
608
614
  if (dataInput !== void 0) {
609
615
  input.push({
610
616
  alias,
@@ -622,7 +628,7 @@ class MutationGenerator {
622
628
  });
623
629
  }
624
630
  } else {
625
- const dataInput = this.getCreateDataInput(processedPlaceholdersByEntity, childState);
631
+ const dataInput = this.getCreateDataInput(childState);
626
632
  if (dataInput !== void 0) {
627
633
  input.push({
628
634
  alias,
@@ -635,6 +641,11 @@ class MutationGenerator {
635
641
  for (const [removedId, removalType] of fieldState.plannedRemovals) {
636
642
  const alias = MutationAlias.MutationAlias.encodeEntityId(new bindingCommon.ServerId(removedId, fieldState.entityName));
637
643
  if (removalType === "delete") {
644
+ const key = `${fieldState.entityName}#${removedId}`;
645
+ if (this.processedDeletes.has(key)) {
646
+ continue;
647
+ }
648
+ this.processedDeletes.add(key);
638
649
  input.push({
639
650
  alias,
640
651
  delete: { [bindingCommon.PRIMARY_KEY_NAME]: removedId }