@contember/binding-legacy 2.1.0-alpha.9 → 2.1.0-beta.1

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 (199) hide show
  1. package/dist/development/index.cjs +8 -8
  2. package/dist/development/index.js +4 -4
  3. package/dist/development/src/accessorTree/NormalizedPersistedData.cjs.map +1 -1
  4. package/dist/development/src/accessorTree/NormalizedPersistedData.js.map +1 -1
  5. package/dist/development/src/accessors/EntityAccessorImpl.cjs.map +1 -1
  6. package/dist/development/src/accessors/EntityAccessorImpl.js.map +1 -1
  7. package/dist/development/src/accessors/EntityListAccessorImpl.cjs +1 -1
  8. package/dist/development/src/accessors/EntityListAccessorImpl.cjs.map +1 -1
  9. package/dist/development/src/accessors/EntityListAccessorImpl.js +1 -1
  10. package/dist/development/src/accessors/EntityListAccessorImpl.js.map +1 -1
  11. package/dist/development/src/accessors/FieldAccessorImpl.cjs.map +1 -1
  12. package/dist/development/src/accessors/FieldAccessorImpl.js.map +1 -1
  13. package/dist/development/src/core/AccessorErrorManager.cjs.map +1 -1
  14. package/dist/development/src/core/AccessorErrorManager.js.map +1 -1
  15. package/dist/development/src/core/Config.cjs.map +1 -1
  16. package/dist/development/src/core/Config.js.map +1 -1
  17. package/dist/development/src/core/DataBinding.cjs +10 -7
  18. package/dist/development/src/core/DataBinding.cjs.map +1 -1
  19. package/dist/development/src/core/DataBinding.js +10 -7
  20. package/dist/development/src/core/DataBinding.js.map +1 -1
  21. package/dist/development/src/core/DirtinessTracker.cjs.map +1 -1
  22. package/dist/development/src/core/DirtinessTracker.js.map +1 -1
  23. package/dist/development/src/core/ErrorsPreprocessor.cjs.map +1 -1
  24. package/dist/development/src/core/ErrorsPreprocessor.js.map +1 -1
  25. package/dist/development/src/core/EventManager.cjs.map +1 -1
  26. package/dist/development/src/core/EventManager.js.map +1 -1
  27. package/dist/development/src/core/MutationGenerator.cjs.map +1 -1
  28. package/dist/development/src/core/MutationGenerator.js.map +1 -1
  29. package/dist/development/src/core/QueryGenerator.cjs.map +1 -1
  30. package/dist/development/src/core/QueryGenerator.js.map +1 -1
  31. package/dist/development/src/core/RealmKeyGenerator.cjs.map +1 -1
  32. package/dist/development/src/core/RealmKeyGenerator.js.map +1 -1
  33. package/dist/development/src/core/RequestResponseNormalizer.cjs.map +1 -1
  34. package/dist/development/src/core/RequestResponseNormalizer.js.map +1 -1
  35. package/dist/development/src/core/StateInitializer.cjs +1 -1
  36. package/dist/development/src/core/StateInitializer.cjs.map +1 -1
  37. package/dist/development/src/core/StateInitializer.js +2 -2
  38. package/dist/development/src/core/StateInitializer.js.map +1 -1
  39. package/dist/development/src/core/TreeAugmenter.cjs +2 -5
  40. package/dist/development/src/core/TreeAugmenter.cjs.map +1 -1
  41. package/dist/development/src/core/TreeAugmenter.js +2 -5
  42. package/dist/development/src/core/TreeAugmenter.js.map +1 -1
  43. package/dist/development/src/core/TreeStore.cjs.map +1 -1
  44. package/dist/development/src/core/TreeStore.js.map +1 -1
  45. package/dist/development/src/core/exceptions/ErrorLocator.cjs.map +1 -1
  46. package/dist/development/src/core/exceptions/ErrorLocator.js.map +1 -1
  47. package/dist/development/src/core/exceptions/MarkerSugarer.cjs.map +1 -1
  48. package/dist/development/src/core/exceptions/MarkerSugarer.js.map +1 -1
  49. package/dist/development/src/core/exceptions/TreeParameterSugarer.cjs.map +1 -1
  50. package/dist/development/src/core/exceptions/TreeParameterSugarer.js.map +1 -1
  51. package/dist/development/src/core/factories/createBatchUpdatesOptions.cjs.map +1 -1
  52. package/dist/development/src/core/factories/createBatchUpdatesOptions.js.map +1 -1
  53. package/dist/development/src/core/operations/EntityOperations.cjs +1 -3
  54. package/dist/development/src/core/operations/EntityOperations.cjs.map +1 -1
  55. package/dist/development/src/core/operations/EntityOperations.js +1 -3
  56. package/dist/development/src/core/operations/EntityOperations.js.map +1 -1
  57. package/dist/development/src/core/operations/FieldOperations.cjs.map +1 -1
  58. package/dist/development/src/core/operations/FieldOperations.js.map +1 -1
  59. package/dist/development/src/core/operations/ListOperations.cjs.map +1 -1
  60. package/dist/development/src/core/operations/ListOperations.js.map +1 -1
  61. package/dist/development/src/core/operations/OperationsHelpers.cjs.map +1 -1
  62. package/dist/development/src/core/operations/OperationsHelpers.js.map +1 -1
  63. package/dist/development/src/core/requestAliases/MutationAlias.cjs.map +1 -1
  64. package/dist/development/src/core/requestAliases/MutationAlias.js.map +1 -1
  65. package/dist/development/src/core/state/StateIterator.cjs.map +1 -1
  66. package/dist/development/src/core/state/StateIterator.js.map +1 -1
  67. package/dist/development/src/core/utils/getCombinedSignal.cjs.map +1 -1
  68. package/dist/development/src/core/utils/getCombinedSignal.js.map +1 -1
  69. package/dist/development/src/fieldHelpers/helpers/FieldHelper.cjs.map +1 -1
  70. package/dist/development/src/fieldHelpers/helpers/FieldHelper.js.map +1 -1
  71. package/dist/development/src/fieldHelpers/helpers/TemporalFieldHelper.cjs +3 -3
  72. package/dist/development/src/fieldHelpers/helpers/TemporalFieldHelper.cjs.map +1 -1
  73. package/dist/development/src/fieldHelpers/helpers/TemporalFieldHelper.js +3 -3
  74. package/dist/development/src/fieldHelpers/helpers/TemporalFieldHelper.js.map +1 -1
  75. package/dist/development/src/fieldHelpers/helpers/UuidFieldHelper.cjs.map +1 -1
  76. package/dist/development/src/fieldHelpers/helpers/UuidFieldHelper.js.map +1 -1
  77. package/dist/development/src/utils/generateEnumerabilityPreventingEntropy.cjs.map +1 -1
  78. package/dist/development/src/utils/generateEnumerabilityPreventingEntropy.js.map +1 -1
  79. package/dist/development/src/utils/isEmptyObject.cjs.map +1 -1
  80. package/dist/development/src/utils/isEmptyObject.js.map +1 -1
  81. package/dist/production/index.cjs +8 -8
  82. package/dist/production/index.js +4 -4
  83. package/dist/production/src/accessorTree/NormalizedPersistedData.cjs.map +1 -1
  84. package/dist/production/src/accessorTree/NormalizedPersistedData.js.map +1 -1
  85. package/dist/production/src/accessors/EntityAccessorImpl.cjs.map +1 -1
  86. package/dist/production/src/accessors/EntityAccessorImpl.js.map +1 -1
  87. package/dist/production/src/accessors/EntityListAccessorImpl.cjs +1 -1
  88. package/dist/production/src/accessors/EntityListAccessorImpl.cjs.map +1 -1
  89. package/dist/production/src/accessors/EntityListAccessorImpl.js +1 -1
  90. package/dist/production/src/accessors/EntityListAccessorImpl.js.map +1 -1
  91. package/dist/production/src/accessors/FieldAccessorImpl.cjs.map +1 -1
  92. package/dist/production/src/accessors/FieldAccessorImpl.js.map +1 -1
  93. package/dist/production/src/core/AccessorErrorManager.cjs.map +1 -1
  94. package/dist/production/src/core/AccessorErrorManager.js.map +1 -1
  95. package/dist/production/src/core/Config.cjs.map +1 -1
  96. package/dist/production/src/core/Config.js.map +1 -1
  97. package/dist/production/src/core/DataBinding.cjs +10 -7
  98. package/dist/production/src/core/DataBinding.cjs.map +1 -1
  99. package/dist/production/src/core/DataBinding.js +10 -7
  100. package/dist/production/src/core/DataBinding.js.map +1 -1
  101. package/dist/production/src/core/DirtinessTracker.cjs.map +1 -1
  102. package/dist/production/src/core/DirtinessTracker.js.map +1 -1
  103. package/dist/production/src/core/ErrorsPreprocessor.cjs.map +1 -1
  104. package/dist/production/src/core/ErrorsPreprocessor.js.map +1 -1
  105. package/dist/production/src/core/EventManager.cjs.map +1 -1
  106. package/dist/production/src/core/EventManager.js.map +1 -1
  107. package/dist/production/src/core/MutationGenerator.cjs.map +1 -1
  108. package/dist/production/src/core/MutationGenerator.js.map +1 -1
  109. package/dist/production/src/core/QueryGenerator.cjs.map +1 -1
  110. package/dist/production/src/core/QueryGenerator.js.map +1 -1
  111. package/dist/production/src/core/RealmKeyGenerator.cjs.map +1 -1
  112. package/dist/production/src/core/RealmKeyGenerator.js.map +1 -1
  113. package/dist/production/src/core/RequestResponseNormalizer.cjs.map +1 -1
  114. package/dist/production/src/core/RequestResponseNormalizer.js.map +1 -1
  115. package/dist/production/src/core/StateInitializer.cjs +1 -1
  116. package/dist/production/src/core/StateInitializer.cjs.map +1 -1
  117. package/dist/production/src/core/StateInitializer.js +2 -2
  118. package/dist/production/src/core/StateInitializer.js.map +1 -1
  119. package/dist/production/src/core/TreeAugmenter.cjs +2 -5
  120. package/dist/production/src/core/TreeAugmenter.cjs.map +1 -1
  121. package/dist/production/src/core/TreeAugmenter.js +2 -5
  122. package/dist/production/src/core/TreeAugmenter.js.map +1 -1
  123. package/dist/production/src/core/TreeStore.cjs.map +1 -1
  124. package/dist/production/src/core/TreeStore.js.map +1 -1
  125. package/dist/production/src/core/exceptions/ErrorLocator.cjs.map +1 -1
  126. package/dist/production/src/core/exceptions/ErrorLocator.js.map +1 -1
  127. package/dist/production/src/core/exceptions/MarkerSugarer.cjs.map +1 -1
  128. package/dist/production/src/core/exceptions/MarkerSugarer.js.map +1 -1
  129. package/dist/production/src/core/exceptions/TreeParameterSugarer.cjs.map +1 -1
  130. package/dist/production/src/core/exceptions/TreeParameterSugarer.js.map +1 -1
  131. package/dist/production/src/core/factories/createBatchUpdatesOptions.cjs.map +1 -1
  132. package/dist/production/src/core/factories/createBatchUpdatesOptions.js.map +1 -1
  133. package/dist/production/src/core/operations/EntityOperations.cjs +1 -3
  134. package/dist/production/src/core/operations/EntityOperations.cjs.map +1 -1
  135. package/dist/production/src/core/operations/EntityOperations.js +1 -3
  136. package/dist/production/src/core/operations/EntityOperations.js.map +1 -1
  137. package/dist/production/src/core/operations/FieldOperations.cjs.map +1 -1
  138. package/dist/production/src/core/operations/FieldOperations.js.map +1 -1
  139. package/dist/production/src/core/operations/ListOperations.cjs.map +1 -1
  140. package/dist/production/src/core/operations/ListOperations.js.map +1 -1
  141. package/dist/production/src/core/operations/OperationsHelpers.cjs.map +1 -1
  142. package/dist/production/src/core/operations/OperationsHelpers.js.map +1 -1
  143. package/dist/production/src/core/requestAliases/MutationAlias.cjs.map +1 -1
  144. package/dist/production/src/core/requestAliases/MutationAlias.js.map +1 -1
  145. package/dist/production/src/core/state/StateIterator.cjs.map +1 -1
  146. package/dist/production/src/core/state/StateIterator.js.map +1 -1
  147. package/dist/production/src/core/utils/getCombinedSignal.cjs.map +1 -1
  148. package/dist/production/src/core/utils/getCombinedSignal.js.map +1 -1
  149. package/dist/production/src/fieldHelpers/helpers/FieldHelper.cjs.map +1 -1
  150. package/dist/production/src/fieldHelpers/helpers/FieldHelper.js.map +1 -1
  151. package/dist/production/src/fieldHelpers/helpers/TemporalFieldHelper.cjs +3 -3
  152. package/dist/production/src/fieldHelpers/helpers/TemporalFieldHelper.cjs.map +1 -1
  153. package/dist/production/src/fieldHelpers/helpers/TemporalFieldHelper.js +3 -3
  154. package/dist/production/src/fieldHelpers/helpers/TemporalFieldHelper.js.map +1 -1
  155. package/dist/production/src/fieldHelpers/helpers/UuidFieldHelper.cjs.map +1 -1
  156. package/dist/production/src/fieldHelpers/helpers/UuidFieldHelper.js.map +1 -1
  157. package/dist/production/src/utils/generateEnumerabilityPreventingEntropy.cjs.map +1 -1
  158. package/dist/production/src/utils/generateEnumerabilityPreventingEntropy.js.map +1 -1
  159. package/dist/production/src/utils/isEmptyObject.cjs.map +1 -1
  160. package/dist/production/src/utils/isEmptyObject.js.map +1 -1
  161. package/dist/types/accessors/EntityAccessorImpl.d.ts.map +1 -1
  162. package/dist/types/accessors/EntityListAccessorImpl.d.ts.map +1 -1
  163. package/dist/types/core/DataBinding.d.ts.map +1 -1
  164. package/dist/types/core/ErrorsPreprocessor.d.ts.map +1 -1
  165. package/dist/types/core/EventManager.d.ts.map +1 -1
  166. package/dist/types/core/MutationGenerator.d.ts.map +1 -1
  167. package/dist/types/core/QueryGenerator.d.ts.map +1 -1
  168. package/dist/types/core/RealmKeyGenerator.d.ts.map +1 -1
  169. package/dist/types/core/RequestResponseNormalizer.d.ts.map +1 -1
  170. package/dist/types/core/StateInitializer.d.ts.map +1 -1
  171. package/dist/types/core/TreeAugmenter.d.ts.map +1 -1
  172. package/dist/types/core/TreeStore.d.ts.map +1 -1
  173. package/dist/types/core/operations/FieldOperations.d.ts.map +1 -1
  174. package/dist/types/core/operations/ListOperations.d.ts.map +1 -1
  175. package/dist/types/core/state/EntityListState.d.ts.map +1 -1
  176. package/dist/types/tsconfig.tsbuildinfo +1 -1
  177. package/package.json +10 -5
  178. package/src/accessorTree/NormalizedPersistedData.ts +1 -1
  179. package/src/accessors/EntityAccessorImpl.ts +13 -11
  180. package/src/accessors/EntityListAccessorImpl.ts +16 -7
  181. package/src/core/AccessorErrorManager.ts +2 -2
  182. package/src/core/DataBinding.ts +24 -15
  183. package/src/core/ErrorsPreprocessor.ts +4 -5
  184. package/src/core/EventManager.ts +34 -46
  185. package/src/core/MutationGenerator.ts +23 -27
  186. package/src/core/QueryGenerator.ts +4 -5
  187. package/src/core/RealmKeyGenerator.ts +5 -3
  188. package/src/core/RequestResponseNormalizer.ts +3 -8
  189. package/src/core/StateInitializer.ts +10 -18
  190. package/src/core/TreeAugmenter.ts +8 -15
  191. package/src/core/TreeStore.ts +5 -4
  192. package/src/core/operations/EntityOperations.ts +25 -25
  193. package/src/core/operations/FieldOperations.ts +15 -17
  194. package/src/core/operations/ListOperations.ts +15 -15
  195. package/src/core/operations/OperationsHelpers.ts +2 -2
  196. package/src/core/state/EntityListState.ts +3 -9
  197. package/src/fieldHelpers/helpers/TemporalFieldHelper.ts +3 -3
  198. package/src/utils/assertNever.ts +4 -4
  199. package/tests/example.test.ts +2 -2
@@ -1 +1 @@
1
- {"version":3,"file":"QueryGenerator.cjs","sources":["../../../../../packages/binding-legacy/src/core/QueryGenerator.ts"],"sourcesContent":["import {\n\tassertNever,\n\tEntityFieldMarkers,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker, FieldMeta,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMarkerTreeRoot,\n\tPRIMARY_KEY_NAME,\n} from '@contember/binding-common'\nimport { ContentEntitySelection, ContentQuery, ContentQueryBuilder } from '@contember/client'\n\n\nexport class QueryGenerator {\n\tconstructor(\n\t\tprivate tree: MarkerTreeRoot,\n\t\tprivate qb: ContentQueryBuilder,\n\t) {}\n\n\tpublic getReadQuery(): Record<string, ContentQuery<any>> {\n\t\tconst result: Record<string, ContentQuery<any>> = {}\n\t\tfor (const [, subTreeMarker] of this.tree.subTrees) {\n\t\t\tif (subTreeMarker.parameters.isCreating) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (subTreeMarker instanceof EntitySubTreeMarker) {\n\t\t\t\tresult[subTreeMarker.placeholderName] = this.addGetQuery(subTreeMarker)\n\t\t\t} else if (subTreeMarker instanceof EntityListSubTreeMarker) {\n\t\t\t\tresult[subTreeMarker.placeholderName] = this.createListQuery(subTreeMarker)\n\t\t\t}\n\t\t}\n\t\treturn result\n\t}\n\n\tprivate addGetQuery(subTree: EntitySubTreeMarker): ContentQuery<any> {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\tthrow new Error()\n\t\t}\n\n\t\treturn this.qb.get(\n\t\t\tsubTree.entityName,\n\t\t\t{\n\t\t\t\tby: subTree.parameters.where,\n\t\t\t\tfilter: subTree.parameters.filter,\n\t\t\t},\n\t\t\tit => QueryGenerator.registerQueryPart(subTree.fields.markers, it),\n\t\t)\n\t}\n\n\tprivate createListQuery(subTree: EntityListSubTreeMarker): ContentQuery<any> {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\tthrow new Error()\n\t\t}\n\t\treturn this.qb.list(\n\t\t\tsubTree.entityName,\n\t\t\t{\n\t\t\t\tfilter: subTree.parameters.filter,\n\t\t\t\torderBy: subTree.parameters.orderBy,\n\t\t\t\toffset: subTree.parameters.offset,\n\t\t\t\tlimit: subTree.parameters.limit,\n\t\t\t},\n\t\t\tit => QueryGenerator.registerQueryPart(subTree.fields.markers, it),\n\t\t)\n\t}\n\n\tpublic static registerQueryPart(fields: EntityFieldMarkers, selection: ContentEntitySelection): ContentEntitySelection {\n\t\tselection = selection.$(PRIMARY_KEY_NAME).$('__typename')\n\n\t\tfor (const [, fieldValue] of fields) {\n\t\t\tif (fieldValue instanceof FieldMarker) {\n\t\t\t\tif (fieldValue.fieldName !== PRIMARY_KEY_NAME) {\n\t\t\t\t\tselection = selection.$(fieldValue.fieldName)\n\t\t\t\t}\n\t\t\t} else if (fieldValue instanceof HasOneRelationMarker) {\n\t\t\t\tconst relation = fieldValue.parameters\n\n\t\t\t\tif (relation.reducedBy) {\n\t\t\t\t\t// Assuming there's exactly one reducer field as enforced by MarkerTreeGenerator\n\t\t\t\t\tconst relationField = `${relation.field}By${ucfirst(Object.keys(relation.reducedBy)[0])}`\n\t\t\t\t\tselection = selection.$(\n\t\t\t\t\t\trelationField,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tby: relation.reducedBy,\n\t\t\t\t\t\t\tfilter: relation.filter,\n\t\t\t\t\t\t\tas: fieldValue.placeholderName,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tit => QueryGenerator.registerQueryPart(fieldValue.fields.markers, it),\n\t\t\t\t\t)\n\t\t\t\t} else {\n\t\t\t\t\tselection = selection.$(\n\t\t\t\t\t\trelation.field,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfilter: relation.filter,\n\t\t\t\t\t\t\tas: fieldValue.placeholderName,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tit => QueryGenerator.registerQueryPart(fieldValue.fields.markers, it),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else if (fieldValue instanceof HasManyRelationMarker) {\n\t\t\t\tconst relation = fieldValue.parameters\n\n\n\t\t\t\tselection = selection.$(\n\t\t\t\t\trelation.field,\n\t\t\t\t\t{\n\t\t\t\t\t\tas: fieldValue.placeholderName,\n\t\t\t\t\t\tfilter: relation.filter,\n\t\t\t\t\t\torderBy: relation.orderBy,\n\t\t\t\t\t\toffset: relation.offset,\n\t\t\t\t\t\tlimit: relation.limit,\n\t\t\t\t\t},\n\t\t\t\t\tit => QueryGenerator.registerQueryPart(fieldValue.fields.markers, it),\n\t\t\t\t)\n\t\t\t} else {\n\t\t\t\tassertNever(fieldValue)\n\t\t\t}\n\t\t\tselection = QueryGenerator.withMetaField(selection, fieldValue.parameters.field, fieldValue.parameters.meta)\n\t\t}\n\n\t\treturn selection\n\t}\n\n\tprivate static withMetaField(selection: ContentEntitySelection, fieldName: string, metaField: FieldMeta): ContentEntitySelection {\n\t\tif (metaField.length === 0) {\n\t\t\treturn selection\n\t\t}\n\t\treturn selection.meta(fieldName, metaField)\n\t}\n}\n\nconst ucfirst = (string: string) => `${string.charAt(0).toUpperCase()}${string.substring(1)}`\n"],"names":["EntitySubTreeMarker","EntityListSubTreeMarker","PRIMARY_KEY_NAME","FieldMarker","HasOneRelationMarker","HasManyRelationMarker","assertNever"],"mappings":";;;AAcO,MAAM,eAAe;AAAA,EAC3B,YACS,MACA,IACP;AAFO,SAAA,OAAA;AACA,SAAA,KAAA;AAAA,EAAA;AAAA,EAGF,eAAkD;AACxD,UAAM,SAA4C,CAAC;AACnD,eAAW,CAAA,EAAG,aAAa,KAAK,KAAK,KAAK,UAAU;AAC/C,UAAA,cAAc,WAAW,YAAY;AACxC;AAAA,MAAA;AAED,UAAI,yBAAyBA,cAAAA,qBAAqB;AACjD,eAAO,cAAc,eAAe,IAAI,KAAK,YAAY,aAAa;AAAA,MAAA,WAC5D,yBAAyBC,uCAAyB;AAC5D,eAAO,cAAc,eAAe,IAAI,KAAK,gBAAgB,aAAa;AAAA,MAAA;AAAA,IAC3E;AAEM,WAAA;AAAA,EAAA;AAAA,EAGA,YAAY,SAAiD;AAChE,QAAA,QAAQ,WAAW,YAAY;AAClC,YAAM,IAAI,MAAM;AAAA,IAAA;AAGjB,WAAO,KAAK,GAAG;AAAA,MACd,QAAQ;AAAA,MACR;AAAA,QACC,IAAI,QAAQ,WAAW;AAAA,QACvB,QAAQ,QAAQ,WAAW;AAAA,MAC5B;AAAA,MACA,QAAO,eAAe,kBAAkB,QAAQ,OAAO,SAAS,EAAE;AAAA,IACnE;AAAA,EAAA;AAAA,EAGO,gBAAgB,SAAqD;AACxE,QAAA,QAAQ,WAAW,YAAY;AAClC,YAAM,IAAI,MAAM;AAAA,IAAA;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AA,YACC,IAAI,WAAW;AAAA,YACf,QAAQ,SAAS;AAAA,YACjB,SAAS,SAAS;AAAA,YAClB,QAAQ,SAAS;AAAA,YACjB,OAAO,SAAS;AAAA,UACjB;AAAA,UACA,QAAM,eAAe,kBAAkB,WAAW,OAAO,SAAS,EAAE;AAAA,QACrE;AAAA,MAAA,OACM;AACNC,sBAAAA,YAAY,UAAU;AAAA,MAAA;AAEX,kBAAA,eAAe,cAAc,WAAW,WAAW,WAAW,OAAO,WAAW,WAAW,IAAI;AAAA,IAAA;AAGrG,WAAA;AAAA,EAAA;AAAA,EAGR,OAAe,cAAc,WAAmC,WAAmB,WAA8C;AAC5H,QAAA,UAAU,WAAW,GAAG;AACpB,aAAA;AAAA,IAAA;AAED,WAAA,UAAU,KAAK,WAAW,SAAS;AAAA,EAAA;AAE5C;AAEA,MAAM,UAAU,CAAC,WAAmB,GAAG,OAAO,OAAO,CAAC,EAAE,YAAa,CAAA,GAAG,OAAO,UAAU,CAAC,CAAC;;"}
1
+ {"version":3,"file":"QueryGenerator.cjs","sources":["../../../../../packages/binding-legacy/src/core/QueryGenerator.ts"],"sourcesContent":["import {\n\tassertNever,\n\tEntityFieldMarkers,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tFieldMeta,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMarkerTreeRoot,\n\tPRIMARY_KEY_NAME,\n} from '@contember/binding-common'\nimport { ContentEntitySelection, ContentQuery, ContentQueryBuilder } from '@contember/client'\n\nexport class QueryGenerator {\n\tconstructor(\n\t\tprivate tree: MarkerTreeRoot,\n\t\tprivate qb: ContentQueryBuilder,\n\t) {}\n\n\tpublic getReadQuery(): Record<string, ContentQuery<any>> {\n\t\tconst result: Record<string, ContentQuery<any>> = {}\n\t\tfor (const [, subTreeMarker] of this.tree.subTrees) {\n\t\t\tif (subTreeMarker.parameters.isCreating) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (subTreeMarker instanceof EntitySubTreeMarker) {\n\t\t\t\tresult[subTreeMarker.placeholderName] = this.addGetQuery(subTreeMarker)\n\t\t\t} else if (subTreeMarker instanceof EntityListSubTreeMarker) {\n\t\t\t\tresult[subTreeMarker.placeholderName] = this.createListQuery(subTreeMarker)\n\t\t\t}\n\t\t}\n\t\treturn result\n\t}\n\n\tprivate addGetQuery(subTree: EntitySubTreeMarker): ContentQuery<any> {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\tthrow new Error()\n\t\t}\n\n\t\treturn this.qb.get(\n\t\t\tsubTree.entityName,\n\t\t\t{\n\t\t\t\tby: subTree.parameters.where,\n\t\t\t\tfilter: subTree.parameters.filter,\n\t\t\t},\n\t\t\tit => QueryGenerator.registerQueryPart(subTree.fields.markers, it),\n\t\t)\n\t}\n\n\tprivate createListQuery(subTree: EntityListSubTreeMarker): ContentQuery<any> {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\tthrow new Error()\n\t\t}\n\t\treturn this.qb.list(\n\t\t\tsubTree.entityName,\n\t\t\t{\n\t\t\t\tfilter: subTree.parameters.filter,\n\t\t\t\torderBy: subTree.parameters.orderBy,\n\t\t\t\toffset: subTree.parameters.offset,\n\t\t\t\tlimit: subTree.parameters.limit,\n\t\t\t},\n\t\t\tit => QueryGenerator.registerQueryPart(subTree.fields.markers, it),\n\t\t)\n\t}\n\n\tpublic static registerQueryPart(fields: EntityFieldMarkers, selection: ContentEntitySelection): ContentEntitySelection {\n\t\tselection = selection.$(PRIMARY_KEY_NAME).$('__typename')\n\n\t\tfor (const [, fieldValue] of fields) {\n\t\t\tif (fieldValue instanceof FieldMarker) {\n\t\t\t\tif (fieldValue.fieldName !== PRIMARY_KEY_NAME) {\n\t\t\t\t\tselection = selection.$(fieldValue.fieldName)\n\t\t\t\t}\n\t\t\t} else if (fieldValue instanceof HasOneRelationMarker) {\n\t\t\t\tconst relation = fieldValue.parameters\n\n\t\t\t\tif (relation.reducedBy) {\n\t\t\t\t\t// Assuming there's exactly one reducer field as enforced by MarkerTreeGenerator\n\t\t\t\t\tconst relationField = `${relation.field}By${ucfirst(Object.keys(relation.reducedBy)[0])}`\n\t\t\t\t\tselection = selection.$(\n\t\t\t\t\t\trelationField,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tby: relation.reducedBy,\n\t\t\t\t\t\t\tfilter: relation.filter,\n\t\t\t\t\t\t\tas: fieldValue.placeholderName,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tit => QueryGenerator.registerQueryPart(fieldValue.fields.markers, it),\n\t\t\t\t\t)\n\t\t\t\t} else {\n\t\t\t\t\tselection = selection.$(\n\t\t\t\t\t\trelation.field,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfilter: relation.filter,\n\t\t\t\t\t\t\tas: fieldValue.placeholderName,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tit => QueryGenerator.registerQueryPart(fieldValue.fields.markers, it),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else if (fieldValue instanceof HasManyRelationMarker) {\n\t\t\t\tconst relation = fieldValue.parameters\n\n\t\t\t\tselection = selection.$(\n\t\t\t\t\trelation.field,\n\t\t\t\t\t{\n\t\t\t\t\t\tas: fieldValue.placeholderName,\n\t\t\t\t\t\tfilter: relation.filter,\n\t\t\t\t\t\torderBy: relation.orderBy,\n\t\t\t\t\t\toffset: relation.offset,\n\t\t\t\t\t\tlimit: relation.limit,\n\t\t\t\t\t},\n\t\t\t\t\tit => QueryGenerator.registerQueryPart(fieldValue.fields.markers, it),\n\t\t\t\t)\n\t\t\t} else {\n\t\t\t\tassertNever(fieldValue)\n\t\t\t}\n\t\t\tselection = QueryGenerator.withMetaField(selection, fieldValue.parameters.field, fieldValue.parameters.meta)\n\t\t}\n\n\t\treturn selection\n\t}\n\n\tprivate static withMetaField(selection: ContentEntitySelection, fieldName: string, metaField: FieldMeta): ContentEntitySelection {\n\t\tif (metaField.length === 0) {\n\t\t\treturn selection\n\t\t}\n\t\treturn selection.meta(fieldName, metaField)\n\t}\n}\n\nconst ucfirst = (string: string) => `${string.charAt(0).toUpperCase()}${string.substring(1)}`\n"],"names":["EntitySubTreeMarker","EntityListSubTreeMarker","PRIMARY_KEY_NAME","FieldMarker","HasOneRelationMarker","HasManyRelationMarker","assertNever"],"mappings":";;;AAcO,MAAM,eAAe;AAAA,EAC3B,YACS,MACA,IACP;AAFO,SAAA,OAAA;AACA,SAAA,KAAA;AAAA,EACN;AAAA,EAEI,eAAkD;AACxD,UAAM,SAA4C,CAAA;AAClD,eAAW,CAAA,EAAG,aAAa,KAAK,KAAK,KAAK,UAAU;AACnD,UAAI,cAAc,WAAW,YAAY;AACxC;AAAA,MACD;AACA,UAAI,yBAAyBA,cAAAA,qBAAqB;AACjD,eAAO,cAAc,eAAe,IAAI,KAAK,YAAY,aAAa;AAAA,MACvE,WAAW,yBAAyBC,uCAAyB;AAC5D,eAAO,cAAc,eAAe,IAAI,KAAK,gBAAgB,aAAa;AAAA,MAC3E;AAAA,IACD;AACA,WAAO;AAAA,EACR;AAAA,EAEQ,YAAY,SAAiD;AACpE,QAAI,QAAQ,WAAW,YAAY;AAClC,YAAM,IAAI,MAAA;AAAA,IACX;AAEA,WAAO,KAAK,GAAG;AAAA,MACd,QAAQ;AAAA,MACR;AAAA,QACC,IAAI,QAAQ,WAAW;AAAA,QACvB,QAAQ,QAAQ,WAAW;AAAA,MAAA;AAAA,MAE5B,QAAM,eAAe,kBAAkB,QAAQ,OAAO,SAAS,EAAE;AAAA,IAAA;AAAA,EAEnE;AAAA,EAEQ,gBAAgB,SAAqD;AAC5E,QAAI,QAAQ,WAAW,YAAY;AAClC,YAAM,IAAI,MAAA;AAAA,IACX;AACA,WAAO,KAAK,GAAG;AAAA,MACd,QAAQ;AAAA,MACR;AAAA,QACC,QAAQ,QAAQ,WAAW;AAAA,QAC3B,SAAS,QAAQ,WAAW;AAAA,QAC5B,QAAQ,QAAQ,WAAW;AAAA,QAC3B,OAAO,QAAQ,WAAW;AAAA,MAAA;AAAA,M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AAA,QAAA;AAAA,MAEtE,OAAO;AACNC,sBAAAA,YAAY,UAAU;AAAA,MACvB;AACA,kBAAY,eAAe,cAAc,WAAW,WAAW,WAAW,OAAO,WAAW,WAAW,IAAI;AAAA,IAC5G;AAEA,WAAO;AAAA,EACR;AAAA,EAEA,OAAe,cAAc,WAAmC,WAAmB,WAA8C;AAChI,QAAI,UAAU,WAAW,GAAG;AAC3B,aAAO;AAAA,IACR;AACA,WAAO,UAAU,KAAK,WAAW,SAAS;AAAA,EAC3C;AACD;AAEA,MAAM,UAAU,CAAC,WAAmB,GAAG,OAAO,OAAO,CAAC,EAAE,YAAA,CAAa,GAAG,OAAO,UAAU,CAAC,CAAC;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"QueryGenerator.js","sources":["../../../../../packages/binding-legacy/src/core/QueryGenerator.ts"],"sourcesContent":["import {\n\tassertNever,\n\tEntityFieldMarkers,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker, FieldMeta,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMarkerTreeRoot,\n\tPRIMARY_KEY_NAME,\n} from '@contember/binding-common'\nimport { ContentEntitySelection, ContentQuery, ContentQueryBuilder } from '@contember/client'\n\n\nexport class QueryGenerator {\n\tconstructor(\n\t\tprivate tree: MarkerTreeRoot,\n\t\tprivate qb: ContentQueryBuilder,\n\t) {}\n\n\tpublic getReadQuery(): Record<string, ContentQuery<any>> {\n\t\tconst result: Record<string, ContentQuery<any>> = {}\n\t\tfor (const [, subTreeMarker] of this.tree.subTrees) {\n\t\t\tif (subTreeMarker.parameters.isCreating) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (subTreeMarker instanceof EntitySubTreeMarker) {\n\t\t\t\tresult[subTreeMarker.placeholderName] = this.addGetQuery(subTreeMarker)\n\t\t\t} else if (subTreeMarker instanceof EntityListSubTreeMarker) {\n\t\t\t\tresult[subTreeMarker.placeholderName] = this.createListQuery(subTreeMarker)\n\t\t\t}\n\t\t}\n\t\treturn result\n\t}\n\n\tprivate addGetQuery(subTree: EntitySubTreeMarker): ContentQuery<any> {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\tthrow new Error()\n\t\t}\n\n\t\treturn this.qb.get(\n\t\t\tsubTree.entityName,\n\t\t\t{\n\t\t\t\tby: subTree.parameters.where,\n\t\t\t\tfilter: subTree.parameters.filter,\n\t\t\t},\n\t\t\tit => QueryGenerator.registerQueryPart(subTree.fields.markers, it),\n\t\t)\n\t}\n\n\tprivate createListQuery(subTree: EntityListSubTreeMarker): ContentQuery<any> {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\tthrow new Error()\n\t\t}\n\t\treturn this.qb.list(\n\t\t\tsubTree.entityName,\n\t\t\t{\n\t\t\t\tfilter: subTree.parameters.filter,\n\t\t\t\torderBy: subTree.parameters.orderBy,\n\t\t\t\toffset: subTree.parameters.offset,\n\t\t\t\tlimit: subTree.parameters.limit,\n\t\t\t},\n\t\t\tit => QueryGenerator.registerQueryPart(subTree.fields.markers, it),\n\t\t)\n\t}\n\n\tpublic static registerQueryPart(fields: EntityFieldMarkers, selection: ContentEntitySelection): ContentEntitySelection {\n\t\tselection = selection.$(PRIMARY_KEY_NAME).$('__typename')\n\n\t\tfor (const [, fieldValue] of fields) {\n\t\t\tif (fieldValue instanceof FieldMarker) {\n\t\t\t\tif (fieldValue.fieldName !== PRIMARY_KEY_NAME) {\n\t\t\t\t\tselection = selection.$(fieldValue.fieldName)\n\t\t\t\t}\n\t\t\t} else if (fieldValue instanceof HasOneRelationMarker) {\n\t\t\t\tconst relation = fieldValue.parameters\n\n\t\t\t\tif (relation.reducedBy) {\n\t\t\t\t\t// Assuming there's exactly one reducer field as enforced by MarkerTreeGenerator\n\t\t\t\t\tconst relationField = `${relation.field}By${ucfirst(Object.keys(relation.reducedBy)[0])}`\n\t\t\t\t\tselection = selection.$(\n\t\t\t\t\t\trelationField,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tby: relation.reducedBy,\n\t\t\t\t\t\t\tfilter: relation.filter,\n\t\t\t\t\t\t\tas: fieldValue.placeholderName,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tit => QueryGenerator.registerQueryPart(fieldValue.fields.markers, it),\n\t\t\t\t\t)\n\t\t\t\t} else {\n\t\t\t\t\tselection = selection.$(\n\t\t\t\t\t\trelation.field,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfilter: relation.filter,\n\t\t\t\t\t\t\tas: fieldValue.placeholderName,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tit => QueryGenerator.registerQueryPart(fieldValue.fields.markers, it),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else if (fieldValue instanceof HasManyRelationMarker) {\n\t\t\t\tconst relation = fieldValue.parameters\n\n\n\t\t\t\tselection = selection.$(\n\t\t\t\t\trelation.field,\n\t\t\t\t\t{\n\t\t\t\t\t\tas: fieldValue.placeholderName,\n\t\t\t\t\t\tfilter: relation.filter,\n\t\t\t\t\t\torderBy: relation.orderBy,\n\t\t\t\t\t\toffset: relation.offset,\n\t\t\t\t\t\tlimit: relation.limit,\n\t\t\t\t\t},\n\t\t\t\t\tit => QueryGenerator.registerQueryPart(fieldValue.fields.markers, it),\n\t\t\t\t)\n\t\t\t} else {\n\t\t\t\tassertNever(fieldValue)\n\t\t\t}\n\t\t\tselection = QueryGenerator.withMetaField(selection, fieldValue.parameters.field, fieldValue.parameters.meta)\n\t\t}\n\n\t\treturn selection\n\t}\n\n\tprivate static withMetaField(selection: ContentEntitySelection, fieldName: string, metaField: FieldMeta): ContentEntitySelection {\n\t\tif (metaField.length === 0) {\n\t\t\treturn selection\n\t\t}\n\t\treturn selection.meta(fieldName, metaField)\n\t}\n}\n\nconst ucfirst = (string: string) => `${string.charAt(0).toUpperCase()}${string.substring(1)}`\n"],"names":[],"mappings":";AAcO,MAAM,eAAe;AAAA,EAC3B,YACS,MACA,IACP;AAFO,SAAA,OAAA;AACA,SAAA,KAAA;AAAA,EAAA;A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AA;AAE5C;AAEA,MAAM,UAAU,CAAC,WAAmB,GAAG,OAAO,OAAO,CAAC,EAAE,YAAa,CAAA,GAAG,OAAO,UAAU,CAAC,CAAC;"}
1
+ {"version":3,"file":"QueryGenerator.js","sources":["../../../../../packages/binding-legacy/src/core/QueryGenerator.ts"],"sourcesContent":["import {\n\tassertNever,\n\tEntityFieldMarkers,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tFieldMeta,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMarkerTreeRoot,\n\tPRIMARY_KEY_NAME,\n} from '@contember/binding-common'\nimport { ContentEntitySelection, ContentQuery, ContentQueryBuilder } from '@contember/client'\n\nexport class QueryGenerator {\n\tconstructor(\n\t\tprivate tree: MarkerTreeRoot,\n\t\tprivate qb: ContentQueryBuilder,\n\t) {}\n\n\tpublic getReadQuery(): Record<string, ContentQuery<any>> {\n\t\tconst result: Record<string, ContentQuery<any>> = {}\n\t\tfor (const [, subTreeMarker] of this.tree.subTrees) {\n\t\t\tif (subTreeMarker.parameters.isCreating) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (subTreeMarker instanceof EntitySubTreeMarker) {\n\t\t\t\tresult[subTreeMarker.placeholderName] = this.addGetQuery(subTreeMarker)\n\t\t\t} else if (subTreeMarker instanceof EntityListSubTreeMarker) {\n\t\t\t\tresult[subTreeMarker.placeholderName] = this.createListQuery(subTreeMarker)\n\t\t\t}\n\t\t}\n\t\treturn result\n\t}\n\n\tprivate addGetQuery(subTree: EntitySubTreeMarker): ContentQuery<any> {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\tthrow new Error()\n\t\t}\n\n\t\treturn this.qb.get(\n\t\t\tsubTree.entityName,\n\t\t\t{\n\t\t\t\tby: subTree.parameters.where,\n\t\t\t\tfilter: subTree.parameters.filter,\n\t\t\t},\n\t\t\tit => QueryGenerator.registerQueryPart(subTree.fields.markers, it),\n\t\t)\n\t}\n\n\tprivate createListQuery(subTree: EntityListSubTreeMarker): ContentQuery<any> {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\tthrow new Error()\n\t\t}\n\t\treturn this.qb.list(\n\t\t\tsubTree.entityName,\n\t\t\t{\n\t\t\t\tfilter: subTree.parameters.filter,\n\t\t\t\torderBy: subTree.parameters.orderBy,\n\t\t\t\toffset: subTree.parameters.offset,\n\t\t\t\tlimit: subTree.parameters.limit,\n\t\t\t},\n\t\t\tit => QueryGenerator.registerQueryPart(subTree.fields.markers, it),\n\t\t)\n\t}\n\n\tpublic static registerQueryPart(fields: EntityFieldMarkers, selection: ContentEntitySelection): ContentEntitySelection {\n\t\tselection = selection.$(PRIMARY_KEY_NAME).$('__typename')\n\n\t\tfor (const [, fieldValue] of fields) {\n\t\t\tif (fieldValue instanceof FieldMarker) {\n\t\t\t\tif (fieldValue.fieldName !== PRIMARY_KEY_NAME) {\n\t\t\t\t\tselection = selection.$(fieldValue.fieldName)\n\t\t\t\t}\n\t\t\t} else if (fieldValue instanceof HasOneRelationMarker) {\n\t\t\t\tconst relation = fieldValue.parameters\n\n\t\t\t\tif (relation.reducedBy) {\n\t\t\t\t\t// Assuming there's exactly one reducer field as enforced by MarkerTreeGenerator\n\t\t\t\t\tconst relationField = `${relation.field}By${ucfirst(Object.keys(relation.reducedBy)[0])}`\n\t\t\t\t\tselection = selection.$(\n\t\t\t\t\t\trelationField,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tby: relation.reducedBy,\n\t\t\t\t\t\t\tfilter: relation.filter,\n\t\t\t\t\t\t\tas: fieldValue.placeholderName,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tit => QueryGenerator.registerQueryPart(fieldValue.fields.markers, it),\n\t\t\t\t\t)\n\t\t\t\t} else {\n\t\t\t\t\tselection = selection.$(\n\t\t\t\t\t\trelation.field,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfilter: relation.filter,\n\t\t\t\t\t\t\tas: fieldValue.placeholderName,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tit => QueryGenerator.registerQueryPart(fieldValue.fields.markers, it),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else if (fieldValue instanceof HasManyRelationMarker) {\n\t\t\t\tconst relation = fieldValue.parameters\n\n\t\t\t\tselection = selection.$(\n\t\t\t\t\trelation.field,\n\t\t\t\t\t{\n\t\t\t\t\t\tas: fieldValue.placeholderName,\n\t\t\t\t\t\tfilter: relation.filter,\n\t\t\t\t\t\torderBy: relation.orderBy,\n\t\t\t\t\t\toffset: relation.offset,\n\t\t\t\t\t\tlimit: relation.limit,\n\t\t\t\t\t},\n\t\t\t\t\tit => QueryGenerator.registerQueryPart(fieldValue.fields.markers, it),\n\t\t\t\t)\n\t\t\t} else {\n\t\t\t\tassertNever(fieldValue)\n\t\t\t}\n\t\t\tselection = QueryGenerator.withMetaField(selection, fieldValue.parameters.field, fieldValue.parameters.meta)\n\t\t}\n\n\t\treturn selection\n\t}\n\n\tprivate static withMetaField(selection: ContentEntitySelection, fieldName: string, metaField: FieldMeta): ContentEntitySelection {\n\t\tif (metaField.length === 0) {\n\t\t\treturn selection\n\t\t}\n\t\treturn selection.meta(fieldName, metaField)\n\t}\n}\n\nconst ucfirst = (string: string) => `${string.charAt(0).toUpperCase()}${string.substring(1)}`\n"],"names":[],"mappings":";AAcO,MAAM,eAAe;AAAA,EAC3B,YACS,MACA,IACP;AAFO,SAAA,OAAA;AACA,SAAA,KAAA;AAAA,EACN;AAAA,EAEI,eAAkD;AACxD,UAAM,SAA4C,CAAA;AAClD,eAAW,CAAA,EAAG,aAAa,KAAK,KAAK,KAAK,UAAU;AACnD,UAAI,cAAc,WAAW,YAAY;AACxC;AAAA,MACD;AACA,UAAI,yBAAyB,qBAAqB;AACjD,eAAO,cAAc,eAAe,IAAI,KAAK,YAAY,aAAa;AAAA,MACvE,WAAW,yBAAyB,yBAAyB;AAC5D,eAAO,cAAc,eAAe,IAAI,KAAK,gBAAgB,aAAa;AAAA,MAC3E;AAAA,IAC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jB,IAAI,WAAW;AAAA,YAAA;AAAA,YAEhB,QAAM,eAAe,kBAAkB,WAAW,OAAO,SAAS,EAAE;AAAA,UAAA;AAAA,QAEtE,OAAO;AACN,sBAAY,UAAU;AAAA,YACrB,SAAS;AAAA,YACT;AAAA,cACC,QAAQ,SAAS;AAAA,cACjB,IAAI,WAAW;AAAA,YAAA;AAAA,YAEhB,QAAM,eAAe,kBAAkB,WAAW,OAAO,SAAS,EAAE;AAAA,UAAA;AAAA,QAEtE;AAAA,MACD,WAAW,sBAAsB,uBAAuB;AACvD,cAAM,WAAW,WAAW;AAE5B,oBAAY,UAAU;AAAA,UACrB,SAAS;AAAA,UACT;AAAA,YACC,IAAI,WAAW;AAAA,YACf,QAAQ,SAAS;AAAA,YACjB,SAAS,SAAS;AAAA,YAClB,QAAQ,SAAS;AAAA,YACjB,OAAO,SAAS;AAAA,UAAA;AAAA,UAEjB,QAAM,eAAe,kBAAkB,WAAW,OAAO,SAAS,EAAE;AAAA,QAAA;AAAA,MAEtE,OAAO;AACN,oBAAY,UAAU;AAAA,MACvB;AACA,kBAAY,eAAe,cAAc,WAAW,WAAW,WAAW,OAAO,WAAW,WAAW,IAAI;AAAA,IAC5G;AAEA,WAAO;AAAA,EACR;AAAA,EAEA,OAAe,cAAc,WAAmC,WAAmB,WAA8C;AAChI,QAAI,UAAU,WAAW,GAAG;AAC3B,aAAO;AAAA,IACR;AACA,WAAO,UAAU,KAAK,WAAW,SAAS;AAAA,EAC3C;AACD;AAEA,MAAM,UAAU,CAAC,WAAmB,GAAG,OAAO,OAAO,CAAC,EAAE,YAAA,CAAa,GAAG,OAAO,UAAU,CAAC,CAAC;"}
@@ -1 +1 @@
1
- {"version":3,"file":"RealmKeyGenerator.cjs","sources":["../../../../../packages/binding-legacy/src/core/RealmKeyGenerator.ts"],"sourcesContent":["import { ClientGeneratedUuid, EntityFieldMarkersContainer, EntityId, EntityRealmKey, PlaceholderName, RuntimeId } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { EntityListState, EntityRealmBlueprint, EntityRealmState } from './state'\nimport { WeakIdCache } from '@contember/utilities'\n\nconst GLUE = '--'\nconst keyEndRegex = new RegExp(`${GLUE}\\\\d+$`)\n\nexport class RealmKeyGenerator {\n\t// We're using this id, and not, well, the entity id, because it *SIGNIFICANTLY* reduces the complexity of\n\t// entity id changes.\n\tprivate static parentStateIdCache = new WeakIdCache<EntityRealmState | EntityListState>()\n\tprivate static markerIdCache = new WeakIdCache<EntityFieldMarkersContainer>()\n\n\tpublic static vaguelyAppearsToBeAKey(candidate: string): boolean {\n\t\t// This is just for error messages. No need to be super precise.\n\t\treturn candidate.startsWith(`key${GLUE}`) && keyEndRegex.test(candidate)\n\t}\n\n\tprivate static generateKey(\n\t\tparent: EntityRealmState | EntityListState | undefined,\n\t\tplaceholderName: PlaceholderName,\n\t\tid: string,\n\t\tcontainer: EntityFieldMarkersContainer,\n\t): EntityRealmKey {\n\t\t// The static prefix serves to really emphasize that this is *NOT* just a uuid\n\t\treturn `key${GLUE}${this.parentStateIdCache.getOptionalKeyId(\n\t\t\tparent,\n\t\t)}${GLUE}${placeholderName}${id}${GLUE}${this.markerIdCache.getId(container)}`\n\t}\n\n\tpublic static getRealmKey(id: RuntimeId, blueprint: EntityRealmBlueprint): EntityRealmKey {\n\t\tconst parent = blueprint.parent\n\t\tlet placeholderName: PlaceholderName\n\n\t\tif (blueprint.type === 'subTree') {\n\t\t\tplaceholderName = blueprint.marker.placeholderName\n\t\t} else if (blueprint.type === 'listEntity') {\n\t\t\tplaceholderName = blueprint.parent.blueprint.marker.placeholderName\n\t\t} else if (blueprint.type === 'hasOne') {\n\t\t\tplaceholderName = blueprint.marker.placeholderName\n\t\t} else {\n\t\t\treturn assertNever(blueprint)\n\t\t}\n\t\treturn this.generateKey(\n\t\t\tparent,\n\t\t\tplaceholderName,\n\t\t\t(id instanceof ClientGeneratedUuid ? 'C-' : '') + id.value.toString(),\n\t\t\tblueprint.type === 'listEntity' ? blueprint.parent.blueprint.marker.fields : blueprint.marker.fields,\n\t\t)\n\t}\n}\n"],"names":["assertNever","ClientGeneratedUuid","WeakIdCache"],"mappings":";;;;;;;AAKA,MAAM,OAAO;AACb,MAAM,cAAc,IAAI,OAAO,GAAG,IAAI,OAAO;AAEtC,MAAM,kBAAkB;AAAA,EAM9B,OAAc,uBAAuB,WAA4B;AAEzD,WAAA,UAAU,WAAW,MAAM,IAAI,EAAE,KAAK,YAAY,KAAK,SAAS;AAAA,EAAA;AAAA,EAGxE,OAAe,YACd,QACA,iBACA,IACA,WACiB;AAEjB,WAAO,MAAM,IAAI,GAAG,KAAK,mBAAmB;AAAA,MAC3C;AAAA,IACA,CAAA,GAAG,IAAI,GAAG,eAAe,GAAG,EAAE,GAAG,IAAI,GAAG,KAAK,cAAc,MAAM,SAAS,CAAC;AAAA,EAAA;AAAA,EAG7E,OAAc,YAAY,IAAe,WAAiD;AACzF,UAAM,SAAS,UAAU;AACrB,QAAA;AAEA,QAAA,UAAU,SAAS,WAAW;AACjC,wBAAkB,UAAU,OAAO;AAAA,IAAA,WACzB,UAAU,SAAS,cAAc;AACzB,wBAAA,UAAU,OAAO,UAAU,OAAO;AAAA,IAAA,WAC1C,UAAU,SAAS,UAAU;AACvC,wBAAkB,UAAU,OAAO;AAAA,IAAA,OAC7B;AACN,aAAOA,cAAAA,YAAY,SAAS;AAAA,IAAA;AAE7B,WAAO,KAAK;AAAA,MACX;AAAA,MACA;AAAA,OACC,cAAcC,cAAAA,sBAAsB,OAAO,MAAM,GAAG,MAAM,SAAS;AAAA,MACpE,UAAU,SAAS,eAAe,UAAU,OAAO,UAAU,OAAO,SAAS,UAAU,OAAO;AAAA,IAC/F;AAAA,EAAA;AAEF;AAxCC,cAHY,mBAGG,sBAAqB,IAAIC,UAAAA,aAAgD;AACxF,cAJY,mBAIG,iBAAgB,IAAIA,UAAAA,aAAyC;;"}
1
+ {"version":3,"file":"RealmKeyGenerator.cjs","sources":["../../../../../packages/binding-legacy/src/core/RealmKeyGenerator.ts"],"sourcesContent":["import { ClientGeneratedUuid, EntityFieldMarkersContainer, EntityId, EntityRealmKey, PlaceholderName, RuntimeId } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { EntityListState, EntityRealmBlueprint, EntityRealmState } from './state'\nimport { WeakIdCache } from '@contember/utilities'\n\nconst GLUE = '--'\nconst keyEndRegex = new RegExp(`${GLUE}\\\\d+$`)\n\nexport class RealmKeyGenerator {\n\t// We're using this id, and not, well, the entity id, because it *SIGNIFICANTLY* reduces the complexity of\n\t// entity id changes.\n\tprivate static parentStateIdCache = new WeakIdCache<EntityRealmState | EntityListState>()\n\tprivate static markerIdCache = new WeakIdCache<EntityFieldMarkersContainer>()\n\n\tpublic static vaguelyAppearsToBeAKey(candidate: string): boolean {\n\t\t// This is just for error messages. No need to be super precise.\n\t\treturn candidate.startsWith(`key${GLUE}`) && keyEndRegex.test(candidate)\n\t}\n\n\tprivate static generateKey(\n\t\tparent: EntityRealmState | EntityListState | undefined,\n\t\tplaceholderName: PlaceholderName,\n\t\tid: string,\n\t\tcontainer: EntityFieldMarkersContainer,\n\t): EntityRealmKey {\n\t\t// The static prefix serves to really emphasize that this is *NOT* just a uuid\n\t\treturn `key${GLUE}${\n\t\t\tthis.parentStateIdCache.getOptionalKeyId(\n\t\t\t\tparent,\n\t\t\t)\n\t\t}${GLUE}${placeholderName}${id}${GLUE}${this.markerIdCache.getId(container)}`\n\t}\n\n\tpublic static getRealmKey(id: RuntimeId, blueprint: EntityRealmBlueprint): EntityRealmKey {\n\t\tconst parent = blueprint.parent\n\t\tlet placeholderName: PlaceholderName\n\n\t\tif (blueprint.type === 'subTree') {\n\t\t\tplaceholderName = blueprint.marker.placeholderName\n\t\t} else if (blueprint.type === 'listEntity') {\n\t\t\tplaceholderName = blueprint.parent.blueprint.marker.placeholderName\n\t\t} else if (blueprint.type === 'hasOne') {\n\t\t\tplaceholderName = blueprint.marker.placeholderName\n\t\t} else {\n\t\t\treturn assertNever(blueprint)\n\t\t}\n\t\treturn this.generateKey(\n\t\t\tparent,\n\t\t\tplaceholderName,\n\t\t\t(id instanceof ClientGeneratedUuid ? 'C-' : '') + id.value.toString(),\n\t\t\tblueprint.type === 'listEntity' ? blueprint.parent.blueprint.marker.fields : blueprint.marker.fields,\n\t\t)\n\t}\n}\n"],"names":["assertNever","ClientGeneratedUuid","WeakIdCache"],"mappings":";;;;;;;AAKA,MAAM,OAAO;AACb,MAAM,cAAc,IAAI,OAAO,GAAG,IAAI,OAAO;AAEtC,MAAM,kBAAkB;AAAA,EAM9B,OAAc,uBAAuB,WAA4B;AAEhE,WAAO,UAAU,WAAW,MAAM,IAAI,EAAE,KAAK,YAAY,KAAK,SAAS;AAAA,EACxE;AAAA,EAEA,OAAe,YACd,QACA,iBACA,IACA,WACiB;AAEjB,WAAO,MAAM,IAAI,GAChB,KAAK,mBAAmB;AAAA,MACvB;AAAA,IAAA,CAEF,GAAG,IAAI,GAAG,eAAe,GAAG,EAAE,GAAG,IAAI,GAAG,KAAK,cAAc,MAAM,SAAS,CAAC;AAAA,EAC5E;AAAA,EAEA,OAAc,YAAY,IAAe,WAAiD;AACzF,UAAM,SAAS,UAAU;AACzB,QAAI;AAEJ,QAAI,UAAU,SAAS,WAAW;AACjC,wBAAkB,UAAU,OAAO;AAAA,IACpC,WAAW,UAAU,SAAS,cAAc;AAC3C,wBAAkB,UAAU,OAAO,UAAU,OAAO;AAAA,IACrD,WAAW,UAAU,SAAS,UAAU;AACvC,wBAAkB,UAAU,OAAO;AAAA,IACpC,OAAO;AACN,aAAOA,cAAAA,YAAY,SAAS;AAAA,IAC7B;AACA,WAAO,KAAK;AAAA,MACX;AAAA,MACA;AAAA,OACC,cAAcC,cAAAA,sBAAsB,OAAO,MAAM,GAAG,MAAM,SAAA;AAAA,MAC3D,UAAU,SAAS,eAAe,UAAU,OAAO,UAAU,OAAO,SAAS,UAAU,OAAO;AAAA,IAAA;AAAA,EAEhG;AACD;AA1CC,cAHY,mBAGG,sBAAqB,IAAIC,UAAAA,aAAgD;AACxF,cAJY,mBAIG,iBAAgB,IAAIA,UAAAA,aAAyC;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"RealmKeyGenerator.js","sources":["../../../../../packages/binding-legacy/src/core/RealmKeyGenerator.ts"],"sourcesContent":["import { ClientGeneratedUuid, EntityFieldMarkersContainer, EntityId, EntityRealmKey, PlaceholderName, RuntimeId } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { EntityListState, EntityRealmBlueprint, EntityRealmState } from './state'\nimport { WeakIdCache } from '@contember/utilities'\n\nconst GLUE = '--'\nconst keyEndRegex = new RegExp(`${GLUE}\\\\d+$`)\n\nexport class RealmKeyGenerator {\n\t// We're using this id, and not, well, the entity id, because it *SIGNIFICANTLY* reduces the complexity of\n\t// entity id changes.\n\tprivate static parentStateIdCache = new WeakIdCache<EntityRealmState | EntityListState>()\n\tprivate static markerIdCache = new WeakIdCache<EntityFieldMarkersContainer>()\n\n\tpublic static vaguelyAppearsToBeAKey(candidate: string): boolean {\n\t\t// This is just for error messages. No need to be super precise.\n\t\treturn candidate.startsWith(`key${GLUE}`) && keyEndRegex.test(candidate)\n\t}\n\n\tprivate static generateKey(\n\t\tparent: EntityRealmState | EntityListState | undefined,\n\t\tplaceholderName: PlaceholderName,\n\t\tid: string,\n\t\tcontainer: EntityFieldMarkersContainer,\n\t): EntityRealmKey {\n\t\t// The static prefix serves to really emphasize that this is *NOT* just a uuid\n\t\treturn `key${GLUE}${this.parentStateIdCache.getOptionalKeyId(\n\t\t\tparent,\n\t\t)}${GLUE}${placeholderName}${id}${GLUE}${this.markerIdCache.getId(container)}`\n\t}\n\n\tpublic static getRealmKey(id: RuntimeId, blueprint: EntityRealmBlueprint): EntityRealmKey {\n\t\tconst parent = blueprint.parent\n\t\tlet placeholderName: PlaceholderName\n\n\t\tif (blueprint.type === 'subTree') {\n\t\t\tplaceholderName = blueprint.marker.placeholderName\n\t\t} else if (blueprint.type === 'listEntity') {\n\t\t\tplaceholderName = blueprint.parent.blueprint.marker.placeholderName\n\t\t} else if (blueprint.type === 'hasOne') {\n\t\t\tplaceholderName = blueprint.marker.placeholderName\n\t\t} else {\n\t\t\treturn assertNever(blueprint)\n\t\t}\n\t\treturn this.generateKey(\n\t\t\tparent,\n\t\t\tplaceholderName,\n\t\t\t(id instanceof ClientGeneratedUuid ? 'C-' : '') + id.value.toString(),\n\t\t\tblueprint.type === 'listEntity' ? blueprint.parent.blueprint.marker.fields : blueprint.marker.fields,\n\t\t)\n\t}\n}\n"],"names":[],"mappings":";;;;;AAKA,MAAM,OAAO;AACb,MAAM,cAAc,IAAI,OAAO,GAAG,IAAI,OAAO;AAEtC,MAAM,kBAAkB;AAAA,EAM9B,OAAc,uBAAuB,WAA4B;AAEzD,WAAA,UAAU,WAAW,MAAM,IAAI,EAAE,KAAK,YAAY,KAAK,SAAS;AAAA,EAAA;AAAA,EAGxE,OAAe,YACd,QACA,iBACA,IACA,WACiB;AAEjB,WAAO,MAAM,IAAI,GAAG,KAAK,mBAAmB;AAAA,MAC3C;AAAA,IACA,CAAA,GAAG,IAAI,GAAG,eAAe,GAAG,EAAE,GAAG,IAAI,GAAG,KAAK,cAAc,MAAM,SAAS,CAAC;AAAA,EAAA;AAAA,EAG7E,OAAc,YAAY,IAAe,WAAiD;AACzF,UAAM,SAAS,UAAU;AACrB,QAAA;AAEA,QAAA,UAAU,SAAS,WAAW;AACjC,wBAAkB,UAAU,OAAO;AAAA,IAAA,WACzB,UAAU,SAAS,cAAc;AACzB,wBAAA,UAAU,OAAO,UAAU,OAAO;AAAA,IAAA,WAC1C,UAAU,SAAS,UAAU;AACvC,wBAAkB,UAAU,OAAO;AAAA,IAAA,OAC7B;AACN,aAAO,YAAY,SAAS;AAAA,IAAA;AAE7B,WAAO,KAAK;AAAA,MACX;AAAA,MACA;AAAA,OACC,cAAc,sBAAsB,OAAO,MAAM,GAAG,MAAM,SAAS;AAAA,MACpE,UAAU,SAAS,eAAe,UAAU,OAAO,UAAU,OAAO,SAAS,UAAU,OAAO;AAAA,IAC/F;AAAA,EAAA;AAEF;AAxCC,cAHY,mBAGG,sBAAqB,IAAI,aAAgD;AACxF,cAJY,mBAIG,iBAAgB,IAAI,aAAyC;"}
1
+ {"version":3,"file":"RealmKeyGenerator.js","sources":["../../../../../packages/binding-legacy/src/core/RealmKeyGenerator.ts"],"sourcesContent":["import { ClientGeneratedUuid, EntityFieldMarkersContainer, EntityId, EntityRealmKey, PlaceholderName, RuntimeId } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { EntityListState, EntityRealmBlueprint, EntityRealmState } from './state'\nimport { WeakIdCache } from '@contember/utilities'\n\nconst GLUE = '--'\nconst keyEndRegex = new RegExp(`${GLUE}\\\\d+$`)\n\nexport class RealmKeyGenerator {\n\t// We're using this id, and not, well, the entity id, because it *SIGNIFICANTLY* reduces the complexity of\n\t// entity id changes.\n\tprivate static parentStateIdCache = new WeakIdCache<EntityRealmState | EntityListState>()\n\tprivate static markerIdCache = new WeakIdCache<EntityFieldMarkersContainer>()\n\n\tpublic static vaguelyAppearsToBeAKey(candidate: string): boolean {\n\t\t// This is just for error messages. No need to be super precise.\n\t\treturn candidate.startsWith(`key${GLUE}`) && keyEndRegex.test(candidate)\n\t}\n\n\tprivate static generateKey(\n\t\tparent: EntityRealmState | EntityListState | undefined,\n\t\tplaceholderName: PlaceholderName,\n\t\tid: string,\n\t\tcontainer: EntityFieldMarkersContainer,\n\t): EntityRealmKey {\n\t\t// The static prefix serves to really emphasize that this is *NOT* just a uuid\n\t\treturn `key${GLUE}${\n\t\t\tthis.parentStateIdCache.getOptionalKeyId(\n\t\t\t\tparent,\n\t\t\t)\n\t\t}${GLUE}${placeholderName}${id}${GLUE}${this.markerIdCache.getId(container)}`\n\t}\n\n\tpublic static getRealmKey(id: RuntimeId, blueprint: EntityRealmBlueprint): EntityRealmKey {\n\t\tconst parent = blueprint.parent\n\t\tlet placeholderName: PlaceholderName\n\n\t\tif (blueprint.type === 'subTree') {\n\t\t\tplaceholderName = blueprint.marker.placeholderName\n\t\t} else if (blueprint.type === 'listEntity') {\n\t\t\tplaceholderName = blueprint.parent.blueprint.marker.placeholderName\n\t\t} else if (blueprint.type === 'hasOne') {\n\t\t\tplaceholderName = blueprint.marker.placeholderName\n\t\t} else {\n\t\t\treturn assertNever(blueprint)\n\t\t}\n\t\treturn this.generateKey(\n\t\t\tparent,\n\t\t\tplaceholderName,\n\t\t\t(id instanceof ClientGeneratedUuid ? 'C-' : '') + id.value.toString(),\n\t\t\tblueprint.type === 'listEntity' ? blueprint.parent.blueprint.marker.fields : blueprint.marker.fields,\n\t\t)\n\t}\n}\n"],"names":[],"mappings":";;;;;AAKA,MAAM,OAAO;AACb,MAAM,cAAc,IAAI,OAAO,GAAG,IAAI,OAAO;AAEtC,MAAM,kBAAkB;AAAA,EAM9B,OAAc,uBAAuB,WAA4B;AAEhE,WAAO,UAAU,WAAW,MAAM,IAAI,EAAE,KAAK,YAAY,KAAK,SAAS;AAAA,EACxE;AAAA,EAEA,OAAe,YACd,QACA,iBACA,IACA,WACiB;AAEjB,WAAO,MAAM,IAAI,GAChB,KAAK,mBAAmB;AAAA,MACvB;AAAA,IAAA,CAEF,GAAG,IAAI,GAAG,eAAe,GAAG,EAAE,GAAG,IAAI,GAAG,KAAK,cAAc,MAAM,SAAS,CAAC;AAAA,EAC5E;AAAA,EAEA,OAAc,YAAY,IAAe,WAAiD;AACzF,UAAM,SAAS,UAAU;AACzB,QAAI;AAEJ,QAAI,UAAU,SAAS,WAAW;AACjC,wBAAkB,UAAU,OAAO;AAAA,IACpC,WAAW,UAAU,SAAS,cAAc;AAC3C,wBAAkB,UAAU,OAAO,UAAU,OAAO;AAAA,IACrD,WAAW,UAAU,SAAS,UAAU;AACvC,wBAAkB,UAAU,OAAO;AAAA,IACpC,OAAO;AACN,aAAO,YAAY,SAAS;AAAA,IAC7B;AACA,WAAO,KAAK;AAAA,MACX;AAAA,MACA;AAAA,OACC,cAAc,sBAAsB,OAAO,MAAM,GAAG,MAAM,SAAA;AAAA,MAC3D,UAAU,SAAS,eAAe,UAAU,OAAO,UAAU,OAAO,SAAS,UAAU,OAAO;AAAA,IAAA;AAAA,EAEhG;AACD;AA1CC,cAHY,mBAGG,sBAAqB,IAAI,aAAgD;AACxF,cAJY,mBAIG,iBAAgB,IAAI,aAAyC;"}
@@ -1 +1 @@
1
- {"version":3,"file":"RequestResponseNormalizer.cjs","sources":["../../../../../packages/binding-legacy/src/core/RequestResponseNormalizer.ts"],"sourcesContent":["import {\n\tReceivedDataTree,\n\tReceivedEntityData,\n\tServerId,\n} from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { PRIMARY_KEY_NAME } from '@contember/binding-common'\nimport { EntityId } from '@contember/binding-common'\nimport {\n\tEntityFieldMarkers,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMarkerTreeRoot,\n} from '@contember/binding-common'\nimport { SubMutationOperation } from './MutationGenerator'\nimport { assertNever } from '@contember/binding-common'\nimport { NormalizedPersistedData, PersistedEntityDataStore, SingleEntityPersistedData } from '../accessorTree'\n\nexport class RequestResponseNormalizer {\n\tpublic static mergeInQueryResponse(\n\t\toriginal: NormalizedPersistedData,\n\t\tnewPersistedData: ReceivedDataTree,\n\t\tmarkerTree: MarkerTreeRoot,\n\t): void {\n\t\tconst { subTreeDataStore, persistedEntityDataStore } = original\n\n\t\tfor (const [placeholder, marker] of markerTree.subTrees) {\n\t\t\tconst data = newPersistedData[placeholder] ?? null\n\t\t\tif (marker instanceof EntitySubTreeMarker) {\n\t\t\t\tif (data === null) {\n\t\t\t\t\tsubTreeDataStore.delete(placeholder)\n\t\t\t\t} else {\n\t\t\t\t\tif (Array.isArray(data)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tconst id = this.processEntityData(persistedEntityDataStore, data, marker.fields.markers)\n\t\t\t\t\tsubTreeDataStore.set(placeholder, id)\n\t\t\t\t}\n\t\t\t} else if (marker instanceof EntityListSubTreeMarker) {\n\t\t\t\tconst subTreeListIds = new Set<EntityId>()\n\t\t\t\tif (!Array.isArray(data)) {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tfor (const row of data) {\n\t\t\t\t\tconst id = this.processEntityData(persistedEntityDataStore, row, marker.fields.markers)\n\t\t\t\t\tsubTreeListIds.add(id.value)\n\t\t\t\t}\n\t\t\t\tsubTreeDataStore.set(placeholder, subTreeListIds)\n\t\t\t} else {\n\t\t\t\tthrow new BindingError()\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic static mergeInMutationResponse(\n\t\toriginal: NormalizedPersistedData,\n\t\tnewPersistedData: ReceivedDataTree,\n\t\toperations: SubMutationOperation[],\n\t): void {\n\n\t\tconst { subTreeDataStore, persistedEntityDataStore } = original\n\n\t\tfor (const operation of operations) {\n\t\t\tconst { subTreePlaceholder, subTreeType, alias } = operation\n\n\t\t\tconst receivedData = newPersistedData[alias]\n\n\t\t\t// probably multiple operations removing same entity, resulting in no data for some of them\n\t\t\tif (!receivedData) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (Array.isArray(receivedData)) {\n\t\t\t\tthrow new BindingError()\n\t\t\t}\n\n\t\t\tif (operation.type === 'update' || operation.type === 'create') {\n\t\t\t\tconst id = this.processEntityData(persistedEntityDataStore, receivedData, operation.markers)\n\t\t\t\tif (subTreeType === 'single') {\n\t\t\t\t\tsubTreeDataStore.set(subTreePlaceholder, id)\n\t\t\t\t} else if (subTreeType === 'list') {\n\t\t\t\t\tconst list = subTreeDataStore.get(subTreePlaceholder)\n\n\t\t\t\t\tif (list instanceof Set) {\n\t\t\t\t\t\t// TODO this is somewhat dubious because we're essentially just guessing the order of the entities\n\t\t\t\t\t\t//\t\tand just carelessly put the new one at the end.\n\t\t\t\t\t\tlist.add(id.value)\n\t\t\t\t\t} else if (list === undefined) {\n\t\t\t\t\t\t// That's fine. This is probably just an isCreating sub-tree.\n\t\t\t\t\t\tsubTreeDataStore.set(subTreePlaceholder, new Set([id.value]))\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\treturn assertNever(subTreeType)\n\t\t\t\t}\n\t\t\t} else if (operation.type === 'delete') {\n\t\t\t\tconst id = this.extractId(receivedData)\n\t\t\t\t// TODO there are potentially some references to entityId that this whole process won't quite remove.\n\t\t\t\t//\t\tThat's a memory leak. Probably not particularly severe in most cases but still.\n\t\t\t\tpersistedEntityDataStore.delete(id.uniqueValue)\n\t\t\t\tif (subTreeType === 'single') {\n\t\t\t\t\tsubTreeDataStore.delete(subTreePlaceholder)\n\t\t\t\t} else if (subTreeType === 'list') {\n\t\t\t\t\tconst list = subTreeDataStore.get(subTreePlaceholder)\n\n\t\t\t\t\tif (!(list instanceof Set)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tlist.delete(id.value)\n\t\t\t\t} else {\n\t\t\t\t\treturn assertNever(subTreeType)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\treturn assertNever(operation)\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate static processEntityData(entityMap: PersistedEntityDataStore, entityData: ReceivedEntityData, fields: EntityFieldMarkers): ServerId {\n\t\tconst id = this.extractId(entityData)\n\t\tconst presentEntityData: SingleEntityPersistedData = entityMap.get(id.uniqueValue) ?? new Map()\n\t\tentityMap.set(id.uniqueValue, presentEntityData)\n\n\t\tfor (const [placeholder, field] of fields) {\n\t\t\tconst value = entityData[placeholder]\n\t\t\tconst metaFlags = {\n\t\t\t\treadable: entityData._meta?.[field.parameters.field]?.readable ?? undefined,\n\t\t\t\tupdatable: entityData._meta?.[field.parameters.field]?.updatable ?? undefined,\n\t\t\t}\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\tvalue,\n\t\t\t\t\t...metaFlags,\n\t\t\t\t})\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tif (value === null) {\n\t\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\t\tvalue: null,\n\t\t\t\t\t\t...metaFlags,\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\t\tvalue: this.processEntityData(entityMap, value as ReceivedEntityData, field.fields.markers),\n\t\t\t\t\t\t...metaFlags,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\tconst ids = presentEntityData.get(placeholder)?.value ?? new Set()\n\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\tvalue: ids,\n\t\t\t\t\t...metaFlags,\n\t\t\t\t})\n\t\t\t\tif (!(ids instanceof Set)) {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tids.clear()\n\t\t\t\tfor (const row of value as ReceivedEntityData[]) {\n\t\t\t\t\tconst id = this.processEntityData(entityMap, row, field.fields.markers)\n\t\t\t\t\tids.add(id.value)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tthrow new Error()\n\t\t\t}\n\t\t}\n\n\t\treturn id\n\t}\n\n\tprivate static extractId(entityData: ReceivedEntityData): ServerId {\n\t\tconst primaryKey: EntityId | undefined = entityData[PRIMARY_KEY_NAME]\n\n\t\tif (primaryKey === undefined) {\n\t\t\tthrow new BindingError(`The server has responded with an entity that lacks a primary key.`)\n\t\t}\n\n\t\treturn new ServerId(primaryKey, entityData.__typename)\n\t}\n}\n"],"names":["EntitySubTreeMarker","BindingError","EntityListSubTreeMarker","assertNever","FieldMarker","HasOneRelationMarker","HasManyRelationMarker","id","PRIMARY_KEY_NAME","ServerId"],"mappings":";;;AAqBO,MAAM,0BAA0B;AAAA,EACtC,OAAc,qBACb,UACA,kBACA,YACO;AACD,UAAA,EAA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cjs","sources":["../../../../../packages/binding-legacy/src/core/RequestResponseNormalizer.ts"],"sourcesContent":["import { ReceivedDataTree, ReceivedEntityData, ServerId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { PRIMARY_KEY_NAME } from '@contember/binding-common'\nimport { EntityId } from '@contember/binding-common'\nimport {\n\tEntityFieldMarkers,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMarkerTreeRoot,\n} from '@contember/binding-common'\nimport { SubMutationOperation } from './MutationGenerator'\nimport { assertNever } from '@contember/binding-common'\nimport { NormalizedPersistedData, PersistedEntityDataStore, SingleEntityPersistedData } from '../accessorTree'\n\nexport class RequestResponseNormalizer {\n\tpublic static mergeInQueryResponse(\n\t\toriginal: NormalizedPersistedData,\n\t\tnewPersistedData: ReceivedDataTree,\n\t\tmarkerTree: MarkerTreeRoot,\n\t): void {\n\t\tconst { subTreeDataStore, persistedEntityDataStore } = original\n\n\t\tfor (const [placeholder, marker] of markerTree.subTrees) {\n\t\t\tconst data = newPersistedData[placeholder] ?? null\n\t\t\tif (marker instanceof EntitySubTreeMarker) {\n\t\t\t\tif (data === null) {\n\t\t\t\t\tsubTreeDataStore.delete(placeholder)\n\t\t\t\t} else {\n\t\t\t\t\tif (Array.isArray(data)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tconst id = this.processEntityData(persistedEntityDataStore, data, marker.fields.markers)\n\t\t\t\t\tsubTreeDataStore.set(placeholder, id)\n\t\t\t\t}\n\t\t\t} else if (marker instanceof EntityListSubTreeMarker) {\n\t\t\t\tconst subTreeListIds = new Set<EntityId>()\n\t\t\t\tif (!Array.isArray(data)) {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tfor (const row of data) {\n\t\t\t\t\tconst id = this.processEntityData(persistedEntityDataStore, row, marker.fields.markers)\n\t\t\t\t\tsubTreeListIds.add(id.value)\n\t\t\t\t}\n\t\t\t\tsubTreeDataStore.set(placeholder, subTreeListIds)\n\t\t\t} else {\n\t\t\t\tthrow new BindingError()\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic static mergeInMutationResponse(\n\t\toriginal: NormalizedPersistedData,\n\t\tnewPersistedData: ReceivedDataTree,\n\t\toperations: SubMutationOperation[],\n\t): void {\n\t\tconst { subTreeDataStore, persistedEntityDataStore } = original\n\n\t\tfor (const operation of operations) {\n\t\t\tconst { subTreePlaceholder, subTreeType, alias } = operation\n\n\t\t\tconst receivedData = newPersistedData[alias]\n\n\t\t\t// probably multiple operations removing same entity, resulting in no data for some of them\n\t\t\tif (!receivedData) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (Array.isArray(receivedData)) {\n\t\t\t\tthrow new BindingError()\n\t\t\t}\n\n\t\t\tif (operation.type === 'update' || operation.type === 'create') {\n\t\t\t\tconst id = this.processEntityData(persistedEntityDataStore, receivedData, operation.markers)\n\t\t\t\tif (subTreeType === 'single') {\n\t\t\t\t\tsubTreeDataStore.set(subTreePlaceholder, id)\n\t\t\t\t} else if (subTreeType === 'list') {\n\t\t\t\t\tconst list = subTreeDataStore.get(subTreePlaceholder)\n\n\t\t\t\t\tif (list instanceof Set) {\n\t\t\t\t\t\t// TODO this is somewhat dubious because we're essentially just guessing the order of the entities\n\t\t\t\t\t\t// \t\tand just carelessly put the new one at the end.\n\t\t\t\t\t\tlist.add(id.value)\n\t\t\t\t\t} else if (list === undefined) {\n\t\t\t\t\t\t// That's fine. This is probably just an isCreating sub-tree.\n\t\t\t\t\t\tsubTreeDataStore.set(subTreePlaceholder, new Set([id.value]))\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\treturn assertNever(subTreeType)\n\t\t\t\t}\n\t\t\t} else if (operation.type === 'delete') {\n\t\t\t\tconst id = this.extractId(receivedData)\n\t\t\t\t// TODO there are potentially some references to entityId that this whole process won't quite remove.\n\t\t\t\t// \t\tThat's a memory leak. Probably not particularly severe in most cases but still.\n\t\t\t\tpersistedEntityDataStore.delete(id.uniqueValue)\n\t\t\t\tif (subTreeType === 'single') {\n\t\t\t\t\tsubTreeDataStore.delete(subTreePlaceholder)\n\t\t\t\t} else if (subTreeType === 'list') {\n\t\t\t\t\tconst list = subTreeDataStore.get(subTreePlaceholder)\n\n\t\t\t\t\tif (!(list instanceof Set)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tlist.delete(id.value)\n\t\t\t\t} else {\n\t\t\t\t\treturn assertNever(subTreeType)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\treturn assertNever(operation)\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate static processEntityData(entityMap: PersistedEntityDataStore, entityData: ReceivedEntityData, fields: EntityFieldMarkers): ServerId {\n\t\tconst id = this.extractId(entityData)\n\t\tconst presentEntityData: SingleEntityPersistedData = entityMap.get(id.uniqueValue) ?? new Map()\n\t\tentityMap.set(id.uniqueValue, presentEntityData)\n\n\t\tfor (const [placeholder, field] of fields) {\n\t\t\tconst value = entityData[placeholder]\n\t\t\tconst metaFlags = {\n\t\t\t\treadable: entityData._meta?.[field.parameters.field]?.readable ?? undefined,\n\t\t\t\tupdatable: entityData._meta?.[field.parameters.field]?.updatable ?? undefined,\n\t\t\t}\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\tvalue,\n\t\t\t\t\t...metaFlags,\n\t\t\t\t})\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tif (value === null) {\n\t\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\t\tvalue: null,\n\t\t\t\t\t\t...metaFlags,\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\t\tvalue: this.processEntityData(entityMap, value as ReceivedEntityData, field.fields.markers),\n\t\t\t\t\t\t...metaFlags,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\tconst ids = presentEntityData.get(placeholder)?.value ?? new Set()\n\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\tvalue: ids,\n\t\t\t\t\t...metaFlags,\n\t\t\t\t})\n\t\t\t\tif (!(ids instanceof Set)) {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tids.clear()\n\t\t\t\tfor (const row of value as ReceivedEntityData[]) {\n\t\t\t\t\tconst id = this.processEntityData(entityMap, row, field.fields.markers)\n\t\t\t\t\tids.add(id.value)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tthrow new Error()\n\t\t\t}\n\t\t}\n\n\t\treturn id\n\t}\n\n\tprivate static extractId(entityData: ReceivedEntityData): ServerId {\n\t\tconst primaryKey: EntityId | undefined = entityData[PRIMARY_KEY_NAME]\n\n\t\tif (primaryKey === undefined) {\n\t\t\tthrow new BindingError(`The server has responded with an entity that lacks a primary key.`)\n\t\t}\n\n\t\treturn new ServerId(primaryKey, entityData.__typename)\n\t}\n}\n"],"names":["EntitySubTreeMarker","BindingError","EntityListSubTreeMarker","assertNever","FieldMarker","HasOneRelationMarker","HasManyRelationMarker","id","PRIMARY_KEY_NAME","ServerId"],"mappings":";;;AAiBO,MAAM,0BAA0B;AAAA,EACtC,OAAc,qBACb,UACA,kBACA,YACO;AACP,UAAM,EAAE,kBAAkB,yBAAA,IAA6B;AAEvD,eAAW,CAAC,aAAa,MAAM,KAAK,WAAW,UAAU;AACxD,YAAM,OAAO,iBAAiB,WAAW,KAAK;AAC9C,UAAI,kBAAkBA,cAAAA,qBAAqB;AAC1C,YAAI,SAAS,MAAM;AAClB,2BAAiB,OAAO,WAAW;AAAA,QACpC,OAAO;AACN,cAAI,MAAM,QAAQ,IAAI,GAAG;AACxB,kBAAM,IAAIC,cAAAA,aAAA;A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YAClC,OAAO;AAAA,YACP,GAAG;AAAA,UAAA,CACH;AAAA,QACF,OAAO;AACN,4BAAkB,IAAI,aAAa;AAAA,YAClC,OAAO,KAAK,kBAAkB,WAAW,OAA6B,MAAM,OAAO,OAAO;AAAA,YAC1F,GAAG;AAAA,UAAA,CACH;AAAA,QACF;AAAA,MACD,WAAW,iBAAiBC,qCAAuB;AAClD,cAAM,MAAM,kBAAkB,IAAI,WAAW,GAAG,6BAAa,IAAA;AAC7D,0BAAkB,IAAI,aAAa;AAAA,UAClC,OAAO;AAAA,UACP,GAAG;AAAA,QAAA,CACH;AACD,YAAI,EAAE,eAAe,MAAM;AAC1B,gBAAM,IAAIL,cAAAA,aAAA;AAAA,QACX;AACA,YAAI,MAAA;AACJ,mBAAW,OAAO,OAA+B;AAChD,gBAAMM,MAAK,KAAK,kBAAkB,WAAW,KAAK,MAAM,OAAO,OAAO;AACtE,cAAI,IAAIA,IAAG,KAAK;AAAA,QACjB;AAAA,MACD,OAAO;AACN,cAAM,IAAI,MAAA;AAAA,MACX;AAAA,IACD;AAEA,WAAO;AAAA,EACR;AAAA,EAEA,OAAe,UAAU,YAA0C;AAClE,UAAM,aAAmC,WAAWC,8BAAgB;AAEpE,QAAI,eAAe,QAAW;AAC7B,YAAM,IAAIP,cAAAA,aAAa,mEAAmE;AAAA,IAC3F;AAEA,WAAO,IAAIQ,cAAAA,SAAS,YAAY,WAAW,UAAU;AAAA,EACtD;AACD;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"RequestResponseNormalizer.js","sources":["../../../../../packages/binding-legacy/src/core/RequestResponseNormalizer.ts"],"sourcesContent":["import {\n\tReceivedDataTree,\n\tReceivedEntityData,\n\tServerId,\n} from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { PRIMARY_KEY_NAME } from '@contember/binding-common'\nimport { EntityId } from '@contember/binding-common'\nimport {\n\tEntityFieldMarkers,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMarkerTreeRoot,\n} from '@contember/binding-common'\nimport { SubMutationOperation } from './MutationGenerator'\nimport { assertNever } from '@contember/binding-common'\nimport { NormalizedPersistedData, PersistedEntityDataStore, SingleEntityPersistedData } from '../accessorTree'\n\nexport class RequestResponseNormalizer {\n\tpublic static mergeInQueryResponse(\n\t\toriginal: NormalizedPersistedData,\n\t\tnewPersistedData: ReceivedDataTree,\n\t\tmarkerTree: MarkerTreeRoot,\n\t): void {\n\t\tconst { subTreeDataStore, persistedEntityDataStore } = original\n\n\t\tfor (const [placeholder, marker] of markerTree.subTrees) {\n\t\t\tconst data = newPersistedData[placeholder] ?? null\n\t\t\tif (marker instanceof EntitySubTreeMarker) {\n\t\t\t\tif (data === null) {\n\t\t\t\t\tsubTreeDataStore.delete(placeholder)\n\t\t\t\t} else {\n\t\t\t\t\tif (Array.isArray(data)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tconst id = this.processEntityData(persistedEntityDataStore, data, marker.fields.markers)\n\t\t\t\t\tsubTreeDataStore.set(placeholder, id)\n\t\t\t\t}\n\t\t\t} else if (marker instanceof EntityListSubTreeMarker) {\n\t\t\t\tconst subTreeListIds = new Set<EntityId>()\n\t\t\t\tif (!Array.isArray(data)) {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tfor (const row of data) {\n\t\t\t\t\tconst id = this.processEntityData(persistedEntityDataStore, row, marker.fields.markers)\n\t\t\t\t\tsubTreeListIds.add(id.value)\n\t\t\t\t}\n\t\t\t\tsubTreeDataStore.set(placeholder, subTreeListIds)\n\t\t\t} else {\n\t\t\t\tthrow new BindingError()\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic static mergeInMutationResponse(\n\t\toriginal: NormalizedPersistedData,\n\t\tnewPersistedData: ReceivedDataTree,\n\t\toperations: SubMutationOperation[],\n\t): void {\n\n\t\tconst { subTreeDataStore, persistedEntityDataStore } = original\n\n\t\tfor (const operation of operations) {\n\t\t\tconst { subTreePlaceholder, subTreeType, alias } = operation\n\n\t\t\tconst receivedData = newPersistedData[alias]\n\n\t\t\t// probably multiple operations removing same entity, resulting in no data for some of them\n\t\t\tif (!receivedData) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (Array.isArray(receivedData)) {\n\t\t\t\tthrow new BindingError()\n\t\t\t}\n\n\t\t\tif (operation.type === 'update' || operation.type === 'create') {\n\t\t\t\tconst id = this.processEntityData(persistedEntityDataStore, receivedData, operation.markers)\n\t\t\t\tif (subTreeType === 'single') {\n\t\t\t\t\tsubTreeDataStore.set(subTreePlaceholder, id)\n\t\t\t\t} else if (subTreeType === 'list') {\n\t\t\t\t\tconst list = subTreeDataStore.get(subTreePlaceholder)\n\n\t\t\t\t\tif (list instanceof Set) {\n\t\t\t\t\t\t// TODO this is somewhat dubious because we're essentially just guessing the order of the entities\n\t\t\t\t\t\t//\t\tand just carelessly put the new one at the end.\n\t\t\t\t\t\tlist.add(id.value)\n\t\t\t\t\t} else if (list === undefined) {\n\t\t\t\t\t\t// That's fine. This is probably just an isCreating sub-tree.\n\t\t\t\t\t\tsubTreeDataStore.set(subTreePlaceholder, new Set([id.value]))\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\treturn assertNever(subTreeType)\n\t\t\t\t}\n\t\t\t} else if (operation.type === 'delete') {\n\t\t\t\tconst id = this.extractId(receivedData)\n\t\t\t\t// TODO there are potentially some references to entityId that this whole process won't quite remove.\n\t\t\t\t//\t\tThat's a memory leak. Probably not particularly severe in most cases but still.\n\t\t\t\tpersistedEntityDataStore.delete(id.uniqueValue)\n\t\t\t\tif (subTreeType === 'single') {\n\t\t\t\t\tsubTreeDataStore.delete(subTreePlaceholder)\n\t\t\t\t} else if (subTreeType === 'list') {\n\t\t\t\t\tconst list = subTreeDataStore.get(subTreePlaceholder)\n\n\t\t\t\t\tif (!(list instanceof Set)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tlist.delete(id.value)\n\t\t\t\t} else {\n\t\t\t\t\treturn assertNever(subTreeType)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\treturn assertNever(operation)\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate static processEntityData(entityMap: PersistedEntityDataStore, entityData: ReceivedEntityData, fields: EntityFieldMarkers): ServerId {\n\t\tconst id = this.extractId(entityData)\n\t\tconst presentEntityData: SingleEntityPersistedData = entityMap.get(id.uniqueValue) ?? new Map()\n\t\tentityMap.set(id.uniqueValue, presentEntityData)\n\n\t\tfor (const [placeholder, field] of fields) {\n\t\t\tconst value = entityData[placeholder]\n\t\t\tconst metaFlags = {\n\t\t\t\treadable: entityData._meta?.[field.parameters.field]?.readable ?? undefined,\n\t\t\t\tupdatable: entityData._meta?.[field.parameters.field]?.updatable ?? undefined,\n\t\t\t}\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\tvalue,\n\t\t\t\t\t...metaFlags,\n\t\t\t\t})\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tif (value === null) {\n\t\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\t\tvalue: null,\n\t\t\t\t\t\t...metaFlags,\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\t\tvalue: this.processEntityData(entityMap, value as ReceivedEntityData, field.fields.markers),\n\t\t\t\t\t\t...metaFlags,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\tconst ids = presentEntityData.get(placeholder)?.value ?? new Set()\n\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\tvalue: ids,\n\t\t\t\t\t...metaFlags,\n\t\t\t\t})\n\t\t\t\tif (!(ids instanceof Set)) {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tids.clear()\n\t\t\t\tfor (const row of value as ReceivedEntityData[]) {\n\t\t\t\t\tconst id = this.processEntityData(entityMap, row, field.fields.markers)\n\t\t\t\t\tids.add(id.value)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tthrow new Error()\n\t\t\t}\n\t\t}\n\n\t\treturn id\n\t}\n\n\tprivate static extractId(entityData: ReceivedEntityData): ServerId {\n\t\tconst primaryKey: EntityId | undefined = entityData[PRIMARY_KEY_NAME]\n\n\t\tif (primaryKey === undefined) {\n\t\t\tthrow new BindingError(`The server has responded with an entity that lacks a primary key.`)\n\t\t}\n\n\t\treturn new ServerId(primaryKey, entityData.__typename)\n\t}\n}\n"],"names":["id"],"mappings":";AAqBO,MAAM,0BAA0B;AAAA,EACtC,OAAc,qBACb,UACA,kBACA,YACO;AACD,UAAA,EAAE,kBAAkB,yBAAA,IAA6B;AAEvD,eAAW,CAAC,aAAa,MAAM,KAAK,WAAW,UAAU;AAClD,YAAA,OAAO,iBAAiB,WAAW,KAAK;AAC9C,UAAI,kBAAkB,qBAAqB;AAC1C,YAAI,SAAS,MAAM;AAClB,2BAAiB,OAAO,WAAW;AAAA,QAAA,OAC7B;AACF,cAAA,MAAM,QAAQ,IAAI,GAAG;AACxB,kBAAM,IAAI,aAAa;AAAA,UAAA;AAExB,gBAAM,KAAK,KAAK,kBAAkB,0BAA0B,MAAM,OAAO,OAAO,OAAO;AACtE,2BAAA,IAAI,aAAa,EAAE;AAAA,QAAA;AAAA,MACrC,WACU,kBAAkB,yBAAyB;AAC/C,cAAA,qCAAqB,IAAc;AACzC,YAAI,CAAC,MAAM,QAAQ,IAAI,GAAG;AACzB,gBAAM,IAAI,aAAa;AAAA,QAAA;AAExB,mBAAW,OAAO,MAAM;AACvB,gBAAM,KAAK,KAAK,kBAAkB,0BAA0B,KAAK,OAAO,OAAO,OAAO;AACvE,yBAAA,IAAI,GAAG,KAAK;AAAA,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AAO,OAA+B;AAChD,gBAAMA,MAAK,KAAK,kBAAkB,WAAW,KAAK,MAAM,OAAO,OAAO;AAClE,cAAA,IAAIA,IAAG,KAAK;AAAA,QAAA;AAAA,MACjB,OACM;AACN,cAAM,IAAI,MAAM;AAAA,MAAA;AAAA,IACjB;AAGM,WAAA;AAAA,EAAA;AAAA,EAGR,OAAe,UAAU,YAA0C;AAC5D,UAAA,aAAmC,WAAW,gBAAgB;AAEpE,QAAI,eAAe,QAAW;AACvB,YAAA,IAAI,aAAa,mEAAmE;AAAA,IAAA;AAG3F,WAAO,IAAI,SAAS,YAAY,WAAW,UAAU;AAAA,EAAA;AAEvD;"}
1
+ {"version":3,"file":"RequestResponseNormalizer.js","sources":["../../../../../packages/binding-legacy/src/core/RequestResponseNormalizer.ts"],"sourcesContent":["import { ReceivedDataTree, ReceivedEntityData, ServerId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { PRIMARY_KEY_NAME } from '@contember/binding-common'\nimport { EntityId } from '@contember/binding-common'\nimport {\n\tEntityFieldMarkers,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMarkerTreeRoot,\n} from '@contember/binding-common'\nimport { SubMutationOperation } from './MutationGenerator'\nimport { assertNever } from '@contember/binding-common'\nimport { NormalizedPersistedData, PersistedEntityDataStore, SingleEntityPersistedData } from '../accessorTree'\n\nexport class RequestResponseNormalizer {\n\tpublic static mergeInQueryResponse(\n\t\toriginal: NormalizedPersistedData,\n\t\tnewPersistedData: ReceivedDataTree,\n\t\tmarkerTree: MarkerTreeRoot,\n\t): void {\n\t\tconst { subTreeDataStore, persistedEntityDataStore } = original\n\n\t\tfor (const [placeholder, marker] of markerTree.subTrees) {\n\t\t\tconst data = newPersistedData[placeholder] ?? null\n\t\t\tif (marker instanceof EntitySubTreeMarker) {\n\t\t\t\tif (data === null) {\n\t\t\t\t\tsubTreeDataStore.delete(placeholder)\n\t\t\t\t} else {\n\t\t\t\t\tif (Array.isArray(data)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tconst id = this.processEntityData(persistedEntityDataStore, data, marker.fields.markers)\n\t\t\t\t\tsubTreeDataStore.set(placeholder, id)\n\t\t\t\t}\n\t\t\t} else if (marker instanceof EntityListSubTreeMarker) {\n\t\t\t\tconst subTreeListIds = new Set<EntityId>()\n\t\t\t\tif (!Array.isArray(data)) {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tfor (const row of data) {\n\t\t\t\t\tconst id = this.processEntityData(persistedEntityDataStore, row, marker.fields.markers)\n\t\t\t\t\tsubTreeListIds.add(id.value)\n\t\t\t\t}\n\t\t\t\tsubTreeDataStore.set(placeholder, subTreeListIds)\n\t\t\t} else {\n\t\t\t\tthrow new BindingError()\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic static mergeInMutationResponse(\n\t\toriginal: NormalizedPersistedData,\n\t\tnewPersistedData: ReceivedDataTree,\n\t\toperations: SubMutationOperation[],\n\t): void {\n\t\tconst { subTreeDataStore, persistedEntityDataStore } = original\n\n\t\tfor (const operation of operations) {\n\t\t\tconst { subTreePlaceholder, subTreeType, alias } = operation\n\n\t\t\tconst receivedData = newPersistedData[alias]\n\n\t\t\t// probably multiple operations removing same entity, resulting in no data for some of them\n\t\t\tif (!receivedData) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (Array.isArray(receivedData)) {\n\t\t\t\tthrow new BindingError()\n\t\t\t}\n\n\t\t\tif (operation.type === 'update' || operation.type === 'create') {\n\t\t\t\tconst id = this.processEntityData(persistedEntityDataStore, receivedData, operation.markers)\n\t\t\t\tif (subTreeType === 'single') {\n\t\t\t\t\tsubTreeDataStore.set(subTreePlaceholder, id)\n\t\t\t\t} else if (subTreeType === 'list') {\n\t\t\t\t\tconst list = subTreeDataStore.get(subTreePlaceholder)\n\n\t\t\t\t\tif (list instanceof Set) {\n\t\t\t\t\t\t// TODO this is somewhat dubious because we're essentially just guessing the order of the entities\n\t\t\t\t\t\t// \t\tand just carelessly put the new one at the end.\n\t\t\t\t\t\tlist.add(id.value)\n\t\t\t\t\t} else if (list === undefined) {\n\t\t\t\t\t\t// That's fine. This is probably just an isCreating sub-tree.\n\t\t\t\t\t\tsubTreeDataStore.set(subTreePlaceholder, new Set([id.value]))\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\treturn assertNever(subTreeType)\n\t\t\t\t}\n\t\t\t} else if (operation.type === 'delete') {\n\t\t\t\tconst id = this.extractId(receivedData)\n\t\t\t\t// TODO there are potentially some references to entityId that this whole process won't quite remove.\n\t\t\t\t// \t\tThat's a memory leak. Probably not particularly severe in most cases but still.\n\t\t\t\tpersistedEntityDataStore.delete(id.uniqueValue)\n\t\t\t\tif (subTreeType === 'single') {\n\t\t\t\t\tsubTreeDataStore.delete(subTreePlaceholder)\n\t\t\t\t} else if (subTreeType === 'list') {\n\t\t\t\t\tconst list = subTreeDataStore.get(subTreePlaceholder)\n\n\t\t\t\t\tif (!(list instanceof Set)) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\tlist.delete(id.value)\n\t\t\t\t} else {\n\t\t\t\t\treturn assertNever(subTreeType)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\treturn assertNever(operation)\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate static processEntityData(entityMap: PersistedEntityDataStore, entityData: ReceivedEntityData, fields: EntityFieldMarkers): ServerId {\n\t\tconst id = this.extractId(entityData)\n\t\tconst presentEntityData: SingleEntityPersistedData = entityMap.get(id.uniqueValue) ?? new Map()\n\t\tentityMap.set(id.uniqueValue, presentEntityData)\n\n\t\tfor (const [placeholder, field] of fields) {\n\t\t\tconst value = entityData[placeholder]\n\t\t\tconst metaFlags = {\n\t\t\t\treadable: entityData._meta?.[field.parameters.field]?.readable ?? undefined,\n\t\t\t\tupdatable: entityData._meta?.[field.parameters.field]?.updatable ?? undefined,\n\t\t\t}\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\tvalue,\n\t\t\t\t\t...metaFlags,\n\t\t\t\t})\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tif (value === null) {\n\t\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\t\tvalue: null,\n\t\t\t\t\t\t...metaFlags,\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\t\tvalue: this.processEntityData(entityMap, value as ReceivedEntityData, field.fields.markers),\n\t\t\t\t\t\t...metaFlags,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\tconst ids = presentEntityData.get(placeholder)?.value ?? new Set()\n\t\t\t\tpresentEntityData.set(placeholder, {\n\t\t\t\t\tvalue: ids,\n\t\t\t\t\t...metaFlags,\n\t\t\t\t})\n\t\t\t\tif (!(ids instanceof Set)) {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tids.clear()\n\t\t\t\tfor (const row of value as ReceivedEntityData[]) {\n\t\t\t\t\tconst id = this.processEntityData(entityMap, row, field.fields.markers)\n\t\t\t\t\tids.add(id.value)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tthrow new Error()\n\t\t\t}\n\t\t}\n\n\t\treturn id\n\t}\n\n\tprivate static extractId(entityData: ReceivedEntityData): ServerId {\n\t\tconst primaryKey: EntityId | undefined = entityData[PRIMARY_KEY_NAME]\n\n\t\tif (primaryKey === undefined) {\n\t\t\tthrow new BindingError(`The server has responded with an entity that lacks a primary key.`)\n\t\t}\n\n\t\treturn new ServerId(primaryKey, entityData.__typename)\n\t}\n}\n"],"names":["id"],"mappings":";AAiBO,MAAM,0BAA0B;AAAA,EACtC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AW,QAAQ,MAAM,WAAW,KAAK,GAAG,aAAa;AAAA,MAAA;AAErE,UAAI,iBAAiB,aAAa;AACjC,0BAAkB,IAAI,aAAa;AAAA,UAClC;AAAA,UACA,GAAG;AAAA,QAAA,CACH;AAAA,MACF,WAAW,iBAAiB,sBAAsB;AACjD,YAAI,UAAU,MAAM;AACnB,4BAAkB,IAAI,aAAa;AAAA,YAClC,OAAO;AAAA,YACP,GAAG;AAAA,UAAA,CACH;AAAA,QACF,OAAO;AACN,4BAAkB,IAAI,aAAa;AAAA,YAClC,OAAO,KAAK,kBAAkB,WAAW,OAA6B,MAAM,OAAO,OAAO;AAAA,YAC1F,GAAG;AAAA,UAAA,CACH;AAAA,QACF;AAAA,MACD,WAAW,iBAAiB,uBAAuB;AAClD,cAAM,MAAM,kBAAkB,IAAI,WAAW,GAAG,6BAAa,IAAA;AAC7D,0BAAkB,IAAI,aAAa;AAAA,UAClC,OAAO;AAAA,UACP,GAAG;AAAA,QAAA,CACH;AACD,YAAI,EAAE,eAAe,MAAM;AAC1B,gBAAM,IAAI,aAAA;AAAA,QACX;AACA,YAAI,MAAA;AACJ,mBAAW,OAAO,OAA+B;AAChD,gBAAMA,MAAK,KAAK,kBAAkB,WAAW,KAAK,MAAM,OAAO,OAAO;AACtE,cAAI,IAAIA,IAAG,KAAK;AAAA,QACjB;AAAA,MACD,OAAO;AACN,cAAM,IAAI,MAAA;AAAA,MACX;AAAA,IACD;AAEA,WAAO;AAAA,EACR;AAAA,EAEA,OAAe,UAAU,YAA0C;AAClE,UAAM,aAAmC,WAAW,gBAAgB;AAEpE,QAAI,eAAe,QAAW;AAC7B,YAAM,IAAI,aAAa,mEAAmE;AAAA,IAC3F;AAEA,WAAO,IAAI,SAAS,YAAY,WAAW,UAAU;AAAA,EACtD;AACD;"}
@@ -86,7 +86,7 @@ class StateInitializer {
86
86
  this.initializeFromHasManyRelationMarker(realm, field, new Set(value.children.keys()));
87
87
  } else if (field instanceof bindingCommon.HasOneRelationMarker) {
88
88
  let runtimeId;
89
- let subCopyFrom = void 0;
89
+ let subCopyFrom;
90
90
  if (pathBack?.fieldBackToParent === field.parameters.field && !field.parameters.reducedBy) {
91
91
  runtimeId = pathBack.parent.entity.id;
92
92
  } else {
@@ -1 +1 @@
1
- {"version":3,"file":"StateInitializer.cjs","sources":["../../../../../packages/binding-legacy/src/core/StateInitializer.ts"],"sourcesContent":["import { BatchUpdatesOptions, EntityAccessor, FieldAccessor } from '@contember/binding-common'\nimport { RuntimeId, ServerId, UnpersistedEntityDummyId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport {\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n} from '@contember/binding-common'\nimport { BijectiveIndexedMap } from '@contember/utilities'\nimport type {\n\tEntityEventListenerStore,\n\tEntityId,\n\tEntityListEventListenerStore,\n\tEntityName,\n\tFieldEventListenerStore,\n\tFieldName,\n\tFieldValue,\n} from '@contember/binding-common'\nimport { EventListenersStore } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { AccessorErrorManager } from './AccessorErrorManager'\nimport type { EventManager } from './EventManager'\nimport { EntityOperations, FieldOperations, ListOperations } from './operations'\nimport { RealmKeyGenerator } from './RealmKeyGenerator'\nimport {\n\tEntityListBlueprint,\n\tEntityListState,\n\tEntityRealmBlueprint,\n\tEntityRealmState,\n\tEntityRealmStateStub,\n\tEntityState,\n\tFieldState,\n\tgetEntityMarker,\n\tRootStateNode,\n} from './state'\nimport { TreeParameterMerger } from '@contember/binding-common'\nimport type { TreeStore } from './TreeStore'\nimport { EntityAccessorImpl, EntityListAccessorImpl, FieldAccessorImpl } from '../accessors'\nimport { EntityFieldPersistedData, EntityFieldPersistedValue } from '../accessorTree'\n\nexport class StateInitializer {\n\tprivate readonly fieldOperations: FieldOperations\n\tprivate readonly entityOperations: EntityOperations\n\tprivate readonly listOperations: ListOperations\n\n\tpublic constructor(\n\t\taccessorErrorManager: AccessorErrorManager,\n\t\tbatchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {\n\t\tthis.fieldOperations = new FieldOperations(accessorErrorManager, this.eventManager, this, this.treeStore)\n\t\tthis.entityOperations = new EntityOperations(accessorErrorManager, batchUpdatesOptions, this.eventManager, this, this.treeStore)\n\t\tthis.listOperations = new ListOperations(accessorErrorManager, batchUpdatesOptions, this.eventManager, this, this.treeStore)\n\t}\n\n\tpublic initializeSubTree(tree: EntitySubTreeMarker | EntityListSubTreeMarker): RootStateNode {\n\t\tlet subTreeState: RootStateNode\n\t\tconst persistedRootData = this.treeStore.subTreePersistedData.get(tree.placeholderName)\n\n\t\tif (tree instanceof EntityListSubTreeMarker) {\n\t\t\tconst persistedEntityIds: Set<EntityId> = persistedRootData instanceof Set ? persistedRootData : new Set()\n\t\t\tsubTreeState = this.initializeEntityListState(\n\t\t\t\t{ type: 'subTree', marker: tree, parent: undefined },\n\t\t\t\ttree.entityName,\n\t\t\t\tpersistedEntityIds,\n\t\t\t)\n\t\t} else {\n\t\t\tsubTreeState = this.materializeEntityRealm(\n\t\t\t\tthis.initializeEntityRealm(\n\t\t\t\t\tpersistedRootData instanceof ServerId ? persistedRootData : new UnpersistedEntityDummyId(),\n\t\t\t\t\ttree.entityName,\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'subTree',\n\t\t\t\t\t\tmarker: tree,\n\t\t\t\t\t\tparent: undefined,\n\t\t\t\t\t},\n\t\t\t\t),\n\t\t\t)\n\t\t}\n\n\t\treturn subTreeState\n\t}\n\n\tpublic initializeEntityRealm(\n\t\tid: RuntimeId,\n\t\tentityName: EntityName,\n\t\tblueprint: EntityRealmBlueprint,\n\t\tcopyFrom?: EntityRealmState | EntityRealmStateStub,\n\t): EntityRealmState | EntityRealmStateStub {\n\t\t// This is counter-intuitive a bit. The method can also return an EntityRealmState, not just a stub.\n\t\t// The reason is so that each call-site can just default to trying to initialize a stub without having to keep\n\t\t// in mind that the realm might have already been initialized.\n\n\t\tconst entity = this.initializeEntityState(id, entityName)\n\t\tconst realmKey = RealmKeyGenerator.getRealmKey(id, blueprint)\n\n\t\tconst existing = this.treeStore.entityRealmStore.get(realmKey)\n\t\tif (existing !== undefined) {\n\t\t\treturn existing\n\t\t}\n\n\t\tif (!copyFrom || copyFrom.type !== 'entityRealm') {\n\t\t\tconst stub: EntityRealmStateStub = {\n\t\t\t\ttype: 'entityRealmStub',\n\n\t\t\t\tblueprint,\n\t\t\t\tentity,\n\t\t\t\trealmKey,\n\t\t\t\tgetAccessor: () => this.materializeEntityRealm(stub).getAccessor(),\n\t\t\t}\n\t\t\tthis.registerEntityRealm(stub)\n\n\t\t\treturn stub\n\t\t}\n\t\tconst realm = this.createEntityRealm(blueprint, realmKey, entity, copyFrom)\n\n\t\tthis.registerEntityRealm(realm)\n\n\t\tconst marker = getEntityMarker(realm)\n\t\tconst pathBack = this.treeStore.getPathBackToParent(realm)\n\n\t\tfor (const [placeholderName, field] of marker.fields.markers) {\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\tif (!value || value.type !== 'field') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromFieldMarker(realm, field, undefined, value)\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\tif (!value || value.type !== 'entityList') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasManyRelationMarker(realm, field, new Set(value.children.keys()))\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tlet runtimeId: RuntimeId\n\t\t\t\tlet subCopyFrom: EntityRealmState | undefined = undefined\n\t\t\t\tif (pathBack?.fieldBackToParent === field.parameters.field && !field.parameters.reducedBy) {\n\t\t\t\t\truntimeId = pathBack.parent.entity.id\n\t\t\t\t} else {\n\t\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\t\tif (!value || (value.type !== 'entityRealm' && value.type !== 'entityRealmStub')) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\truntimeId = !(value.entity.id instanceof UnpersistedEntityDummyId) ? value.entity.id : new UnpersistedEntityDummyId()\n\t\t\t\t\tif (value.type === 'entityRealm') {\n\t\t\t\t\t\tsubCopyFrom = value\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasOneRelationMarker(realm, field, runtimeId, subCopyFrom)\n\t\t\t} else {\n\t\t\t\tassertNever(field)\n\t\t\t}\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(realm)\n\t\treturn realm\n\t}\n\n\tprivate createEntityRealm(\n\t\tblueprint: EntityRealmBlueprint,\n\t\trealmKey: string,\n\t\tentity: EntityState,\n\t\tcopyFrom?: EntityRealmState,\n\t): EntityRealmState {\n\t\tconst entityRealm: EntityRealmState = {\n\t\t\ttype: 'entityRealm',\n\n\t\t\tblueprint,\n\t\t\trealmKey,\n\t\t\tentity,\n\n\t\t\taccessor: undefined,\n\t\t\tchildren: new Map(),\n\t\t\tchildrenWithPendingUpdates: undefined,\n\t\t\terrors: undefined,\n\t\t\teventListeners: this.initializeEntityEventListenerStore(blueprint),\n\t\t\tfieldsWithPendingConnectionUpdates: undefined,\n\t\t\tplannedHasOneDeletions: undefined,\n\t\t\tunpersistedChangesCount: copyFrom?.unpersistedChangesCount ?? 0,\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (entityRealm.accessor === undefined) {\n\t\t\t\t\tconst entity = entityRealm.entity\n\t\t\t\t\tentityRealm.accessor = new EntityAccessorImpl(\n\t\t\t\t\t\tentityRealm,\n\t\t\t\t\t\tthis.entityOperations,\n\t\t\t\t\t\tentity.id,\n\t\t\t\t\t\tentityRealm.realmKey,\n\t\t\t\t\t\tentity.entityName,\n\n\t\t\t\t\t\t// We're technically exposing more info in runtime than we'd like but that way we don't have to allocate and\n\t\t\t\t\t\t// keep in sync two copies of the same data. TS hides the extra info anyway.\n\t\t\t\t\t\tentityRealm.children,\n\t\t\t\t\t\tthis.treeStore.persistedEntityData.get(entity.id.uniqueValue),\n\t\t\t\t\t\tentityRealm.unpersistedChangesCount !== 0,\n\t\t\t\t\t\tentityRealm.errors,\n\t\t\t\t\t\tgetEntityMarker(entityRealm).environment,\n\t\t\t\t\t\tentityRealm.getAccessor,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn entityRealm.accessor\n\t\t\t},\n\t\t}\n\t\treturn entityRealm\n\t}\n\n\tprivate materializeEntityRealm(state: EntityRealmState | EntityRealmStateStub): EntityRealmState {\n\t\tif (state.type === 'entityRealm') {\n\t\t\treturn state\n\t\t}\n\t\tconst { realmKey, entity, blueprint } = state\n\n\t\tconst existing = this.treeStore.entityRealmStore.get(realmKey)\n\t\tif (existing !== undefined && existing.type === 'entityRealm') {\n\t\t\treturn existing\n\t\t}\n\n\t\tconst entityRealm = this.createEntityRealm(blueprint, realmKey, entity)\n\n\t\tthis.registerEntityRealm(entityRealm)\n\n\t\t// if (blueprint.creationParameters.forceCreation && !entity.id.existsOnServer) {\n\t\t// \tentityRealm.unpersistedChangesCount += 1\n\t\t// }\n\n\t\tconst persistedData = this.treeStore.persistedEntityData.get(entity.id.uniqueValue)\n\n\t\tconst marker = getEntityMarker(state)\n\t\tconst pathBack = this.treeStore.getPathBackToParent(entityRealm)\n\n\t\tfor (const [placeholderName, field] of marker.fields.markers) {\n\t\t\tconst fieldDatum = persistedData?.get(placeholderName)?.value\n\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tthis.initializeFromFieldMarker(entityRealm, field, fieldDatum)\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\t// if (pathBack?.fieldBackToParent === field.parameters.field) {\n\t\t\t\t// \t// TODO this is probably wrong?\n\t\t\t\t// \tconst ids = pathBack.parent.children.get(field.placeholderName)\n\t\t\t\t// \tconst newData = this.treeStore.persistedEntityData.get(pathBack.parent.entity.id.value)\n\t\t\t\t// \tconsole.log('many?', pathBack, field, entityRealm, fieldDatum, ids)\n\t\t\t\t// }\n\t\t\t\tthis.initializeFromHasManyRelationMarker(entityRealm, field, fieldDatum instanceof Set ? fieldDatum : new Set())\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tlet runtimeId: RuntimeId\n\t\t\t\tif (pathBack?.fieldBackToParent === field.parameters.field && !field.parameters.reducedBy) {\n\t\t\t\t\truntimeId = pathBack.parent.entity.id\n\t\t\t\t} else {\n\t\t\t\t\truntimeId = fieldDatum instanceof ServerId ? fieldDatum : new UnpersistedEntityDummyId()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasOneRelationMarker(entityRealm, field, runtimeId)\n\t\t\t} else {\n\t\t\t\tassertNever(field)\n\t\t\t}\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(entityRealm)\n\n\t\treturn entityRealm\n\t}\n\n\tprivate registerEntityRealm(entityRealm: EntityRealmState | EntityRealmStateStub) {\n\t\tconst { realmKey, blueprint, entity } = entityRealm\n\n\t\tthis.treeStore.entityRealmStore.set(entityRealm.realmKey, entityRealm)\n\n\t\tif (blueprint.type === 'listEntity') {\n\t\t\tblueprint.parent.children.set(entity.id.value, entityRealm)\n\t\t} else if (blueprint.type === 'hasOne') {\n\t\t\tblueprint.parent.children.set(blueprint.marker.placeholderName, entityRealm)\n\t\t}\n\t\tentity.realms.set(realmKey, entityRealm)\n\t}\n\n\tpublic initializeEntityState(id: RuntimeId, entityName: EntityName): EntityState {\n\t\tconst entityId = id.uniqueValue\n\n\t\tconst existing = this.treeStore.entityStore.get(entityId)\n\t\tif (existing !== undefined) {\n\t\t\treturn existing\n\t\t}\n\n\t\tconst entityState: EntityState = {\n\t\t\tentityName,\n\t\t\thasIdSetInStone: id instanceof ServerId,\n\t\t\tid,\n\t\t\tisScheduledForDeletion: false,\n\t\t\tmaidenId: id instanceof UnpersistedEntityDummyId ? id : undefined,\n\t\t\trealms: new Map(),\n\t\t}\n\t\tthis.treeStore.entityStore.set(entityId, entityState)\n\n\t\treturn entityState\n\t}\n\n\tprivate initializeEntityListState(\n\t\tblueprint: EntityListBlueprint,\n\t\tentityName: EntityName,\n\t\tinitialEntityIds: Set<EntityId>,\n\t): EntityListState {\n\t\tconst entityListState: EntityListState = {\n\t\t\ttype: 'entityList',\n\t\t\taccessor: undefined,\n\t\t\tblueprint,\n\t\t\tchildren: new BijectiveIndexedMap(realm => realm.entity.id.value),\n\t\t\tchildrenWithPendingUpdates: undefined,\n\t\t\tentityName,\n\t\t\teventListeners: this.initializeEntityListEventListenerStore(blueprint),\n\t\t\tchildEventListeners: this.initializeEntityListChildEventListenerStore(blueprint),\n\t\t\terrors: undefined,\n\t\t\tplannedRemovals: undefined,\n\t\t\tunpersistedChangesCount: 0, // TODO force creation?\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (entityListState.accessor === undefined) {\n\t\t\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(entityListState)\n\t\t\t\t\tentityListState.accessor = new EntityListAccessorImpl(\n\t\t\t\t\t\tentityListState,\n\t\t\t\t\t\tthis.listOperations,\n\t\t\t\t\t\tentityListState.entityName,\n\t\t\t\t\t\tentityListState.children,\n\t\t\t\t\t\tpersistedEntityIds,\n\t\t\t\t\t\tentityListState.unpersistedChangesCount !== 0,\n\t\t\t\t\t\tentityListState.errors,\n\t\t\t\t\t\tentityListState.blueprint.marker.environment,\n\t\t\t\t\t\tentityListState.getAccessor,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn entityListState.accessor\n\t\t\t},\n\t\t}\n\n\t\tconst initialData: Iterable<EntityId | undefined> =\n\t\t\tinitialEntityIds.size === 0\n\t\t\t\t? Array.from({ length: blueprint.marker.parameters.initialEntityCount })\n\t\t\t\t: initialEntityIds\n\t\tfor (const entityId of initialData) {\n\t\t\tthis.initializeEntityRealm(\n\t\t\t\tentityId ? new ServerId(entityId, entityName) : new UnpersistedEntityDummyId(),\n\t\t\t\tentityListState.entityName,\n\t\t\t\t{\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: entityListState,\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(entityListState)\n\n\t\treturn entityListState\n\t}\n\n\tprivate initializeFieldState(\n\t\tparent: EntityRealmState,\n\t\tplaceholderName: FieldName,\n\t\tfieldMarker: FieldMarker,\n\t\tpersistedValue: FieldValue | undefined,\n\t\tcopyFromState: FieldState | undefined,\n\t): FieldState {\n\t\tconst resolvedFieldValue = copyFromState ? copyFromState.value : (persistedValue ?? fieldMarker.defaultValue ?? null)\n\n\t\tconst fieldState: FieldState = {\n\t\t\ttype: 'field',\n\t\t\taccessor: undefined,\n\t\t\tfieldMarker,\n\t\t\tplaceholderName,\n\t\t\tpersistedValue,\n\t\t\tparent,\n\t\t\tvalue: resolvedFieldValue,\n\t\t\teventListeners: this.initializeFieldEventListenerStore(fieldMarker),\n\t\t\terrors: undefined,\n\t\t\ttouchLog: undefined,\n\t\t\thasUnpersistedChanges: copyFromState?.hasUnpersistedChanges ?? false,\n\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (fieldState.accessor === undefined) {\n\t\t\t\t\tconst fieldSchema = this.treeStore.schema.getEntityColumn(parent.entity.entityName, fieldMarker.fieldName)\n\n\t\t\t\t\tfieldState.accessor = new FieldAccessorImpl(\n\t\t\t\t\t\tfieldState,\n\t\t\t\t\t\tthis.fieldOperations,\n\t\t\t\t\t\tfieldState.placeholderName,\n\t\t\t\t\t\tfieldState.value,\n\t\t\t\t\t\tfieldState.persistedValue === undefined ? null : fieldState.persistedValue,\n\t\t\t\t\t\tfieldState.fieldMarker.defaultValue,\n\t\t\t\t\t\tfieldState.errors,\n\t\t\t\t\t\tfieldState.hasUnpersistedChanges,\n\t\t\t\t\t\tfieldState.touchLog,\n\t\t\t\t\t\tfieldState.getAccessor,\n\t\t\t\t\t\tfieldSchema,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn fieldState.accessor\n\t\t\t},\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(fieldState)\n\t\treturn fieldState\n\t}\n\n\tprivate initializeFromFieldMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: FieldMarker,\n\t\tpersistedValue: EntityFieldPersistedValue | undefined,\n\t\tcopyFromState?: FieldState,\n\t) {\n\t\tif (persistedValue instanceof Set) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Received a collection of referenced entities where a single '${field.fieldName}' field was expected. ` +\n\t\t\t\t\t`Perhaps you wanted to use a <Repeater />?`,\n\t\t\t)\n\t\t}\n\t\tif (persistedValue instanceof ServerId) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Received a referenced entity where a single '${field.fieldName}' field was expected. ` +\n\t\t\t\t\t`Perhaps you wanted to use <HasOne />?`,\n\t\t\t)\n\t\t}\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeFieldState(parent, field.placeholderName, field, persistedValue, copyFromState),\n\t\t)\n\t}\n\n\tprivate initializeFromHasOneRelationMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: HasOneRelationMarker,\n\t\tentityId: RuntimeId,\n\t\tcopyFrom?: EntityRealmState | EntityRealmStateStub,\n\t) {\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeEntityRealm(entityId, this.getRelationTargetEntityName(parent, field), {\n\t\t\t\ttype: 'hasOne',\n\t\t\t\tparent,\n\t\t\t\tmarker: field,\n\t\t\t}, copyFrom),\n\t\t)\n\t\t// if (fieldDatum instanceof ServerGeneratedUuid || fieldDatum === null || fieldDatum === undefined) {\n\t\t// \tconst entityId = fieldDatum instanceof ServerGeneratedUuid ? fieldDatum : new UnpersistedEntityDummyId()\n\t\t//\n\t\t//\n\t\t// } else if (fieldDatum instanceof Set) {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a collection of entities for field '${relation.field}' where a single entity was expected. ` +\n\t\t// \t\t\t`Perhaps you wanted to use a <Repeater />?`,\n\t\t// \t)\n\t\t// } else {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a scalar value for field '${relation.field}' where a single entity was expected.` +\n\t\t// \t\t\t`Perhaps you meant to use a variant of <Field />?`,\n\t\t// \t)\n\t\t// }\n\t}\n\n\tprivate initializeFromHasManyRelationMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: HasManyRelationMarker,\n\t\tpersistedEntityIds: Set<EntityId>,\n\t) {\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeEntityListState(\n\t\t\t\t{ type: 'hasMany', marker: field, parent },\n\t\t\t\tthis.getRelationTargetEntityName(parent, field),\n\t\t\t\tpersistedEntityIds,\n\t\t\t),\n\t\t)\n\t\t// const relation = field.parameters\n\t\t//\n\t\t// if (fieldDatum === undefined || fieldDatum instanceof Set) {\n\t\t//\n\t\t// } else if (typeof fieldDatum === 'object') {\n\t\t// \t// Intentionally allowing `fieldDatum === null` here as well since this should only happen when a *hasOne\n\t\t// \t// relation is unlinked, e.g. a Person does not have a linked Nationality.\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a referenced entity for field '${relation.field}' where a collection of entities was expected.` +\n\t\t// \t\t\t`Perhaps you wanted to use a <HasOne />?`,\n\t\t// \t)\n\t\t// } else {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a scalar value for field '${relation.field}' where a collection of entities was expected.` +\n\t\t// \t\t\t`Perhaps you meant to use a variant of <Field />?`,\n\t\t// \t)\n\t\t// }\n\t}\n\n\tprivate getRelationTargetEntityName(\n\t\tentityRealm: EntityRealmState,\n\t\tfield: HasOneRelationMarker | HasManyRelationMarker,\n\t): EntityName {\n\t\tconst targetField = this.treeStore.schema.getEntityRelation(entityRealm.entity.entityName, field.parameters.field)\n\t\treturn targetField.targetEntity\n\t}\n\n\tpublic runImmediateUserInitialization(\n\t\trealm: EntityRealmState | EntityRealmStateStub,\n\t\tinitialize: EntityAccessor.BatchUpdatesHandler | undefined,\n\t) {\n\t\tif (initialize === undefined) {\n\t\t\treturn\n\t\t}\n\t\tconst entityRealm = this.materializeEntityRealm(realm)\n\n\t\trealm.getAccessor().batchUpdates(initialize)\n\n\t\tconst initializeListeners = this.eventManager.getEventListeners(entityRealm, { type: 'initialize' })\n\t\tif (initializeListeners === undefined || initializeListeners.size === 0) {\n\t\t\trealm.entity.hasIdSetInStone = true\n\t\t}\n\t}\n\n\tpublic initializeEntityEventListenerStore(blueprint: EntityRealmBlueprint): EntityEventListenerStore | undefined {\n\t\tif (blueprint.type === 'listEntity') {\n\t\t\tconst blueprintListeners = blueprint.parent.blueprint.marker.parameters.childEventListeners\n\t\t\tconst childInitialize = blueprintListeners?.get({ type: 'initialize' })\n\t\t\tconst store: EntityEventListenerStore = new EventListenersStore(() => blueprint.parent.childEventListeners)\n\n\t\t\tif (childInitialize !== undefined) {\n\t\t\t\tstore.set({ type: 'initialize' }, childInitialize)\n\t\t\t}\n\t\t\treturn store\n\t\t}\n\n\t\tconst blueprintListeners = blueprint.marker.parameters.eventListeners\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprintListeners.clone()\n\t}\n\n\tprivate initializeEntityListEventListenerStore(\n\t\tblueprint: EntityListBlueprint,\n\t): EntityListEventListenerStore | undefined {\n\t\tconst blueprintListeners = blueprint.marker.parameters.eventListeners\n\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprintListeners.clone()\n\t}\n\n\tprivate initializeEntityListChildEventListenerStore(\n\t\tblueprint: EntityListBlueprint,\n\t): EntityEventListenerStore | undefined {\n\t\tconst blueprintListeners = blueprint.marker.parameters.childEventListeners\n\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprintListeners.clone()\n\t}\n\n\tprivate initializeFieldEventListenerStore(marker: FieldMarker): FieldEventListenerStore | undefined {\n\t\treturn marker.parameters.eventListeners?.clone()\n\t}\n}\n"],"names":["FieldOperations","EntityOperations","ListOperations","EntityListSubTreeMarker","ServerId","UnpersistedEntityDummyId","RealmKeyGenerator","getEntityMarker","FieldMarker","BindingError","HasManyRelationMarker","HasOneRelationMarker","assertNever","entity","EntityAccessorImpl","BijectiveIndexedMap","EntityListAccessorImpl","FieldAccessorImpl","blueprintListeners","EventListenersStore"],"mappings":";;;;;;;;;;;;;;;AA0CO,MAAM,iBAAiB;AAAA,EAKtB,YACN,sBACA,qBACiB,cACA,WAChB;AAFgB,SAAA,eAAA;AACA,SAAA,YAAA;AARD,kBAAA,MAAA,iBAAA;AACA,kBAAA,MAAA,kBAAA;AACA,kBAAA,MAAA,gBAAA;AAQX,SAAA,kBAAkB,IAAIA,gBAAAA,gBAAgB,sBAAsB,KAAK,cAAc,MAAM,KAAK,SAAS;AACnG,SAAA,mBAAmB,IAAIC,iBAAiB,iBAAA,sBAAsB,qBAAqB,KAAK,cAAc,MAAM,KAAK,SAAS;AAC1H,SAAA,iBAAiB,IAAIC,eAAe,eAAA,sBAAsB,qBAAqB,KAAK,cAAc,MAAM,KAAK,SAAS;AAAA,EAAA;AAAA,EAGrH,kBAAkB,MAAoE;AACxF,QAAA;AACJ,UAAM,oBAAoB,KAAK,UAAU,qBAAqB,IAAI,KAAK,eAAe;AAEtF,QAAI,gBAAgBC,cAAAA,yBAAyB;AAC5C,YAAM,qBAAoC,6BAA6B,MAAM,wCAAwB,IAAI;AACzG,qBAAe,KAAK;AAAA,QACnB,EAAE,MAAM,WAAW,QAAQ,MAAM,QAAQ,OAAU;AAAA,QACnD,KAAK;AAAA,QACL;AAAA,MACD;AAAA,IAAA,OACM;AACN,qBAAe,KAAK;AAAA,QACnB,KAAK;AAAA,UACJ,6BAA6BC,cAAA,WAAW,oBAAoB,IAAIC,uCAAyB;AAAA,UACzF,KAAK;AAA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aAAA;AAAA,IAAA;AAER,WAAO,mBAAmB,MAAM;AAAA,EAAA;AAAA,EAGzB,kCAAkC,QAA0D;AAC5F,WAAA,OAAO,WAAW,gBAAgB,MAAM;AAAA,EAAA;AAEjD;;"}
1
+ {"version":3,"file":"StateInitializer.cjs","sources":["../../../../../packages/binding-legacy/src/core/StateInitializer.ts"],"sourcesContent":["import { BatchUpdatesOptions, EntityAccessor, FieldAccessor } from '@contember/binding-common'\nimport { RuntimeId, ServerId, UnpersistedEntityDummyId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { EntityListSubTreeMarker, EntitySubTreeMarker, FieldMarker, HasManyRelationMarker, HasOneRelationMarker } from '@contember/binding-common'\nimport { BijectiveIndexedMap } from '@contember/utilities'\nimport type {\n\tEntityEventListenerStore,\n\tEntityId,\n\tEntityListEventListenerStore,\n\tEntityName,\n\tFieldEventListenerStore,\n\tFieldName,\n\tFieldValue,\n} from '@contember/binding-common'\nimport { EventListenersStore } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { AccessorErrorManager } from './AccessorErrorManager'\nimport type { EventManager } from './EventManager'\nimport { EntityOperations, FieldOperations, ListOperations } from './operations'\nimport { RealmKeyGenerator } from './RealmKeyGenerator'\nimport {\n\tEntityListBlueprint,\n\tEntityListState,\n\tEntityRealmBlueprint,\n\tEntityRealmState,\n\tEntityRealmStateStub,\n\tEntityState,\n\tFieldState,\n\tgetEntityMarker,\n\tRootStateNode,\n} from './state'\nimport { TreeParameterMerger } from '@contember/binding-common'\nimport type { TreeStore } from './TreeStore'\nimport { EntityAccessorImpl, EntityListAccessorImpl, FieldAccessorImpl } from '../accessors'\nimport { EntityFieldPersistedData, EntityFieldPersistedValue } from '../accessorTree'\n\nexport class StateInitializer {\n\tprivate readonly fieldOperations: FieldOperations\n\tprivate readonly entityOperations: EntityOperations\n\tprivate readonly listOperations: ListOperations\n\n\tpublic constructor(\n\t\taccessorErrorManager: AccessorErrorManager,\n\t\tbatchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {\n\t\tthis.fieldOperations = new FieldOperations(accessorErrorManager, this.eventManager, this, this.treeStore)\n\t\tthis.entityOperations = new EntityOperations(accessorErrorManager, batchUpdatesOptions, this.eventManager, this, this.treeStore)\n\t\tthis.listOperations = new ListOperations(accessorErrorManager, batchUpdatesOptions, this.eventManager, this, this.treeStore)\n\t}\n\n\tpublic initializeSubTree(tree: EntitySubTreeMarker | EntityListSubTreeMarker): RootStateNode {\n\t\tlet subTreeState: RootStateNode\n\t\tconst persistedRootData = this.treeStore.subTreePersistedData.get(tree.placeholderName)\n\n\t\tif (tree instanceof EntityListSubTreeMarker) {\n\t\t\tconst persistedEntityIds: Set<EntityId> = persistedRootData instanceof Set ? persistedRootData : new Set()\n\t\t\tsubTreeState = this.initializeEntityListState(\n\t\t\t\t{ type: 'subTree', marker: tree, parent: undefined },\n\t\t\t\ttree.entityName,\n\t\t\t\tpersistedEntityIds,\n\t\t\t)\n\t\t} else {\n\t\t\tsubTreeState = this.materializeEntityRealm(\n\t\t\t\tthis.initializeEntityRealm(\n\t\t\t\t\tpersistedRootData instanceof ServerId ? persistedRootData : new UnpersistedEntityDummyId(),\n\t\t\t\t\ttree.entityName,\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'subTree',\n\t\t\t\t\t\tmarker: tree,\n\t\t\t\t\t\tparent: undefined,\n\t\t\t\t\t},\n\t\t\t\t),\n\t\t\t)\n\t\t}\n\n\t\treturn subTreeState\n\t}\n\n\tpublic initializeEntityRealm(\n\t\tid: RuntimeId,\n\t\tentityName: EntityName,\n\t\tblueprint: EntityRealmBlueprint,\n\t\tcopyFrom?: EntityRealmState | EntityRealmStateStub,\n\t): EntityRealmState | EntityRealmStateStub {\n\t\t// This is counter-intuitive a bit. The method can also return an EntityRealmState, not just a stub.\n\t\t// The reason is so that each call-site can just default to trying to initialize a stub without having to keep\n\t\t// in mind that the realm might have already been initialized.\n\n\t\tconst entity = this.initializeEntityState(id, entityName)\n\t\tconst realmKey = RealmKeyGenerator.getRealmKey(id, blueprint)\n\n\t\tconst existing = this.treeStore.entityRealmStore.get(realmKey)\n\t\tif (existing !== undefined) {\n\t\t\treturn existing\n\t\t}\n\n\t\tif (!copyFrom || copyFrom.type !== 'entityRealm') {\n\t\t\tconst stub: EntityRealmStateStub = {\n\t\t\t\ttype: 'entityRealmStub',\n\n\t\t\t\tblueprint,\n\t\t\t\tentity,\n\t\t\t\trealmKey,\n\t\t\t\tgetAccessor: () => this.materializeEntityRealm(stub).getAccessor(),\n\t\t\t}\n\t\t\tthis.registerEntityRealm(stub)\n\n\t\t\treturn stub\n\t\t}\n\t\tconst realm = this.createEntityRealm(blueprint, realmKey, entity, copyFrom)\n\n\t\tthis.registerEntityRealm(realm)\n\n\t\tconst marker = getEntityMarker(realm)\n\t\tconst pathBack = this.treeStore.getPathBackToParent(realm)\n\n\t\tfor (const [placeholderName, field] of marker.fields.markers) {\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\tif (!value || value.type !== 'field') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromFieldMarker(realm, field, undefined, value)\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\tif (!value || value.type !== 'entityList') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasManyRelationMarker(realm, field, new Set(value.children.keys()))\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tlet runtimeId: RuntimeId\n\t\t\t\tlet subCopyFrom: EntityRealmState | undefined\n\t\t\t\tif (pathBack?.fieldBackToParent === field.parameters.field && !field.parameters.reducedBy) {\n\t\t\t\t\truntimeId = pathBack.parent.entity.id\n\t\t\t\t} else {\n\t\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\t\tif (!value || (value.type !== 'entityRealm' && value.type !== 'entityRealmStub')) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\truntimeId = !(value.entity.id instanceof UnpersistedEntityDummyId) ? value.entity.id : new UnpersistedEntityDummyId()\n\t\t\t\t\tif (value.type === 'entityRealm') {\n\t\t\t\t\t\tsubCopyFrom = value\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasOneRelationMarker(realm, field, runtimeId, subCopyFrom)\n\t\t\t} else {\n\t\t\t\tassertNever(field)\n\t\t\t}\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(realm)\n\t\treturn realm\n\t}\n\n\tprivate createEntityRealm(\n\t\tblueprint: EntityRealmBlueprint,\n\t\trealmKey: string,\n\t\tentity: EntityState,\n\t\tcopyFrom?: EntityRealmState,\n\t): EntityRealmState {\n\t\tconst entityRealm: EntityRealmState = {\n\t\t\ttype: 'entityRealm',\n\n\t\t\tblueprint,\n\t\t\trealmKey,\n\t\t\tentity,\n\n\t\t\taccessor: undefined,\n\t\t\tchildren: new Map(),\n\t\t\tchildrenWithPendingUpdates: undefined,\n\t\t\terrors: undefined,\n\t\t\teventListeners: this.initializeEntityEventListenerStore(blueprint),\n\t\t\tfieldsWithPendingConnectionUpdates: undefined,\n\t\t\tplannedHasOneDeletions: undefined,\n\t\t\tunpersistedChangesCount: copyFrom?.unpersistedChangesCount ?? 0,\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (entityRealm.accessor === undefined) {\n\t\t\t\t\tconst entity = entityRealm.entity\n\t\t\t\t\tentityRealm.accessor = new EntityAccessorImpl(\n\t\t\t\t\t\tentityRealm,\n\t\t\t\t\t\tthis.entityOperations,\n\t\t\t\t\t\tentity.id,\n\t\t\t\t\t\tentityRealm.realmKey,\n\t\t\t\t\t\tentity.entityName,\n\t\t\t\t\t\t// We're technically exposing more info in runtime than we'd like but that way we don't have to allocate and\n\t\t\t\t\t\t// keep in sync two copies of the same data. TS hides the extra info anyway.\n\t\t\t\t\t\tentityRealm.children,\n\t\t\t\t\t\tthis.treeStore.persistedEntityData.get(entity.id.uniqueValue),\n\t\t\t\t\t\tentityRealm.unpersistedChangesCount !== 0,\n\t\t\t\t\t\tentityRealm.errors,\n\t\t\t\t\t\tgetEntityMarker(entityRealm).environment,\n\t\t\t\t\t\tentityRealm.getAccessor,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn entityRealm.accessor\n\t\t\t},\n\t\t}\n\t\treturn entityRealm\n\t}\n\n\tprivate materializeEntityRealm(state: EntityRealmState | EntityRealmStateStub): EntityRealmState {\n\t\tif (state.type === 'entityRealm') {\n\t\t\treturn state\n\t\t}\n\t\tconst { realmKey, entity, blueprint } = state\n\n\t\tconst existing = this.treeStore.entityRealmStore.get(realmKey)\n\t\tif (existing !== undefined && existing.type === 'entityRealm') {\n\t\t\treturn existing\n\t\t}\n\n\t\tconst entityRealm = this.createEntityRealm(blueprint, realmKey, entity)\n\n\t\tthis.registerEntityRealm(entityRealm)\n\n\t\t// if (blueprint.creationParameters.forceCreation && !entity.id.existsOnServer) {\n\t\t// \tentityRealm.unpersistedChangesCount += 1\n\t\t// }\n\n\t\tconst persistedData = this.treeStore.persistedEntityData.get(entity.id.uniqueValue)\n\n\t\tconst marker = getEntityMarker(state)\n\t\tconst pathBack = this.treeStore.getPathBackToParent(entityRealm)\n\n\t\tfor (const [placeholderName, field] of marker.fields.markers) {\n\t\t\tconst fieldDatum = persistedData?.get(placeholderName)?.value\n\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tthis.initializeFromFieldMarker(entityRealm, field, fieldDatum)\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\t// if (pathBack?.fieldBackToParent === field.parameters.field) {\n\t\t\t\t// \t// TODO this is probably wrong?\n\t\t\t\t// \tconst ids = pathBack.parent.children.get(field.placeholderName)\n\t\t\t\t// \tconst newData = this.treeStore.persistedEntityData.get(pathBack.parent.entity.id.value)\n\t\t\t\t// \tconsole.log('many?', pathBack, field, entityRealm, fieldDatum, ids)\n\t\t\t\t// }\n\t\t\t\tthis.initializeFromHasManyRelationMarker(entityRealm, field, fieldDatum instanceof Set ? fieldDatum : new Set())\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tlet runtimeId: RuntimeId\n\t\t\t\tif (pathBack?.fieldBackToParent === field.parameters.field && !field.parameters.reducedBy) {\n\t\t\t\t\truntimeId = pathBack.parent.entity.id\n\t\t\t\t} else {\n\t\t\t\t\truntimeId = fieldDatum instanceof ServerId ? fieldDatum : new UnpersistedEntityDummyId()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasOneRelationMarker(entityRealm, field, runtimeId)\n\t\t\t} else {\n\t\t\t\tassertNever(field)\n\t\t\t}\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(entityRealm)\n\n\t\treturn entityRealm\n\t}\n\n\tprivate registerEntityRealm(entityRealm: EntityRealmState | EntityRealmStateStub) {\n\t\tconst { realmKey, blueprint, entity } = entityRealm\n\n\t\tthis.treeStore.entityRealmStore.set(entityRealm.realmKey, entityRealm)\n\n\t\tif (blueprint.type === 'listEntity') {\n\t\t\tblueprint.parent.children.set(entity.id.value, entityRealm)\n\t\t} else if (blueprint.type === 'hasOne') {\n\t\t\tblueprint.parent.children.set(blueprint.marker.placeholderName, entityRealm)\n\t\t}\n\t\tentity.realms.set(realmKey, entityRealm)\n\t}\n\n\tpublic initializeEntityState(id: RuntimeId, entityName: EntityName): EntityState {\n\t\tconst entityId = id.uniqueValue\n\n\t\tconst existing = this.treeStore.entityStore.get(entityId)\n\t\tif (existing !== undefined) {\n\t\t\treturn existing\n\t\t}\n\n\t\tconst entityState: EntityState = {\n\t\t\tentityName,\n\t\t\thasIdSetInStone: id instanceof ServerId,\n\t\t\tid,\n\t\t\tisScheduledForDeletion: false,\n\t\t\tmaidenId: id instanceof UnpersistedEntityDummyId ? id : undefined,\n\t\t\trealms: new Map(),\n\t\t}\n\t\tthis.treeStore.entityStore.set(entityId, entityState)\n\n\t\treturn entityState\n\t}\n\n\tprivate initializeEntityListState(\n\t\tblueprint: EntityListBlueprint,\n\t\tentityName: EntityName,\n\t\tinitialEntityIds: Set<EntityId>,\n\t): EntityListState {\n\t\tconst entityListState: EntityListState = {\n\t\t\ttype: 'entityList',\n\t\t\taccessor: undefined,\n\t\t\tblueprint,\n\t\t\tchildren: new BijectiveIndexedMap(realm => realm.entity.id.value),\n\t\t\tchildrenWithPendingUpdates: undefined,\n\t\t\tentityName,\n\t\t\teventListeners: this.initializeEntityListEventListenerStore(blueprint),\n\t\t\tchildEventListeners: this.initializeEntityListChildEventListenerStore(blueprint),\n\t\t\terrors: undefined,\n\t\t\tplannedRemovals: undefined,\n\t\t\tunpersistedChangesCount: 0, // TODO force creation?\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (entityListState.accessor === undefined) {\n\t\t\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(entityListState)\n\t\t\t\t\tentityListState.accessor = new EntityListAccessorImpl(\n\t\t\t\t\t\tentityListState,\n\t\t\t\t\t\tthis.listOperations,\n\t\t\t\t\t\tentityListState.entityName,\n\t\t\t\t\t\tentityListState.children,\n\t\t\t\t\t\tpersistedEntityIds,\n\t\t\t\t\t\tentityListState.unpersistedChangesCount !== 0,\n\t\t\t\t\t\tentityListState.errors,\n\t\t\t\t\t\tentityListState.blueprint.marker.environment,\n\t\t\t\t\t\tentityListState.getAccessor,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn entityListState.accessor\n\t\t\t},\n\t\t}\n\n\t\tconst initialData: Iterable<EntityId | undefined> = initialEntityIds.size === 0\n\t\t\t? Array.from({ length: blueprint.marker.parameters.initialEntityCount })\n\t\t\t: initialEntityIds\n\t\tfor (const entityId of initialData) {\n\t\t\tthis.initializeEntityRealm(\n\t\t\t\tentityId ? new ServerId(entityId, entityName) : new UnpersistedEntityDummyId(),\n\t\t\t\tentityListState.entityName,\n\t\t\t\t{\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: entityListState,\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(entityListState)\n\n\t\treturn entityListState\n\t}\n\n\tprivate initializeFieldState(\n\t\tparent: EntityRealmState,\n\t\tplaceholderName: FieldName,\n\t\tfieldMarker: FieldMarker,\n\t\tpersistedValue: FieldValue | undefined,\n\t\tcopyFromState: FieldState | undefined,\n\t): FieldState {\n\t\tconst resolvedFieldValue = copyFromState ? copyFromState.value : (persistedValue ?? fieldMarker.defaultValue ?? null)\n\n\t\tconst fieldState: FieldState = {\n\t\t\ttype: 'field',\n\t\t\taccessor: undefined,\n\t\t\tfieldMarker,\n\t\t\tplaceholderName,\n\t\t\tpersistedValue,\n\t\t\tparent,\n\t\t\tvalue: resolvedFieldValue,\n\t\t\teventListeners: this.initializeFieldEventListenerStore(fieldMarker),\n\t\t\terrors: undefined,\n\t\t\ttouchLog: undefined,\n\t\t\thasUnpersistedChanges: copyFromState?.hasUnpersistedChanges ?? false,\n\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (fieldState.accessor === undefined) {\n\t\t\t\t\tconst fieldSchema = this.treeStore.schema.getEntityColumn(parent.entity.entityName, fieldMarker.fieldName)\n\n\t\t\t\t\tfieldState.accessor = new FieldAccessorImpl(\n\t\t\t\t\t\tfieldState,\n\t\t\t\t\t\tthis.fieldOperations,\n\t\t\t\t\t\tfieldState.placeholderName,\n\t\t\t\t\t\tfieldState.value,\n\t\t\t\t\t\tfieldState.persistedValue === undefined ? null : fieldState.persistedValue,\n\t\t\t\t\t\tfieldState.fieldMarker.defaultValue,\n\t\t\t\t\t\tfieldState.errors,\n\t\t\t\t\t\tfieldState.hasUnpersistedChanges,\n\t\t\t\t\t\tfieldState.touchLog,\n\t\t\t\t\t\tfieldState.getAccessor,\n\t\t\t\t\t\tfieldSchema,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn fieldState.accessor\n\t\t\t},\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(fieldState)\n\t\treturn fieldState\n\t}\n\n\tprivate initializeFromFieldMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: FieldMarker,\n\t\tpersistedValue: EntityFieldPersistedValue | undefined,\n\t\tcopyFromState?: FieldState,\n\t) {\n\t\tif (persistedValue instanceof Set) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Received a collection of referenced entities where a single '${field.fieldName}' field was expected. `\n\t\t\t\t\t+ `Perhaps you wanted to use a <Repeater />?`,\n\t\t\t)\n\t\t}\n\t\tif (persistedValue instanceof ServerId) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Received a referenced entity where a single '${field.fieldName}' field was expected. `\n\t\t\t\t\t+ `Perhaps you wanted to use <HasOne />?`,\n\t\t\t)\n\t\t}\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeFieldState(parent, field.placeholderName, field, persistedValue, copyFromState),\n\t\t)\n\t}\n\n\tprivate initializeFromHasOneRelationMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: HasOneRelationMarker,\n\t\tentityId: RuntimeId,\n\t\tcopyFrom?: EntityRealmState | EntityRealmStateStub,\n\t) {\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeEntityRealm(entityId, this.getRelationTargetEntityName(parent, field), {\n\t\t\t\ttype: 'hasOne',\n\t\t\t\tparent,\n\t\t\t\tmarker: field,\n\t\t\t}, copyFrom),\n\t\t)\n\t\t// if (fieldDatum instanceof ServerGeneratedUuid || fieldDatum === null || fieldDatum === undefined) {\n\t\t// \tconst entityId = fieldDatum instanceof ServerGeneratedUuid ? fieldDatum : new UnpersistedEntityDummyId()\n\t\t//\n\t\t//\n\t\t// } else if (fieldDatum instanceof Set) {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a collection of entities for field '${relation.field}' where a single entity was expected. ` +\n\t\t// \t\t\t`Perhaps you wanted to use a <Repeater />?`,\n\t\t// \t)\n\t\t// } else {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a scalar value for field '${relation.field}' where a single entity was expected.` +\n\t\t// \t\t\t`Perhaps you meant to use a variant of <Field />?`,\n\t\t// \t)\n\t\t// }\n\t}\n\n\tprivate initializeFromHasManyRelationMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: HasManyRelationMarker,\n\t\tpersistedEntityIds: Set<EntityId>,\n\t) {\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeEntityListState(\n\t\t\t\t{ type: 'hasMany', marker: field, parent },\n\t\t\t\tthis.getRelationTargetEntityName(parent, field),\n\t\t\t\tpersistedEntityIds,\n\t\t\t),\n\t\t)\n\t\t// const relation = field.parameters\n\t\t//\n\t\t// if (fieldDatum === undefined || fieldDatum instanceof Set) {\n\t\t//\n\t\t// } else if (typeof fieldDatum === 'object') {\n\t\t// \t// Intentionally allowing `fieldDatum === null` here as well since this should only happen when a *hasOne\n\t\t// \t// relation is unlinked, e.g. a Person does not have a linked Nationality.\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a referenced entity for field '${relation.field}' where a collection of entities was expected.` +\n\t\t// \t\t\t`Perhaps you wanted to use a <HasOne />?`,\n\t\t// \t)\n\t\t// } else {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a scalar value for field '${relation.field}' where a collection of entities was expected.` +\n\t\t// \t\t\t`Perhaps you meant to use a variant of <Field />?`,\n\t\t// \t)\n\t\t// }\n\t}\n\n\tprivate getRelationTargetEntityName(\n\t\tentityRealm: EntityRealmState,\n\t\tfield: HasOneRelationMarker | HasManyRelationMarker,\n\t): EntityName {\n\t\tconst targetField = this.treeStore.schema.getEntityRelation(entityRealm.entity.entityName, field.parameters.field)\n\t\treturn targetField.targetEntity\n\t}\n\n\tpublic runImmediateUserInitialization(\n\t\trealm: EntityRealmState | EntityRealmStateStub,\n\t\tinitialize: EntityAccessor.BatchUpdatesHandler | undefined,\n\t) {\n\t\tif (initialize === undefined) {\n\t\t\treturn\n\t\t}\n\t\tconst entityRealm = this.materializeEntityRealm(realm)\n\n\t\trealm.getAccessor().batchUpdates(initialize)\n\n\t\tconst initializeListeners = this.eventManager.getEventListeners(entityRealm, { type: 'initialize' })\n\t\tif (initializeListeners === undefined || initializeListeners.size === 0) {\n\t\t\trealm.entity.hasIdSetInStone = true\n\t\t}\n\t}\n\n\tpublic initializeEntityEventListenerStore(blueprint: EntityRealmBlueprint): EntityEventListenerStore | undefined {\n\t\tif (blueprint.type === 'listEntity') {\n\t\t\tconst blueprintListeners = blueprint.parent.blueprint.marker.parameters.childEventListeners\n\t\t\tconst childInitialize = blueprintListeners?.get({ type: 'initialize' })\n\t\t\tconst store: EntityEventListenerStore = new EventListenersStore(() => blueprint.parent.childEventListeners)\n\n\t\t\tif (childInitialize !== undefined) {\n\t\t\t\tstore.set({ type: 'initialize' }, childInitialize)\n\t\t\t}\n\t\t\treturn store\n\t\t}\n\n\t\tconst blueprintListeners = blueprint.marker.parameters.eventListeners\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprintListeners.clone()\n\t}\n\n\tprivate initializeEntityListEventListenerStore(\n\t\tblueprint: EntityListBlueprint,\n\t): EntityListEventListenerStore | undefined {\n\t\tconst blueprintListeners = blueprint.marker.parameters.eventListeners\n\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprintListeners.clone()\n\t}\n\n\tprivate initializeEntityListChildEventListenerStore(\n\t\tblueprint: EntityListBlueprint,\n\t): EntityEventListenerStore | undefined {\n\t\tconst blueprintListeners = blueprint.marker.parameters.childEventListeners\n\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprintListeners.clone()\n\t}\n\n\tprivate initializeFieldEventListenerStore(marker: FieldMarker): FieldEventListenerStore | undefined {\n\t\treturn marker.parameters.eventListeners?.clone()\n\t}\n}\n"],"names":["FieldOperations","EntityOperations","ListOperations","EntityListSubTreeMarker","ServerId","UnpersistedEntityDummyId","RealmKeyGenerator","getEntityMarker","FieldMarker","BindingError","HasManyRelationMarker","HasOneRelationMarker","assertNever","entity","EntityAccessorImpl","BijectiveIndexedMap","EntityListAccessorImpl","FieldAccessorImpl","blueprintListeners","EventListenersStore"],"mappings":";;;;;;;;;;;;;;;AAoCO,MAAM,iBAAiB;AAAA,EAKtB,YACN,sBACA,qBACiB,cACA,WAChB;AAFgB,SAAA,eAAA;AACA,SAAA,YAAA;AARlB,kBAAA,MAAiB,iBAAA;AACjB,kBAAA,MAAiB,kBAAA;AACjB,kBAAA,MAAiB,gBAAA;AAQhB,SAAK,kBAAkB,IAAIA,gBAAAA,gBAAgB,sBAAsB,KAAK,cAAc,MAAM,KAAK,SAAS;AACxG,SAAK,mBAAmB,IAAIC,iBAAAA,iBAAiB,sBAAsB,qBAAqB,KAAK,cAAc,MAAM,KAAK,SAAS;AAC/H,SAAK,iBAAiB,IAAIC,eAAAA,eAAe,sBAAsB,qBAAqB,KAAK,cAAc,MAAM,KAAK,SAAS;AAAA,EAC5H;AAAA,EAEO,kBAAkB,MAAoE;AAC5F,QAAI;AACJ,UAAM,oBAAoB,KAAK,UAAU,qBAAqB,IAAI,KAAK,eAAe;AAEtF,QAAI,gBAAgBC,cAAAA,yBAAyB;AAC5C,YAAM,qBAAoC,6BAA6B,MAAM,wCAAwB,IAAA;AACrG,qBAAe,KAAK;AAAA,QACnB,EAAE,MAAM,WAAW,QAAQ,MAAM,QAAQ,OAAA;AAAA,QACzC,KAAK;AAAA,QACL;AAAA,MAAA;AAAA,IAEF,OAAO;AACN,qBAAe,KAAK;AAAA,QACnB,KAAK;AAAA,UACJ,6BAA6BC,cAAAA,WAAW,oBAAoB,IAAIC,uCAAA;AAAA,UAChE,KAAK;AAAA,UACL;AAAA,YACC,MAAM;AAAA,YACN,QAAQ;AAAA,YAC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AA,EAEQ,kCAAkC,QAA0D;AACnG,WAAO,OAAO,WAAW,gBAAgB,MAAA;AAAA,EAC1C;AACD;;"}
@@ -1,4 +1,4 @@
1
- import { EntityListSubTreeMarker, ServerId, UnpersistedEntityDummyId, FieldMarker, BindingError, HasManyRelationMarker, HasOneRelationMarker, assertNever, EventListenersStore } from "@contember/binding-common";
1
+ import { EntityListSubTreeMarker, UnpersistedEntityDummyId, ServerId, FieldMarker, BindingError, HasManyRelationMarker, HasOneRelationMarker, assertNever, EventListenersStore } from "@contember/binding-common";
2
2
  import { BijectiveIndexedMap } from "@contember/utilities";
3
3
  import { RealmKeyGenerator } from "./RealmKeyGenerator.js";
4
4
  import { FieldOperations } from "./operations/FieldOperations.js";
@@ -84,7 +84,7 @@ class StateInitializer {
84
84
  this.initializeFromHasManyRelationMarker(realm, field, new Set(value.children.keys()));
85
85
  } else if (field instanceof HasOneRelationMarker) {
86
86
  let runtimeId;
87
- let subCopyFrom = void 0;
87
+ let subCopyFrom;
88
88
  if (pathBack?.fieldBackToParent === field.parameters.field && !field.parameters.reducedBy) {
89
89
  runtimeId = pathBack.parent.entity.id;
90
90
  } else {
@@ -1 +1 @@
1
- {"version":3,"file":"StateInitializer.js","sources":["../../../../../packages/binding-legacy/src/core/StateInitializer.ts"],"sourcesContent":["import { BatchUpdatesOptions, EntityAccessor, FieldAccessor } from '@contember/binding-common'\nimport { RuntimeId, ServerId, UnpersistedEntityDummyId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport {\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n} from '@contember/binding-common'\nimport { BijectiveIndexedMap } from '@contember/utilities'\nimport type {\n\tEntityEventListenerStore,\n\tEntityId,\n\tEntityListEventListenerStore,\n\tEntityName,\n\tFieldEventListenerStore,\n\tFieldName,\n\tFieldValue,\n} from '@contember/binding-common'\nimport { EventListenersStore } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { AccessorErrorManager } from './AccessorErrorManager'\nimport type { EventManager } from './EventManager'\nimport { EntityOperations, FieldOperations, ListOperations } from './operations'\nimport { RealmKeyGenerator } from './RealmKeyGenerator'\nimport {\n\tEntityListBlueprint,\n\tEntityListState,\n\tEntityRealmBlueprint,\n\tEntityRealmState,\n\tEntityRealmStateStub,\n\tEntityState,\n\tFieldState,\n\tgetEntityMarker,\n\tRootStateNode,\n} from './state'\nimport { TreeParameterMerger } from '@contember/binding-common'\nimport type { TreeStore } from './TreeStore'\nimport { EntityAccessorImpl, EntityListAccessorImpl, FieldAccessorImpl } from '../accessors'\nimport { EntityFieldPersistedData, EntityFieldPersistedValue } from '../accessorTree'\n\nexport class StateInitializer {\n\tprivate readonly fieldOperations: FieldOperations\n\tprivate readonly entityOperations: EntityOperations\n\tprivate readonly listOperations: ListOperations\n\n\tpublic constructor(\n\t\taccessorErrorManager: AccessorErrorManager,\n\t\tbatchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {\n\t\tthis.fieldOperations = new FieldOperations(accessorErrorManager, this.eventManager, this, this.treeStore)\n\t\tthis.entityOperations = new EntityOperations(accessorErrorManager, batchUpdatesOptions, this.eventManager, this, this.treeStore)\n\t\tthis.listOperations = new ListOperations(accessorErrorManager, batchUpdatesOptions, this.eventManager, this, this.treeStore)\n\t}\n\n\tpublic initializeSubTree(tree: EntitySubTreeMarker | EntityListSubTreeMarker): RootStateNode {\n\t\tlet subTreeState: RootStateNode\n\t\tconst persistedRootData = this.treeStore.subTreePersistedData.get(tree.placeholderName)\n\n\t\tif (tree instanceof EntityListSubTreeMarker) {\n\t\t\tconst persistedEntityIds: Set<EntityId> = persistedRootData instanceof Set ? persistedRootData : new Set()\n\t\t\tsubTreeState = this.initializeEntityListState(\n\t\t\t\t{ type: 'subTree', marker: tree, parent: undefined },\n\t\t\t\ttree.entityName,\n\t\t\t\tpersistedEntityIds,\n\t\t\t)\n\t\t} else {\n\t\t\tsubTreeState = this.materializeEntityRealm(\n\t\t\t\tthis.initializeEntityRealm(\n\t\t\t\t\tpersistedRootData instanceof ServerId ? persistedRootData : new UnpersistedEntityDummyId(),\n\t\t\t\t\ttree.entityName,\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'subTree',\n\t\t\t\t\t\tmarker: tree,\n\t\t\t\t\t\tparent: undefined,\n\t\t\t\t\t},\n\t\t\t\t),\n\t\t\t)\n\t\t}\n\n\t\treturn subTreeState\n\t}\n\n\tpublic initializeEntityRealm(\n\t\tid: RuntimeId,\n\t\tentityName: EntityName,\n\t\tblueprint: EntityRealmBlueprint,\n\t\tcopyFrom?: EntityRealmState | EntityRealmStateStub,\n\t): EntityRealmState | EntityRealmStateStub {\n\t\t// This is counter-intuitive a bit. The method can also return an EntityRealmState, not just a stub.\n\t\t// The reason is so that each call-site can just default to trying to initialize a stub without having to keep\n\t\t// in mind that the realm might have already been initialized.\n\n\t\tconst entity = this.initializeEntityState(id, entityName)\n\t\tconst realmKey = RealmKeyGenerator.getRealmKey(id, blueprint)\n\n\t\tconst existing = this.treeStore.entityRealmStore.get(realmKey)\n\t\tif (existing !== undefined) {\n\t\t\treturn existing\n\t\t}\n\n\t\tif (!copyFrom || copyFrom.type !== 'entityRealm') {\n\t\t\tconst stub: EntityRealmStateStub = {\n\t\t\t\ttype: 'entityRealmStub',\n\n\t\t\t\tblueprint,\n\t\t\t\tentity,\n\t\t\t\trealmKey,\n\t\t\t\tgetAccessor: () => this.materializeEntityRealm(stub).getAccessor(),\n\t\t\t}\n\t\t\tthis.registerEntityRealm(stub)\n\n\t\t\treturn stub\n\t\t}\n\t\tconst realm = this.createEntityRealm(blueprint, realmKey, entity, copyFrom)\n\n\t\tthis.registerEntityRealm(realm)\n\n\t\tconst marker = getEntityMarker(realm)\n\t\tconst pathBack = this.treeStore.getPathBackToParent(realm)\n\n\t\tfor (const [placeholderName, field] of marker.fields.markers) {\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\tif (!value || value.type !== 'field') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromFieldMarker(realm, field, undefined, value)\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\tif (!value || value.type !== 'entityList') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasManyRelationMarker(realm, field, new Set(value.children.keys()))\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tlet runtimeId: RuntimeId\n\t\t\t\tlet subCopyFrom: EntityRealmState | undefined = undefined\n\t\t\t\tif (pathBack?.fieldBackToParent === field.parameters.field && !field.parameters.reducedBy) {\n\t\t\t\t\truntimeId = pathBack.parent.entity.id\n\t\t\t\t} else {\n\t\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\t\tif (!value || (value.type !== 'entityRealm' && value.type !== 'entityRealmStub')) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\truntimeId = !(value.entity.id instanceof UnpersistedEntityDummyId) ? value.entity.id : new UnpersistedEntityDummyId()\n\t\t\t\t\tif (value.type === 'entityRealm') {\n\t\t\t\t\t\tsubCopyFrom = value\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasOneRelationMarker(realm, field, runtimeId, subCopyFrom)\n\t\t\t} else {\n\t\t\t\tassertNever(field)\n\t\t\t}\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(realm)\n\t\treturn realm\n\t}\n\n\tprivate createEntityRealm(\n\t\tblueprint: EntityRealmBlueprint,\n\t\trealmKey: string,\n\t\tentity: EntityState,\n\t\tcopyFrom?: EntityRealmState,\n\t): EntityRealmState {\n\t\tconst entityRealm: EntityRealmState = {\n\t\t\ttype: 'entityRealm',\n\n\t\t\tblueprint,\n\t\t\trealmKey,\n\t\t\tentity,\n\n\t\t\taccessor: undefined,\n\t\t\tchildren: new Map(),\n\t\t\tchildrenWithPendingUpdates: undefined,\n\t\t\terrors: undefined,\n\t\t\teventListeners: this.initializeEntityEventListenerStore(blueprint),\n\t\t\tfieldsWithPendingConnectionUpdates: undefined,\n\t\t\tplannedHasOneDeletions: undefined,\n\t\t\tunpersistedChangesCount: copyFrom?.unpersistedChangesCount ?? 0,\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (entityRealm.accessor === undefined) {\n\t\t\t\t\tconst entity = entityRealm.entity\n\t\t\t\t\tentityRealm.accessor = new EntityAccessorImpl(\n\t\t\t\t\t\tentityRealm,\n\t\t\t\t\t\tthis.entityOperations,\n\t\t\t\t\t\tentity.id,\n\t\t\t\t\t\tentityRealm.realmKey,\n\t\t\t\t\t\tentity.entityName,\n\n\t\t\t\t\t\t// We're technically exposing more info in runtime than we'd like but that way we don't have to allocate and\n\t\t\t\t\t\t// keep in sync two copies of the same data. TS hides the extra info anyway.\n\t\t\t\t\t\tentityRealm.children,\n\t\t\t\t\t\tthis.treeStore.persistedEntityData.get(entity.id.uniqueValue),\n\t\t\t\t\t\tentityRealm.unpersistedChangesCount !== 0,\n\t\t\t\t\t\tentityRealm.errors,\n\t\t\t\t\t\tgetEntityMarker(entityRealm).environment,\n\t\t\t\t\t\tentityRealm.getAccessor,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn entityRealm.accessor\n\t\t\t},\n\t\t}\n\t\treturn entityRealm\n\t}\n\n\tprivate materializeEntityRealm(state: EntityRealmState | EntityRealmStateStub): EntityRealmState {\n\t\tif (state.type === 'entityRealm') {\n\t\t\treturn state\n\t\t}\n\t\tconst { realmKey, entity, blueprint } = state\n\n\t\tconst existing = this.treeStore.entityRealmStore.get(realmKey)\n\t\tif (existing !== undefined && existing.type === 'entityRealm') {\n\t\t\treturn existing\n\t\t}\n\n\t\tconst entityRealm = this.createEntityRealm(blueprint, realmKey, entity)\n\n\t\tthis.registerEntityRealm(entityRealm)\n\n\t\t// if (blueprint.creationParameters.forceCreation && !entity.id.existsOnServer) {\n\t\t// \tentityRealm.unpersistedChangesCount += 1\n\t\t// }\n\n\t\tconst persistedData = this.treeStore.persistedEntityData.get(entity.id.uniqueValue)\n\n\t\tconst marker = getEntityMarker(state)\n\t\tconst pathBack = this.treeStore.getPathBackToParent(entityRealm)\n\n\t\tfor (const [placeholderName, field] of marker.fields.markers) {\n\t\t\tconst fieldDatum = persistedData?.get(placeholderName)?.value\n\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tthis.initializeFromFieldMarker(entityRealm, field, fieldDatum)\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\t// if (pathBack?.fieldBackToParent === field.parameters.field) {\n\t\t\t\t// \t// TODO this is probably wrong?\n\t\t\t\t// \tconst ids = pathBack.parent.children.get(field.placeholderName)\n\t\t\t\t// \tconst newData = this.treeStore.persistedEntityData.get(pathBack.parent.entity.id.value)\n\t\t\t\t// \tconsole.log('many?', pathBack, field, entityRealm, fieldDatum, ids)\n\t\t\t\t// }\n\t\t\t\tthis.initializeFromHasManyRelationMarker(entityRealm, field, fieldDatum instanceof Set ? fieldDatum : new Set())\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tlet runtimeId: RuntimeId\n\t\t\t\tif (pathBack?.fieldBackToParent === field.parameters.field && !field.parameters.reducedBy) {\n\t\t\t\t\truntimeId = pathBack.parent.entity.id\n\t\t\t\t} else {\n\t\t\t\t\truntimeId = fieldDatum instanceof ServerId ? fieldDatum : new UnpersistedEntityDummyId()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasOneRelationMarker(entityRealm, field, runtimeId)\n\t\t\t} else {\n\t\t\t\tassertNever(field)\n\t\t\t}\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(entityRealm)\n\n\t\treturn entityRealm\n\t}\n\n\tprivate registerEntityRealm(entityRealm: EntityRealmState | EntityRealmStateStub) {\n\t\tconst { realmKey, blueprint, entity } = entityRealm\n\n\t\tthis.treeStore.entityRealmStore.set(entityRealm.realmKey, entityRealm)\n\n\t\tif (blueprint.type === 'listEntity') {\n\t\t\tblueprint.parent.children.set(entity.id.value, entityRealm)\n\t\t} else if (blueprint.type === 'hasOne') {\n\t\t\tblueprint.parent.children.set(blueprint.marker.placeholderName, entityRealm)\n\t\t}\n\t\tentity.realms.set(realmKey, entityRealm)\n\t}\n\n\tpublic initializeEntityState(id: RuntimeId, entityName: EntityName): EntityState {\n\t\tconst entityId = id.uniqueValue\n\n\t\tconst existing = this.treeStore.entityStore.get(entityId)\n\t\tif (existing !== undefined) {\n\t\t\treturn existing\n\t\t}\n\n\t\tconst entityState: EntityState = {\n\t\t\tentityName,\n\t\t\thasIdSetInStone: id instanceof ServerId,\n\t\t\tid,\n\t\t\tisScheduledForDeletion: false,\n\t\t\tmaidenId: id instanceof UnpersistedEntityDummyId ? id : undefined,\n\t\t\trealms: new Map(),\n\t\t}\n\t\tthis.treeStore.entityStore.set(entityId, entityState)\n\n\t\treturn entityState\n\t}\n\n\tprivate initializeEntityListState(\n\t\tblueprint: EntityListBlueprint,\n\t\tentityName: EntityName,\n\t\tinitialEntityIds: Set<EntityId>,\n\t): EntityListState {\n\t\tconst entityListState: EntityListState = {\n\t\t\ttype: 'entityList',\n\t\t\taccessor: undefined,\n\t\t\tblueprint,\n\t\t\tchildren: new BijectiveIndexedMap(realm => realm.entity.id.value),\n\t\t\tchildrenWithPendingUpdates: undefined,\n\t\t\tentityName,\n\t\t\teventListeners: this.initializeEntityListEventListenerStore(blueprint),\n\t\t\tchildEventListeners: this.initializeEntityListChildEventListenerStore(blueprint),\n\t\t\terrors: undefined,\n\t\t\tplannedRemovals: undefined,\n\t\t\tunpersistedChangesCount: 0, // TODO force creation?\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (entityListState.accessor === undefined) {\n\t\t\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(entityListState)\n\t\t\t\t\tentityListState.accessor = new EntityListAccessorImpl(\n\t\t\t\t\t\tentityListState,\n\t\t\t\t\t\tthis.listOperations,\n\t\t\t\t\t\tentityListState.entityName,\n\t\t\t\t\t\tentityListState.children,\n\t\t\t\t\t\tpersistedEntityIds,\n\t\t\t\t\t\tentityListState.unpersistedChangesCount !== 0,\n\t\t\t\t\t\tentityListState.errors,\n\t\t\t\t\t\tentityListState.blueprint.marker.environment,\n\t\t\t\t\t\tentityListState.getAccessor,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn entityListState.accessor\n\t\t\t},\n\t\t}\n\n\t\tconst initialData: Iterable<EntityId | undefined> =\n\t\t\tinitialEntityIds.size === 0\n\t\t\t\t? Array.from({ length: blueprint.marker.parameters.initialEntityCount })\n\t\t\t\t: initialEntityIds\n\t\tfor (const entityId of initialData) {\n\t\t\tthis.initializeEntityRealm(\n\t\t\t\tentityId ? new ServerId(entityId, entityName) : new UnpersistedEntityDummyId(),\n\t\t\t\tentityListState.entityName,\n\t\t\t\t{\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: entityListState,\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(entityListState)\n\n\t\treturn entityListState\n\t}\n\n\tprivate initializeFieldState(\n\t\tparent: EntityRealmState,\n\t\tplaceholderName: FieldName,\n\t\tfieldMarker: FieldMarker,\n\t\tpersistedValue: FieldValue | undefined,\n\t\tcopyFromState: FieldState | undefined,\n\t): FieldState {\n\t\tconst resolvedFieldValue = copyFromState ? copyFromState.value : (persistedValue ?? fieldMarker.defaultValue ?? null)\n\n\t\tconst fieldState: FieldState = {\n\t\t\ttype: 'field',\n\t\t\taccessor: undefined,\n\t\t\tfieldMarker,\n\t\t\tplaceholderName,\n\t\t\tpersistedValue,\n\t\t\tparent,\n\t\t\tvalue: resolvedFieldValue,\n\t\t\teventListeners: this.initializeFieldEventListenerStore(fieldMarker),\n\t\t\terrors: undefined,\n\t\t\ttouchLog: undefined,\n\t\t\thasUnpersistedChanges: copyFromState?.hasUnpersistedChanges ?? false,\n\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (fieldState.accessor === undefined) {\n\t\t\t\t\tconst fieldSchema = this.treeStore.schema.getEntityColumn(parent.entity.entityName, fieldMarker.fieldName)\n\n\t\t\t\t\tfieldState.accessor = new FieldAccessorImpl(\n\t\t\t\t\t\tfieldState,\n\t\t\t\t\t\tthis.fieldOperations,\n\t\t\t\t\t\tfieldState.placeholderName,\n\t\t\t\t\t\tfieldState.value,\n\t\t\t\t\t\tfieldState.persistedValue === undefined ? null : fieldState.persistedValue,\n\t\t\t\t\t\tfieldState.fieldMarker.defaultValue,\n\t\t\t\t\t\tfieldState.errors,\n\t\t\t\t\t\tfieldState.hasUnpersistedChanges,\n\t\t\t\t\t\tfieldState.touchLog,\n\t\t\t\t\t\tfieldState.getAccessor,\n\t\t\t\t\t\tfieldSchema,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn fieldState.accessor\n\t\t\t},\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(fieldState)\n\t\treturn fieldState\n\t}\n\n\tprivate initializeFromFieldMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: FieldMarker,\n\t\tpersistedValue: EntityFieldPersistedValue | undefined,\n\t\tcopyFromState?: FieldState,\n\t) {\n\t\tif (persistedValue instanceof Set) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Received a collection of referenced entities where a single '${field.fieldName}' field was expected. ` +\n\t\t\t\t\t`Perhaps you wanted to use a <Repeater />?`,\n\t\t\t)\n\t\t}\n\t\tif (persistedValue instanceof ServerId) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Received a referenced entity where a single '${field.fieldName}' field was expected. ` +\n\t\t\t\t\t`Perhaps you wanted to use <HasOne />?`,\n\t\t\t)\n\t\t}\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeFieldState(parent, field.placeholderName, field, persistedValue, copyFromState),\n\t\t)\n\t}\n\n\tprivate initializeFromHasOneRelationMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: HasOneRelationMarker,\n\t\tentityId: RuntimeId,\n\t\tcopyFrom?: EntityRealmState | EntityRealmStateStub,\n\t) {\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeEntityRealm(entityId, this.getRelationTargetEntityName(parent, field), {\n\t\t\t\ttype: 'hasOne',\n\t\t\t\tparent,\n\t\t\t\tmarker: field,\n\t\t\t}, copyFrom),\n\t\t)\n\t\t// if (fieldDatum instanceof ServerGeneratedUuid || fieldDatum === null || fieldDatum === undefined) {\n\t\t// \tconst entityId = fieldDatum instanceof ServerGeneratedUuid ? fieldDatum : new UnpersistedEntityDummyId()\n\t\t//\n\t\t//\n\t\t// } else if (fieldDatum instanceof Set) {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a collection of entities for field '${relation.field}' where a single entity was expected. ` +\n\t\t// \t\t\t`Perhaps you wanted to use a <Repeater />?`,\n\t\t// \t)\n\t\t// } else {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a scalar value for field '${relation.field}' where a single entity was expected.` +\n\t\t// \t\t\t`Perhaps you meant to use a variant of <Field />?`,\n\t\t// \t)\n\t\t// }\n\t}\n\n\tprivate initializeFromHasManyRelationMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: HasManyRelationMarker,\n\t\tpersistedEntityIds: Set<EntityId>,\n\t) {\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeEntityListState(\n\t\t\t\t{ type: 'hasMany', marker: field, parent },\n\t\t\t\tthis.getRelationTargetEntityName(parent, field),\n\t\t\t\tpersistedEntityIds,\n\t\t\t),\n\t\t)\n\t\t// const relation = field.parameters\n\t\t//\n\t\t// if (fieldDatum === undefined || fieldDatum instanceof Set) {\n\t\t//\n\t\t// } else if (typeof fieldDatum === 'object') {\n\t\t// \t// Intentionally allowing `fieldDatum === null` here as well since this should only happen when a *hasOne\n\t\t// \t// relation is unlinked, e.g. a Person does not have a linked Nationality.\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a referenced entity for field '${relation.field}' where a collection of entities was expected.` +\n\t\t// \t\t\t`Perhaps you wanted to use a <HasOne />?`,\n\t\t// \t)\n\t\t// } else {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a scalar value for field '${relation.field}' where a collection of entities was expected.` +\n\t\t// \t\t\t`Perhaps you meant to use a variant of <Field />?`,\n\t\t// \t)\n\t\t// }\n\t}\n\n\tprivate getRelationTargetEntityName(\n\t\tentityRealm: EntityRealmState,\n\t\tfield: HasOneRelationMarker | HasManyRelationMarker,\n\t): EntityName {\n\t\tconst targetField = this.treeStore.schema.getEntityRelation(entityRealm.entity.entityName, field.parameters.field)\n\t\treturn targetField.targetEntity\n\t}\n\n\tpublic runImmediateUserInitialization(\n\t\trealm: EntityRealmState | EntityRealmStateStub,\n\t\tinitialize: EntityAccessor.BatchUpdatesHandler | undefined,\n\t) {\n\t\tif (initialize === undefined) {\n\t\t\treturn\n\t\t}\n\t\tconst entityRealm = this.materializeEntityRealm(realm)\n\n\t\trealm.getAccessor().batchUpdates(initialize)\n\n\t\tconst initializeListeners = this.eventManager.getEventListeners(entityRealm, { type: 'initialize' })\n\t\tif (initializeListeners === undefined || initializeListeners.size === 0) {\n\t\t\trealm.entity.hasIdSetInStone = true\n\t\t}\n\t}\n\n\tpublic initializeEntityEventListenerStore(blueprint: EntityRealmBlueprint): EntityEventListenerStore | undefined {\n\t\tif (blueprint.type === 'listEntity') {\n\t\t\tconst blueprintListeners = blueprint.parent.blueprint.marker.parameters.childEventListeners\n\t\t\tconst childInitialize = blueprintListeners?.get({ type: 'initialize' })\n\t\t\tconst store: EntityEventListenerStore = new EventListenersStore(() => blueprint.parent.childEventListeners)\n\n\t\t\tif (childInitialize !== undefined) {\n\t\t\t\tstore.set({ type: 'initialize' }, childInitialize)\n\t\t\t}\n\t\t\treturn store\n\t\t}\n\n\t\tconst blueprintListeners = blueprint.marker.parameters.eventListeners\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprintListeners.clone()\n\t}\n\n\tprivate initializeEntityListEventListenerStore(\n\t\tblueprint: EntityListBlueprint,\n\t): EntityListEventListenerStore | undefined {\n\t\tconst blueprintListeners = blueprint.marker.parameters.eventListeners\n\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprintListeners.clone()\n\t}\n\n\tprivate initializeEntityListChildEventListenerStore(\n\t\tblueprint: EntityListBlueprint,\n\t): EntityEventListenerStore | undefined {\n\t\tconst blueprintListeners = blueprint.marker.parameters.childEventListeners\n\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprintListeners.clone()\n\t}\n\n\tprivate initializeFieldEventListenerStore(marker: FieldMarker): FieldEventListenerStore | undefined {\n\t\treturn marker.parameters.eventListeners?.clone()\n\t}\n}\n"],"names":["entity","blueprintListeners"],"mappings":";;;;;;;;;;;;;AA0CO,MAAM,iBAAiB;AAAA,EAKtB,YACN,sBACA,qBACiB,cACA,WAChB;AAFgB,SAAA,eAAA;AACA,SAAA,YAAA;AARD,kBAAA,MAAA,iBAAA;AACA,kBAAA,MAAA,kBAAA;AACA,kBAAA,MAAA,gBAAA;AAQX,SAAA,kBAAkB,IAAI,gBAAgB,sBAAsB,KAAK,cAAc,MAAM,KAAK,SAAS;AACnG,SAAA,mBAAmB,IAAI,iBAAiB,sBAAsB,qBAAqB,KAAK,cAAc,MAAM,KAAK,SAAS;AAC1H,SAAA,iBAAiB,IAAI,eAAe,sBAAsB,qBAAqB,KAAK,cAAc,MAAM,KAAK,SAAS;AAAA,EAAA;AAAA,EAGrH,kBAAkB,MAAoE;AACxF,QAAA;AACJ,UAAM,oBAAoB,KAAK,UAAU,qBAAqB,IAAI,KAAK,eAAe;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BAAmB,MAAM;AAAA,EAAA;AAAA,EAGzB,4CACP,WACuC;AACjC,UAAA,qBAAqB,UAAU,OAAO,WAAW;AAEvD,QAAI,uBAAuB,QAAW;AAC9B,aAAA;AAAA,IAAA;AAER,WAAO,mBAAmB,MAAM;AAAA,EAAA;AAAA,EAGzB,kCAAkC,QAA0D;AAC5F,WAAA,OAAO,WAAW,gBAAgB,MAAM;AAAA,EAAA;AAEjD;"}
1
+ {"version":3,"file":"StateInitializer.js","sources":["../../../../../packages/binding-legacy/src/core/StateInitializer.ts"],"sourcesContent":["import { BatchUpdatesOptions, EntityAccessor, FieldAccessor } from '@contember/binding-common'\nimport { RuntimeId, ServerId, UnpersistedEntityDummyId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { EntityListSubTreeMarker, EntitySubTreeMarker, FieldMarker, HasManyRelationMarker, HasOneRelationMarker } from '@contember/binding-common'\nimport { BijectiveIndexedMap } from '@contember/utilities'\nimport type {\n\tEntityEventListenerStore,\n\tEntityId,\n\tEntityListEventListenerStore,\n\tEntityName,\n\tFieldEventListenerStore,\n\tFieldName,\n\tFieldValue,\n} from '@contember/binding-common'\nimport { EventListenersStore } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { AccessorErrorManager } from './AccessorErrorManager'\nimport type { EventManager } from './EventManager'\nimport { EntityOperations, FieldOperations, ListOperations } from './operations'\nimport { RealmKeyGenerator } from './RealmKeyGenerator'\nimport {\n\tEntityListBlueprint,\n\tEntityListState,\n\tEntityRealmBlueprint,\n\tEntityRealmState,\n\tEntityRealmStateStub,\n\tEntityState,\n\tFieldState,\n\tgetEntityMarker,\n\tRootStateNode,\n} from './state'\nimport { TreeParameterMerger } from '@contember/binding-common'\nimport type { TreeStore } from './TreeStore'\nimport { EntityAccessorImpl, EntityListAccessorImpl, FieldAccessorImpl } from '../accessors'\nimport { EntityFieldPersistedData, EntityFieldPersistedValue } from '../accessorTree'\n\nexport class StateInitializer {\n\tprivate readonly fieldOperations: FieldOperations\n\tprivate readonly entityOperations: EntityOperations\n\tprivate readonly listOperations: ListOperations\n\n\tpublic constructor(\n\t\taccessorErrorManager: AccessorErrorManager,\n\t\tbatchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {\n\t\tthis.fieldOperations = new FieldOperations(accessorErrorManager, this.eventManager, this, this.treeStore)\n\t\tthis.entityOperations = new EntityOperations(accessorErrorManager, batchUpdatesOptions, this.eventManager, this, this.treeStore)\n\t\tthis.listOperations = new ListOperations(accessorErrorManager, batchUpdatesOptions, this.eventManager, this, this.treeStore)\n\t}\n\n\tpublic initializeSubTree(tree: EntitySubTreeMarker | EntityListSubTreeMarker): RootStateNode {\n\t\tlet subTreeState: RootStateNode\n\t\tconst persistedRootData = this.treeStore.subTreePersistedData.get(tree.placeholderName)\n\n\t\tif (tree instanceof EntityListSubTreeMarker) {\n\t\t\tconst persistedEntityIds: Set<EntityId> = persistedRootData instanceof Set ? persistedRootData : new Set()\n\t\t\tsubTreeState = this.initializeEntityListState(\n\t\t\t\t{ type: 'subTree', marker: tree, parent: undefined },\n\t\t\t\ttree.entityName,\n\t\t\t\tpersistedEntityIds,\n\t\t\t)\n\t\t} else {\n\t\t\tsubTreeState = this.materializeEntityRealm(\n\t\t\t\tthis.initializeEntityRealm(\n\t\t\t\t\tpersistedRootData instanceof ServerId ? persistedRootData : new UnpersistedEntityDummyId(),\n\t\t\t\t\ttree.entityName,\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'subTree',\n\t\t\t\t\t\tmarker: tree,\n\t\t\t\t\t\tparent: undefined,\n\t\t\t\t\t},\n\t\t\t\t),\n\t\t\t)\n\t\t}\n\n\t\treturn subTreeState\n\t}\n\n\tpublic initializeEntityRealm(\n\t\tid: RuntimeId,\n\t\tentityName: EntityName,\n\t\tblueprint: EntityRealmBlueprint,\n\t\tcopyFrom?: EntityRealmState | EntityRealmStateStub,\n\t): EntityRealmState | EntityRealmStateStub {\n\t\t// This is counter-intuitive a bit. The method can also return an EntityRealmState, not just a stub.\n\t\t// The reason is so that each call-site can just default to trying to initialize a stub without having to keep\n\t\t// in mind that the realm might have already been initialized.\n\n\t\tconst entity = this.initializeEntityState(id, entityName)\n\t\tconst realmKey = RealmKeyGenerator.getRealmKey(id, blueprint)\n\n\t\tconst existing = this.treeStore.entityRealmStore.get(realmKey)\n\t\tif (existing !== undefined) {\n\t\t\treturn existing\n\t\t}\n\n\t\tif (!copyFrom || copyFrom.type !== 'entityRealm') {\n\t\t\tconst stub: EntityRealmStateStub = {\n\t\t\t\ttype: 'entityRealmStub',\n\n\t\t\t\tblueprint,\n\t\t\t\tentity,\n\t\t\t\trealmKey,\n\t\t\t\tgetAccessor: () => this.materializeEntityRealm(stub).getAccessor(),\n\t\t\t}\n\t\t\tthis.registerEntityRealm(stub)\n\n\t\t\treturn stub\n\t\t}\n\t\tconst realm = this.createEntityRealm(blueprint, realmKey, entity, copyFrom)\n\n\t\tthis.registerEntityRealm(realm)\n\n\t\tconst marker = getEntityMarker(realm)\n\t\tconst pathBack = this.treeStore.getPathBackToParent(realm)\n\n\t\tfor (const [placeholderName, field] of marker.fields.markers) {\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\tif (!value || value.type !== 'field') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromFieldMarker(realm, field, undefined, value)\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\tif (!value || value.type !== 'entityList') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasManyRelationMarker(realm, field, new Set(value.children.keys()))\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tlet runtimeId: RuntimeId\n\t\t\t\tlet subCopyFrom: EntityRealmState | undefined\n\t\t\t\tif (pathBack?.fieldBackToParent === field.parameters.field && !field.parameters.reducedBy) {\n\t\t\t\t\truntimeId = pathBack.parent.entity.id\n\t\t\t\t} else {\n\t\t\t\t\tconst value = copyFrom.children.get(placeholderName)\n\t\t\t\t\tif (!value || (value.type !== 'entityRealm' && value.type !== 'entityRealmStub')) {\n\t\t\t\t\t\tthrow new BindingError()\n\t\t\t\t\t}\n\t\t\t\t\truntimeId = !(value.entity.id instanceof UnpersistedEntityDummyId) ? value.entity.id : new UnpersistedEntityDummyId()\n\t\t\t\t\tif (value.type === 'entityRealm') {\n\t\t\t\t\t\tsubCopyFrom = value\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasOneRelationMarker(realm, field, runtimeId, subCopyFrom)\n\t\t\t} else {\n\t\t\t\tassertNever(field)\n\t\t\t}\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(realm)\n\t\treturn realm\n\t}\n\n\tprivate createEntityRealm(\n\t\tblueprint: EntityRealmBlueprint,\n\t\trealmKey: string,\n\t\tentity: EntityState,\n\t\tcopyFrom?: EntityRealmState,\n\t): EntityRealmState {\n\t\tconst entityRealm: EntityRealmState = {\n\t\t\ttype: 'entityRealm',\n\n\t\t\tblueprint,\n\t\t\trealmKey,\n\t\t\tentity,\n\n\t\t\taccessor: undefined,\n\t\t\tchildren: new Map(),\n\t\t\tchildrenWithPendingUpdates: undefined,\n\t\t\terrors: undefined,\n\t\t\teventListeners: this.initializeEntityEventListenerStore(blueprint),\n\t\t\tfieldsWithPendingConnectionUpdates: undefined,\n\t\t\tplannedHasOneDeletions: undefined,\n\t\t\tunpersistedChangesCount: copyFrom?.unpersistedChangesCount ?? 0,\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (entityRealm.accessor === undefined) {\n\t\t\t\t\tconst entity = entityRealm.entity\n\t\t\t\t\tentityRealm.accessor = new EntityAccessorImpl(\n\t\t\t\t\t\tentityRealm,\n\t\t\t\t\t\tthis.entityOperations,\n\t\t\t\t\t\tentity.id,\n\t\t\t\t\t\tentityRealm.realmKey,\n\t\t\t\t\t\tentity.entityName,\n\t\t\t\t\t\t// We're technically exposing more info in runtime than we'd like but that way we don't have to allocate and\n\t\t\t\t\t\t// keep in sync two copies of the same data. TS hides the extra info anyway.\n\t\t\t\t\t\tentityRealm.children,\n\t\t\t\t\t\tthis.treeStore.persistedEntityData.get(entity.id.uniqueValue),\n\t\t\t\t\t\tentityRealm.unpersistedChangesCount !== 0,\n\t\t\t\t\t\tentityRealm.errors,\n\t\t\t\t\t\tgetEntityMarker(entityRealm).environment,\n\t\t\t\t\t\tentityRealm.getAccessor,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn entityRealm.accessor\n\t\t\t},\n\t\t}\n\t\treturn entityRealm\n\t}\n\n\tprivate materializeEntityRealm(state: EntityRealmState | EntityRealmStateStub): EntityRealmState {\n\t\tif (state.type === 'entityRealm') {\n\t\t\treturn state\n\t\t}\n\t\tconst { realmKey, entity, blueprint } = state\n\n\t\tconst existing = this.treeStore.entityRealmStore.get(realmKey)\n\t\tif (existing !== undefined && existing.type === 'entityRealm') {\n\t\t\treturn existing\n\t\t}\n\n\t\tconst entityRealm = this.createEntityRealm(blueprint, realmKey, entity)\n\n\t\tthis.registerEntityRealm(entityRealm)\n\n\t\t// if (blueprint.creationParameters.forceCreation && !entity.id.existsOnServer) {\n\t\t// \tentityRealm.unpersistedChangesCount += 1\n\t\t// }\n\n\t\tconst persistedData = this.treeStore.persistedEntityData.get(entity.id.uniqueValue)\n\n\t\tconst marker = getEntityMarker(state)\n\t\tconst pathBack = this.treeStore.getPathBackToParent(entityRealm)\n\n\t\tfor (const [placeholderName, field] of marker.fields.markers) {\n\t\t\tconst fieldDatum = persistedData?.get(placeholderName)?.value\n\n\t\t\tif (field instanceof FieldMarker) {\n\t\t\t\tthis.initializeFromFieldMarker(entityRealm, field, fieldDatum)\n\t\t\t} else if (field instanceof HasManyRelationMarker) {\n\t\t\t\t// if (pathBack?.fieldBackToParent === field.parameters.field) {\n\t\t\t\t// \t// TODO this is probably wrong?\n\t\t\t\t// \tconst ids = pathBack.parent.children.get(field.placeholderName)\n\t\t\t\t// \tconst newData = this.treeStore.persistedEntityData.get(pathBack.parent.entity.id.value)\n\t\t\t\t// \tconsole.log('many?', pathBack, field, entityRealm, fieldDatum, ids)\n\t\t\t\t// }\n\t\t\t\tthis.initializeFromHasManyRelationMarker(entityRealm, field, fieldDatum instanceof Set ? fieldDatum : new Set())\n\t\t\t} else if (field instanceof HasOneRelationMarker) {\n\t\t\t\tlet runtimeId: RuntimeId\n\t\t\t\tif (pathBack?.fieldBackToParent === field.parameters.field && !field.parameters.reducedBy) {\n\t\t\t\t\truntimeId = pathBack.parent.entity.id\n\t\t\t\t} else {\n\t\t\t\t\truntimeId = fieldDatum instanceof ServerId ? fieldDatum : new UnpersistedEntityDummyId()\n\t\t\t\t}\n\t\t\t\tthis.initializeFromHasOneRelationMarker(entityRealm, field, runtimeId)\n\t\t\t} else {\n\t\t\t\tassertNever(field)\n\t\t\t}\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(entityRealm)\n\n\t\treturn entityRealm\n\t}\n\n\tprivate registerEntityRealm(entityRealm: EntityRealmState | EntityRealmStateStub) {\n\t\tconst { realmKey, blueprint, entity } = entityRealm\n\n\t\tthis.treeStore.entityRealmStore.set(entityRealm.realmKey, entityRealm)\n\n\t\tif (blueprint.type === 'listEntity') {\n\t\t\tblueprint.parent.children.set(entity.id.value, entityRealm)\n\t\t} else if (blueprint.type === 'hasOne') {\n\t\t\tblueprint.parent.children.set(blueprint.marker.placeholderName, entityRealm)\n\t\t}\n\t\tentity.realms.set(realmKey, entityRealm)\n\t}\n\n\tpublic initializeEntityState(id: RuntimeId, entityName: EntityName): EntityState {\n\t\tconst entityId = id.uniqueValue\n\n\t\tconst existing = this.treeStore.entityStore.get(entityId)\n\t\tif (existing !== undefined) {\n\t\t\treturn existing\n\t\t}\n\n\t\tconst entityState: EntityState = {\n\t\t\tentityName,\n\t\t\thasIdSetInStone: id instanceof ServerId,\n\t\t\tid,\n\t\t\tisScheduledForDeletion: false,\n\t\t\tmaidenId: id instanceof UnpersistedEntityDummyId ? id : undefined,\n\t\t\trealms: new Map(),\n\t\t}\n\t\tthis.treeStore.entityStore.set(entityId, entityState)\n\n\t\treturn entityState\n\t}\n\n\tprivate initializeEntityListState(\n\t\tblueprint: EntityListBlueprint,\n\t\tentityName: EntityName,\n\t\tinitialEntityIds: Set<EntityId>,\n\t): EntityListState {\n\t\tconst entityListState: EntityListState = {\n\t\t\ttype: 'entityList',\n\t\t\taccessor: undefined,\n\t\t\tblueprint,\n\t\t\tchildren: new BijectiveIndexedMap(realm => realm.entity.id.value),\n\t\t\tchildrenWithPendingUpdates: undefined,\n\t\t\tentityName,\n\t\t\teventListeners: this.initializeEntityListEventListenerStore(blueprint),\n\t\t\tchildEventListeners: this.initializeEntityListChildEventListenerStore(blueprint),\n\t\t\terrors: undefined,\n\t\t\tplannedRemovals: undefined,\n\t\t\tunpersistedChangesCount: 0, // TODO force creation?\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (entityListState.accessor === undefined) {\n\t\t\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(entityListState)\n\t\t\t\t\tentityListState.accessor = new EntityListAccessorImpl(\n\t\t\t\t\t\tentityListState,\n\t\t\t\t\t\tthis.listOperations,\n\t\t\t\t\t\tentityListState.entityName,\n\t\t\t\t\t\tentityListState.children,\n\t\t\t\t\t\tpersistedEntityIds,\n\t\t\t\t\t\tentityListState.unpersistedChangesCount !== 0,\n\t\t\t\t\t\tentityListState.errors,\n\t\t\t\t\t\tentityListState.blueprint.marker.environment,\n\t\t\t\t\t\tentityListState.getAccessor,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn entityListState.accessor\n\t\t\t},\n\t\t}\n\n\t\tconst initialData: Iterable<EntityId | undefined> = initialEntityIds.size === 0\n\t\t\t? Array.from({ length: blueprint.marker.parameters.initialEntityCount })\n\t\t\t: initialEntityIds\n\t\tfor (const entityId of initialData) {\n\t\t\tthis.initializeEntityRealm(\n\t\t\t\tentityId ? new ServerId(entityId, entityName) : new UnpersistedEntityDummyId(),\n\t\t\t\tentityListState.entityName,\n\t\t\t\t{\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: entityListState,\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(entityListState)\n\n\t\treturn entityListState\n\t}\n\n\tprivate initializeFieldState(\n\t\tparent: EntityRealmState,\n\t\tplaceholderName: FieldName,\n\t\tfieldMarker: FieldMarker,\n\t\tpersistedValue: FieldValue | undefined,\n\t\tcopyFromState: FieldState | undefined,\n\t): FieldState {\n\t\tconst resolvedFieldValue = copyFromState ? copyFromState.value : (persistedValue ?? fieldMarker.defaultValue ?? null)\n\n\t\tconst fieldState: FieldState = {\n\t\t\ttype: 'field',\n\t\t\taccessor: undefined,\n\t\t\tfieldMarker,\n\t\t\tplaceholderName,\n\t\t\tpersistedValue,\n\t\t\tparent,\n\t\t\tvalue: resolvedFieldValue,\n\t\t\teventListeners: this.initializeFieldEventListenerStore(fieldMarker),\n\t\t\terrors: undefined,\n\t\t\ttouchLog: undefined,\n\t\t\thasUnpersistedChanges: copyFromState?.hasUnpersistedChanges ?? false,\n\n\t\t\tgetAccessor: () => {\n\t\t\t\tif (fieldState.accessor === undefined) {\n\t\t\t\t\tconst fieldSchema = this.treeStore.schema.getEntityColumn(parent.entity.entityName, fieldMarker.fieldName)\n\n\t\t\t\t\tfieldState.accessor = new FieldAccessorImpl(\n\t\t\t\t\t\tfieldState,\n\t\t\t\t\t\tthis.fieldOperations,\n\t\t\t\t\t\tfieldState.placeholderName,\n\t\t\t\t\t\tfieldState.value,\n\t\t\t\t\t\tfieldState.persistedValue === undefined ? null : fieldState.persistedValue,\n\t\t\t\t\t\tfieldState.fieldMarker.defaultValue,\n\t\t\t\t\t\tfieldState.errors,\n\t\t\t\t\t\tfieldState.hasUnpersistedChanges,\n\t\t\t\t\t\tfieldState.touchLog,\n\t\t\t\t\t\tfieldState.getAccessor,\n\t\t\t\t\t\tfieldSchema,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn fieldState.accessor\n\t\t\t},\n\t\t}\n\n\t\tthis.eventManager.registerNewlyInitialized(fieldState)\n\t\treturn fieldState\n\t}\n\n\tprivate initializeFromFieldMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: FieldMarker,\n\t\tpersistedValue: EntityFieldPersistedValue | undefined,\n\t\tcopyFromState?: FieldState,\n\t) {\n\t\tif (persistedValue instanceof Set) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Received a collection of referenced entities where a single '${field.fieldName}' field was expected. `\n\t\t\t\t\t+ `Perhaps you wanted to use a <Repeater />?`,\n\t\t\t)\n\t\t}\n\t\tif (persistedValue instanceof ServerId) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Received a referenced entity where a single '${field.fieldName}' field was expected. `\n\t\t\t\t\t+ `Perhaps you wanted to use <HasOne />?`,\n\t\t\t)\n\t\t}\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeFieldState(parent, field.placeholderName, field, persistedValue, copyFromState),\n\t\t)\n\t}\n\n\tprivate initializeFromHasOneRelationMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: HasOneRelationMarker,\n\t\tentityId: RuntimeId,\n\t\tcopyFrom?: EntityRealmState | EntityRealmStateStub,\n\t) {\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeEntityRealm(entityId, this.getRelationTargetEntityName(parent, field), {\n\t\t\t\ttype: 'hasOne',\n\t\t\t\tparent,\n\t\t\t\tmarker: field,\n\t\t\t}, copyFrom),\n\t\t)\n\t\t// if (fieldDatum instanceof ServerGeneratedUuid || fieldDatum === null || fieldDatum === undefined) {\n\t\t// \tconst entityId = fieldDatum instanceof ServerGeneratedUuid ? fieldDatum : new UnpersistedEntityDummyId()\n\t\t//\n\t\t//\n\t\t// } else if (fieldDatum instanceof Set) {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a collection of entities for field '${relation.field}' where a single entity was expected. ` +\n\t\t// \t\t\t`Perhaps you wanted to use a <Repeater />?`,\n\t\t// \t)\n\t\t// } else {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a scalar value for field '${relation.field}' where a single entity was expected.` +\n\t\t// \t\t\t`Perhaps you meant to use a variant of <Field />?`,\n\t\t// \t)\n\t\t// }\n\t}\n\n\tprivate initializeFromHasManyRelationMarker(\n\t\tparent: EntityRealmState,\n\t\tfield: HasManyRelationMarker,\n\t\tpersistedEntityIds: Set<EntityId>,\n\t) {\n\t\tparent.children.set(\n\t\t\tfield.placeholderName,\n\t\t\tthis.initializeEntityListState(\n\t\t\t\t{ type: 'hasMany', marker: field, parent },\n\t\t\t\tthis.getRelationTargetEntityName(parent, field),\n\t\t\t\tpersistedEntityIds,\n\t\t\t),\n\t\t)\n\t\t// const relation = field.parameters\n\t\t//\n\t\t// if (fieldDatum === undefined || fieldDatum instanceof Set) {\n\t\t//\n\t\t// } else if (typeof fieldDatum === 'object') {\n\t\t// \t// Intentionally allowing `fieldDatum === null` here as well since this should only happen when a *hasOne\n\t\t// \t// relation is unlinked, e.g. a Person does not have a linked Nationality.\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a referenced entity for field '${relation.field}' where a collection of entities was expected.` +\n\t\t// \t\t\t`Perhaps you wanted to use a <HasOne />?`,\n\t\t// \t)\n\t\t// } else {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Received a scalar value for field '${relation.field}' where a collection of entities was expected.` +\n\t\t// \t\t\t`Perhaps you meant to use a variant of <Field />?`,\n\t\t// \t)\n\t\t// }\n\t}\n\n\tprivate getRelationTargetEntityName(\n\t\tentityRealm: EntityRealmState,\n\t\tfield: HasOneRelationMarker | HasManyRelationMarker,\n\t): EntityName {\n\t\tconst targetField = this.treeStore.schema.getEntityRelation(entityRealm.entity.entityName, field.parameters.field)\n\t\treturn targetField.targetEntity\n\t}\n\n\tpublic runImmediateUserInitialization(\n\t\trealm: EntityRealmState | EntityRealmStateStub,\n\t\tinitialize: EntityAccessor.BatchUpdatesHandler | undefined,\n\t) {\n\t\tif (initialize === undefined) {\n\t\t\treturn\n\t\t}\n\t\tconst entityRealm = this.materializeEntityRealm(realm)\n\n\t\trealm.getAccessor().batchUpdates(initialize)\n\n\t\tconst initializeListeners = this.eventManager.getEventListeners(entityRealm, { type: 'initialize' })\n\t\tif (initializeListeners === undefined || initializeListeners.size === 0) {\n\t\t\trealm.entity.hasIdSetInStone = true\n\t\t}\n\t}\n\n\tpublic initializeEntityEventListenerStore(blueprint: EntityRealmBlueprint): EntityEventListenerStore | undefined {\n\t\tif (blueprint.type === 'listEntity') {\n\t\t\tconst blueprintListeners = blueprint.parent.blueprint.marker.parameters.childEventListeners\n\t\t\tconst childInitialize = blueprintListeners?.get({ type: 'initialize' })\n\t\t\tconst store: EntityEventListenerStore = new EventListenersStore(() => blueprint.parent.childEventListeners)\n\n\t\t\tif (childInitialize !== undefined) {\n\t\t\t\tstore.set({ type: 'initialize' }, childInitialize)\n\t\t\t}\n\t\t\treturn store\n\t\t}\n\n\t\tconst blueprintListeners = blueprint.marker.parameters.eventListeners\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprintListeners.clone()\n\t}\n\n\tprivate initializeEntityListEventListenerStore(\n\t\tblueprint: EntityListBlueprint,\n\t): EntityListEventListenerStore | undefined {\n\t\tconst blueprintListeners = blueprint.marker.parameters.eventListeners\n\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprintListeners.clone()\n\t}\n\n\tprivate initializeEntityListChildEventListenerStore(\n\t\tblueprint: EntityListBlueprint,\n\t): EntityEventListenerStore | undefined {\n\t\tconst blueprintListeners = blueprint.marker.parameters.childEventListeners\n\n\t\tif (blueprintListeners === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\treturn blueprintListeners.clone()\n\t}\n\n\tprivate initializeFieldEventListenerStore(marker: FieldMarker): FieldEventListenerStore | undefined {\n\t\treturn marker.parameters.eventListeners?.clone()\n\t}\n}\n"],"names":["entity","blueprintListeners"],"mappings":";;;;;;;;;;;;;AAoCO,MAAM,iBAAiB;AAAA,EAKtB,YACN,sBACA,qBACiB,cACA,WAChB;AAFgB,SAAA,eAAA;AACA,SAAA,YAAA;AARlB,kBAAA,MAAiB,iBAAA;AACjB,kBAAA,MAAiB,kBAAA;AACjB,kBAAA,MAAiB,gBAAA;AAQhB,SAAK,kBAAkB,IAAI,gBAAgB,sBAAsB,KAAK,cAAc,MAAM,KAAK,SAAS;AACxG,SAAK,mBAAmB,IAAI,iBAAiB,sBAAsB,qBAAqB,KAAK,cAAc,MAAM,KAAK,SAAS;AAC/H,SAAK,iBAAiB,IAAI,eAAe,sBAAsB,qBAAqB,KAAK,cAAc,MAAM,KAAK,SAAS;AAAA,EAC5H;AAAA,EAEO,kBAAkB,MAAoE;AAC5F,QAAI;AACJ,UAAM,oBAAoB,KAAK,UAAU,qBAAqB,IAAI,KAAK,eAAe;AAEtF,QAAI,gBAAgB,yBAAyB;AAC5C,YAAM,qBAAoC,6BAA6B,MAAM,wCAAwB,IAAA;AACrG,qBAAe,KAAK;AAAA,QACnB,EAAE,MAAM,WAAW,QAAQ,MAAM,QAAQ,OAAA;AAAA,QACzC,KAAK;AAAA,QACL;AAAA,MAAA;AAAA,IAEF,OAAO;AACN,qBAAe,KAAK;AAAA,QACnB,KAAK;AAAA,UACJ,6BAA6B,WAAW,oBAAoB,IAAI,yBAAA;AAAA,UAChE,KAAK;AAAA,UACL;AAAA,YACC,MAAM;AAAA,YACN,QAAQ;AAAA,YACR,QAAQ;AAAA,UAAA;AAAA,QACT;AAAA,MACD;AAAA,IAEF;AAEA,WAAO;AAAA,EACR;AAAA,EAEO,sBACN,IACA,YACA,WACA,UAC0C;AAK1C,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M,cAAc;AACnG,QAAI,wBAAwB,UAAa,oBAAoB,SAAS,GAAG;AACxE,YAAM,OAAO,kBAAkB;AAAA,IAChC;AAAA,EACD;AAAA,EAEO,mCAAmC,WAAuE;AAChH,QAAI,UAAU,SAAS,cAAc;AACpC,YAAMC,sBAAqB,UAAU,OAAO,UAAU,OAAO,WAAW;AACxE,YAAM,kBAAkBA,qBAAoB,IAAI,EAAE,MAAM,cAAc;AACtE,YAAM,QAAkC,IAAI,oBAAoB,MAAM,UAAU,OAAO,mBAAmB;AAE1G,UAAI,oBAAoB,QAAW;AAClC,cAAM,IAAI,EAAE,MAAM,aAAA,GAAgB,eAAe;AAAA,MAClD;AACA,aAAO;AAAA,IACR;AAEA,UAAM,qBAAqB,UAAU,OAAO,WAAW;AACvD,QAAI,uBAAuB,QAAW;AACrC,aAAO;AAAA,IACR;AACA,WAAO,mBAAmB,MAAA;AAAA,EAC3B;AAAA,EAEQ,uCACP,WAC2C;AAC3C,UAAM,qBAAqB,UAAU,OAAO,WAAW;AAEvD,QAAI,uBAAuB,QAAW;AACrC,aAAO;AAAA,IACR;AACA,WAAO,mBAAmB,MAAA;AAAA,EAC3B;AAAA,EAEQ,4CACP,WACuC;AACvC,UAAM,qBAAqB,UAAU,OAAO,WAAW;AAEvD,QAAI,uBAAuB,QAAW;AACrC,aAAO;AAAA,IACR;AACA,WAAO,mBAAmB,MAAA;AAAA,EAC3B;AAAA,EAEQ,kCAAkC,QAA0D;AACnG,WAAO,OAAO,WAAW,gBAAgB,MAAA;AAAA,EAC1C;AACD;"}
@@ -97,7 +97,7 @@ class TreeAugmenter {
97
97
  }
98
98
  updateRealmIdIfNecessary(realm, newId) {
99
99
  const currentId = realm.entity.id;
100
- let idToChangeTo = void 0;
100
+ let idToChangeTo;
101
101
  if (currentId instanceof bindingCommon.ServerId) {
102
102
  if (newId === void 0 || newId === null) {
103
103
  idToChangeTo = new bindingCommon.UnpersistedEntityDummyId();
@@ -174,10 +174,7 @@ class TreeAugmenter {
174
174
  if (
175
175
  // It's a uuid but it's not among the persisted so either it got deleted/disconnected or the client-side
176
176
  // id generation didn't quite pan out.
177
- childRuntimeId instanceof bindingCommon.ServerId || childRuntimeId instanceof bindingCommon.ClientGeneratedUuid || // No persisted entity id to allocate to this so that's a goodbye.
178
- persistedWithoutState.size === 0 || // not a persisted entity
179
- child.type === "entityRealmStub" || // We couldn't find a matching persisted entity
180
- !this.tryAssignIdToMatching(child, persistedWithoutState)
177
+ childRuntimeId instanceof bindingCommon.ServerId || childRuntimeId instanceof bindingCommon.ClientGeneratedUuid || persistedWithoutState.size === 0 || child.type === "entityRealmStub" || !this.tryAssignIdToMatching(child, persistedWithoutState)
181
178
  ) {
182
179
  state.getAccessor().disconnectEntity(child.getAccessor());
183
180
  continue;