@contember/binding-legacy 2.1.0-beta.1 → 2.2.0-alpha.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 (230) hide show
  1. package/dist/development/src/accessors/EntityAccessorImpl.cjs.map +1 -1
  2. package/dist/development/src/accessors/EntityAccessorImpl.js.map +1 -1
  3. package/dist/development/src/accessors/EntityListAccessorImpl.cjs.map +1 -1
  4. package/dist/development/src/accessors/EntityListAccessorImpl.js.map +1 -1
  5. package/dist/development/src/accessors/FieldAccessorImpl.cjs.map +1 -1
  6. package/dist/development/src/accessors/FieldAccessorImpl.js.map +1 -1
  7. package/dist/development/src/core/AccessorErrorManager.cjs.map +1 -1
  8. package/dist/development/src/core/AccessorErrorManager.js.map +1 -1
  9. package/dist/development/src/core/Config.cjs.map +1 -1
  10. package/dist/development/src/core/Config.js.map +1 -1
  11. package/dist/development/src/core/DataBinding.cjs.map +1 -1
  12. package/dist/development/src/core/DataBinding.js.map +1 -1
  13. package/dist/development/src/core/ErrorsPreprocessor.cjs.map +1 -1
  14. package/dist/development/src/core/ErrorsPreprocessor.js.map +1 -1
  15. package/dist/development/src/core/EventManager.cjs.map +1 -1
  16. package/dist/development/src/core/EventManager.js.map +1 -1
  17. package/dist/development/src/core/MutationGenerator.cjs.map +1 -1
  18. package/dist/development/src/core/MutationGenerator.js.map +1 -1
  19. package/dist/development/src/core/RealmKeyGenerator.cjs.map +1 -1
  20. package/dist/development/src/core/RealmKeyGenerator.js.map +1 -1
  21. package/dist/development/src/core/RequestResponseNormalizer.cjs.map +1 -1
  22. package/dist/development/src/core/RequestResponseNormalizer.js.map +1 -1
  23. package/dist/development/src/core/StateInitializer.cjs +23 -1
  24. package/dist/development/src/core/StateInitializer.cjs.map +1 -1
  25. package/dist/development/src/core/StateInitializer.js +23 -1
  26. package/dist/development/src/core/StateInitializer.js.map +1 -1
  27. package/dist/development/src/core/TreeAugmenter.cjs.map +1 -1
  28. package/dist/development/src/core/TreeAugmenter.js.map +1 -1
  29. package/dist/development/src/core/TreeStore.cjs.map +1 -1
  30. package/dist/development/src/core/TreeStore.js.map +1 -1
  31. package/dist/development/src/core/exceptions/ErrorLocator.cjs.map +1 -1
  32. package/dist/development/src/core/exceptions/ErrorLocator.js.map +1 -1
  33. package/dist/development/src/core/exceptions/MarkerSugarer.cjs.map +1 -1
  34. package/dist/development/src/core/exceptions/MarkerSugarer.js.map +1 -1
  35. package/dist/development/src/core/factories/createBatchUpdatesOptions.cjs.map +1 -1
  36. package/dist/development/src/core/factories/createBatchUpdatesOptions.js.map +1 -1
  37. package/dist/development/src/core/operations/EntityOperations.cjs.map +1 -1
  38. package/dist/development/src/core/operations/EntityOperations.js.map +1 -1
  39. package/dist/development/src/core/operations/FieldOperations.cjs.map +1 -1
  40. package/dist/development/src/core/operations/FieldOperations.js.map +1 -1
  41. package/dist/development/src/core/operations/ListOperations.cjs.map +1 -1
  42. package/dist/development/src/core/operations/ListOperations.js.map +1 -1
  43. package/dist/development/src/core/operations/OperationsHelpers.cjs.map +1 -1
  44. package/dist/development/src/core/operations/OperationsHelpers.js.map +1 -1
  45. package/dist/development/src/core/state/StateIterator.cjs.map +1 -1
  46. package/dist/development/src/core/state/StateIterator.js.map +1 -1
  47. package/dist/development/src/core/state/getEntityMarker.cjs.map +1 -1
  48. package/dist/development/src/core/state/getEntityMarker.js.map +1 -1
  49. package/dist/development/src/fieldHelpers/helpers/TemporalFieldHelper.cjs.map +1 -1
  50. package/dist/development/src/fieldHelpers/helpers/TemporalFieldHelper.js.map +1 -1
  51. package/dist/development/src/fieldHelpers/helpers/UuidFieldHelper.cjs.map +1 -1
  52. package/dist/development/src/fieldHelpers/helpers/UuidFieldHelper.js.map +1 -1
  53. package/dist/production/src/accessors/EntityAccessorImpl.cjs.map +1 -1
  54. package/dist/production/src/accessors/EntityAccessorImpl.js.map +1 -1
  55. package/dist/production/src/accessors/EntityListAccessorImpl.cjs.map +1 -1
  56. package/dist/production/src/accessors/EntityListAccessorImpl.js.map +1 -1
  57. package/dist/production/src/accessors/FieldAccessorImpl.cjs.map +1 -1
  58. package/dist/production/src/accessors/FieldAccessorImpl.js.map +1 -1
  59. package/dist/production/src/core/AccessorErrorManager.cjs.map +1 -1
  60. package/dist/production/src/core/AccessorErrorManager.js.map +1 -1
  61. package/dist/production/src/core/Config.cjs.map +1 -1
  62. package/dist/production/src/core/Config.js.map +1 -1
  63. package/dist/production/src/core/DataBinding.cjs.map +1 -1
  64. package/dist/production/src/core/DataBinding.js.map +1 -1
  65. package/dist/production/src/core/ErrorsPreprocessor.cjs.map +1 -1
  66. package/dist/production/src/core/ErrorsPreprocessor.js.map +1 -1
  67. package/dist/production/src/core/EventManager.cjs.map +1 -1
  68. package/dist/production/src/core/EventManager.js.map +1 -1
  69. package/dist/production/src/core/MutationGenerator.cjs.map +1 -1
  70. package/dist/production/src/core/MutationGenerator.js.map +1 -1
  71. package/dist/production/src/core/RealmKeyGenerator.cjs.map +1 -1
  72. package/dist/production/src/core/RealmKeyGenerator.js.map +1 -1
  73. package/dist/production/src/core/RequestResponseNormalizer.cjs.map +1 -1
  74. package/dist/production/src/core/RequestResponseNormalizer.js.map +1 -1
  75. package/dist/production/src/core/StateInitializer.cjs +23 -1
  76. package/dist/production/src/core/StateInitializer.cjs.map +1 -1
  77. package/dist/production/src/core/StateInitializer.js +23 -1
  78. package/dist/production/src/core/StateInitializer.js.map +1 -1
  79. package/dist/production/src/core/TreeAugmenter.cjs.map +1 -1
  80. package/dist/production/src/core/TreeAugmenter.js.map +1 -1
  81. package/dist/production/src/core/TreeStore.cjs.map +1 -1
  82. package/dist/production/src/core/TreeStore.js.map +1 -1
  83. package/dist/production/src/core/exceptions/ErrorLocator.cjs.map +1 -1
  84. package/dist/production/src/core/exceptions/ErrorLocator.js.map +1 -1
  85. package/dist/production/src/core/exceptions/MarkerSugarer.cjs.map +1 -1
  86. package/dist/production/src/core/exceptions/MarkerSugarer.js.map +1 -1
  87. package/dist/production/src/core/factories/createBatchUpdatesOptions.cjs.map +1 -1
  88. package/dist/production/src/core/factories/createBatchUpdatesOptions.js.map +1 -1
  89. package/dist/production/src/core/operations/EntityOperations.cjs.map +1 -1
  90. package/dist/production/src/core/operations/EntityOperations.js.map +1 -1
  91. package/dist/production/src/core/operations/FieldOperations.cjs.map +1 -1
  92. package/dist/production/src/core/operations/FieldOperations.js.map +1 -1
  93. package/dist/production/src/core/operations/ListOperations.cjs.map +1 -1
  94. package/dist/production/src/core/operations/ListOperations.js.map +1 -1
  95. package/dist/production/src/core/operations/OperationsHelpers.cjs.map +1 -1
  96. package/dist/production/src/core/operations/OperationsHelpers.js.map +1 -1
  97. package/dist/production/src/core/state/StateIterator.cjs.map +1 -1
  98. package/dist/production/src/core/state/StateIterator.js.map +1 -1
  99. package/dist/production/src/core/state/getEntityMarker.cjs.map +1 -1
  100. package/dist/production/src/core/state/getEntityMarker.js.map +1 -1
  101. package/dist/production/src/fieldHelpers/helpers/TemporalFieldHelper.cjs.map +1 -1
  102. package/dist/production/src/fieldHelpers/helpers/TemporalFieldHelper.js.map +1 -1
  103. package/dist/production/src/fieldHelpers/helpers/UuidFieldHelper.cjs.map +1 -1
  104. package/dist/production/src/fieldHelpers/helpers/UuidFieldHelper.js.map +1 -1
  105. package/dist/types/accessorTree/index.d.ts +1 -1
  106. package/dist/types/accessorTree/index.d.ts.map +1 -1
  107. package/dist/types/accessors/EntityAccessorImpl.d.ts +3 -3
  108. package/dist/types/accessors/EntityAccessorImpl.d.ts.map +1 -1
  109. package/dist/types/accessors/EntityListAccessorImpl.d.ts +2 -2
  110. package/dist/types/accessors/EntityListAccessorImpl.d.ts.map +1 -1
  111. package/dist/types/accessors/FieldAccessorImpl.d.ts +3 -3
  112. package/dist/types/accessors/FieldAccessorImpl.d.ts.map +1 -1
  113. package/dist/types/accessors/index.d.ts +4 -4
  114. package/dist/types/accessors/index.d.ts.map +1 -1
  115. package/dist/types/core/AccessorErrorManager.d.ts +4 -4
  116. package/dist/types/core/AccessorErrorManager.d.ts.map +1 -1
  117. package/dist/types/core/Config.d.ts +1 -1
  118. package/dist/types/core/Config.d.ts.map +1 -1
  119. package/dist/types/core/DataBinding.d.ts +1 -1
  120. package/dist/types/core/DataBinding.d.ts.map +1 -1
  121. package/dist/types/core/ErrorsPreprocessor.d.ts +1 -1
  122. package/dist/types/core/ErrorsPreprocessor.d.ts.map +1 -1
  123. package/dist/types/core/EventManager.d.ts +5 -5
  124. package/dist/types/core/EventManager.d.ts.map +1 -1
  125. package/dist/types/core/MutationGenerator.d.ts +1 -1
  126. package/dist/types/core/MutationGenerator.d.ts.map +1 -1
  127. package/dist/types/core/RealmKeyGenerator.d.ts +1 -1
  128. package/dist/types/core/RealmKeyGenerator.d.ts.map +1 -1
  129. package/dist/types/core/RequestResponseNormalizer.d.ts +2 -2
  130. package/dist/types/core/RequestResponseNormalizer.d.ts.map +1 -1
  131. package/dist/types/core/StateInitializer.d.ts +5 -4
  132. package/dist/types/core/StateInitializer.d.ts.map +1 -1
  133. package/dist/types/core/TreeAugmenter.d.ts +4 -4
  134. package/dist/types/core/TreeAugmenter.d.ts.map +1 -1
  135. package/dist/types/core/TreeStore.d.ts +3 -3
  136. package/dist/types/core/TreeStore.d.ts.map +1 -1
  137. package/dist/types/core/exceptions/ErrorLocator.d.ts +1 -1
  138. package/dist/types/core/exceptions/ErrorLocator.d.ts.map +1 -1
  139. package/dist/types/core/factories/createBatchUpdatesOptions.d.ts +1 -1
  140. package/dist/types/core/factories/createBatchUpdatesOptions.d.ts.map +1 -1
  141. package/dist/types/core/factories/index.d.ts +1 -1
  142. package/dist/types/core/factories/index.d.ts.map +1 -1
  143. package/dist/types/core/index.d.ts +8 -8
  144. package/dist/types/core/index.d.ts.map +1 -1
  145. package/dist/types/core/operations/EntityOperations.d.ts +5 -5
  146. package/dist/types/core/operations/EntityOperations.d.ts.map +1 -1
  147. package/dist/types/core/operations/FieldOperations.d.ts +5 -5
  148. package/dist/types/core/operations/FieldOperations.d.ts.map +1 -1
  149. package/dist/types/core/operations/ListOperations.d.ts +5 -5
  150. package/dist/types/core/operations/ListOperations.d.ts.map +1 -1
  151. package/dist/types/core/operations/OperationsHelpers.d.ts +4 -4
  152. package/dist/types/core/operations/OperationsHelpers.d.ts.map +1 -1
  153. package/dist/types/core/operations/index.d.ts +3 -3
  154. package/dist/types/core/operations/index.d.ts.map +1 -1
  155. package/dist/types/core/requestAliases/index.d.ts +1 -1
  156. package/dist/types/core/requestAliases/index.d.ts.map +1 -1
  157. package/dist/types/core/state/EntityListState.d.ts +1 -1
  158. package/dist/types/core/state/EntityListState.d.ts.map +1 -1
  159. package/dist/types/core/state/EntityRealmState.d.ts +3 -3
  160. package/dist/types/core/state/EntityRealmState.d.ts.map +1 -1
  161. package/dist/types/core/state/EntityState.d.ts +1 -1
  162. package/dist/types/core/state/EntityState.d.ts.map +1 -1
  163. package/dist/types/core/state/FieldState.d.ts +1 -1
  164. package/dist/types/core/state/FieldState.d.ts.map +1 -1
  165. package/dist/types/core/state/StateIterator.d.ts +5 -5
  166. package/dist/types/core/state/StateIterator.d.ts.map +1 -1
  167. package/dist/types/core/state/StateNode.d.ts +3 -3
  168. package/dist/types/core/state/StateNode.d.ts.map +1 -1
  169. package/dist/types/core/state/getEntityMarker.d.ts +1 -1
  170. package/dist/types/core/state/getEntityMarker.d.ts.map +1 -1
  171. package/dist/types/core/state/index.d.ts +7 -7
  172. package/dist/types/core/state/index.d.ts.map +1 -1
  173. package/dist/types/core/utils/index.d.ts +1 -1
  174. package/dist/types/core/utils/index.d.ts.map +1 -1
  175. package/dist/types/fieldHelpers/helpers/TemporalFieldHelper.d.ts +1 -1
  176. package/dist/types/fieldHelpers/helpers/TemporalFieldHelper.d.ts.map +1 -1
  177. package/dist/types/fieldHelpers/helpers/UuidFieldHelper.d.ts +1 -1
  178. package/dist/types/fieldHelpers/helpers/UuidFieldHelper.d.ts.map +1 -1
  179. package/dist/types/fieldHelpers/helpers/index.d.ts +2 -2
  180. package/dist/types/fieldHelpers/helpers/index.d.ts.map +1 -1
  181. package/dist/types/fieldHelpers/index.d.ts +1 -1
  182. package/dist/types/fieldHelpers/index.d.ts.map +1 -1
  183. package/dist/types/index.d.ts +3 -3
  184. package/dist/types/index.d.ts.map +1 -1
  185. package/dist/types/tsconfig.tsbuildinfo +1 -1
  186. package/dist/types/utils/index.d.ts +3 -3
  187. package/dist/types/utils/index.d.ts.map +1 -1
  188. package/package.json +4 -4
  189. package/src/accessorTree/index.ts +1 -1
  190. package/src/accessors/EntityAccessorImpl.ts +3 -3
  191. package/src/accessors/EntityListAccessorImpl.ts +2 -2
  192. package/src/accessors/FieldAccessorImpl.ts +3 -3
  193. package/src/accessors/index.ts +4 -4
  194. package/src/core/AccessorErrorManager.ts +5 -5
  195. package/src/core/Config.ts +1 -1
  196. package/src/core/DataBinding.ts +14 -14
  197. package/src/core/ErrorsPreprocessor.ts +2 -2
  198. package/src/core/EventManager.ts +5 -5
  199. package/src/core/MutationGenerator.ts +6 -6
  200. package/src/core/RealmKeyGenerator.ts +1 -1
  201. package/src/core/RequestResponseNormalizer.ts +2 -2
  202. package/src/core/StateInitializer.ts +32 -9
  203. package/src/core/TreeAugmenter.ts +7 -7
  204. package/src/core/TreeStore.ts +4 -4
  205. package/src/core/exceptions/ErrorLocator.ts +3 -3
  206. package/src/core/exceptions/MarkerSugarer.ts +1 -1
  207. package/src/core/factories/createBatchUpdatesOptions.ts +1 -1
  208. package/src/core/factories/index.ts +1 -1
  209. package/src/core/index.ts +8 -8
  210. package/src/core/operations/EntityOperations.ts +7 -7
  211. package/src/core/operations/FieldOperations.ts +6 -6
  212. package/src/core/operations/ListOperations.ts +8 -8
  213. package/src/core/operations/OperationsHelpers.ts +5 -5
  214. package/src/core/operations/index.ts +3 -3
  215. package/src/core/requestAliases/index.ts +1 -1
  216. package/src/core/state/EntityListState.ts +1 -1
  217. package/src/core/state/EntityRealmState.ts +3 -3
  218. package/src/core/state/EntityState.ts +1 -1
  219. package/src/core/state/FieldState.ts +1 -1
  220. package/src/core/state/StateIterator.ts +6 -6
  221. package/src/core/state/StateNode.ts +3 -3
  222. package/src/core/state/getEntityMarker.ts +1 -1
  223. package/src/core/state/index.ts +7 -7
  224. package/src/core/utils/index.ts +1 -1
  225. package/src/fieldHelpers/helpers/TemporalFieldHelper.ts +1 -1
  226. package/src/fieldHelpers/helpers/UuidFieldHelper.ts +1 -1
  227. package/src/fieldHelpers/helpers/index.ts +2 -2
  228. package/src/fieldHelpers/index.ts +1 -1
  229. package/src/index.ts +3 -3
  230. package/src/utils/index.ts +3 -3
@@ -1 +1 @@
1
- {"version":3,"file":"MarkerSugarer.js","sources":["../../../../../../packages/binding-legacy/src/core/exceptions/MarkerSugarer.ts"],"sourcesContent":["import {\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMeaningfulMarker,\n} from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport { TreeParameterSugarer } from './TreeParameterSugarer'\n\nexport class MarkerSugarer {\n\tpublic static sugarMarker(marker: MeaningfulMarker): string {\n\t\tif (marker instanceof FieldMarker) {\n\t\t\treturn this.sugarFieldMarker(marker)\n\t\t}\n\t\tif (marker instanceof HasOneRelationMarker) {\n\t\t\treturn this.sugarHasOneRelationMarker(marker)\n\t\t}\n\t\tif (marker instanceof HasManyRelationMarker) {\n\t\t\treturn this.sugarHasManyRelationMarker(marker)\n\t\t}\n\t\tif (marker instanceof EntityListSubTreeMarker) {\n\t\t\treturn this.sugarEntityListSubTreeMarker(marker)\n\t\t}\n\t\tif (marker instanceof EntitySubTreeMarker) {\n\t\t\treturn this.sugarEntitySubTreeMarker(marker)\n\t\t}\n\t\treturn assertNever(marker)\n\t}\n\n\tpublic static sugarFieldMarker(field: FieldMarker) {\n\t\treturn TreeParameterSugarer.sugarField(field.fieldName)\n\t}\n\n\tpublic static sugarHasOneRelationMarker(hasOne: HasOneRelationMarker) {\n\t\treturn TreeParameterSugarer.sugarHasOneRelation(\n\t\t\thasOne.parameters.field,\n\t\t\thasOne.parameters.reducedBy,\n\t\t\thasOne.parameters.filter,\n\t\t)\n\t}\n\n\tpublic static sugarHasManyRelationMarker(hasMany: HasManyRelationMarker) {\n\t\treturn TreeParameterSugarer.sugarHasManyRelation(hasMany.parameters.field, hasMany.parameters.filter)\n\t}\n\n\tpublic static sugarEntitySubTreeMarker(subTree: EntitySubTreeMarker) {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\treturn TreeParameterSugarer.sugarRootEntity(subTree.entityName, undefined)\n\t\t}\n\t\treturn TreeParameterSugarer.sugarRootEntity(subTree.entityName, subTree.parameters.where)\n\t}\n\n\tpublic static sugarEntityListSubTreeMarker(subTree: EntityListSubTreeMarker) {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\treturn TreeParameterSugarer.sugarRootEntityList(subTree.entityName, undefined)\n\t\t}\n\t\treturn TreeParameterSugarer.sugarRootEntityList(subTree.entityName, subTree.parameters.filter)\n\t}\n}\n"],"names":[],"mappings":";;AAWO,MAAM,cAAc;AAAA,EAC1B,OAAc,YAAY,QAAkC;AAC3D,QAAI,kBAAkB,aAAa;AAClC,aAAO,KAAK,iBAAiB,MAAM;AAAA,IACpC;AACA,QAAI,kBAAkB,sBAAsB;AAC3C,aAAO,KAAK,0BAA0B,MAAM;AAAA,IAC7C;AACA,QAAI,kBAAkB,uBAAuB;AAC5C,aAAO,KAAK,2BAA2B,MAAM;AAAA,IAC9C;AACA,QAAI,kBAAkB,yBAAyB;AAC9C,aAAO,KAAK,6BAA6B,MAAM;AAAA,IAChD;AACA,QAAI,kBAAkB,qBAAqB;AAC1C,aAAO,KAAK,yBAAyB,MAAM;AAAA,IAC5C;AACA,WAAO,YAAY,MAAM;AAAA,EAC1B;AAAA,EAEA,OAAc,iBAAiB,OAAoB;AAClD,WAAO,qBAAqB,WAAW,MAAM,SAAS;AAAA,EACvD;AAAA,EAEA,OAAc,0BAA0B,QAA8B;AACrE,WAAO,qBAAqB;AAAA,MAC3B,OAAO,WAAW;AAAA,MAClB,OAAO,WAAW;AAAA,MAClB,OAAO,WAAW;AAAA,IAAA;AAAA,EAEpB;AAAA,EAEA,OAAc,2BAA2B,SAAgC;AACxE,WAAO,qBAAqB,qBAAqB,QAAQ,WAAW,OAAO,QAAQ,WAAW,MAAM;AAAA,EACrG;AAAA,EAEA,OAAc,yBAAyB,SAA8B;AACpE,QAAI,QAAQ,WAAW,YAAY;AAClC,aAAO,qBAAqB,gBAAgB,QAAQ,YAAY,MAAS;AAAA,IAC1E;AACA,WAAO,qBAAqB,gBAAgB,QAAQ,YAAY,QAAQ,WAAW,KAAK;AAAA,EACzF;AAAA,EAEA,OAAc,6BAA6B,SAAkC;AAC5E,QAAI,QAAQ,WAAW,YAAY;AAClC,aAAO,qBAAqB,oBAAoB,QAAQ,YAAY,MAAS;AAAA,IAC9E;AACA,WAAO,qBAAqB,oBAAoB,QAAQ,YAAY,QAAQ,WAAW,MAAM;AAAA,EAC9F;AACD;"}
1
+ {"version":3,"file":"MarkerSugarer.js","sources":["../../../../../../packages/binding-legacy/src/core/exceptions/MarkerSugarer.ts"],"sourcesContent":["import {\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMeaningfulMarker,\n} from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport { TreeParameterSugarer } from './TreeParameterSugarer.js'\n\nexport class MarkerSugarer {\n\tpublic static sugarMarker(marker: MeaningfulMarker): string {\n\t\tif (marker instanceof FieldMarker) {\n\t\t\treturn this.sugarFieldMarker(marker)\n\t\t}\n\t\tif (marker instanceof HasOneRelationMarker) {\n\t\t\treturn this.sugarHasOneRelationMarker(marker)\n\t\t}\n\t\tif (marker instanceof HasManyRelationMarker) {\n\t\t\treturn this.sugarHasManyRelationMarker(marker)\n\t\t}\n\t\tif (marker instanceof EntityListSubTreeMarker) {\n\t\t\treturn this.sugarEntityListSubTreeMarker(marker)\n\t\t}\n\t\tif (marker instanceof EntitySubTreeMarker) {\n\t\t\treturn this.sugarEntitySubTreeMarker(marker)\n\t\t}\n\t\treturn assertNever(marker)\n\t}\n\n\tpublic static sugarFieldMarker(field: FieldMarker) {\n\t\treturn TreeParameterSugarer.sugarField(field.fieldName)\n\t}\n\n\tpublic static sugarHasOneRelationMarker(hasOne: HasOneRelationMarker) {\n\t\treturn TreeParameterSugarer.sugarHasOneRelation(\n\t\t\thasOne.parameters.field,\n\t\t\thasOne.parameters.reducedBy,\n\t\t\thasOne.parameters.filter,\n\t\t)\n\t}\n\n\tpublic static sugarHasManyRelationMarker(hasMany: HasManyRelationMarker) {\n\t\treturn TreeParameterSugarer.sugarHasManyRelation(hasMany.parameters.field, hasMany.parameters.filter)\n\t}\n\n\tpublic static sugarEntitySubTreeMarker(subTree: EntitySubTreeMarker) {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\treturn TreeParameterSugarer.sugarRootEntity(subTree.entityName, undefined)\n\t\t}\n\t\treturn TreeParameterSugarer.sugarRootEntity(subTree.entityName, subTree.parameters.where)\n\t}\n\n\tpublic static sugarEntityListSubTreeMarker(subTree: EntityListSubTreeMarker) {\n\t\tif (subTree.parameters.isCreating) {\n\t\t\treturn TreeParameterSugarer.sugarRootEntityList(subTree.entityName, undefined)\n\t\t}\n\t\treturn TreeParameterSugarer.sugarRootEntityList(subTree.entityName, subTree.parameters.filter)\n\t}\n}\n"],"names":[],"mappings":";;AAWO,MAAM,cAAc;AAAA,EAC1B,OAAc,YAAY,QAAkC;AAC3D,QAAI,kBAAkB,aAAa;AAClC,aAAO,KAAK,iBAAiB,MAAM;AAAA,IACpC;AACA,QAAI,kBAAkB,sBAAsB;AAC3C,aAAO,KAAK,0BAA0B,MAAM;AAAA,IAC7C;AACA,QAAI,kBAAkB,uBAAuB;AAC5C,aAAO,KAAK,2BAA2B,MAAM;AAAA,IAC9C;AACA,QAAI,kBAAkB,yBAAyB;AAC9C,aAAO,KAAK,6BAA6B,MAAM;AAAA,IAChD;AACA,QAAI,kBAAkB,qBAAqB;AAC1C,aAAO,KAAK,yBAAyB,MAAM;AAAA,IAC5C;AACA,WAAO,YAAY,MAAM;AAAA,EAC1B;AAAA,EAEA,OAAc,iBAAiB,OAAoB;AAClD,WAAO,qBAAqB,WAAW,MAAM,SAAS;AAAA,EACvD;AAAA,EAEA,OAAc,0BAA0B,QAA8B;AACrE,WAAO,qBAAqB;AAAA,MAC3B,OAAO,WAAW;AAAA,MAClB,OAAO,WAAW;AAAA,MAClB,OAAO,WAAW;AAAA,IAAA;AAAA,EAEpB;AAAA,EAEA,OAAc,2BAA2B,SAAgC;AACxE,WAAO,qBAAqB,qBAAqB,QAAQ,WAAW,OAAO,QAAQ,WAAW,MAAM;AAAA,EACrG;AAAA,EAEA,OAAc,yBAAyB,SAA8B;AACpE,QAAI,QAAQ,WAAW,YAAY;AAClC,aAAO,qBAAqB,gBAAgB,QAAQ,YAAY,MAAS;AAAA,IAC1E;AACA,WAAO,qBAAqB,gBAAgB,QAAQ,YAAY,QAAQ,WAAW,KAAK;AAAA,EACzF;AAAA,EAEA,OAAc,6BAA6B,SAAkC;AAC5E,QAAI,QAAQ,WAAW,YAAY;AAClC,aAAO,qBAAqB,oBAAoB,QAAQ,YAAY,MAAS;AAAA,IAC9E;AACA,WAAO,qBAAqB,oBAAoB,QAAQ,YAAY,QAAQ,WAAW,MAAM;AAAA,EAC9F;AACD;"}
@@ -1 +1 @@
1
- {"version":3,"file":"createBatchUpdatesOptions.cjs","sources":["../../../../../../packages/binding-legacy/src/core/factories/createBatchUpdatesOptions.ts"],"sourcesContent":["import type { BatchUpdatesOptions, EntityAccessor, EntityListAccessor } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport type { Environment } from '@contember/binding-common'\nimport type {\n\tAlias,\n\tSugaredQualifiedEntityList,\n\tSugaredQualifiedSingleEntity,\n\tSugaredUnconstrainedQualifiedEntityList,\n\tSugaredUnconstrainedQualifiedSingleEntity,\n\tTreeRootId,\n} from '@contember/binding-common'\nimport type { TreeStore } from '../TreeStore'\n\nexport const createBatchUpdatesOptions = (rootEnvironment: Environment, treeStore: TreeStore): BatchUpdatesOptions =>\n\tObject.freeze({\n\t\tgetEntityByKey: (key: string | (() => EntityAccessor)) => {\n\t\t\tif (typeof key === 'function') {\n\t\t\t\treturn key()\n\t\t\t}\n\t\t\tconst realm = treeStore.entityRealmStore.get(key)\n\n\t\t\tif (realm === undefined) {\n\t\t\t\tthrow new BindingError(`Trying to retrieve a non-existent entity: key '${key}' was not found.`)\n\t\t\t}\n\t\t\treturn realm.getAccessor()\n\t\t},\n\t\tgetEntityListSubTree: (\n\t\t\taliasOrParameters: Alias | SugaredQualifiedEntityList | SugaredUnconstrainedQualifiedEntityList,\n\t\t\ttreeId: TreeRootId | undefined,\n\t\t\tenvironment = rootEnvironment,\n\t\t): EntityListAccessor => {\n\t\t\treturn treeStore.getSubTreeState('entityList', treeId, aliasOrParameters, environment).getAccessor()\n\t\t},\n\t\tgetEntitySubTree: (\n\t\t\taliasOrParameters: Alias | SugaredQualifiedSingleEntity | SugaredUnconstrainedQualifiedSingleEntity,\n\t\t\ttreeId: TreeRootId | undefined,\n\t\t\tenvironment = rootEnvironment,\n\t\t): EntityAccessor => {\n\t\t\treturn treeStore.getSubTreeState('entity', treeId, aliasOrParameters, environment).getAccessor()\n\t\t},\n\t})\n"],"names":["BindingError"],"mappings":";;;AAaO,MAAM,4BAA4B,CAAC,iBAA8B,cACvE,OAAO,OAAO;AAAA,EACb,gBAAgB,CAAC,QAAyC;AACzD,QAAI,OAAO,QAAQ,YAAY;AAC9B,aAAO,IAAA;AAAA,IACR;AACA,UAAM,QAAQ,UAAU,iBAAiB,IAAI,GAAG;AAEhD,QAAI,UAAU,QAAW;AACxB,YAAM,IAAIA,cAAAA,aAAa,kDAAkD,GAAG,kBAAkB;AAAA,IAC/F;AACA,WAAO,MAAM,YAAA;AAAA,EACd;AAAA,EACA,sBAAsB,CACrB,mBACA,QACA,cAAc,oBACU;AACxB,WAAO,UAAU,gBAAgB,cAAc,QAAQ,mBAAmB,WAAW,EAAE,YAAA;AAAA,EACxF;AAAA,EACA,kBAAkB,CACjB,mBACA,QACA,cAAc,oBACM;AACpB,WAAO,UAAU,gBAAgB,UAAU,QAAQ,mBAAmB,WAAW,EAAE,YAAA;AAAA,EACpF;AACD,CAAC;;"}
1
+ {"version":3,"file":"createBatchUpdatesOptions.cjs","sources":["../../../../../../packages/binding-legacy/src/core/factories/createBatchUpdatesOptions.ts"],"sourcesContent":["import type { BatchUpdatesOptions, EntityAccessor, EntityListAccessor } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport type { Environment } from '@contember/binding-common'\nimport type {\n\tAlias,\n\tSugaredQualifiedEntityList,\n\tSugaredQualifiedSingleEntity,\n\tSugaredUnconstrainedQualifiedEntityList,\n\tSugaredUnconstrainedQualifiedSingleEntity,\n\tTreeRootId,\n} from '@contember/binding-common'\nimport type { TreeStore } from '../TreeStore.js'\n\nexport const createBatchUpdatesOptions = (rootEnvironment: Environment, treeStore: TreeStore): BatchUpdatesOptions =>\n\tObject.freeze({\n\t\tgetEntityByKey: (key: string | (() => EntityAccessor)) => {\n\t\t\tif (typeof key === 'function') {\n\t\t\t\treturn key()\n\t\t\t}\n\t\t\tconst realm = treeStore.entityRealmStore.get(key)\n\n\t\t\tif (realm === undefined) {\n\t\t\t\tthrow new BindingError(`Trying to retrieve a non-existent entity: key '${key}' was not found.`)\n\t\t\t}\n\t\t\treturn realm.getAccessor()\n\t\t},\n\t\tgetEntityListSubTree: (\n\t\t\taliasOrParameters: Alias | SugaredQualifiedEntityList | SugaredUnconstrainedQualifiedEntityList,\n\t\t\ttreeId: TreeRootId | undefined,\n\t\t\tenvironment = rootEnvironment,\n\t\t): EntityListAccessor => {\n\t\t\treturn treeStore.getSubTreeState('entityList', treeId, aliasOrParameters, environment).getAccessor()\n\t\t},\n\t\tgetEntitySubTree: (\n\t\t\taliasOrParameters: Alias | SugaredQualifiedSingleEntity | SugaredUnconstrainedQualifiedSingleEntity,\n\t\t\ttreeId: TreeRootId | undefined,\n\t\t\tenvironment = rootEnvironment,\n\t\t): EntityAccessor => {\n\t\t\treturn treeStore.getSubTreeState('entity', treeId, aliasOrParameters, environment).getAccessor()\n\t\t},\n\t})\n"],"names":["BindingError"],"mappings":";;;AAaO,MAAM,4BAA4B,CAAC,iBAA8B,cACvE,OAAO,OAAO;AAAA,EACb,gBAAgB,CAAC,QAAyC;AACzD,QAAI,OAAO,QAAQ,YAAY;AAC9B,aAAO,IAAA;AAAA,IACR;AACA,UAAM,QAAQ,UAAU,iBAAiB,IAAI,GAAG;AAEhD,QAAI,UAAU,QAAW;AACxB,YAAM,IAAIA,cAAAA,aAAa,kDAAkD,GAAG,kBAAkB;AAAA,IAC/F;AACA,WAAO,MAAM,YAAA;AAAA,EACd;AAAA,EACA,sBAAsB,CACrB,mBACA,QACA,cAAc,oBACU;AACxB,WAAO,UAAU,gBAAgB,cAAc,QAAQ,mBAAmB,WAAW,EAAE,YAAA;AAAA,EACxF;AAAA,EACA,kBAAkB,CACjB,mBACA,QACA,cAAc,oBACM;AACpB,WAAO,UAAU,gBAAgB,UAAU,QAAQ,mBAAmB,WAAW,EAAE,YAAA;AAAA,EACpF;AACD,CAAC;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"createBatchUpdatesOptions.js","sources":["../../../../../../packages/binding-legacy/src/core/factories/createBatchUpdatesOptions.ts"],"sourcesContent":["import type { BatchUpdatesOptions, EntityAccessor, EntityListAccessor } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport type { Environment } from '@contember/binding-common'\nimport type {\n\tAlias,\n\tSugaredQualifiedEntityList,\n\tSugaredQualifiedSingleEntity,\n\tSugaredUnconstrainedQualifiedEntityList,\n\tSugaredUnconstrainedQualifiedSingleEntity,\n\tTreeRootId,\n} from '@contember/binding-common'\nimport type { TreeStore } from '../TreeStore'\n\nexport const createBatchUpdatesOptions = (rootEnvironment: Environment, treeStore: TreeStore): BatchUpdatesOptions =>\n\tObject.freeze({\n\t\tgetEntityByKey: (key: string | (() => EntityAccessor)) => {\n\t\t\tif (typeof key === 'function') {\n\t\t\t\treturn key()\n\t\t\t}\n\t\t\tconst realm = treeStore.entityRealmStore.get(key)\n\n\t\t\tif (realm === undefined) {\n\t\t\t\tthrow new BindingError(`Trying to retrieve a non-existent entity: key '${key}' was not found.`)\n\t\t\t}\n\t\t\treturn realm.getAccessor()\n\t\t},\n\t\tgetEntityListSubTree: (\n\t\t\taliasOrParameters: Alias | SugaredQualifiedEntityList | SugaredUnconstrainedQualifiedEntityList,\n\t\t\ttreeId: TreeRootId | undefined,\n\t\t\tenvironment = rootEnvironment,\n\t\t): EntityListAccessor => {\n\t\t\treturn treeStore.getSubTreeState('entityList', treeId, aliasOrParameters, environment).getAccessor()\n\t\t},\n\t\tgetEntitySubTree: (\n\t\t\taliasOrParameters: Alias | SugaredQualifiedSingleEntity | SugaredUnconstrainedQualifiedSingleEntity,\n\t\t\ttreeId: TreeRootId | undefined,\n\t\t\tenvironment = rootEnvironment,\n\t\t): EntityAccessor => {\n\t\t\treturn treeStore.getSubTreeState('entity', treeId, aliasOrParameters, environment).getAccessor()\n\t\t},\n\t})\n"],"names":[],"mappings":";AAaO,MAAM,4BAA4B,CAAC,iBAA8B,cACvE,OAAO,OAAO;AAAA,EACb,gBAAgB,CAAC,QAAyC;AACzD,QAAI,OAAO,QAAQ,YAAY;AAC9B,aAAO,IAAA;AAAA,IACR;AACA,UAAM,QAAQ,UAAU,iBAAiB,IAAI,GAAG;AAEhD,QAAI,UAAU,QAAW;AACxB,YAAM,IAAI,aAAa,kDAAkD,GAAG,kBAAkB;AAAA,IAC/F;AACA,WAAO,MAAM,YAAA;AAAA,EACd;AAAA,EACA,sBAAsB,CACrB,mBACA,QACA,cAAc,oBACU;AACxB,WAAO,UAAU,gBAAgB,cAAc,QAAQ,mBAAmB,WAAW,EAAE,YAAA;AAAA,EACxF;AAAA,EACA,kBAAkB,CACjB,mBACA,QACA,cAAc,oBACM;AACpB,WAAO,UAAU,gBAAgB,UAAU,QAAQ,mBAAmB,WAAW,EAAE,YAAA;AAAA,EACpF;AACD,CAAC;"}
1
+ {"version":3,"file":"createBatchUpdatesOptions.js","sources":["../../../../../../packages/binding-legacy/src/core/factories/createBatchUpdatesOptions.ts"],"sourcesContent":["import type { BatchUpdatesOptions, EntityAccessor, EntityListAccessor } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport type { Environment } from '@contember/binding-common'\nimport type {\n\tAlias,\n\tSugaredQualifiedEntityList,\n\tSugaredQualifiedSingleEntity,\n\tSugaredUnconstrainedQualifiedEntityList,\n\tSugaredUnconstrainedQualifiedSingleEntity,\n\tTreeRootId,\n} from '@contember/binding-common'\nimport type { TreeStore } from '../TreeStore.js'\n\nexport const createBatchUpdatesOptions = (rootEnvironment: Environment, treeStore: TreeStore): BatchUpdatesOptions =>\n\tObject.freeze({\n\t\tgetEntityByKey: (key: string | (() => EntityAccessor)) => {\n\t\t\tif (typeof key === 'function') {\n\t\t\t\treturn key()\n\t\t\t}\n\t\t\tconst realm = treeStore.entityRealmStore.get(key)\n\n\t\t\tif (realm === undefined) {\n\t\t\t\tthrow new BindingError(`Trying to retrieve a non-existent entity: key '${key}' was not found.`)\n\t\t\t}\n\t\t\treturn realm.getAccessor()\n\t\t},\n\t\tgetEntityListSubTree: (\n\t\t\taliasOrParameters: Alias | SugaredQualifiedEntityList | SugaredUnconstrainedQualifiedEntityList,\n\t\t\ttreeId: TreeRootId | undefined,\n\t\t\tenvironment = rootEnvironment,\n\t\t): EntityListAccessor => {\n\t\t\treturn treeStore.getSubTreeState('entityList', treeId, aliasOrParameters, environment).getAccessor()\n\t\t},\n\t\tgetEntitySubTree: (\n\t\t\taliasOrParameters: Alias | SugaredQualifiedSingleEntity | SugaredUnconstrainedQualifiedSingleEntity,\n\t\t\ttreeId: TreeRootId | undefined,\n\t\t\tenvironment = rootEnvironment,\n\t\t): EntityAccessor => {\n\t\t\treturn treeStore.getSubTreeState('entity', treeId, aliasOrParameters, environment).getAccessor()\n\t\t},\n\t})\n"],"names":[],"mappings":";AAaO,MAAM,4BAA4B,CAAC,iBAA8B,cACvE,OAAO,OAAO;AAAA,EACb,gBAAgB,CAAC,QAAyC;AACzD,QAAI,OAAO,QAAQ,YAAY;AAC9B,aAAO,IAAA;AAAA,IACR;AACA,UAAM,QAAQ,UAAU,iBAAiB,IAAI,GAAG;AAEhD,QAAI,UAAU,QAAW;AACxB,YAAM,IAAI,aAAa,kDAAkD,GAAG,kBAAkB;AAAA,IAC/F;AACA,WAAO,MAAM,YAAA;AAAA,EACd;AAAA,EACA,sBAAsB,CACrB,mBACA,QACA,cAAc,oBACU;AACxB,WAAO,UAAU,gBAAgB,cAAc,QAAQ,mBAAmB,WAAW,EAAE,YAAA;AAAA,EACxF;AAAA,EACA,kBAAkB,CACjB,mBACA,QACA,cAAc,oBACM;AACpB,WAAO,UAAU,gBAAgB,UAAU,QAAQ,mBAAmB,WAAW,EAAE,YAAA;AAAA,EACpF;AACD,CAAC;"}
@@ -1 +1 @@
1
- {"version":3,"file":"EntityOperations.cjs","sources":["../../../../../../packages/binding-legacy/src/core/operations/EntityOperations.ts"],"sourcesContent":["import type { BatchUpdatesOptions, EntityAccessor, ErrorAccessor } from '@contember/binding-common'\nimport { ServerId, UnpersistedEntityDummyId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { EntityFieldMarkersContainer, HasOneRelationMarker } from '@contember/binding-common'\nimport type { FieldName } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { AccessorErrorManager } from '../AccessorErrorManager'\nimport type { EventManager } from '../EventManager'\nimport { LocalizedBindingError } from '@contember/binding-common'\nimport { MarkerComparator } from '@contember/binding-common'\nimport { EntityListState, EntityRealmState, EntityRealmStateStub, getEntityMarker, StateIterator } from '../state'\nimport type { StateInitializer } from '../StateInitializer'\nimport type { TreeStore } from '../TreeStore'\nimport { OperationsHelpers } from './OperationsHelpers'\nimport { EventListenersStore } from '@contember/binding-common'\nimport { ErrorLocator } from '../exceptions/ErrorLocator'\n\nexport class EntityOperations {\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly batchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic addError(entityRealm: EntityRealmState, error: ErrorAccessor.Error): () => void {\n\t\treturn this.accessorErrorManager.addError(entityRealm, error)\n\t}\n\n\tpublic addEventListener<Type extends keyof EntityAccessor.RuntimeEntityEventListenerMap>(\n\t\tstate: EntityRealmState,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityAccessor.EntityEventListenerMap[Type],\n\t) {\n\t\tif (state.eventListeners === undefined) {\n\t\t\tstate.eventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.eventListeners.add(event, listener)\n\t}\n\n\tpublic batchUpdates(state: EntityRealmState, performUpdates: EntityAccessor.BatchUpdatesHandler) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tperformUpdates(state.getAccessor, this.batchUpdatesOptions)\n\t\t})\n\t}\n\n\tpublic connectEntityAtField(outerState: EntityRealmState, fieldName: FieldName, entityToConnect: EntityAccessor) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst stateToConnect = OperationsHelpers.resolveAndPrepareEntityToConnect(this.treeStore, entityToConnect)\n\t\t\tconst fieldsToConnect = getEntityMarker(stateToConnect).fields\n\t\t\tconst persistedData = this.treeStore.persistedEntityData.get(outerState.entity.id.uniqueValue)\n\n\t\t\tconst entityName = outerState.entity.entityName\n\t\t\tconst fieldSchema = this.treeStore.schema.getEntityFieldOrUndefined(entityName, fieldName)\n\n\t\t\tif (fieldSchema === undefined) {\n\t\t\t\tthrow new BindingError(\n\t\t\t\t\t`EntityAccessor.connectEntityAtField: Unknown field ${entityName}.${fieldName}.\\n\\n`\n\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t)\n\t\t\t} else if (fieldSchema.__typename === '_Column') {\n\t\t\t\tthrow new BindingError(\n\t\t\t\t\t`EntityAccessor.connectEntityAtField: Cannot connect at field ${entityName}.${fieldName} because `\n\t\t\t\t\t\t+ `it's not a has-one relation.\\n\\n`\n\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealm(outerState)) {\n\t\t\t\tconst targetHasOneMarkers = this.resolveHasOneRelationMarkers(getEntityMarker(state).fields, fieldName, 'connect', state === outerState)\n\t\t\t\tfor (const targetHasOneMarker of targetHasOneMarkers) {\n\t\t\t\t\tconst previouslyConnectedState = state.children.get(targetHasOneMarker.placeholderName)\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tpreviouslyConnectedState === undefined\n\t\t\t\t\t\t|| previouslyConnectedState.type === 'field'\n\t\t\t\t\t\t|| previouslyConnectedState.type === 'entityList'\n\t\t\t\t\t) {\n\t\t\t\t\t\tOperationsHelpers.rejectInvalidAccessorTree()\n\t\t\t\t\t}\n\n\t\t\t\t\tif (previouslyConnectedState.entity.entityName !== stateToConnect.entity.entityName) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`EntityAccessor.connectEntityAtField: Attempting to connect at field ${entityName}.${fieldName} `\n\t\t\t\t\t\t\t\t+ `an entity of type '${stateToConnect.entity.entityName}' but `\n\t\t\t\t\t\t\t\t+ `'${previouslyConnectedState.entity.entityName}' is expected.\\n\\n`\n\t\t\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tMarkerComparator.assertEntityMarkersSubsetOf(targetHasOneMarker.fields, fieldsToConnect)\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tif (error instanceof LocalizedBindingError) {\n\t\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t\t`EntityAccessor: cannot connect entity with key '${entityToConnect.key}' because its fields are`\n\t\t\t\t\t\t\t\t\t+ `incompatible with entities found at field '${fieldName}'. Make sure both trees are equivalent.\\n\\n`\n\t\t\t\t\t\t\t\t\t+ `${error.message}\\n\\n`\n\t\t\t\t\t\t\t\t\t+ (error.markerPath.length > 1\n\t\t\t\t\t\t\t\t\t\t? `Incompatibility found at: ${ErrorLocator.locateMarkerPath(error.markerPath)}.\\n\\n`\n\t\t\t\t\t\t\t\t\t\t: '')\n\t\t\t\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(state)}.`,\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthrow error\n\t\t\t\t\t}\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tpreviouslyConnectedState === stateToConnect\n\t\t\t\t\t\t|| previouslyConnectedState.entity.id.value === stateToConnect.entity.id.value\n\t\t\t\t\t) {\n\t\t\t\t\t\tcontinue // Do nothing.\n\t\t\t\t\t}\n\n\t\t\t\t\t// TODO remove from planned deletions if appropriate\n\n\t\t\t\t\tthis.treeStore.disposeOfRealm(previouslyConnectedState)\n\n\t\t\t\t\tlet changesDelta = 0\n\n\t\t\t\t\tif (previouslyConnectedState.type === 'entityRealm') {\n\t\t\t\t\t\tchangesDelta -= previouslyConnectedState.unpersistedChangesCount\n\t\t\t\t\t}\n\n\t\t\t\t\tconst persistedId = persistedData?.get(targetHasOneMarker.placeholderName)?.value\n\t\t\t\t\tif (persistedId instanceof ServerId && persistedId.value === stateToConnect.entity.id.value) {\n\t\t\t\t\t\tchangesDelta-- // It was removed from the list but now we're adding it back.\n\t\t\t\t\t} else if (persistedId instanceof ServerId && persistedId.value === previouslyConnectedState.entity.id.value) {\n\t\t\t\t\t\tchangesDelta++ // We're changing it from the persisted id.\n\t\t\t\t\t} else if (!previouslyConnectedState.entity.id.existsOnServer) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tpersistedId === null // We're updating.\n\t\t\t\t\t\t\t|| (persistedId === undefined // We're creating.\n\t\t\t\t\t\t\t\t&& (!fieldsToConnect.hasAtLeastOneBearingField || !targetHasOneMarker.parameters.isNonbearing))\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (stateToConnect.type === 'entityRealm') {\n\t\t\t\t\t\tchangesDelta += stateToConnect.unpersistedChangesCount\n\t\t\t\t\t}\n\n\t\t\t\t\t// TODO do something about the existing state…\n\n\t\t\t\t\tthis.stateInitializer.initializeEntityRealm(\n\t\t\t\t\t\tstateToConnect.entity.id,\n\t\t\t\t\t\tstateToConnect.entity.entityName,\n\t\t\t\t\t\tpreviouslyConnectedState.blueprint,\n\t\t\t\t\t\tstateToConnect,\n\t\t\t\t\t)\n\n\t\t\t\t\tthis.eventManager.registerUpdatedConnection(state, targetHasOneMarker.placeholderName)\n\t\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic disconnectEntityAtField(\n\t\touterState: EntityRealmState,\n\t\tfieldName: FieldName,\n\t\tinitializeReplacement: EntityAccessor.BatchUpdatesHandler | undefined,\n\t) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst persistedData = this.treeStore.persistedEntityData.get(outerState.entity.id.uniqueValue)\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealm(outerState)) {\n\t\t\t\tconst targetHasOneMarkers = this.resolveHasOneRelationMarkers(getEntityMarker(state).fields, fieldName, 'disconnect', state === outerState)\n\t\t\t\tfor (const targetHasOneMarker of targetHasOneMarkers) {\n\t\t\t\t\tconst stateToDisconnect = state.children.get(targetHasOneMarker.placeholderName)\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tstateToDisconnect === undefined\n\t\t\t\t\t\t|| (stateToDisconnect.type !== 'entityRealm' && stateToDisconnect.type !== 'entityRealmStub')\n\t\t\t\t\t) {\n\t\t\t\t\t\tOperationsHelpers.rejectInvalidAccessorTree()\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.treeStore.disposeOfRealm(stateToDisconnect)\n\n\t\t\t\t\tlet changesDelta = 0\n\n\t\t\t\t\tif (stateToDisconnect.type === 'entityRealm') {\n\t\t\t\t\t\tchangesDelta -= stateToDisconnect.unpersistedChangesCount\n\t\t\t\t\t}\n\t\t\t\t\tconst persistedId = persistedData?.get(targetHasOneMarker.placeholderName)?.value\n\n\t\t\t\t\tif (persistedId instanceof ServerId) {\n\t\t\t\t\t\tif (persistedId.value === stateToDisconnect.entity.id.value) {\n\t\t\t\t\t\t\tchangesDelta++ // Disconnecting the persisted entity.\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Do nothing. The entity that is being disconnected is already different from the one that is persisted,\n\t\t\t\t\t\t\t// and so we must have already accounted for this change.\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (persistedId === undefined || persistedId === null) {\n\t\t\t\t\t\tif (stateToDisconnect.entity.id.existsOnServer) {\n\t\t\t\t\t\t\t// We had connected a persisted entity, which increased the changes count,\n\t\t\t\t\t\t\t// but now are disconnecting it again.\n\t\t\t\t\t\t\tchangesDelta--\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// Do nothing. Disconnecting unpersisted entities doesn't change the count.\n\t\t\t\t\t}\n\n\t\t\t\t\tconst newEntity = this.stateInitializer.initializeEntityRealm(\n\t\t\t\t\t\tnew UnpersistedEntityDummyId(),\n\t\t\t\t\t\tstateToDisconnect.entity.entityName,\n\t\t\t\t\t\tstateToDisconnect.blueprint,\n\t\t\t\t\t)\n\t\t\t\t\tthis.stateInitializer.runImmediateUserInitialization(newEntity, initializeReplacement)\n\n\t\t\t\t\tthis.eventManager.registerUpdatedConnection(state, targetHasOneMarker.placeholderName)\n\t\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic deleteEntity(realm: EntityRealmState) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst outerState = realm.entity\n\t\t\touterState.isScheduledForDeletion = true\n\n\t\t\tconst entityId = outerState.id\n\n\t\t\tfor (const realmToDelete of outerState.realms.values()) {\n\t\t\t\tlet parent: EntityRealmState | EntityRealmStateStub | EntityListState | undefined\n\t\t\t\tlet changesDelta = 0\n\n\t\t\t\tif (realmToDelete.blueprint.type === 'listEntity') {\n\t\t\t\t\tparent = realmToDelete.blueprint.parent\n\t\t\t\t\tparent.children.delete(entityId.value)\n\n\t\t\t\t\tif (entityId.existsOnServer) {\n\t\t\t\t\t\tif (parent.plannedRemovals === undefined) {\n\t\t\t\t\t\t\tparent.plannedRemovals = new Map()\n\t\t\t\t\t\t}\n\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t\tparent.plannedRemovals.set(entityId.value, 'delete')\n\t\t\t\t\t}\n\t\t\t\t} else if (realmToDelete.blueprint.type === 'hasOne') {\n\t\t\t\t\tparent = realmToDelete.blueprint.parent\n\t\t\t\t\tconst placeholderName = realmToDelete.blueprint.marker.placeholderName\n\n\t\t\t\t\tif (entityId.existsOnServer) {\n\t\t\t\t\t\tif (parent.plannedHasOneDeletions === undefined) {\n\t\t\t\t\t\t\tparent.plannedHasOneDeletions = new Map()\n\t\t\t\t\t\t}\n\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t\tparent.plannedHasOneDeletions.set(placeholderName, realmToDelete)\n\t\t\t\t\t}\n\t\t\t\t\tthis.stateInitializer.initializeEntityRealm(\n\t\t\t\t\t\tnew UnpersistedEntityDummyId(),\n\t\t\t\t\t\touterState.entityName,\n\t\t\t\t\t\trealmToDelete.blueprint,\n\t\t\t\t\t)\n\n\t\t\t\t\tthis.eventManager.registerUpdatedConnection(parent, placeholderName)\n\t\t\t\t} else if (realmToDelete.blueprint.type === 'subTree') {\n\t\t\t\t\tparent = undefined\n\t\t\t\t\tif (realmToDelete.type === 'entityRealm') {\n\t\t\t\t\t\tthis.eventManager.registerJustUpdated(realmToDelete, 1)\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\treturn assertNever(realmToDelete.blueprint)\n\t\t\t\t}\n\n\t\t\t\tif (parent) {\n\t\t\t\t\tif (realmToDelete.type === 'entityRealm') {\n\t\t\t\t\t\t// Undoing whatever this had caused\n\t\t\t\t\t\tchangesDelta -= realmToDelete.unpersistedChangesCount\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.eventManager.registerJustUpdated(parent, changesDelta)\n\n\t\t\t\t\tif (realmToDelete.type === 'entityRealm') {\n\t\t\t\t\t\tparent.childrenWithPendingUpdates?.delete(realmToDelete)\n\t\t\t\t\t}\n\t\t\t\t\tthis.treeStore.disposeOfRealm(realmToDelete)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tprivate *resolveHasOneRelationMarkers(\n\t\tcontainer: EntityFieldMarkersContainer,\n\t\tfield: FieldName,\n\t\ttype: 'connect' | 'disconnect',\n\t\tmustExists: boolean,\n\t): IterableIterator<HasOneRelationMarker> {\n\t\tconst placeholders = container.placeholders.get(field)\n\n\t\tif (placeholders === undefined) {\n\t\t\tif (!mustExists) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthrow new BindingError(`Cannot ${type} at field '${field}' as it wasn't registered during static render.`)\n\t\t}\n\t\tconst normalizedPlaceholders = placeholders instanceof Set ? placeholders : [placeholders]\n\n\t\tfor (const placeholderName of normalizedPlaceholders) {\n\t\t\tconst hasOneRelation = container.markers.get(placeholderName)\n\n\t\t\tif (!(hasOneRelation instanceof HasOneRelationMarker)) {\n\t\t\t\tthrow new BindingError(`Cannot ${type} at field '${field}' as it doesn't refer to a has one relation.`)\n\t\t\t}\n\t\t\tyield hasOneRelation\n\t\t}\n\t}\n}\n"],"names":["EventListenersStore","OperationsHelpers","getEntityMarker","BindingError","ErrorLocator","StateIterator","MarkerComparator","LocalizedBindingError","ServerId","UnpersistedEntityDummyId","assertNever","HasOneRelationMarker"],"mappings":";;;;;;;AAiBO,MAAM,iBAAiB;AAAA,EACtB,YACW,sBACA,qBACA,cACA,kBACA,WAChB;AALgB,SAAA,uBAAA;AACA,SAAA,sBAAA;AACA,SAAA,eAAA;AACA,SAAA,mBAAA;AACA,SAAA,YAAA;AAAA,EACf;AAAA,EAEI,SAAS,aAA+B,OAAwC;AACtF,WAAO,KAAK,qBAAqB,SAAS,aAAa,KAAK;AAAA,EAC7D;AAAA,EAEO,iBACN,OACA,OACA,UACC;AACD,QAAI,MAAM,mBAAmB,QAAW;AACvC,YAAM,iBAAiB,IAAIA,kCAAA;AAAA,IAC5B;AACA,WAAO,MAAM,eAAe,IAAI,OAAO,QAAQ;AAAA,EAChD;AAAA,EAEO,aAAa,OAAyB,gBAAoD;AAChG,SAAK,aAAa,cAAc,MAAM;AACrC,qBAAe,MAAM,aAAa,KAAK,mBAAmB;AAAA,IAC3D,CAAC;AAAA,EACF;AAAA,EAEO,qBAAqB,YAA8B,WAAsB,iBAAiC;AAChH,SAAK,aAAa,cAAc,MAAM;AACrC,YAAM,iBAAiBC,kBAAAA,kBAAkB,iCAAiC,KAAK,WAAW,eAAe;AACzG,YAAM,kBAAkBC,gBAAAA,gBAAgB,cAAc,EAAE;AACxD,YAAM,gBAAgB,KAAK,UAAU,oBAAoB,IAAI,WAAW,OAAO,GAAG,WAAW;AAE7F,YAAM,aAAa,WAAW,OAAO;AACrC,YAAM,cAAc,KAAK,UAAU,OAAO,0BAA0B,YAAY,SAAS;AAEzF,UAAI,gBAAgB,QAAW;AAC9B,cAAM,IAAIC,cAAAA;AAAAA,UACT,sDAAsD,UAAU,IAAI,SAAS;AAAA;AAAA,qBACpDC,0BAAa,oBAAoB,UAAU,CAAC;AAAA,QAAA;AAAA,MAEvE,WAAW,YA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AwB;AACrD,YAAM,iBAAiB,UAAU,QAAQ,IAAI,eAAe;AAE5D,UAAI,EAAE,0BAA0BQ,cAAAA,uBAAuB;AACtD,cAAM,IAAIR,cAAAA,aAAa,UAAU,IAAI,cAAc,KAAK,8CAA8C;AAAA,MACvG;AACA,YAAM;AAAA,IACP;AAAA,EACD;AACD;;"}
1
+ {"version":3,"file":"EntityOperations.cjs","sources":["../../../../../../packages/binding-legacy/src/core/operations/EntityOperations.ts"],"sourcesContent":["import type { BatchUpdatesOptions, EntityAccessor, ErrorAccessor } from '@contember/binding-common'\nimport { ServerId, UnpersistedEntityDummyId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { EntityFieldMarkersContainer, HasOneRelationMarker } from '@contember/binding-common'\nimport type { FieldName } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { AccessorErrorManager } from '../AccessorErrorManager.js'\nimport type { EventManager } from '../EventManager.js'\nimport { LocalizedBindingError } from '@contember/binding-common'\nimport { MarkerComparator } from '@contember/binding-common'\nimport { EntityListState, EntityRealmState, EntityRealmStateStub, getEntityMarker, StateIterator } from '../state/index.js'\nimport type { StateInitializer } from '../StateInitializer.js'\nimport type { TreeStore } from '../TreeStore.js'\nimport { OperationsHelpers } from './OperationsHelpers.js'\nimport { EventListenersStore } from '@contember/binding-common'\nimport { ErrorLocator } from '../exceptions/ErrorLocator.js'\n\nexport class EntityOperations {\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly batchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic addError(entityRealm: EntityRealmState, error: ErrorAccessor.Error): () => void {\n\t\treturn this.accessorErrorManager.addError(entityRealm, error)\n\t}\n\n\tpublic addEventListener<Type extends keyof EntityAccessor.RuntimeEntityEventListenerMap>(\n\t\tstate: EntityRealmState,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityAccessor.EntityEventListenerMap[Type],\n\t) {\n\t\tif (state.eventListeners === undefined) {\n\t\t\tstate.eventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.eventListeners.add(event, listener)\n\t}\n\n\tpublic batchUpdates(state: EntityRealmState, performUpdates: EntityAccessor.BatchUpdatesHandler) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tperformUpdates(state.getAccessor, this.batchUpdatesOptions)\n\t\t})\n\t}\n\n\tpublic connectEntityAtField(outerState: EntityRealmState, fieldName: FieldName, entityToConnect: EntityAccessor) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst stateToConnect = OperationsHelpers.resolveAndPrepareEntityToConnect(this.treeStore, entityToConnect)\n\t\t\tconst fieldsToConnect = getEntityMarker(stateToConnect).fields\n\t\t\tconst persistedData = this.treeStore.persistedEntityData.get(outerState.entity.id.uniqueValue)\n\n\t\t\tconst entityName = outerState.entity.entityName\n\t\t\tconst fieldSchema = this.treeStore.schema.getEntityFieldOrUndefined(entityName, fieldName)\n\n\t\t\tif (fieldSchema === undefined) {\n\t\t\t\tthrow new BindingError(\n\t\t\t\t\t`EntityAccessor.connectEntityAtField: Unknown field ${entityName}.${fieldName}.\\n\\n`\n\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t)\n\t\t\t} else if (fieldSchema.__typename === '_Column') {\n\t\t\t\tthrow new BindingError(\n\t\t\t\t\t`EntityAccessor.connectEntityAtField: Cannot connect at field ${entityName}.${fieldName} because `\n\t\t\t\t\t\t+ `it's not a has-one relation.\\n\\n`\n\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealm(outerState)) {\n\t\t\t\tconst targetHasOneMarkers = this.resolveHasOneRelationMarkers(getEntityMarker(state).fields, fieldName, 'connect', state === outerState)\n\t\t\t\tfor (const targetHasOneMarker of targetHasOneMarkers) {\n\t\t\t\t\tconst previouslyConnectedState = state.children.get(targetHasOneMarker.placeholderName)\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tpreviouslyConnectedState === undefined\n\t\t\t\t\t\t|| previouslyConnectedState.type === 'field'\n\t\t\t\t\t\t|| previouslyConnectedState.type === 'entityList'\n\t\t\t\t\t) {\n\t\t\t\t\t\tOperationsHelpers.rejectInvalidAccessorTree()\n\t\t\t\t\t}\n\n\t\t\t\t\tif (previouslyConnectedState.entity.entityName !== stateToConnect.entity.entityName) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`EntityAccessor.connectEntityAtField: Attempting to connect at field ${entityName}.${fieldName} `\n\t\t\t\t\t\t\t\t+ `an entity of type '${stateToConnect.entity.entityName}' but `\n\t\t\t\t\t\t\t\t+ `'${previouslyConnectedState.entity.entityName}' is expected.\\n\\n`\n\t\t\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tMarkerComparator.assertEntityMarkersSubsetOf(targetHasOneMarker.fields, fieldsToConnect)\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tif (error instanceof LocalizedBindingError) {\n\t\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t\t`EntityAccessor: cannot connect entity with key '${entityToConnect.key}' because its fields are`\n\t\t\t\t\t\t\t\t\t+ `incompatible with entities found at field '${fieldName}'. Make sure both trees are equivalent.\\n\\n`\n\t\t\t\t\t\t\t\t\t+ `${error.message}\\n\\n`\n\t\t\t\t\t\t\t\t\t+ (error.markerPath.length > 1\n\t\t\t\t\t\t\t\t\t\t? `Incompatibility found at: ${ErrorLocator.locateMarkerPath(error.markerPath)}.\\n\\n`\n\t\t\t\t\t\t\t\t\t\t: '')\n\t\t\t\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(state)}.`,\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthrow error\n\t\t\t\t\t}\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tpreviouslyConnectedState === stateToConnect\n\t\t\t\t\t\t|| previouslyConnectedState.entity.id.value === stateToConnect.entity.id.value\n\t\t\t\t\t) {\n\t\t\t\t\t\tcontinue // Do nothing.\n\t\t\t\t\t}\n\n\t\t\t\t\t// TODO remove from planned deletions if appropriate\n\n\t\t\t\t\tthis.treeStore.disposeOfRealm(previouslyConnectedState)\n\n\t\t\t\t\tlet changesDelta = 0\n\n\t\t\t\t\tif (previouslyConnectedState.type === 'entityRealm') {\n\t\t\t\t\t\tchangesDelta -= previouslyConnectedState.unpersistedChangesCount\n\t\t\t\t\t}\n\n\t\t\t\t\tconst persistedId = persistedData?.get(targetHasOneMarker.placeholderName)?.value\n\t\t\t\t\tif (persistedId instanceof ServerId && persistedId.value === stateToConnect.entity.id.value) {\n\t\t\t\t\t\tchangesDelta-- // It was removed from the list but now we're adding it back.\n\t\t\t\t\t} else if (persistedId instanceof ServerId && persistedId.value === previouslyConnectedState.entity.id.value) {\n\t\t\t\t\t\tchangesDelta++ // We're changing it from the persisted id.\n\t\t\t\t\t} else if (!previouslyConnectedState.entity.id.existsOnServer) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tpersistedId === null // We're updating.\n\t\t\t\t\t\t\t|| (persistedId === undefined // We're creating.\n\t\t\t\t\t\t\t\t&& (!fieldsToConnect.hasAtLeastOneBearingField || !targetHasOneMarker.parameters.isNonbearing))\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (stateToConnect.type === 'entityRealm') {\n\t\t\t\t\t\tchangesDelta += stateToConnect.unpersistedChangesCount\n\t\t\t\t\t}\n\n\t\t\t\t\t// TODO do something about the existing state…\n\n\t\t\t\t\tthis.stateInitializer.initializeEntityRealm(\n\t\t\t\t\t\tstateToConnect.entity.id,\n\t\t\t\t\t\tstateToConnect.entity.entityName,\n\t\t\t\t\t\tpreviouslyConnectedState.blueprint,\n\t\t\t\t\t\tstateToConnect,\n\t\t\t\t\t)\n\n\t\t\t\t\tthis.eventManager.registerUpdatedConnection(state, targetHasOneMarker.placeholderName)\n\t\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic disconnectEntityAtField(\n\t\touterState: EntityRealmState,\n\t\tfieldName: FieldName,\n\t\tinitializeReplacement: EntityAccessor.BatchUpdatesHandler | undefined,\n\t) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst persistedData = this.treeStore.persistedEntityData.get(outerState.entity.id.uniqueValue)\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealm(outerState)) {\n\t\t\t\tconst targetHasOneMarkers = this.resolveHasOneRelationMarkers(getEntityMarker(state).fields, fieldName, 'disconnect', state === outerState)\n\t\t\t\tfor (const targetHasOneMarker of targetHasOneMarkers) {\n\t\t\t\t\tconst stateToDisconnect = state.children.get(targetHasOneMarker.placeholderName)\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tstateToDisconnect === undefined\n\t\t\t\t\t\t|| (stateToDisconnect.type !== 'entityRealm' && stateToDisconnect.type !== 'entityRealmStub')\n\t\t\t\t\t) {\n\t\t\t\t\t\tOperationsHelpers.rejectInvalidAccessorTree()\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.treeStore.disposeOfRealm(stateToDisconnect)\n\n\t\t\t\t\tlet changesDelta = 0\n\n\t\t\t\t\tif (stateToDisconnect.type === 'entityRealm') {\n\t\t\t\t\t\tchangesDelta -= stateToDisconnect.unpersistedChangesCount\n\t\t\t\t\t}\n\t\t\t\t\tconst persistedId = persistedData?.get(targetHasOneMarker.placeholderName)?.value\n\n\t\t\t\t\tif (persistedId instanceof ServerId) {\n\t\t\t\t\t\tif (persistedId.value === stateToDisconnect.entity.id.value) {\n\t\t\t\t\t\t\tchangesDelta++ // Disconnecting the persisted entity.\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Do nothing. The entity that is being disconnected is already different from the one that is persisted,\n\t\t\t\t\t\t\t// and so we must have already accounted for this change.\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (persistedId === undefined || persistedId === null) {\n\t\t\t\t\t\tif (stateToDisconnect.entity.id.existsOnServer) {\n\t\t\t\t\t\t\t// We had connected a persisted entity, which increased the changes count,\n\t\t\t\t\t\t\t// but now are disconnecting it again.\n\t\t\t\t\t\t\tchangesDelta--\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// Do nothing. Disconnecting unpersisted entities doesn't change the count.\n\t\t\t\t\t}\n\n\t\t\t\t\tconst newEntity = this.stateInitializer.initializeEntityRealm(\n\t\t\t\t\t\tnew UnpersistedEntityDummyId(),\n\t\t\t\t\t\tstateToDisconnect.entity.entityName,\n\t\t\t\t\t\tstateToDisconnect.blueprint,\n\t\t\t\t\t)\n\t\t\t\t\tthis.stateInitializer.runImmediateUserInitialization(newEntity, initializeReplacement)\n\n\t\t\t\t\tthis.eventManager.registerUpdatedConnection(state, targetHasOneMarker.placeholderName)\n\t\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic deleteEntity(realm: EntityRealmState) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst outerState = realm.entity\n\t\t\touterState.isScheduledForDeletion = true\n\n\t\t\tconst entityId = outerState.id\n\n\t\t\tfor (const realmToDelete of outerState.realms.values()) {\n\t\t\t\tlet parent: EntityRealmState | EntityRealmStateStub | EntityListState | undefined\n\t\t\t\tlet changesDelta = 0\n\n\t\t\t\tif (realmToDelete.blueprint.type === 'listEntity') {\n\t\t\t\t\tparent = realmToDelete.blueprint.parent\n\t\t\t\t\tparent.children.delete(entityId.value)\n\n\t\t\t\t\tif (entityId.existsOnServer) {\n\t\t\t\t\t\tif (parent.plannedRemovals === undefined) {\n\t\t\t\t\t\t\tparent.plannedRemovals = new Map()\n\t\t\t\t\t\t}\n\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t\tparent.plannedRemovals.set(entityId.value, 'delete')\n\t\t\t\t\t}\n\t\t\t\t} else if (realmToDelete.blueprint.type === 'hasOne') {\n\t\t\t\t\tparent = realmToDelete.blueprint.parent\n\t\t\t\t\tconst placeholderName = realmToDelete.blueprint.marker.placeholderName\n\n\t\t\t\t\tif (entityId.existsOnServer) {\n\t\t\t\t\t\tif (parent.plannedHasOneDeletions === undefined) {\n\t\t\t\t\t\t\tparent.plannedHasOneDeletions = new Map()\n\t\t\t\t\t\t}\n\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t\tparent.plannedHasOneDeletions.set(placeholderName, realmToDelete)\n\t\t\t\t\t}\n\t\t\t\t\tthis.stateInitializer.initializeEntityRealm(\n\t\t\t\t\t\tnew UnpersistedEntityDummyId(),\n\t\t\t\t\t\touterState.entityName,\n\t\t\t\t\t\trealmToDelete.blueprint,\n\t\t\t\t\t)\n\n\t\t\t\t\tthis.eventManager.registerUpdatedConnection(parent, placeholderName)\n\t\t\t\t} else if (realmToDelete.blueprint.type === 'subTree') {\n\t\t\t\t\tparent = undefined\n\t\t\t\t\tif (realmToDelete.type === 'entityRealm') {\n\t\t\t\t\t\tthis.eventManager.registerJustUpdated(realmToDelete, 1)\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\treturn assertNever(realmToDelete.blueprint)\n\t\t\t\t}\n\n\t\t\t\tif (parent) {\n\t\t\t\t\tif (realmToDelete.type === 'entityRealm') {\n\t\t\t\t\t\t// Undoing whatever this had caused\n\t\t\t\t\t\tchangesDelta -= realmToDelete.unpersistedChangesCount\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.eventManager.registerJustUpdated(parent, changesDelta)\n\n\t\t\t\t\tif (realmToDelete.type === 'entityRealm') {\n\t\t\t\t\t\tparent.childrenWithPendingUpdates?.delete(realmToDelete)\n\t\t\t\t\t}\n\t\t\t\t\tthis.treeStore.disposeOfRealm(realmToDelete)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tprivate *resolveHasOneRelationMarkers(\n\t\tcontainer: EntityFieldMarkersContainer,\n\t\tfield: FieldName,\n\t\ttype: 'connect' | 'disconnect',\n\t\tmustExists: boolean,\n\t): IterableIterator<HasOneRelationMarker> {\n\t\tconst placeholders = container.placeholders.get(field)\n\n\t\tif (placeholders === undefined) {\n\t\t\tif (!mustExists) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthrow new BindingError(`Cannot ${type} at field '${field}' as it wasn't registered during static render.`)\n\t\t}\n\t\tconst normalizedPlaceholders = placeholders instanceof Set ? placeholders : [placeholders]\n\n\t\tfor (const placeholderName of normalizedPlaceholders) {\n\t\t\tconst hasOneRelation = container.markers.get(placeholderName)\n\n\t\t\tif (!(hasOneRelation instanceof HasOneRelationMarker)) {\n\t\t\t\tthrow new BindingError(`Cannot ${type} at field '${field}' as it doesn't refer to a has one relation.`)\n\t\t\t}\n\t\t\tyield hasOneRelation\n\t\t}\n\t}\n}\n"],"names":["EventListenersStore","OperationsHelpers","getEntityMarker","BindingError","ErrorLocator","StateIterator","MarkerComparator","LocalizedBindingError","ServerId","UnpersistedEntityDummyId","assertNever","HasOneRelationMarker"],"mappings":";;;;;;;AAiBO,MAAM,iBAAiB;AAAA,EACtB,YACW,sBACA,qBACA,cACA,kBACA,WAChB;AALgB,SAAA,uBAAA;AACA,SAAA,sBAAA;AACA,SAAA,eAAA;AACA,SAAA,mBAAA;AACA,SAAA,YAAA;AAAA,EACf;AAAA,EAEI,SAAS,aAA+B,OAAwC;AACtF,WAAO,KAAK,qBAAqB,SAAS,aAAa,KAAK;AAAA,EAC7D;AAAA,EAEO,iBACN,OACA,OACA,UACC;AACD,QAAI,MAAM,mBAAmB,QAAW;A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WAAW;AACtD,mBAAS;AACT,cAAI,cAAc,SAAS,eAAe;AACzC,iBAAK,aAAa,oBAAoB,eAAe,CAAC;AAAA,UACvD;AAAA,QACD,OAAO;AACN,iBAAOC,cAAAA,YAAY,cAAc,SAAS;AAAA,QAC3C;AAEA,YAAI,QAAQ;AACX,cAAI,cAAc,SAAS,eAAe;AAEzC,4BAAgB,cAAc;AAAA,UAC/B;AAEA,eAAK,aAAa,oBAAoB,QAAQ,YAAY;AAE1D,cAAI,cAAc,SAAS,eAAe;AACzC,mBAAO,4BAA4B,OAAO,aAAa;AAAA,UACxD;AACA,eAAK,UAAU,eAAe,aAAa;AAAA,QAC5C;AAAA,MACD;AAAA,IACD,CAAC;AAAA,EACF;AAAA,EAEA,CAAS,6BACR,WACA,OACA,MACA,YACyC;AACzC,UAAM,eAAe,UAAU,aAAa,IAAI,KAAK;AAErD,QAAI,iBAAiB,QAAW;AAC/B,UAAI,CAAC,YAAY;AAChB;AAAA,MACD;AACA,YAAM,IAAIP,cAAAA,aAAa,UAAU,IAAI,cAAc,KAAK,iDAAiD;AAAA,IAC1G;AACA,UAAM,yBAAyB,wBAAwB,MAAM,eAAe,CAAC,YAAY;AAEzF,eAAW,mBAAmB,wBAAwB;AACrD,YAAM,iBAAiB,UAAU,QAAQ,IAAI,eAAe;AAE5D,UAAI,EAAE,0BAA0BQ,cAAAA,uBAAuB;AACtD,cAAM,IAAIR,cAAAA,aAAa,UAAU,IAAI,cAAc,KAAK,8CAA8C;AAAA,MACvG;AACA,YAAM;AAAA,IACP;AAAA,EACD;AACD;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"EntityOperations.js","sources":["../../../../../../packages/binding-legacy/src/core/operations/EntityOperations.ts"],"sourcesContent":["import type { BatchUpdatesOptions, EntityAccessor, ErrorAccessor } from '@contember/binding-common'\nimport { ServerId, UnpersistedEntityDummyId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { EntityFieldMarkersContainer, HasOneRelationMarker } from '@contember/binding-common'\nimport type { FieldName } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { AccessorErrorManager } from '../AccessorErrorManager'\nimport type { EventManager } from '../EventManager'\nimport { LocalizedBindingError } from '@contember/binding-common'\nimport { MarkerComparator } from '@contember/binding-common'\nimport { EntityListState, EntityRealmState, EntityRealmStateStub, getEntityMarker, StateIterator } from '../state'\nimport type { StateInitializer } from '../StateInitializer'\nimport type { TreeStore } from '../TreeStore'\nimport { OperationsHelpers } from './OperationsHelpers'\nimport { EventListenersStore } from '@contember/binding-common'\nimport { ErrorLocator } from '../exceptions/ErrorLocator'\n\nexport class EntityOperations {\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly batchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic addError(entityRealm: EntityRealmState, error: ErrorAccessor.Error): () => void {\n\t\treturn this.accessorErrorManager.addError(entityRealm, error)\n\t}\n\n\tpublic addEventListener<Type extends keyof EntityAccessor.RuntimeEntityEventListenerMap>(\n\t\tstate: EntityRealmState,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityAccessor.EntityEventListenerMap[Type],\n\t) {\n\t\tif (state.eventListeners === undefined) {\n\t\t\tstate.eventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.eventListeners.add(event, listener)\n\t}\n\n\tpublic batchUpdates(state: EntityRealmState, performUpdates: EntityAccessor.BatchUpdatesHandler) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tperformUpdates(state.getAccessor, this.batchUpdatesOptions)\n\t\t})\n\t}\n\n\tpublic connectEntityAtField(outerState: EntityRealmState, fieldName: FieldName, entityToConnect: EntityAccessor) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst stateToConnect = OperationsHelpers.resolveAndPrepareEntityToConnect(this.treeStore, entityToConnect)\n\t\t\tconst fieldsToConnect = getEntityMarker(stateToConnect).fields\n\t\t\tconst persistedData = this.treeStore.persistedEntityData.get(outerState.entity.id.uniqueValue)\n\n\t\t\tconst entityName = outerState.entity.entityName\n\t\t\tconst fieldSchema = this.treeStore.schema.getEntityFieldOrUndefined(entityName, fieldName)\n\n\t\t\tif (fieldSchema === undefined) {\n\t\t\t\tthrow new BindingError(\n\t\t\t\t\t`EntityAccessor.connectEntityAtField: Unknown field ${entityName}.${fieldName}.\\n\\n`\n\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t)\n\t\t\t} else if (fieldSchema.__typename === '_Column') {\n\t\t\t\tthrow new BindingError(\n\t\t\t\t\t`EntityAccessor.connectEntityAtField: Cannot connect at field ${entityName}.${fieldName} because `\n\t\t\t\t\t\t+ `it's not a has-one relation.\\n\\n`\n\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealm(outerState)) {\n\t\t\t\tconst targetHasOneMarkers = this.resolveHasOneRelationMarkers(getEntityMarker(state).fields, fieldName, 'connect', state === outerState)\n\t\t\t\tfor (const targetHasOneMarker of targetHasOneMarkers) {\n\t\t\t\t\tconst previouslyConnectedState = state.children.get(targetHasOneMarker.placeholderName)\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tpreviouslyConnectedState === undefined\n\t\t\t\t\t\t|| previouslyConnectedState.type === 'field'\n\t\t\t\t\t\t|| previouslyConnectedState.type === 'entityList'\n\t\t\t\t\t) {\n\t\t\t\t\t\tOperationsHelpers.rejectInvalidAccessorTree()\n\t\t\t\t\t}\n\n\t\t\t\t\tif (previouslyConnectedState.entity.entityName !== stateToConnect.entity.entityName) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`EntityAccessor.connectEntityAtField: Attempting to connect at field ${entityName}.${fieldName} `\n\t\t\t\t\t\t\t\t+ `an entity of type '${stateToConnect.entity.entityName}' but `\n\t\t\t\t\t\t\t\t+ `'${previouslyConnectedState.entity.entityName}' is expected.\\n\\n`\n\t\t\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tMarkerComparator.assertEntityMarkersSubsetOf(targetHasOneMarker.fields, fieldsToConnect)\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tif (error instanceof LocalizedBindingError) {\n\t\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t\t`EntityAccessor: cannot connect entity with key '${entityToConnect.key}' because its fields are`\n\t\t\t\t\t\t\t\t\t+ `incompatible with entities found at field '${fieldName}'. Make sure both trees are equivalent.\\n\\n`\n\t\t\t\t\t\t\t\t\t+ `${error.message}\\n\\n`\n\t\t\t\t\t\t\t\t\t+ (error.markerPath.length > 1\n\t\t\t\t\t\t\t\t\t\t? `Incompatibility found at: ${ErrorLocator.locateMarkerPath(error.markerPath)}.\\n\\n`\n\t\t\t\t\t\t\t\t\t\t: '')\n\t\t\t\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(state)}.`,\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthrow error\n\t\t\t\t\t}\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tpreviouslyConnectedState === stateToConnect\n\t\t\t\t\t\t|| previouslyConnectedState.entity.id.value === stateToConnect.entity.id.value\n\t\t\t\t\t) {\n\t\t\t\t\t\tcontinue // Do nothing.\n\t\t\t\t\t}\n\n\t\t\t\t\t// TODO remove from planned deletions if appropriate\n\n\t\t\t\t\tthis.treeStore.disposeOfRealm(previouslyConnectedState)\n\n\t\t\t\t\tlet changesDelta = 0\n\n\t\t\t\t\tif (previouslyConnectedState.type === 'entityRealm') {\n\t\t\t\t\t\tchangesDelta -= previouslyConnectedState.unpersistedChangesCount\n\t\t\t\t\t}\n\n\t\t\t\t\tconst persistedId = persistedData?.get(targetHasOneMarker.placeholderName)?.value\n\t\t\t\t\tif (persistedId instanceof ServerId && persistedId.value === stateToConnect.entity.id.value) {\n\t\t\t\t\t\tchangesDelta-- // It was removed from the list but now we're adding it back.\n\t\t\t\t\t} else if (persistedId instanceof ServerId && persistedId.value === previouslyConnectedState.entity.id.value) {\n\t\t\t\t\t\tchangesDelta++ // We're changing it from the persisted id.\n\t\t\t\t\t} else if (!previouslyConnectedState.entity.id.existsOnServer) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tpersistedId === null // We're updating.\n\t\t\t\t\t\t\t|| (persistedId === undefined // We're creating.\n\t\t\t\t\t\t\t\t&& (!fieldsToConnect.hasAtLeastOneBearingField || !targetHasOneMarker.parameters.isNonbearing))\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (stateToConnect.type === 'entityRealm') {\n\t\t\t\t\t\tchangesDelta += stateToConnect.unpersistedChangesCount\n\t\t\t\t\t}\n\n\t\t\t\t\t// TODO do something about the existing state…\n\n\t\t\t\t\tthis.stateInitializer.initializeEntityRealm(\n\t\t\t\t\t\tstateToConnect.entity.id,\n\t\t\t\t\t\tstateToConnect.entity.entityName,\n\t\t\t\t\t\tpreviouslyConnectedState.blueprint,\n\t\t\t\t\t\tstateToConnect,\n\t\t\t\t\t)\n\n\t\t\t\t\tthis.eventManager.registerUpdatedConnection(state, targetHasOneMarker.placeholderName)\n\t\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic disconnectEntityAtField(\n\t\touterState: EntityRealmState,\n\t\tfieldName: FieldName,\n\t\tinitializeReplacement: EntityAccessor.BatchUpdatesHandler | undefined,\n\t) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst persistedData = this.treeStore.persistedEntityData.get(outerState.entity.id.uniqueValue)\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealm(outerState)) {\n\t\t\t\tconst targetHasOneMarkers = this.resolveHasOneRelationMarkers(getEntityMarker(state).fields, fieldName, 'disconnect', state === outerState)\n\t\t\t\tfor (const targetHasOneMarker of targetHasOneMarkers) {\n\t\t\t\t\tconst stateToDisconnect = state.children.get(targetHasOneMarker.placeholderName)\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tstateToDisconnect === undefined\n\t\t\t\t\t\t|| (stateToDisconnect.type !== 'entityRealm' && stateToDisconnect.type !== 'entityRealmStub')\n\t\t\t\t\t) {\n\t\t\t\t\t\tOperationsHelpers.rejectInvalidAccessorTree()\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.treeStore.disposeOfRealm(stateToDisconnect)\n\n\t\t\t\t\tlet changesDelta = 0\n\n\t\t\t\t\tif (stateToDisconnect.type === 'entityRealm') {\n\t\t\t\t\t\tchangesDelta -= stateToDisconnect.unpersistedChangesCount\n\t\t\t\t\t}\n\t\t\t\t\tconst persistedId = persistedData?.get(targetHasOneMarker.placeholderName)?.value\n\n\t\t\t\t\tif (persistedId instanceof ServerId) {\n\t\t\t\t\t\tif (persistedId.value === stateToDisconnect.entity.id.value) {\n\t\t\t\t\t\t\tchangesDelta++ // Disconnecting the persisted entity.\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Do nothing. The entity that is being disconnected is already different from the one that is persisted,\n\t\t\t\t\t\t\t// and so we must have already accounted for this change.\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (persistedId === undefined || persistedId === null) {\n\t\t\t\t\t\tif (stateToDisconnect.entity.id.existsOnServer) {\n\t\t\t\t\t\t\t// We had connected a persisted entity, which increased the changes count,\n\t\t\t\t\t\t\t// but now are disconnecting it again.\n\t\t\t\t\t\t\tchangesDelta--\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// Do nothing. Disconnecting unpersisted entities doesn't change the count.\n\t\t\t\t\t}\n\n\t\t\t\t\tconst newEntity = this.stateInitializer.initializeEntityRealm(\n\t\t\t\t\t\tnew UnpersistedEntityDummyId(),\n\t\t\t\t\t\tstateToDisconnect.entity.entityName,\n\t\t\t\t\t\tstateToDisconnect.blueprint,\n\t\t\t\t\t)\n\t\t\t\t\tthis.stateInitializer.runImmediateUserInitialization(newEntity, initializeReplacement)\n\n\t\t\t\t\tthis.eventManager.registerUpdatedConnection(state, targetHasOneMarker.placeholderName)\n\t\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic deleteEntity(realm: EntityRealmState) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst outerState = realm.entity\n\t\t\touterState.isScheduledForDeletion = true\n\n\t\t\tconst entityId = outerState.id\n\n\t\t\tfor (const realmToDelete of outerState.realms.values()) {\n\t\t\t\tlet parent: EntityRealmState | EntityRealmStateStub | EntityListState | undefined\n\t\t\t\tlet changesDelta = 0\n\n\t\t\t\tif (realmToDelete.blueprint.type === 'listEntity') {\n\t\t\t\t\tparent = realmToDelete.blueprint.parent\n\t\t\t\t\tparent.children.delete(entityId.value)\n\n\t\t\t\t\tif (entityId.existsOnServer) {\n\t\t\t\t\t\tif (parent.plannedRemovals === undefined) {\n\t\t\t\t\t\t\tparent.plannedRemovals = new Map()\n\t\t\t\t\t\t}\n\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t\tparent.plannedRemovals.set(entityId.value, 'delete')\n\t\t\t\t\t}\n\t\t\t\t} else if (realmToDelete.blueprint.type === 'hasOne') {\n\t\t\t\t\tparent = realmToDelete.blueprint.parent\n\t\t\t\t\tconst placeholderName = realmToDelete.blueprint.marker.placeholderName\n\n\t\t\t\t\tif (entityId.existsOnServer) {\n\t\t\t\t\t\tif (parent.plannedHasOneDeletions === undefined) {\n\t\t\t\t\t\t\tparent.plannedHasOneDeletions = new Map()\n\t\t\t\t\t\t}\n\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t\tparent.plannedHasOneDeletions.set(placeholderName, realmToDelete)\n\t\t\t\t\t}\n\t\t\t\t\tthis.stateInitializer.initializeEntityRealm(\n\t\t\t\t\t\tnew UnpersistedEntityDummyId(),\n\t\t\t\t\t\touterState.entityName,\n\t\t\t\t\t\trealmToDelete.blueprint,\n\t\t\t\t\t)\n\n\t\t\t\t\tthis.eventManager.registerUpdatedConnection(parent, placeholderName)\n\t\t\t\t} else if (realmToDelete.blueprint.type === 'subTree') {\n\t\t\t\t\tparent = undefined\n\t\t\t\t\tif (realmToDelete.type === 'entityRealm') {\n\t\t\t\t\t\tthis.eventManager.registerJustUpdated(realmToDelete, 1)\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\treturn assertNever(realmToDelete.blueprint)\n\t\t\t\t}\n\n\t\t\t\tif (parent) {\n\t\t\t\t\tif (realmToDelete.type === 'entityRealm') {\n\t\t\t\t\t\t// Undoing whatever this had caused\n\t\t\t\t\t\tchangesDelta -= realmToDelete.unpersistedChangesCount\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.eventManager.registerJustUpdated(parent, changesDelta)\n\n\t\t\t\t\tif (realmToDelete.type === 'entityRealm') {\n\t\t\t\t\t\tparent.childrenWithPendingUpdates?.delete(realmToDelete)\n\t\t\t\t\t}\n\t\t\t\t\tthis.treeStore.disposeOfRealm(realmToDelete)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tprivate *resolveHasOneRelationMarkers(\n\t\tcontainer: EntityFieldMarkersContainer,\n\t\tfield: FieldName,\n\t\ttype: 'connect' | 'disconnect',\n\t\tmustExists: boolean,\n\t): IterableIterator<HasOneRelationMarker> {\n\t\tconst placeholders = container.placeholders.get(field)\n\n\t\tif (placeholders === undefined) {\n\t\t\tif (!mustExists) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthrow new BindingError(`Cannot ${type} at field '${field}' as it wasn't registered during static render.`)\n\t\t}\n\t\tconst normalizedPlaceholders = placeholders instanceof Set ? placeholders : [placeholders]\n\n\t\tfor (const placeholderName of normalizedPlaceholders) {\n\t\t\tconst hasOneRelation = container.markers.get(placeholderName)\n\n\t\t\tif (!(hasOneRelation instanceof HasOneRelationMarker)) {\n\t\t\t\tthrow new BindingError(`Cannot ${type} at field '${field}' as it doesn't refer to a has one relation.`)\n\t\t\t}\n\t\t\tyield hasOneRelation\n\t\t}\n\t}\n}\n"],"names":[],"mappings":";;;;;AAiBO,MAAM,iBAAiB;AAAA,EACtB,YACW,sBACA,qBACA,cACA,kBACA,WAChB;AALgB,SAAA,uBAAA;AACA,SAAA,sBAAA;AACA,SAAA,eAAA;AACA,SAAA,mBAAA;AACA,SAAA,YAAA;AAAA,EACf;AAAA,EAEI,SAAS,aAA+B,OAAwC;AACtF,WAAO,KAAK,qBAAqB,SAAS,aAAa,KAAK;AAAA,EAC7D;AAAA,EAEO,iBACN,OACA,OACA,UACC;AACD,QAAI,MAAM,mBAAmB,QAAW;AACvC,YAAM,iBAAiB,IAAI,oBAAA;AAAA,IAC5B;AACA,WAAO,MAAM,eAAe,IAAI,OAAO,QAAQ;AAAA,EAChD;AAAA,EAEO,aAAa,OAAyB,gBAAoD;AAChG,SAAK,aAAa,cAAc,MAAM;AACrC,qBAAe,MAAM,aAAa,KAAK,mBAAmB;AAAA,IAC3D,CAAC;AAAA,EACF;AAAA,EAEO,qBAAqB,YAA8B,WAAsB,iBAAiC;AAChH,SAAK,aAAa,cAAc,MAAM;AACrC,YAAM,iBAAiB,kBAAkB,iCAAiC,KAAK,WAAW,eAAe;AACzG,YAAM,kBAAkB,gBAAgB,cAAc,EAAE;AACxD,YAAM,gBAAgB,KAAK,UAAU,oBAAoB,IAAI,WAAW,OAAO,GAAG,WAAW;AAE7F,YAAM,aAAa,WAAW,OAAO;AACrC,YAAM,cAAc,KAAK,UAAU,OAAO,0BAA0B,YAAY,SAAS;AAEzF,UAAI,gBAAgB,QAAW;AAC9B,cAAM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UAAU,QAAQ,IAAI,eAAe;AAE5D,UAAI,EAAE,0BAA0B,uBAAuB;AACtD,cAAM,IAAI,aAAa,UAAU,IAAI,cAAc,KAAK,8CAA8C;AAAA,MACvG;AACA,YAAM;AAAA,IACP;AAAA,EACD;AACD;"}
1
+ {"version":3,"file":"EntityOperations.js","sources":["../../../../../../packages/binding-legacy/src/core/operations/EntityOperations.ts"],"sourcesContent":["import type { BatchUpdatesOptions, EntityAccessor, ErrorAccessor } from '@contember/binding-common'\nimport { ServerId, UnpersistedEntityDummyId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { EntityFieldMarkersContainer, HasOneRelationMarker } from '@contember/binding-common'\nimport type { FieldName } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { AccessorErrorManager } from '../AccessorErrorManager.js'\nimport type { EventManager } from '../EventManager.js'\nimport { LocalizedBindingError } from '@contember/binding-common'\nimport { MarkerComparator } from '@contember/binding-common'\nimport { EntityListState, EntityRealmState, EntityRealmStateStub, getEntityMarker, StateIterator } from '../state/index.js'\nimport type { StateInitializer } from '../StateInitializer.js'\nimport type { TreeStore } from '../TreeStore.js'\nimport { OperationsHelpers } from './OperationsHelpers.js'\nimport { EventListenersStore } from '@contember/binding-common'\nimport { ErrorLocator } from '../exceptions/ErrorLocator.js'\n\nexport class EntityOperations {\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly batchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic addError(entityRealm: EntityRealmState, error: ErrorAccessor.Error): () => void {\n\t\treturn this.accessorErrorManager.addError(entityRealm, error)\n\t}\n\n\tpublic addEventListener<Type extends keyof EntityAccessor.RuntimeEntityEventListenerMap>(\n\t\tstate: EntityRealmState,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityAccessor.EntityEventListenerMap[Type],\n\t) {\n\t\tif (state.eventListeners === undefined) {\n\t\t\tstate.eventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.eventListeners.add(event, listener)\n\t}\n\n\tpublic batchUpdates(state: EntityRealmState, performUpdates: EntityAccessor.BatchUpdatesHandler) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tperformUpdates(state.getAccessor, this.batchUpdatesOptions)\n\t\t})\n\t}\n\n\tpublic connectEntityAtField(outerState: EntityRealmState, fieldName: FieldName, entityToConnect: EntityAccessor) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst stateToConnect = OperationsHelpers.resolveAndPrepareEntityToConnect(this.treeStore, entityToConnect)\n\t\t\tconst fieldsToConnect = getEntityMarker(stateToConnect).fields\n\t\t\tconst persistedData = this.treeStore.persistedEntityData.get(outerState.entity.id.uniqueValue)\n\n\t\t\tconst entityName = outerState.entity.entityName\n\t\t\tconst fieldSchema = this.treeStore.schema.getEntityFieldOrUndefined(entityName, fieldName)\n\n\t\t\tif (fieldSchema === undefined) {\n\t\t\t\tthrow new BindingError(\n\t\t\t\t\t`EntityAccessor.connectEntityAtField: Unknown field ${entityName}.${fieldName}.\\n\\n`\n\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t)\n\t\t\t} else if (fieldSchema.__typename === '_Column') {\n\t\t\t\tthrow new BindingError(\n\t\t\t\t\t`EntityAccessor.connectEntityAtField: Cannot connect at field ${entityName}.${fieldName} because `\n\t\t\t\t\t\t+ `it's not a has-one relation.\\n\\n`\n\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealm(outerState)) {\n\t\t\t\tconst targetHasOneMarkers = this.resolveHasOneRelationMarkers(getEntityMarker(state).fields, fieldName, 'connect', state === outerState)\n\t\t\t\tfor (const targetHasOneMarker of targetHasOneMarkers) {\n\t\t\t\t\tconst previouslyConnectedState = state.children.get(targetHasOneMarker.placeholderName)\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tpreviouslyConnectedState === undefined\n\t\t\t\t\t\t|| previouslyConnectedState.type === 'field'\n\t\t\t\t\t\t|| previouslyConnectedState.type === 'entityList'\n\t\t\t\t\t) {\n\t\t\t\t\t\tOperationsHelpers.rejectInvalidAccessorTree()\n\t\t\t\t\t}\n\n\t\t\t\t\tif (previouslyConnectedState.entity.entityName !== stateToConnect.entity.entityName) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`EntityAccessor.connectEntityAtField: Attempting to connect at field ${entityName}.${fieldName} `\n\t\t\t\t\t\t\t\t+ `an entity of type '${stateToConnect.entity.entityName}' but `\n\t\t\t\t\t\t\t\t+ `'${previouslyConnectedState.entity.entityName}' is expected.\\n\\n`\n\t\t\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tMarkerComparator.assertEntityMarkersSubsetOf(targetHasOneMarker.fields, fieldsToConnect)\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tif (error instanceof LocalizedBindingError) {\n\t\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t\t`EntityAccessor: cannot connect entity with key '${entityToConnect.key}' because its fields are`\n\t\t\t\t\t\t\t\t\t+ `incompatible with entities found at field '${fieldName}'. Make sure both trees are equivalent.\\n\\n`\n\t\t\t\t\t\t\t\t\t+ `${error.message}\\n\\n`\n\t\t\t\t\t\t\t\t\t+ (error.markerPath.length > 1\n\t\t\t\t\t\t\t\t\t\t? `Incompatibility found at: ${ErrorLocator.locateMarkerPath(error.markerPath)}.\\n\\n`\n\t\t\t\t\t\t\t\t\t\t: '')\n\t\t\t\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(state)}.`,\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthrow error\n\t\t\t\t\t}\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tpreviouslyConnectedState === stateToConnect\n\t\t\t\t\t\t|| previouslyConnectedState.entity.id.value === stateToConnect.entity.id.value\n\t\t\t\t\t) {\n\t\t\t\t\t\tcontinue // Do nothing.\n\t\t\t\t\t}\n\n\t\t\t\t\t// TODO remove from planned deletions if appropriate\n\n\t\t\t\t\tthis.treeStore.disposeOfRealm(previouslyConnectedState)\n\n\t\t\t\t\tlet changesDelta = 0\n\n\t\t\t\t\tif (previouslyConnectedState.type === 'entityRealm') {\n\t\t\t\t\t\tchangesDelta -= previouslyConnectedState.unpersistedChangesCount\n\t\t\t\t\t}\n\n\t\t\t\t\tconst persistedId = persistedData?.get(targetHasOneMarker.placeholderName)?.value\n\t\t\t\t\tif (persistedId instanceof ServerId && persistedId.value === stateToConnect.entity.id.value) {\n\t\t\t\t\t\tchangesDelta-- // It was removed from the list but now we're adding it back.\n\t\t\t\t\t} else if (persistedId instanceof ServerId && persistedId.value === previouslyConnectedState.entity.id.value) {\n\t\t\t\t\t\tchangesDelta++ // We're changing it from the persisted id.\n\t\t\t\t\t} else if (!previouslyConnectedState.entity.id.existsOnServer) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tpersistedId === null // We're updating.\n\t\t\t\t\t\t\t|| (persistedId === undefined // We're creating.\n\t\t\t\t\t\t\t\t&& (!fieldsToConnect.hasAtLeastOneBearingField || !targetHasOneMarker.parameters.isNonbearing))\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (stateToConnect.type === 'entityRealm') {\n\t\t\t\t\t\tchangesDelta += stateToConnect.unpersistedChangesCount\n\t\t\t\t\t}\n\n\t\t\t\t\t// TODO do something about the existing state…\n\n\t\t\t\t\tthis.stateInitializer.initializeEntityRealm(\n\t\t\t\t\t\tstateToConnect.entity.id,\n\t\t\t\t\t\tstateToConnect.entity.entityName,\n\t\t\t\t\t\tpreviouslyConnectedState.blueprint,\n\t\t\t\t\t\tstateToConnect,\n\t\t\t\t\t)\n\n\t\t\t\t\tthis.eventManager.registerUpdatedConnection(state, targetHasOneMarker.placeholderName)\n\t\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic disconnectEntityAtField(\n\t\touterState: EntityRealmState,\n\t\tfieldName: FieldName,\n\t\tinitializeReplacement: EntityAccessor.BatchUpdatesHandler | undefined,\n\t) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst persistedData = this.treeStore.persistedEntityData.get(outerState.entity.id.uniqueValue)\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealm(outerState)) {\n\t\t\t\tconst targetHasOneMarkers = this.resolveHasOneRelationMarkers(getEntityMarker(state).fields, fieldName, 'disconnect', state === outerState)\n\t\t\t\tfor (const targetHasOneMarker of targetHasOneMarkers) {\n\t\t\t\t\tconst stateToDisconnect = state.children.get(targetHasOneMarker.placeholderName)\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tstateToDisconnect === undefined\n\t\t\t\t\t\t|| (stateToDisconnect.type !== 'entityRealm' && stateToDisconnect.type !== 'entityRealmStub')\n\t\t\t\t\t) {\n\t\t\t\t\t\tOperationsHelpers.rejectInvalidAccessorTree()\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.treeStore.disposeOfRealm(stateToDisconnect)\n\n\t\t\t\t\tlet changesDelta = 0\n\n\t\t\t\t\tif (stateToDisconnect.type === 'entityRealm') {\n\t\t\t\t\t\tchangesDelta -= stateToDisconnect.unpersistedChangesCount\n\t\t\t\t\t}\n\t\t\t\t\tconst persistedId = persistedData?.get(targetHasOneMarker.placeholderName)?.value\n\n\t\t\t\t\tif (persistedId instanceof ServerId) {\n\t\t\t\t\t\tif (persistedId.value === stateToDisconnect.entity.id.value) {\n\t\t\t\t\t\t\tchangesDelta++ // Disconnecting the persisted entity.\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Do nothing. The entity that is being disconnected is already different from the one that is persisted,\n\t\t\t\t\t\t\t// and so we must have already accounted for this change.\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (persistedId === undefined || persistedId === null) {\n\t\t\t\t\t\tif (stateToDisconnect.entity.id.existsOnServer) {\n\t\t\t\t\t\t\t// We had connected a persisted entity, which increased the changes count,\n\t\t\t\t\t\t\t// but now are disconnecting it again.\n\t\t\t\t\t\t\tchangesDelta--\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// Do nothing. Disconnecting unpersisted entities doesn't change the count.\n\t\t\t\t\t}\n\n\t\t\t\t\tconst newEntity = this.stateInitializer.initializeEntityRealm(\n\t\t\t\t\t\tnew UnpersistedEntityDummyId(),\n\t\t\t\t\t\tstateToDisconnect.entity.entityName,\n\t\t\t\t\t\tstateToDisconnect.blueprint,\n\t\t\t\t\t)\n\t\t\t\t\tthis.stateInitializer.runImmediateUserInitialization(newEntity, initializeReplacement)\n\n\t\t\t\t\tthis.eventManager.registerUpdatedConnection(state, targetHasOneMarker.placeholderName)\n\t\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic deleteEntity(realm: EntityRealmState) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst outerState = realm.entity\n\t\t\touterState.isScheduledForDeletion = true\n\n\t\t\tconst entityId = outerState.id\n\n\t\t\tfor (const realmToDelete of outerState.realms.values()) {\n\t\t\t\tlet parent: EntityRealmState | EntityRealmStateStub | EntityListState | undefined\n\t\t\t\tlet changesDelta = 0\n\n\t\t\t\tif (realmToDelete.blueprint.type === 'listEntity') {\n\t\t\t\t\tparent = realmToDelete.blueprint.parent\n\t\t\t\t\tparent.children.delete(entityId.value)\n\n\t\t\t\t\tif (entityId.existsOnServer) {\n\t\t\t\t\t\tif (parent.plannedRemovals === undefined) {\n\t\t\t\t\t\t\tparent.plannedRemovals = new Map()\n\t\t\t\t\t\t}\n\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t\tparent.plannedRemovals.set(entityId.value, 'delete')\n\t\t\t\t\t}\n\t\t\t\t} else if (realmToDelete.blueprint.type === 'hasOne') {\n\t\t\t\t\tparent = realmToDelete.blueprint.parent\n\t\t\t\t\tconst placeholderName = realmToDelete.blueprint.marker.placeholderName\n\n\t\t\t\t\tif (entityId.existsOnServer) {\n\t\t\t\t\t\tif (parent.plannedHasOneDeletions === undefined) {\n\t\t\t\t\t\t\tparent.plannedHasOneDeletions = new Map()\n\t\t\t\t\t\t}\n\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t\tparent.plannedHasOneDeletions.set(placeholderName, realmToDelete)\n\t\t\t\t\t}\n\t\t\t\t\tthis.stateInitializer.initializeEntityRealm(\n\t\t\t\t\t\tnew UnpersistedEntityDummyId(),\n\t\t\t\t\t\touterState.entityName,\n\t\t\t\t\t\trealmToDelete.blueprint,\n\t\t\t\t\t)\n\n\t\t\t\t\tthis.eventManager.registerUpdatedConnection(parent, placeholderName)\n\t\t\t\t} else if (realmToDelete.blueprint.type === 'subTree') {\n\t\t\t\t\tparent = undefined\n\t\t\t\t\tif (realmToDelete.type === 'entityRealm') {\n\t\t\t\t\t\tthis.eventManager.registerJustUpdated(realmToDelete, 1)\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\treturn assertNever(realmToDelete.blueprint)\n\t\t\t\t}\n\n\t\t\t\tif (parent) {\n\t\t\t\t\tif (realmToDelete.type === 'entityRealm') {\n\t\t\t\t\t\t// Undoing whatever this had caused\n\t\t\t\t\t\tchangesDelta -= realmToDelete.unpersistedChangesCount\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.eventManager.registerJustUpdated(parent, changesDelta)\n\n\t\t\t\t\tif (realmToDelete.type === 'entityRealm') {\n\t\t\t\t\t\tparent.childrenWithPendingUpdates?.delete(realmToDelete)\n\t\t\t\t\t}\n\t\t\t\t\tthis.treeStore.disposeOfRealm(realmToDelete)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n\n\tprivate *resolveHasOneRelationMarkers(\n\t\tcontainer: EntityFieldMarkersContainer,\n\t\tfield: FieldName,\n\t\ttype: 'connect' | 'disconnect',\n\t\tmustExists: boolean,\n\t): IterableIterator<HasOneRelationMarker> {\n\t\tconst placeholders = container.placeholders.get(field)\n\n\t\tif (placeholders === undefined) {\n\t\t\tif (!mustExists) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthrow new BindingError(`Cannot ${type} at field '${field}' as it wasn't registered during static render.`)\n\t\t}\n\t\tconst normalizedPlaceholders = placeholders instanceof Set ? placeholders : [placeholders]\n\n\t\tfor (const placeholderName of normalizedPlaceholders) {\n\t\t\tconst hasOneRelation = container.markers.get(placeholderName)\n\n\t\t\tif (!(hasOneRelation instanceof HasOneRelationMarker)) {\n\t\t\t\tthrow new BindingError(`Cannot ${type} at field '${field}' as it doesn't refer to a has one relation.`)\n\t\t\t}\n\t\t\tyield hasOneRelation\n\t\t}\n\t}\n}\n"],"names":[],"mappings":";;;;;AAiBO,MAAM,iBAAiB;AAAA,EACtB,YACW,sBACA,qBACA,cACA,kBACA,WAChB;AALgB,SAAA,uBAAA;AACA,SAAA,sBAAA;AACA,SAAA,eAAA;AACA,SAAA,mBAAA;AACA,SAAA,YAAA;AAAA,EACf;AAAA,EAEI,SAAS,aAA+B,OAAwC;AACtF,WAAO,KAAK,qBAAqB,SAAS,aAAa,KAAK;AAAA,EAC7D;AAAA,EAEO,iBACN,OACA,OACA,UACC;AACD,QAAI,MAAM,mBAAmB,QAAW;AACvC,YAAM,iBAAiB,IAAI,oBAAA;AAAA,IAC5B;AACA,WAAO,MAAM,eAAe,IAAI,OAAO,QAAQ;AAAA,EAChD;AAAA,EAEO,aAAa,OAAyB,gBAAoD;AAChG,SAAK,aAAa,cAAc,MAAM;AAC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A4B,OAAO,aAAa;AAAA,UACxD;AACA,eAAK,UAAU,eAAe,aAAa;AAAA,QAC5C;AAAA,MACD;AAAA,IACD,CAAC;AAAA,EACF;AAAA,EAEA,CAAS,6BACR,WACA,OACA,MACA,YACyC;AACzC,UAAM,eAAe,UAAU,aAAa,IAAI,KAAK;AAErD,QAAI,iBAAiB,QAAW;AAC/B,UAAI,CAAC,YAAY;AAChB;AAAA,MACD;AACA,YAAM,IAAI,aAAa,UAAU,IAAI,cAAc,KAAK,iDAAiD;AAAA,IAC1G;AACA,UAAM,yBAAyB,wBAAwB,MAAM,eAAe,CAAC,YAAY;AAEzF,eAAW,mBAAmB,wBAAwB;AACrD,YAAM,iBAAiB,UAAU,QAAQ,IAAI,eAAe;AAE5D,UAAI,EAAE,0BAA0B,uBAAuB;AACtD,cAAM,IAAI,aAAa,UAAU,IAAI,cAAc,KAAK,8CAA8C;AAAA,MACvG;AACA,YAAM;AAAA,IACP;AAAA,EACD;AACD;"}
@@ -1 +1 @@
1
- {"version":3,"file":"FieldOperations.cjs","sources":["../../../../../../packages/binding-legacy/src/core/operations/FieldOperations.ts"],"sourcesContent":["import { validate as uuidValidate } from 'uuid'\nimport type { FieldValue } from '@contember/binding-common'\nimport {\n\tBindingError,\n\tClientGeneratedUuid,\n\tErrorAccessor,\n\tEventListenersStore,\n\tFieldAccessor,\n\tPRIMARY_KEY_NAME,\n\tServerId,\n} from '@contember/binding-common'\nimport type { AccessorErrorManager } from '../AccessorErrorManager'\nimport { EventManager } from '../EventManager'\nimport { FieldState, getEntityMarker, StateIterator } from '../state'\nimport type { StateInitializer } from '../StateInitializer'\nimport type { TreeStore } from '../TreeStore'\nimport { OperationsHelpers } from './OperationsHelpers'\n\nexport class FieldOperations {\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic addError(fieldState: FieldState<any>, error: ErrorAccessor.Error): () => void {\n\t\treturn this.accessorErrorManager.addError(fieldState, error)\n\t}\n\n\tpublic clearErrors(fieldState: FieldState<any>): void {\n\t\treturn this.accessorErrorManager.clearErrorsByState(fieldState)\n\t}\n\n\tpublic addEventListener<\n\t\tType extends keyof FieldAccessor.FieldEventListenerMap,\n\t\tValue extends FieldValue = FieldValue,\n\t>(\n\t\tstate: FieldState<Value>,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: FieldAccessor.FieldEventListenerMap<Value>[Type],\n\t): () => void {\n\t\tif (!state.eventListeners) {\n\t\t\tstate.eventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.eventListeners.add(event, listener)\n\t}\n\n\tpublic updateValue<Value extends FieldValue = FieldValue>(\n\t\tfieldState: FieldState<Value>,\n\t\tnewValue: Value | null,\n\t\t{ agent = 'user' }: FieldAccessor.UpdateOptions = {},\n\t) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst { placeholderName, parent } = fieldState\n\t\t\tif (placeholderName === PRIMARY_KEY_NAME) {\n\t\t\t\tconst entity = fieldState.parent.entity\n\n\t\t\t\tif (import.meta.env.DEV) {\n\t\t\t\t\tif (newValue !== fieldState.value && (fieldState.touchLog !== undefined || entity.hasIdSetInStone)) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Trying to set the '${PRIMARY_KEY_NAME}' field for the second time. This is prohibited.\\n`\n\t\t\t\t\t\t\t\t+ `Once set, it is immutable.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tif (typeof newValue !== 'string' || !uuidValidate(newValue)) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Invalid value supplied for the '${PRIMARY_KEY_NAME}' field. `\n\t\t\t\t\t\t\t\t+ `Expecting a valid uuid but '${newValue}' was given.\\n`\n\t\t\t\t\t\t\t\t+ `Hint: you may use 'FieldAccessor.asUuid.setToUuid()'.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tif (this.treeStore.entityStore.has(ServerId.formatUniqueValue(newValue, entity.entityName))) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Trying to set the '${PRIMARY_KEY_NAME}' field to '${newValue}' which is a valid uuid but is not unique. `\n\t\t\t\t\t\t\t\t+ `It is already in use by an existing entity.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (typeof newValue !== 'string') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tOperationsHelpers.changeEntityId(\n\t\t\t\t\tthis.treeStore,\n\t\t\t\t\tthis.eventManager,\n\t\t\t\t\tthis.stateInitializer,\n\t\t\t\t\tentity,\n\t\t\t\t\tnew ClientGeneratedUuid(newValue),\n\t\t\t\t)\n\t\t\t}\n\t\t\tfor (const field of StateIterator.eachSiblingRealmChild(this.treeStore, fieldState)) {\n\t\t\t\tif (newValue === field.value) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tif (field.touchLog === undefined) {\n\t\t\t\t\tfield.touchLog = new Set()\n\t\t\t\t}\n\t\t\t\tfield.touchLog.add(agent)\n\t\t\t\tfield.value = newValue\n\n\t\t\t\tconst resolvedValue = field.fieldMarker.defaultValue === undefined\n\t\t\t\t\t? newValue\n\t\t\t\t\t: newValue === null\n\t\t\t\t\t? field.fieldMarker.defaultValue\n\t\t\t\t\t: newValue\n\t\t\t\tconst normalizedPersistedValue = field.persistedValue === undefined ? null : field.persistedValue\n\t\t\t\tconst hadUnpersistedChangesBefore = field.hasUnpersistedChanges\n\t\t\t\tconst hasUnpersistedChangesNow = resolvedValue !== normalizedPersistedValue\n\t\t\t\tfield.hasUnpersistedChanges = hasUnpersistedChangesNow\n\n\t\t\t\tconst shouldInfluenceUpdateCount = !getEntityMarker(parent).fields.hasAtLeastOneBearingField\n\t\t\t\t\t|| !field.fieldMarker.isNonbearing\n\t\t\t\t\t|| field.persistedValue !== undefined\n\n\t\t\t\tlet changesDelta = EventManager.NO_CHANGES_DIFFERENCE\n\n\t\t\t\tif (shouldInfluenceUpdateCount) {\n\t\t\t\t\tif (!hadUnpersistedChangesBefore && hasUnpersistedChangesNow) {\n\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t} else if (hadUnpersistedChangesBefore && !hasUnpersistedChangesNow) {\n\t\t\t\t\t\tchangesDelta--\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.registerJustUpdated(field, changesDelta)\n\t\t\t}\n\t\t})\n\t}\n}\n"],"names":["EventListenersStore","PRIMARY_KEY_NAME","BindingError","OperationsHelpers","ClientGeneratedUuid","StateIterator","getEntityMarker","EventManager"],"mappings":";;;;;;;;AAkBO,MAAM,gBAAgB;AAAA,EACrB,YACW,sBACA,cACA,kBACA,WAChB;AAJgB,SAAA,uBAAA;AACA,SAAA,eAAA;AACA,SAAA,mBAAA;AACA,SAAA,YAAA;AAAA,EACf;AAAA,EAEI,SAAS,YAA6B,OAAwC;AACpF,WAAO,KAAK,qBAAqB,SAAS,YAAY,KAAK;AAAA,EAC5D;AAAA,EAEO,YAAY,YAAmC;AACrD,WAAO,KAAK,qBAAqB,mBAAmB,UAAU;AAAA,EAC/D;AAAA,EAEO,iBAIN,OACA,OACA,UACa;AACb,QAAI,CAAC,MAAM,gBAAgB;AAC1B,YAAM,iBAAiB,IAAIA,kCAAA;AAAA,IAC5B;AACA,WAAO,MAAM,eAAe,IAAI,OAAO,QAAQ;AAAA,EAChD;AAAA,EAEO,YACN,YACA,UACA,EAAE,QAAQ,OAAA,IAAwC,IACjD;AACD,SAAK,aAAa,cAAc,MAAM;AACrC,YAAM,EAAE,iBAAiB,OAAA,IAAW;AACpC,UAAI,oBAAoBC,cAAAA,kBAAkB;AACzC,cAAM,SAAS,WAAW,OAAO;AAuBjC,YAAI,OAAO,aAAa,UAAU;AACjC,gBAAM,IAAIC,cAAAA,aAAA;AAAA,QACX;AACAC,0BAAAA,kBAAkB;AAAA,UACjB,KAAK;AAAA,UACL,KAAK;AAAA,UACL,KAAK;AAAA,UACL;AAAA,UACA,IAAIC,cAAAA,oBAAoB,QAAQ;AAAA,QAAA;AAAA,MAElC;AACA,iBAAW,SAASC,4BAAc,sBAAsB,KAAK,WAAW,UAAU,GAAG;AACpF,YAAI,aAAa,MAAM,OAAO;AAC7B;AAAA,QACD;AACA,YAAI,MAAM,aAAa,QAAW;AACjC,gBAAM,+BAAe,IAAA;AAAA,QACtB;AACA,cAAM,SAAS,IAAI,KAAK;AACxB,cAAM,QAAQ;AAEd,cAAM,gBAAgB,MAAM,YAAY,iBAAiB,SACtD,WACA,aAAa,OACb,MAAM,YAAY,eAClB;AACH,cAAM,2BAA2B,MAAM,mBAAmB,SAAY,OAAO,MAAM;AACnF,cAAM,8BAA8B,MAAM;AAC1C,cAAM,2BAA2B,kBAAkB;AACnD,cAAM,wBAAwB;AAE9B,cAAM,6BAA6B,CAACC,gBAAAA,gBAAgB,MAAM,EAAE,OAAO,6BAC/D,CAAC,MAAM,YAAY,gBACnB,MAAM,mBAAmB;AAE7B,YAAI,eAAeC,aAAAA,aAAa;AAEhC,YAAI,4BAA4B;AAC/B,cAAI,CAAC,+BAA+B,0BAA0B;AAC7D;AAAA,UACD,WAAW,+BAA+B,CAAC,0BAA0B;AACpE;AAAA,UACD;AAAA,QACD;AAEA,aAAK,aAAa,oBAAoB,OAAO,YAAY;AAAA,MAC1D;AAAA,IACD,CAAC;AAAA,EACF;AACD;;"}
1
+ {"version":3,"file":"FieldOperations.cjs","sources":["../../../../../../packages/binding-legacy/src/core/operations/FieldOperations.ts"],"sourcesContent":["import { validate as uuidValidate } from 'uuid'\nimport type { FieldValue } from '@contember/binding-common'\nimport {\n\tBindingError,\n\tClientGeneratedUuid,\n\tErrorAccessor,\n\tEventListenersStore,\n\tFieldAccessor,\n\tPRIMARY_KEY_NAME,\n\tServerId,\n} from '@contember/binding-common'\nimport type { AccessorErrorManager } from '../AccessorErrorManager.js'\nimport { EventManager } from '../EventManager.js'\nimport { FieldState, getEntityMarker, StateIterator } from '../state/index.js'\nimport type { StateInitializer } from '../StateInitializer.js'\nimport type { TreeStore } from '../TreeStore.js'\nimport { OperationsHelpers } from './OperationsHelpers.js'\n\nexport class FieldOperations {\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic addError(fieldState: FieldState<any>, error: ErrorAccessor.Error): () => void {\n\t\treturn this.accessorErrorManager.addError(fieldState, error)\n\t}\n\n\tpublic clearErrors(fieldState: FieldState<any>): void {\n\t\treturn this.accessorErrorManager.clearErrorsByState(fieldState)\n\t}\n\n\tpublic addEventListener<\n\t\tType extends keyof FieldAccessor.FieldEventListenerMap,\n\t\tValue extends FieldValue = FieldValue,\n\t>(\n\t\tstate: FieldState<Value>,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: FieldAccessor.FieldEventListenerMap<Value>[Type],\n\t): () => void {\n\t\tif (!state.eventListeners) {\n\t\t\tstate.eventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.eventListeners.add(event, listener)\n\t}\n\n\tpublic updateValue<Value extends FieldValue = FieldValue>(\n\t\tfieldState: FieldState<Value>,\n\t\tnewValue: Value | null,\n\t\t{ agent = 'user' }: FieldAccessor.UpdateOptions = {},\n\t) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst { placeholderName, parent } = fieldState\n\t\t\tif (placeholderName === PRIMARY_KEY_NAME) {\n\t\t\t\tconst entity = fieldState.parent.entity\n\n\t\t\t\tif (import.meta.env.DEV) {\n\t\t\t\t\tif (newValue !== fieldState.value && (fieldState.touchLog !== undefined || entity.hasIdSetInStone)) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Trying to set the '${PRIMARY_KEY_NAME}' field for the second time. This is prohibited.\\n`\n\t\t\t\t\t\t\t\t+ `Once set, it is immutable.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tif (typeof newValue !== 'string' || !uuidValidate(newValue)) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Invalid value supplied for the '${PRIMARY_KEY_NAME}' field. `\n\t\t\t\t\t\t\t\t+ `Expecting a valid uuid but '${newValue}' was given.\\n`\n\t\t\t\t\t\t\t\t+ `Hint: you may use 'FieldAccessor.asUuid.setToUuid()'.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tif (this.treeStore.entityStore.has(ServerId.formatUniqueValue(newValue, entity.entityName))) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Trying to set the '${PRIMARY_KEY_NAME}' field to '${newValue}' which is a valid uuid but is not unique. `\n\t\t\t\t\t\t\t\t+ `It is already in use by an existing entity.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (typeof newValue !== 'string') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tOperationsHelpers.changeEntityId(\n\t\t\t\t\tthis.treeStore,\n\t\t\t\t\tthis.eventManager,\n\t\t\t\t\tthis.stateInitializer,\n\t\t\t\t\tentity,\n\t\t\t\t\tnew ClientGeneratedUuid(newValue),\n\t\t\t\t)\n\t\t\t}\n\t\t\tfor (const field of StateIterator.eachSiblingRealmChild(this.treeStore, fieldState)) {\n\t\t\t\tif (newValue === field.value) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tif (field.touchLog === undefined) {\n\t\t\t\t\tfield.touchLog = new Set()\n\t\t\t\t}\n\t\t\t\tfield.touchLog.add(agent)\n\t\t\t\tfield.value = newValue\n\n\t\t\t\tconst resolvedValue = field.fieldMarker.defaultValue === undefined\n\t\t\t\t\t? newValue\n\t\t\t\t\t: newValue === null\n\t\t\t\t\t? field.fieldMarker.defaultValue\n\t\t\t\t\t: newValue\n\t\t\t\tconst normalizedPersistedValue = field.persistedValue === undefined ? null : field.persistedValue\n\t\t\t\tconst hadUnpersistedChangesBefore = field.hasUnpersistedChanges\n\t\t\t\tconst hasUnpersistedChangesNow = resolvedValue !== normalizedPersistedValue\n\t\t\t\tfield.hasUnpersistedChanges = hasUnpersistedChangesNow\n\n\t\t\t\tconst shouldInfluenceUpdateCount = !getEntityMarker(parent).fields.hasAtLeastOneBearingField\n\t\t\t\t\t|| !field.fieldMarker.isNonbearing\n\t\t\t\t\t|| field.persistedValue !== undefined\n\n\t\t\t\tlet changesDelta = EventManager.NO_CHANGES_DIFFERENCE\n\n\t\t\t\tif (shouldInfluenceUpdateCount) {\n\t\t\t\t\tif (!hadUnpersistedChangesBefore && hasUnpersistedChangesNow) {\n\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t} else if (hadUnpersistedChangesBefore && !hasUnpersistedChangesNow) {\n\t\t\t\t\t\tchangesDelta--\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.registerJustUpdated(field, changesDelta)\n\t\t\t}\n\t\t})\n\t}\n}\n"],"names":["EventListenersStore","PRIMARY_KEY_NAME","BindingError","OperationsHelpers","ClientGeneratedUuid","StateIterator","getEntityMarker","EventManager"],"mappings":";;;;;;;;AAkBO,MAAM,gBAAgB;AAAA,EACrB,YACW,sBACA,cACA,kBACA,WAChB;AAJgB,SAAA,uBAAA;AACA,SAAA,eAAA;AACA,SAAA,mBAAA;AACA,SAAA,YAAA;AAAA,EACf;AAAA,EAEI,SAAS,YAA6B,OAAwC;AACpF,WAAO,KAAK,qBAAqB,SAAS,YAAY,KAAK;AAAA,EAC5D;AAAA,EAEO,YAAY,YAAmC;AACrD,WAAO,KAAK,qBAAqB,mBAAmB,UAAU;AAAA,EAC/D;AAAA,EAEO,iBAIN,OACA,OACA,UACa;AACb,QAAI,CAAC,MAAM,gBAAgB;AAC1B,YAAM,iBAAiB,IAAIA,k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O,YAAY;AAAA,MAC1D;AAAA,IACD,CAAC;AAAA,EACF;AACD;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"FieldOperations.js","sources":["../../../../../../packages/binding-legacy/src/core/operations/FieldOperations.ts"],"sourcesContent":["import { validate as uuidValidate } from 'uuid'\nimport type { FieldValue } from '@contember/binding-common'\nimport {\n\tBindingError,\n\tClientGeneratedUuid,\n\tErrorAccessor,\n\tEventListenersStore,\n\tFieldAccessor,\n\tPRIMARY_KEY_NAME,\n\tServerId,\n} from '@contember/binding-common'\nimport type { AccessorErrorManager } from '../AccessorErrorManager'\nimport { EventManager } from '../EventManager'\nimport { FieldState, getEntityMarker, StateIterator } from '../state'\nimport type { StateInitializer } from '../StateInitializer'\nimport type { TreeStore } from '../TreeStore'\nimport { OperationsHelpers } from './OperationsHelpers'\n\nexport class FieldOperations {\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic addError(fieldState: FieldState<any>, error: ErrorAccessor.Error): () => void {\n\t\treturn this.accessorErrorManager.addError(fieldState, error)\n\t}\n\n\tpublic clearErrors(fieldState: FieldState<any>): void {\n\t\treturn this.accessorErrorManager.clearErrorsByState(fieldState)\n\t}\n\n\tpublic addEventListener<\n\t\tType extends keyof FieldAccessor.FieldEventListenerMap,\n\t\tValue extends FieldValue = FieldValue,\n\t>(\n\t\tstate: FieldState<Value>,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: FieldAccessor.FieldEventListenerMap<Value>[Type],\n\t): () => void {\n\t\tif (!state.eventListeners) {\n\t\t\tstate.eventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.eventListeners.add(event, listener)\n\t}\n\n\tpublic updateValue<Value extends FieldValue = FieldValue>(\n\t\tfieldState: FieldState<Value>,\n\t\tnewValue: Value | null,\n\t\t{ agent = 'user' }: FieldAccessor.UpdateOptions = {},\n\t) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst { placeholderName, parent } = fieldState\n\t\t\tif (placeholderName === PRIMARY_KEY_NAME) {\n\t\t\t\tconst entity = fieldState.parent.entity\n\n\t\t\t\tif (import.meta.env.DEV) {\n\t\t\t\t\tif (newValue !== fieldState.value && (fieldState.touchLog !== undefined || entity.hasIdSetInStone)) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Trying to set the '${PRIMARY_KEY_NAME}' field for the second time. This is prohibited.\\n`\n\t\t\t\t\t\t\t\t+ `Once set, it is immutable.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tif (typeof newValue !== 'string' || !uuidValidate(newValue)) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Invalid value supplied for the '${PRIMARY_KEY_NAME}' field. `\n\t\t\t\t\t\t\t\t+ `Expecting a valid uuid but '${newValue}' was given.\\n`\n\t\t\t\t\t\t\t\t+ `Hint: you may use 'FieldAccessor.asUuid.setToUuid()'.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tif (this.treeStore.entityStore.has(ServerId.formatUniqueValue(newValue, entity.entityName))) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Trying to set the '${PRIMARY_KEY_NAME}' field to '${newValue}' which is a valid uuid but is not unique. `\n\t\t\t\t\t\t\t\t+ `It is already in use by an existing entity.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (typeof newValue !== 'string') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tOperationsHelpers.changeEntityId(\n\t\t\t\t\tthis.treeStore,\n\t\t\t\t\tthis.eventManager,\n\t\t\t\t\tthis.stateInitializer,\n\t\t\t\t\tentity,\n\t\t\t\t\tnew ClientGeneratedUuid(newValue),\n\t\t\t\t)\n\t\t\t}\n\t\t\tfor (const field of StateIterator.eachSiblingRealmChild(this.treeStore, fieldState)) {\n\t\t\t\tif (newValue === field.value) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tif (field.touchLog === undefined) {\n\t\t\t\t\tfield.touchLog = new Set()\n\t\t\t\t}\n\t\t\t\tfield.touchLog.add(agent)\n\t\t\t\tfield.value = newValue\n\n\t\t\t\tconst resolvedValue = field.fieldMarker.defaultValue === undefined\n\t\t\t\t\t? newValue\n\t\t\t\t\t: newValue === null\n\t\t\t\t\t? field.fieldMarker.defaultValue\n\t\t\t\t\t: newValue\n\t\t\t\tconst normalizedPersistedValue = field.persistedValue === undefined ? null : field.persistedValue\n\t\t\t\tconst hadUnpersistedChangesBefore = field.hasUnpersistedChanges\n\t\t\t\tconst hasUnpersistedChangesNow = resolvedValue !== normalizedPersistedValue\n\t\t\t\tfield.hasUnpersistedChanges = hasUnpersistedChangesNow\n\n\t\t\t\tconst shouldInfluenceUpdateCount = !getEntityMarker(parent).fields.hasAtLeastOneBearingField\n\t\t\t\t\t|| !field.fieldMarker.isNonbearing\n\t\t\t\t\t|| field.persistedValue !== undefined\n\n\t\t\t\tlet changesDelta = EventManager.NO_CHANGES_DIFFERENCE\n\n\t\t\t\tif (shouldInfluenceUpdateCount) {\n\t\t\t\t\tif (!hadUnpersistedChangesBefore && hasUnpersistedChangesNow) {\n\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t} else if (hadUnpersistedChangesBefore && !hasUnpersistedChangesNow) {\n\t\t\t\t\t\tchangesDelta--\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.registerJustUpdated(field, changesDelta)\n\t\t\t}\n\t\t})\n\t}\n}\n"],"names":[],"mappings":";;;;;;AAkBO,MAAM,gBAAgB;AAAA,EACrB,YACW,sBACA,cACA,kBACA,WAChB;AAJgB,SAAA,uBAAA;AACA,SAAA,eAAA;AACA,SAAA,mBAAA;AACA,SAAA,YAAA;AAAA,EACf;AAAA,EAEI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AM,mBAAmB;AAE7B,YAAI,eAAe,aAAa;AAEhC,YAAI,4BAA4B;AAC/B,cAAI,CAAC,+BAA+B,0BAA0B;AAC7D;AAAA,UACD,WAAW,+BAA+B,CAAC,0BAA0B;AACpE;AAAA,UACD;AAAA,QACD;AAEA,aAAK,aAAa,oBAAoB,OAAO,YAAY;AAAA,MAC1D;AAAA,IACD,CAAC;AAAA,EACF;AACD;"}
1
+ {"version":3,"file":"FieldOperations.js","sources":["../../../../../../packages/binding-legacy/src/core/operations/FieldOperations.ts"],"sourcesContent":["import { validate as uuidValidate } from 'uuid'\nimport type { FieldValue } from '@contember/binding-common'\nimport {\n\tBindingError,\n\tClientGeneratedUuid,\n\tErrorAccessor,\n\tEventListenersStore,\n\tFieldAccessor,\n\tPRIMARY_KEY_NAME,\n\tServerId,\n} from '@contember/binding-common'\nimport type { AccessorErrorManager } from '../AccessorErrorManager.js'\nimport { EventManager } from '../EventManager.js'\nimport { FieldState, getEntityMarker, StateIterator } from '../state/index.js'\nimport type { StateInitializer } from '../StateInitializer.js'\nimport type { TreeStore } from '../TreeStore.js'\nimport { OperationsHelpers } from './OperationsHelpers.js'\n\nexport class FieldOperations {\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic addError(fieldState: FieldState<any>, error: ErrorAccessor.Error): () => void {\n\t\treturn this.accessorErrorManager.addError(fieldState, error)\n\t}\n\n\tpublic clearErrors(fieldState: FieldState<any>): void {\n\t\treturn this.accessorErrorManager.clearErrorsByState(fieldState)\n\t}\n\n\tpublic addEventListener<\n\t\tType extends keyof FieldAccessor.FieldEventListenerMap,\n\t\tValue extends FieldValue = FieldValue,\n\t>(\n\t\tstate: FieldState<Value>,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: FieldAccessor.FieldEventListenerMap<Value>[Type],\n\t): () => void {\n\t\tif (!state.eventListeners) {\n\t\t\tstate.eventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.eventListeners.add(event, listener)\n\t}\n\n\tpublic updateValue<Value extends FieldValue = FieldValue>(\n\t\tfieldState: FieldState<Value>,\n\t\tnewValue: Value | null,\n\t\t{ agent = 'user' }: FieldAccessor.UpdateOptions = {},\n\t) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst { placeholderName, parent } = fieldState\n\t\t\tif (placeholderName === PRIMARY_KEY_NAME) {\n\t\t\t\tconst entity = fieldState.parent.entity\n\n\t\t\t\tif (import.meta.env.DEV) {\n\t\t\t\t\tif (newValue !== fieldState.value && (fieldState.touchLog !== undefined || entity.hasIdSetInStone)) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Trying to set the '${PRIMARY_KEY_NAME}' field for the second time. This is prohibited.\\n`\n\t\t\t\t\t\t\t\t+ `Once set, it is immutable.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tif (typeof newValue !== 'string' || !uuidValidate(newValue)) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Invalid value supplied for the '${PRIMARY_KEY_NAME}' field. `\n\t\t\t\t\t\t\t\t+ `Expecting a valid uuid but '${newValue}' was given.\\n`\n\t\t\t\t\t\t\t\t+ `Hint: you may use 'FieldAccessor.asUuid.setToUuid()'.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tif (this.treeStore.entityStore.has(ServerId.formatUniqueValue(newValue, entity.entityName))) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Trying to set the '${PRIMARY_KEY_NAME}' field to '${newValue}' which is a valid uuid but is not unique. `\n\t\t\t\t\t\t\t\t+ `It is already in use by an existing entity.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (typeof newValue !== 'string') {\n\t\t\t\t\tthrow new BindingError()\n\t\t\t\t}\n\t\t\t\tOperationsHelpers.changeEntityId(\n\t\t\t\t\tthis.treeStore,\n\t\t\t\t\tthis.eventManager,\n\t\t\t\t\tthis.stateInitializer,\n\t\t\t\t\tentity,\n\t\t\t\t\tnew ClientGeneratedUuid(newValue),\n\t\t\t\t)\n\t\t\t}\n\t\t\tfor (const field of StateIterator.eachSiblingRealmChild(this.treeStore, fieldState)) {\n\t\t\t\tif (newValue === field.value) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tif (field.touchLog === undefined) {\n\t\t\t\t\tfield.touchLog = new Set()\n\t\t\t\t}\n\t\t\t\tfield.touchLog.add(agent)\n\t\t\t\tfield.value = newValue\n\n\t\t\t\tconst resolvedValue = field.fieldMarker.defaultValue === undefined\n\t\t\t\t\t? newValue\n\t\t\t\t\t: newValue === null\n\t\t\t\t\t? field.fieldMarker.defaultValue\n\t\t\t\t\t: newValue\n\t\t\t\tconst normalizedPersistedValue = field.persistedValue === undefined ? null : field.persistedValue\n\t\t\t\tconst hadUnpersistedChangesBefore = field.hasUnpersistedChanges\n\t\t\t\tconst hasUnpersistedChangesNow = resolvedValue !== normalizedPersistedValue\n\t\t\t\tfield.hasUnpersistedChanges = hasUnpersistedChangesNow\n\n\t\t\t\tconst shouldInfluenceUpdateCount = !getEntityMarker(parent).fields.hasAtLeastOneBearingField\n\t\t\t\t\t|| !field.fieldMarker.isNonbearing\n\t\t\t\t\t|| field.persistedValue !== undefined\n\n\t\t\t\tlet changesDelta = EventManager.NO_CHANGES_DIFFERENCE\n\n\t\t\t\tif (shouldInfluenceUpdateCount) {\n\t\t\t\t\tif (!hadUnpersistedChangesBefore && hasUnpersistedChangesNow) {\n\t\t\t\t\t\tchangesDelta++\n\t\t\t\t\t} else if (hadUnpersistedChangesBefore && !hasUnpersistedChangesNow) {\n\t\t\t\t\t\tchangesDelta--\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.registerJustUpdated(field, changesDelta)\n\t\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B,MAAM,mBAAmB,SAAY,OAAO,MAAM;AACnF,cAAM,8BAA8B,MAAM;AAC1C,cAAM,2BAA2B,kBAAkB;AACnD,cAAM,wBAAwB;AAE9B,cAAM,6BAA6B,CAAC,gBAAgB,MAAM,EAAE,OAAO,6BAC/D,CAAC,MAAM,YAAY,gBACnB,MAAM,mBAAmB;AAE7B,YAAI,eAAe,aAAa;AAEhC,YAAI,4BAA4B;AAC/B,cAAI,CAAC,+BAA+B,0BAA0B;AAC7D;AAAA,UACD,WAAW,+BAA+B,CAAC,0BAA0B;AACpE;AAAA,UACD;AAAA,QACD;AAEA,aAAK,aAAa,oBAAoB,OAAO,YAAY;AAAA,MAC1D;AAAA,IACD,CAAC;AAAA,EACF;AACD;"}
@@ -1 +1 @@
1
- {"version":3,"file":"ListOperations.cjs","sources":["../../../../../../packages/binding-legacy/src/core/operations/ListOperations.ts"],"sourcesContent":["import { validate as uuidValidate } from 'uuid'\nimport type { BatchUpdatesOptions, EntityAccessor, EntityListAccessor, ErrorAccessor } from '@contember/binding-common'\nimport {\n\tBindingError,\n\tEntityId,\n\tEventListenersStore,\n\tLocalizedBindingError,\n\tMarkerComparator,\n\tRuntimeId,\n\tUnpersistedEntityDummyId,\n} from '@contember/binding-common'\nimport type { AccessorErrorManager } from '../AccessorErrorManager'\nimport { EventManager } from '../EventManager'\nimport { RealmKeyGenerator } from '../RealmKeyGenerator'\nimport { EntityListState, getEntityMarker, StateIterator } from '../state'\nimport type { StateInitializer } from '../StateInitializer'\nimport type { TreeStore } from '../TreeStore'\nimport { OperationsHelpers } from './OperationsHelpers'\nimport { ErrorLocator } from '../exceptions/ErrorLocator'\n\nexport class ListOperations {\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly batchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic addError(listState: EntityListState, error: ErrorAccessor.Error): () => void {\n\t\treturn this.accessorErrorManager.addError(listState, error)\n\t}\n\n\tpublic addEventListener<Type extends keyof EntityListAccessor.RuntimeEntityListEventListenerMap>(\n\t\tstate: EntityListState,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityListAccessor.EntityListEventListenerMap[Type],\n\t) {\n\t\tif (state.eventListeners === undefined) {\n\t\t\tstate.eventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.eventListeners.add(event, listener)\n\t}\n\n\tpublic addChildEventListener<Type extends keyof EntityAccessor.EntityEventListenerMap>(\n\t\tstate: EntityListState,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityAccessor.EntityEventListenerMap[Type],\n\t) {\n\t\tif (state.childEventListeners === undefined) {\n\t\t\tstate.childEventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.childEventListeners.add(event, listener)\n\t}\n\n\tpublic batchUpdates(state: EntityListState, performUpdates: EntityListAccessor.BatchUpdatesHandler) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tperformUpdates(state.getAccessor, this.batchUpdatesOptions)\n\t\t})\n\t}\n\n\tpublic connectEntity(outerState: EntityListState, entityToConnect: EntityAccessor) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(outerState)\n\t\t\tconst stateToConnect = OperationsHelpers.resolveAndPrepareEntityToConnect(this.treeStore, entityToConnect)\n\t\t\tconst idToConnect = stateToConnect.entity.id\n\n\t\t\t// TODO disable this at the top-level.\n\t\t\tfor (const state of StateIterator.eachSiblingRealmChild(this.treeStore, outerState)) {\n\t\t\t\tif (state.entityName !== stateToConnect.entity.entityName) {\n\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t`EntityListAccessor.connectEntity: Attempting to connect at an entity of type `\n\t\t\t\t\t\t\t+ `'${stateToConnect.entity.entityName}' but '${state.entityName}' is expected.\\n\\n`\n\t\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tMarkerComparator.assertEntityMarkersSubsetOf(\n\t\t\t\t\t\tstate.blueprint.marker.fields,\n\t\t\t\t\t\tgetEntityMarker(stateToConnect).fields,\n\t\t\t\t\t)\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (error instanceof LocalizedBindingError) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Entity list: cannot connect entity with key '${entityToConnect.key}' because its fields are `\n\t\t\t\t\t\t\t\t+ `incompatible with entities found on this list. Make sure both trees are equivalent.\\n\\n`\n\t\t\t\t\t\t\t\t+ `${error.message}\\n\\n`\n\t\t\t\t\t\t\t\t+ (error.markerPath.length > 1\n\t\t\t\t\t\t\t\t\t? `Incompatibility found at: ${ErrorLocator.locateMarkerPath(error.markerPath)}.\\n\\n`\n\t\t\t\t\t\t\t\t\t: '')\n\t\t\t\t\t\t\t\t+ `Entity list located at: ${ErrorLocator.locateInternalState(state)}.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tthrow error\n\t\t\t\t}\n\n\t\t\t\tif (state.children.has(idToConnect.value)) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tthis.stateInitializer.initializeEntityRealm(idToConnect, state.entityName, {\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: state,\n\t\t\t\t}, stateToConnect)\n\n\t\t\t\tstate.plannedRemovals?.delete(idToConnect.value)\n\n\t\t\t\tlet changesDelta = 0\n\n\t\t\t\tif (persistedEntityIds.has(idToConnect.value)) {\n\t\t\t\t\t// It was removed from the list but now we're adding it back.\n\t\t\t\t\tchangesDelta--\n\t\t\t\t} else if (idToConnect.existsOnServer) {\n\t\t\t\t\t// ignoring unpersisted entities\n\t\t\t\t\tchangesDelta++\n\t\t\t\t}\n\t\t\t\tif (stateToConnect.type === 'entityRealm') {\n\t\t\t\t\tchangesDelta += stateToConnect.unpersistedChangesCount\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic disconnectEntity(listState: EntityListState, childEntity: EntityAccessor, options: { noPersist?: boolean }) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\t// TODO disable this at the top-level.\n\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(listState)\n\n\t\t\tif (!listState.children.has(childEntity.id)) {\n\t\t\t\tthrow new BindingError(\n\t\t\t\t\t`Entity list doesn't include an entity with id '${childEntity.id}' and so it cannot remove it.`,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealmChild(this.treeStore, listState)) {\n\t\t\t\tconst disconnectedChildIdValue = childEntity.id\n\t\t\t\tconst disconnectedChildRealm = state.children.get(disconnectedChildIdValue)\n\n\t\t\t\tif (disconnectedChildRealm === undefined) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tthis.treeStore.disposeOfRealm(disconnectedChildRealm)\n\t\t\t\tstate.children.delete(disconnectedChildIdValue)\n\n\t\t\t\tlet changesDelta = 0\n\n\t\t\t\tif (persistedEntityIds.has(disconnectedChildIdValue)) {\n\t\t\t\t\tchangesDelta++\n\n\t\t\t\t\tif (state.plannedRemovals === undefined) {\n\t\t\t\t\t\tstate.plannedRemovals = new Map()\n\t\t\t\t\t}\n\t\t\t\t\tif (!options.noPersist) {\n\t\t\t\t\t\tstate.plannedRemovals.set(disconnectedChildIdValue, 'disconnect')\n\t\t\t\t\t}\n\t\t\t\t} else if (disconnectedChildRealm.entity.id.existsOnServer) {\n\t\t\t\t\t// Disconnecting unpersisted entities doesn't constitute a change.\n\n\t\t\t\t\t// It wasn't on the list, then it was, and now we're removing it again.\n\t\t\t\t\tchangesDelta--\n\t\t\t\t}\n\n\t\t\t\tif (disconnectedChildRealm.type === 'entityRealm') {\n\t\t\t\t\t// Undoing whatever changes this child had caused.\n\t\t\t\t\tchangesDelta -= disconnectedChildRealm.unpersistedChangesCount\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic createNewEntity(outerState: EntityListState, initialize: EntityAccessor.BatchUpdatesHandler | undefined): RuntimeId {\n\t\treturn this.eventManager.syncOperation((): RuntimeId => {\n\t\t\t// All siblings need to have the same id.\n\t\t\tconst id = new UnpersistedEntityDummyId()\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealmChild(this.treeStore, outerState)) {\n\t\t\t\tconst newEntity = this.stateInitializer.initializeEntityRealm(id, state.entityName, {\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: state,\n\t\t\t\t})\n\n\t\t\t\tthis.stateInitializer.runImmediateUserInitialization(newEntity, initialize)\n\n\t\t\t\t// The act of creating the entity doesn't constitute a change unless there's something inside to persist.\n\t\t\t\t// TODO forceCreate\n\t\t\t\t// TODO defaultValue\n\t\t\t\tthis.eventManager.registerJustUpdated(state, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t}\n\t\t\treturn id\n\t\t})\n\t}\n\n\tpublic getChildEntityById(state: EntityListState, id: EntityId) {\n\t\tconst realm = state.children.get(id)\n\t\tif (realm === undefined) {\n\t\t\tconst isRuntimeId = typeof id === 'number' || uuidValidate(id) || UnpersistedEntityDummyId.matchesDummyId(id)\n\t\t\tif (isRuntimeId) {\n\t\t\t\tthrow new BindingError(`EntityList: cannot retrieve an entity with id '${id}' as it is not on the list.`)\n\t\t\t}\n\t\t\tconst looksLikeKey = typeof id === 'string' && (this.treeStore.entityRealmStore.has(id) || RealmKeyGenerator.vaguelyAppearsToBeAKey(id))\n\t\t\tthrow new BindingError(\n\t\t\t\t`EntityList: cannot retrieve an entity with id '${id}' because it's not a valid id.`\n\t\t\t\t\t+ (looksLikeKey\n\t\t\t\t\t\t? `\\nThe supplied value appears to be an entity *key*, not an *id*. List accessors are indexed by ids `\n\t\t\t\t\t\t\t+ `and you can use \\`EntityAccessor.id\\` to obtain one. Keys, on the other hand, are globally unique and `\n\t\t\t\t\t\t\t+ `you can use \\`getEntityByKey\\` to get its EntityAccessor.`\n\t\t\t\t\t\t: ''),\n\t\t\t)\n\t\t}\n\t\treturn realm.getAccessor()\n\t}\n}\n"],"names":["EventListenersStore","OperationsHelpers","StateIterator","BindingError","ErrorLocator","MarkerComparator","getEntityMarker","LocalizedBindingError","UnpersistedEntityDummyId","EventManager","uuidValidate","RealmKeyGenerator"],"mappings":";;;;;;;;;;AAoBO,MAAM,eAAe;AAAA,EACpB,YACW,sBACA,qBACA,cACA,kBACA,WAChB;AALgB,SAAA,uBAAA;AACA,SAAA,sBAAA;AACA,SAAA,eAAA;AACA,SAAA,mBAAA;AACA,SAAA,YAAA;AAAA,EACf;AAAA,EAEI,SAAS,WAA4B,OAAwC;AACnF,WAAO,KAAK,qBAAqB,SAAS,WAAW,KAAK;AAAA,EAC3D;AAAA,EAEO,iBACN,OACA,OACA,UACC;AACD,QAAI,MAAM,mBAAmB,QAAW;AACvC,YAAM,iBAAiB,IAAIA,kCAAA;AAAA,IAC5B;AACA,WAAO,MAAM,eAAe,IAAI,OAAO,QAAQ;AAAA,EAChD;AAAA,EAEO,sBACN,OACA,OACA,UACC;AACD,QAAI,MAAM,wBAAwB,QAAW;AAC5C,YAAM,sBAAsB,IAAIA,kCAAA;AAAA,IACjC;AACA,WAAO,MAAM,oBAAoB,IAAI,OAAO,QAAQ;AAAA,EACrD;AAAA,EAEO,aAAa,OAAwB,gBAAwD;AACnG,SAAK,aAAa,cAAc,MAAM;AACrC,qBAAe,MAAM,aAAa,KAAK,mBAAmB;AAAA,IAC3D,CAAC;AAAA,EACF;AAAA,EAEO,cAAc,YAA6B,iBAAiC;AAClF,SAAK,aAAa,cAAc,MAAM;AACrC,YAAM,qBAAqB,KAAK,UAAU,0BAA0B,UAAU;AAC9E,YAAM,iBAAiBC,kBAA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;AAAA,UACN,QAAQ;AAAA,QAAA,CACR;AAED,aAAK,iBAAiB,+BAA+B,WAAW,UAAU;AAK1E,aAAK,aAAa,oBAAoB,OAAOO,aAAAA,aAAa,qBAAqB;AAAA,MAChF;AACA,aAAO;AAAA,IACR,CAAC;AAAA,EACF;AAAA,EAEO,mBAAmB,OAAwB,IAAc;AAC/D,UAAM,QAAQ,MAAM,SAAS,IAAI,EAAE;AACnC,QAAI,UAAU,QAAW;AACxB,YAAM,cAAc,OAAO,OAAO,YAAYC,KAAAA,SAAa,EAAE,KAAKF,cAAAA,yBAAyB,eAAe,EAAE;AAC5G,UAAI,aAAa;AAChB,cAAM,IAAIL,cAAAA,aAAa,kDAAkD,EAAE,6BAA6B;AAAA,MACzG;AACA,YAAM,eAAe,OAAO,OAAO,aAAa,KAAK,UAAU,iBAAiB,IAAI,EAAE,KAAKQ,kBAAAA,kBAAkB,uBAAuB,EAAE;AACtI,YAAM,IAAIR,cAAAA;AAAAA,QACT,kDAAkD,EAAE,oCAChD,eACA;AAAA,oQAGA;AAAA,MAAA;AAAA,IAEN;AACA,WAAO,MAAM,YAAA;AAAA,EACd;AACD;;"}
1
+ {"version":3,"file":"ListOperations.cjs","sources":["../../../../../../packages/binding-legacy/src/core/operations/ListOperations.ts"],"sourcesContent":["import { validate as uuidValidate } from 'uuid'\nimport type { BatchUpdatesOptions, EntityAccessor, EntityListAccessor, ErrorAccessor } from '@contember/binding-common'\nimport {\n\tBindingError,\n\tEntityId,\n\tEventListenersStore,\n\tLocalizedBindingError,\n\tMarkerComparator,\n\tRuntimeId,\n\tUnpersistedEntityDummyId,\n} from '@contember/binding-common'\nimport type { AccessorErrorManager } from '../AccessorErrorManager.js'\nimport { EventManager } from '../EventManager.js'\nimport { RealmKeyGenerator } from '../RealmKeyGenerator.js'\nimport { EntityListState, getEntityMarker, StateIterator } from '../state/index.js'\nimport type { StateInitializer } from '../StateInitializer.js'\nimport type { TreeStore } from '../TreeStore.js'\nimport { OperationsHelpers } from './OperationsHelpers.js'\nimport { ErrorLocator } from '../exceptions/ErrorLocator.js'\n\nexport class ListOperations {\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly batchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic addError(listState: EntityListState, error: ErrorAccessor.Error): () => void {\n\t\treturn this.accessorErrorManager.addError(listState, error)\n\t}\n\n\tpublic addEventListener<Type extends keyof EntityListAccessor.RuntimeEntityListEventListenerMap>(\n\t\tstate: EntityListState,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityListAccessor.EntityListEventListenerMap[Type],\n\t) {\n\t\tif (state.eventListeners === undefined) {\n\t\t\tstate.eventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.eventListeners.add(event, listener)\n\t}\n\n\tpublic addChildEventListener<Type extends keyof EntityAccessor.EntityEventListenerMap>(\n\t\tstate: EntityListState,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityAccessor.EntityEventListenerMap[Type],\n\t) {\n\t\tif (state.childEventListeners === undefined) {\n\t\t\tstate.childEventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.childEventListeners.add(event, listener)\n\t}\n\n\tpublic batchUpdates(state: EntityListState, performUpdates: EntityListAccessor.BatchUpdatesHandler) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tperformUpdates(state.getAccessor, this.batchUpdatesOptions)\n\t\t})\n\t}\n\n\tpublic connectEntity(outerState: EntityListState, entityToConnect: EntityAccessor) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(outerState)\n\t\t\tconst stateToConnect = OperationsHelpers.resolveAndPrepareEntityToConnect(this.treeStore, entityToConnect)\n\t\t\tconst idToConnect = stateToConnect.entity.id\n\n\t\t\t// TODO disable this at the top-level.\n\t\t\tfor (const state of StateIterator.eachSiblingRealmChild(this.treeStore, outerState)) {\n\t\t\t\tif (state.entityName !== stateToConnect.entity.entityName) {\n\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t`EntityListAccessor.connectEntity: Attempting to connect at an entity of type `\n\t\t\t\t\t\t\t+ `'${stateToConnect.entity.entityName}' but '${state.entityName}' is expected.\\n\\n`\n\t\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tMarkerComparator.assertEntityMarkersSubsetOf(\n\t\t\t\t\t\tstate.blueprint.marker.fields,\n\t\t\t\t\t\tgetEntityMarker(stateToConnect).fields,\n\t\t\t\t\t)\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (error instanceof LocalizedBindingError) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Entity list: cannot connect entity with key '${entityToConnect.key}' because its fields are `\n\t\t\t\t\t\t\t\t+ `incompatible with entities found on this list. Make sure both trees are equivalent.\\n\\n`\n\t\t\t\t\t\t\t\t+ `${error.message}\\n\\n`\n\t\t\t\t\t\t\t\t+ (error.markerPath.length > 1\n\t\t\t\t\t\t\t\t\t? `Incompatibility found at: ${ErrorLocator.locateMarkerPath(error.markerPath)}.\\n\\n`\n\t\t\t\t\t\t\t\t\t: '')\n\t\t\t\t\t\t\t\t+ `Entity list located at: ${ErrorLocator.locateInternalState(state)}.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tthrow error\n\t\t\t\t}\n\n\t\t\t\tif (state.children.has(idToConnect.value)) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tthis.stateInitializer.initializeEntityRealm(idToConnect, state.entityName, {\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: state,\n\t\t\t\t}, stateToConnect)\n\n\t\t\t\tstate.plannedRemovals?.delete(idToConnect.value)\n\n\t\t\t\tlet changesDelta = 0\n\n\t\t\t\tif (persistedEntityIds.has(idToConnect.value)) {\n\t\t\t\t\t// It was removed from the list but now we're adding it back.\n\t\t\t\t\tchangesDelta--\n\t\t\t\t} else if (idToConnect.existsOnServer) {\n\t\t\t\t\t// ignoring unpersisted entities\n\t\t\t\t\tchangesDelta++\n\t\t\t\t}\n\t\t\t\tif (stateToConnect.type === 'entityRealm') {\n\t\t\t\t\tchangesDelta += stateToConnect.unpersistedChangesCount\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic disconnectEntity(listState: EntityListState, childEntity: EntityAccessor, options: { noPersist?: boolean }) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\t// TODO disable this at the top-level.\n\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(listState)\n\n\t\t\tif (!listState.children.has(childEntity.id)) {\n\t\t\t\tthrow new BindingError(\n\t\t\t\t\t`Entity list doesn't include an entity with id '${childEntity.id}' and so it cannot remove it.`,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealmChild(this.treeStore, listState)) {\n\t\t\t\tconst disconnectedChildIdValue = childEntity.id\n\t\t\t\tconst disconnectedChildRealm = state.children.get(disconnectedChildIdValue)\n\n\t\t\t\tif (disconnectedChildRealm === undefined) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tthis.treeStore.disposeOfRealm(disconnectedChildRealm)\n\t\t\t\tstate.children.delete(disconnectedChildIdValue)\n\n\t\t\t\tlet changesDelta = 0\n\n\t\t\t\tif (persistedEntityIds.has(disconnectedChildIdValue)) {\n\t\t\t\t\tchangesDelta++\n\n\t\t\t\t\tif (state.plannedRemovals === undefined) {\n\t\t\t\t\t\tstate.plannedRemovals = new Map()\n\t\t\t\t\t}\n\t\t\t\t\tif (!options.noPersist) {\n\t\t\t\t\t\tstate.plannedRemovals.set(disconnectedChildIdValue, 'disconnect')\n\t\t\t\t\t}\n\t\t\t\t} else if (disconnectedChildRealm.entity.id.existsOnServer) {\n\t\t\t\t\t// Disconnecting unpersisted entities doesn't constitute a change.\n\n\t\t\t\t\t// It wasn't on the list, then it was, and now we're removing it again.\n\t\t\t\t\tchangesDelta--\n\t\t\t\t}\n\n\t\t\t\tif (disconnectedChildRealm.type === 'entityRealm') {\n\t\t\t\t\t// Undoing whatever changes this child had caused.\n\t\t\t\t\tchangesDelta -= disconnectedChildRealm.unpersistedChangesCount\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic createNewEntity(outerState: EntityListState, initialize: EntityAccessor.BatchUpdatesHandler | undefined): RuntimeId {\n\t\treturn this.eventManager.syncOperation((): RuntimeId => {\n\t\t\t// All siblings need to have the same id.\n\t\t\tconst id = new UnpersistedEntityDummyId()\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealmChild(this.treeStore, outerState)) {\n\t\t\t\tconst newEntity = this.stateInitializer.initializeEntityRealm(id, state.entityName, {\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: state,\n\t\t\t\t})\n\n\t\t\t\tthis.stateInitializer.runImmediateUserInitialization(newEntity, initialize)\n\n\t\t\t\t// The act of creating the entity doesn't constitute a change unless there's something inside to persist.\n\t\t\t\t// TODO forceCreate\n\t\t\t\t// TODO defaultValue\n\t\t\t\tthis.eventManager.registerJustUpdated(state, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t}\n\t\t\treturn id\n\t\t})\n\t}\n\n\tpublic getChildEntityById(state: EntityListState, id: EntityId) {\n\t\tconst realm = state.children.get(id)\n\t\tif (realm === undefined) {\n\t\t\tconst isRuntimeId = typeof id === 'number' || uuidValidate(id) || UnpersistedEntityDummyId.matchesDummyId(id)\n\t\t\tif (isRuntimeId) {\n\t\t\t\tthrow new BindingError(`EntityList: cannot retrieve an entity with id '${id}' as it is not on the list.`)\n\t\t\t}\n\t\t\tconst looksLikeKey = typeof id === 'string' && (this.treeStore.entityRealmStore.has(id) || RealmKeyGenerator.vaguelyAppearsToBeAKey(id))\n\t\t\tthrow new BindingError(\n\t\t\t\t`EntityList: cannot retrieve an entity with id '${id}' because it's not a valid id.`\n\t\t\t\t\t+ (looksLikeKey\n\t\t\t\t\t\t? `\\nThe supplied value appears to be an entity *key*, not an *id*. List accessors are indexed by ids `\n\t\t\t\t\t\t\t+ `and you can use \\`EntityAccessor.id\\` to obtain one. Keys, on the other hand, are globally unique and `\n\t\t\t\t\t\t\t+ `you can use \\`getEntityByKey\\` to get its EntityAccessor.`\n\t\t\t\t\t\t: ''),\n\t\t\t)\n\t\t}\n\t\treturn realm.getAccessor()\n\t}\n}\n"],"names":["EventListenersStore","OperationsHelpers","StateIterator","BindingError","ErrorLocator","MarkerComparator","getEntityMarker","LocalizedBindingError","UnpersistedEntityDummyId","EventManager","uuidValidate","RealmKeyGenerator"],"mappings":";;;;;;;;;;AAoBO,MAAM,eAAe;AAAA,EACpB,YACW,sBACA,qBACA,cACA,kBACA,WAChB;AALgB,SAAA,uBAAA;AACA,SAAA,sBAAA;AACA,SAAA,eAAA;AACA,SAAA,mBAAA;AACA,SAAA,YAAA;AAAA,EACf;AAAA,EAEI,SAAS,WAA4B,OAAwC;AACnF,WAAO,KAAK,qBAAqB,SAAS,WAAW,KAAK;AAAA,EAC3D;AAAA,EAEO,iBACN,OACA,OACA,UACC;AACD,QAAI,MAAM,mBAAmB,QAAW;AACvC,YAAM,iBAAiB,IAAIA,kCAAA;AAAA,IAC5B;AACA,WAAO,MAAM,eAAe,IAAI,OAAO,QAAQ;AAAA,EAChD;AAAA,EAEO,sBACN,OACA,OACA,UACC;AACD,QAAI,MAAM,wBAAwB,QAAW;AAC5C,YAAM,sBAAsB,IAAIA,kCAAA;AAAA,IACjC;AACA,WAAO,MAAM,oBAAoB,IAAI,OAAO,QAAQ;AAAA,EACrD;AAAA,E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,IACD,CAAC;AAAA,EACF;AAAA,EAEO,gBAAgB,YAA6B,YAAuE;AAC1H,WAAO,KAAK,aAAa,cAAc,MAAiB;AAEvD,YAAM,KAAK,IAAIM,uCAAA;AAEf,iBAAW,SAASN,4BAAc,sBAAsB,KAAK,WAAW,UAAU,GAAG;AACpF,cAAM,YAAY,KAAK,iBAAiB,sBAAsB,IAAI,MAAM,YAAY;AAAA,UACnF,MAAM;AAAA,UACN,QAAQ;AAAA,QAAA,CACR;AAED,aAAK,iBAAiB,+BAA+B,WAAW,UAAU;AAK1E,aAAK,aAAa,oBAAoB,OAAOO,aAAAA,aAAa,qBAAqB;AAAA,MAChF;AACA,aAAO;AAAA,IACR,CAAC;AAAA,EACF;AAAA,EAEO,mBAAmB,OAAwB,IAAc;AAC/D,UAAM,QAAQ,MAAM,SAAS,IAAI,EAAE;AACnC,QAAI,UAAU,QAAW;AACxB,YAAM,cAAc,OAAO,OAAO,YAAYC,KAAAA,SAAa,EAAE,KAAKF,cAAAA,yBAAyB,eAAe,EAAE;AAC5G,UAAI,aAAa;AAChB,cAAM,IAAIL,cAAAA,aAAa,kDAAkD,EAAE,6BAA6B;AAAA,MACzG;AACA,YAAM,eAAe,OAAO,OAAO,aAAa,KAAK,UAAU,iBAAiB,IAAI,EAAE,KAAKQ,kBAAAA,kBAAkB,uBAAuB,EAAE;AACtI,YAAM,IAAIR,cAAAA;AAAAA,QACT,kDAAkD,EAAE,oCAChD,eACA;AAAA,oQAGA;AAAA,MAAA;AAAA,IAEN;AACA,WAAO,MAAM,YAAA;AAAA,EACd;AACD;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"ListOperations.js","sources":["../../../../../../packages/binding-legacy/src/core/operations/ListOperations.ts"],"sourcesContent":["import { validate as uuidValidate } from 'uuid'\nimport type { BatchUpdatesOptions, EntityAccessor, EntityListAccessor, ErrorAccessor } from '@contember/binding-common'\nimport {\n\tBindingError,\n\tEntityId,\n\tEventListenersStore,\n\tLocalizedBindingError,\n\tMarkerComparator,\n\tRuntimeId,\n\tUnpersistedEntityDummyId,\n} from '@contember/binding-common'\nimport type { AccessorErrorManager } from '../AccessorErrorManager'\nimport { EventManager } from '../EventManager'\nimport { RealmKeyGenerator } from '../RealmKeyGenerator'\nimport { EntityListState, getEntityMarker, StateIterator } from '../state'\nimport type { StateInitializer } from '../StateInitializer'\nimport type { TreeStore } from '../TreeStore'\nimport { OperationsHelpers } from './OperationsHelpers'\nimport { ErrorLocator } from '../exceptions/ErrorLocator'\n\nexport class ListOperations {\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly batchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic addError(listState: EntityListState, error: ErrorAccessor.Error): () => void {\n\t\treturn this.accessorErrorManager.addError(listState, error)\n\t}\n\n\tpublic addEventListener<Type extends keyof EntityListAccessor.RuntimeEntityListEventListenerMap>(\n\t\tstate: EntityListState,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityListAccessor.EntityListEventListenerMap[Type],\n\t) {\n\t\tif (state.eventListeners === undefined) {\n\t\t\tstate.eventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.eventListeners.add(event, listener)\n\t}\n\n\tpublic addChildEventListener<Type extends keyof EntityAccessor.EntityEventListenerMap>(\n\t\tstate: EntityListState,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityAccessor.EntityEventListenerMap[Type],\n\t) {\n\t\tif (state.childEventListeners === undefined) {\n\t\t\tstate.childEventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.childEventListeners.add(event, listener)\n\t}\n\n\tpublic batchUpdates(state: EntityListState, performUpdates: EntityListAccessor.BatchUpdatesHandler) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tperformUpdates(state.getAccessor, this.batchUpdatesOptions)\n\t\t})\n\t}\n\n\tpublic connectEntity(outerState: EntityListState, entityToConnect: EntityAccessor) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(outerState)\n\t\t\tconst stateToConnect = OperationsHelpers.resolveAndPrepareEntityToConnect(this.treeStore, entityToConnect)\n\t\t\tconst idToConnect = stateToConnect.entity.id\n\n\t\t\t// TODO disable this at the top-level.\n\t\t\tfor (const state of StateIterator.eachSiblingRealmChild(this.treeStore, outerState)) {\n\t\t\t\tif (state.entityName !== stateToConnect.entity.entityName) {\n\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t`EntityListAccessor.connectEntity: Attempting to connect at an entity of type `\n\t\t\t\t\t\t\t+ `'${stateToConnect.entity.entityName}' but '${state.entityName}' is expected.\\n\\n`\n\t\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tMarkerComparator.assertEntityMarkersSubsetOf(\n\t\t\t\t\t\tstate.blueprint.marker.fields,\n\t\t\t\t\t\tgetEntityMarker(stateToConnect).fields,\n\t\t\t\t\t)\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (error instanceof LocalizedBindingError) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Entity list: cannot connect entity with key '${entityToConnect.key}' because its fields are `\n\t\t\t\t\t\t\t\t+ `incompatible with entities found on this list. Make sure both trees are equivalent.\\n\\n`\n\t\t\t\t\t\t\t\t+ `${error.message}\\n\\n`\n\t\t\t\t\t\t\t\t+ (error.markerPath.length > 1\n\t\t\t\t\t\t\t\t\t? `Incompatibility found at: ${ErrorLocator.locateMarkerPath(error.markerPath)}.\\n\\n`\n\t\t\t\t\t\t\t\t\t: '')\n\t\t\t\t\t\t\t\t+ `Entity list located at: ${ErrorLocator.locateInternalState(state)}.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tthrow error\n\t\t\t\t}\n\n\t\t\t\tif (state.children.has(idToConnect.value)) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tthis.stateInitializer.initializeEntityRealm(idToConnect, state.entityName, {\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: state,\n\t\t\t\t}, stateToConnect)\n\n\t\t\t\tstate.plannedRemovals?.delete(idToConnect.value)\n\n\t\t\t\tlet changesDelta = 0\n\n\t\t\t\tif (persistedEntityIds.has(idToConnect.value)) {\n\t\t\t\t\t// It was removed from the list but now we're adding it back.\n\t\t\t\t\tchangesDelta--\n\t\t\t\t} else if (idToConnect.existsOnServer) {\n\t\t\t\t\t// ignoring unpersisted entities\n\t\t\t\t\tchangesDelta++\n\t\t\t\t}\n\t\t\t\tif (stateToConnect.type === 'entityRealm') {\n\t\t\t\t\tchangesDelta += stateToConnect.unpersistedChangesCount\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic disconnectEntity(listState: EntityListState, childEntity: EntityAccessor, options: { noPersist?: boolean }) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\t// TODO disable this at the top-level.\n\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(listState)\n\n\t\t\tif (!listState.children.has(childEntity.id)) {\n\t\t\t\tthrow new BindingError(\n\t\t\t\t\t`Entity list doesn't include an entity with id '${childEntity.id}' and so it cannot remove it.`,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealmChild(this.treeStore, listState)) {\n\t\t\t\tconst disconnectedChildIdValue = childEntity.id\n\t\t\t\tconst disconnectedChildRealm = state.children.get(disconnectedChildIdValue)\n\n\t\t\t\tif (disconnectedChildRealm === undefined) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tthis.treeStore.disposeOfRealm(disconnectedChildRealm)\n\t\t\t\tstate.children.delete(disconnectedChildIdValue)\n\n\t\t\t\tlet changesDelta = 0\n\n\t\t\t\tif (persistedEntityIds.has(disconnectedChildIdValue)) {\n\t\t\t\t\tchangesDelta++\n\n\t\t\t\t\tif (state.plannedRemovals === undefined) {\n\t\t\t\t\t\tstate.plannedRemovals = new Map()\n\t\t\t\t\t}\n\t\t\t\t\tif (!options.noPersist) {\n\t\t\t\t\t\tstate.plannedRemovals.set(disconnectedChildIdValue, 'disconnect')\n\t\t\t\t\t}\n\t\t\t\t} else if (disconnectedChildRealm.entity.id.existsOnServer) {\n\t\t\t\t\t// Disconnecting unpersisted entities doesn't constitute a change.\n\n\t\t\t\t\t// It wasn't on the list, then it was, and now we're removing it again.\n\t\t\t\t\tchangesDelta--\n\t\t\t\t}\n\n\t\t\t\tif (disconnectedChildRealm.type === 'entityRealm') {\n\t\t\t\t\t// Undoing whatever changes this child had caused.\n\t\t\t\t\tchangesDelta -= disconnectedChildRealm.unpersistedChangesCount\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic createNewEntity(outerState: EntityListState, initialize: EntityAccessor.BatchUpdatesHandler | undefined): RuntimeId {\n\t\treturn this.eventManager.syncOperation((): RuntimeId => {\n\t\t\t// All siblings need to have the same id.\n\t\t\tconst id = new UnpersistedEntityDummyId()\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealmChild(this.treeStore, outerState)) {\n\t\t\t\tconst newEntity = this.stateInitializer.initializeEntityRealm(id, state.entityName, {\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: state,\n\t\t\t\t})\n\n\t\t\t\tthis.stateInitializer.runImmediateUserInitialization(newEntity, initialize)\n\n\t\t\t\t// The act of creating the entity doesn't constitute a change unless there's something inside to persist.\n\t\t\t\t// TODO forceCreate\n\t\t\t\t// TODO defaultValue\n\t\t\t\tthis.eventManager.registerJustUpdated(state, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t}\n\t\t\treturn id\n\t\t})\n\t}\n\n\tpublic getChildEntityById(state: EntityListState, id: EntityId) {\n\t\tconst realm = state.children.get(id)\n\t\tif (realm === undefined) {\n\t\t\tconst isRuntimeId = typeof id === 'number' || uuidValidate(id) || UnpersistedEntityDummyId.matchesDummyId(id)\n\t\t\tif (isRuntimeId) {\n\t\t\t\tthrow new BindingError(`EntityList: cannot retrieve an entity with id '${id}' as it is not on the list.`)\n\t\t\t}\n\t\t\tconst looksLikeKey = typeof id === 'string' && (this.treeStore.entityRealmStore.has(id) || RealmKeyGenerator.vaguelyAppearsToBeAKey(id))\n\t\t\tthrow new BindingError(\n\t\t\t\t`EntityList: cannot retrieve an entity with id '${id}' because it's not a valid id.`\n\t\t\t\t\t+ (looksLikeKey\n\t\t\t\t\t\t? `\\nThe supplied value appears to be an entity *key*, not an *id*. List accessors are indexed by ids `\n\t\t\t\t\t\t\t+ `and you can use \\`EntityAccessor.id\\` to obtain one. Keys, on the other hand, are globally unique and `\n\t\t\t\t\t\t\t+ `you can use \\`getEntityByKey\\` to get its EntityAccessor.`\n\t\t\t\t\t\t: ''),\n\t\t\t)\n\t\t}\n\t\treturn realm.getAccessor()\n\t}\n}\n"],"names":["uuidValidate"],"mappings":";;;;;;;;AAoBO,MAAM,eAAe;AAAA,EACpB,YACW,sBACA,qBACA,cACA,kBACA,WAChB;AALgB,SAAA,uBAAA;AACA,SAAA,sBAAA;AACA,SAAA,eAAA;AACA,SAAA,mBAAA;AACA,SAAA,YAAA;AAAA,EACf;AAAA,EAEI,SAAS,WAA4B,OAAwC;AACnF,WAAO,KAAK,qBAAqB,SAAS,WAAW,KAAK;AAAA,EAC3D;AAAA,EAEO,iBACN,OACA,OACA,UACC;AACD,QAAI,MAAM,mBAAmB,QAAW;AACvC,YAAM,iBAAiB,IAAI,oBAAA;AAAA,IAC5B;AACA,WAAO,MAAM,eAAe,IAAI,OAAO,QAAQ;AAAA,EAChD;AAAA,EAEO,sBACN,OACA,OACA,UACC;AACD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aAAK,aAAa,oBAAoB,OAAO,YAAY;AAAA,MAC1D;AAAA,IACD,CAAC;AAAA,EACF;AAAA,EAEO,gBAAgB,YAA6B,YAAuE;AAC1H,WAAO,KAAK,aAAa,cAAc,MAAiB;AAEvD,YAAM,KAAK,IAAI,yBAAA;AAEf,iBAAW,SAAS,cAAc,sBAAsB,KAAK,WAAW,UAAU,GAAG;AACpF,cAAM,YAAY,KAAK,iBAAiB,sBAAsB,IAAI,MAAM,YAAY;AAAA,UACnF,MAAM;AAAA,UACN,QAAQ;AAAA,QAAA,CACR;AAED,aAAK,iBAAiB,+BAA+B,WAAW,UAAU;AAK1E,aAAK,aAAa,oBAAoB,OAAO,aAAa,qBAAqB;AAAA,MAChF;AACA,aAAO;AAAA,IACR,CAAC;AAAA,EACF;AAAA,EAEO,mBAAmB,OAAwB,IAAc;AAC/D,UAAM,QAAQ,MAAM,SAAS,IAAI,EAAE;AACnC,QAAI,UAAU,QAAW;AACxB,YAAM,cAAc,OAAO,OAAO,YAAYA,SAAa,EAAE,KAAK,yBAAyB,eAAe,EAAE;AAC5G,UAAI,aAAa;AAChB,cAAM,IAAI,aAAa,kDAAkD,EAAE,6BAA6B;AAAA,MACzG;AACA,YAAM,eAAe,OAAO,OAAO,aAAa,KAAK,UAAU,iBAAiB,IAAI,EAAE,KAAK,kBAAkB,uBAAuB,EAAE;AACtI,YAAM,IAAI;AAAA,QACT,kDAAkD,EAAE,oCAChD,eACA;AAAA,oQAGA;AAAA,MAAA;AAAA,IAEN;AACA,WAAO,MAAM,YAAA;AAAA,EACd;AACD;"}
1
+ {"version":3,"file":"ListOperations.js","sources":["../../../../../../packages/binding-legacy/src/core/operations/ListOperations.ts"],"sourcesContent":["import { validate as uuidValidate } from 'uuid'\nimport type { BatchUpdatesOptions, EntityAccessor, EntityListAccessor, ErrorAccessor } from '@contember/binding-common'\nimport {\n\tBindingError,\n\tEntityId,\n\tEventListenersStore,\n\tLocalizedBindingError,\n\tMarkerComparator,\n\tRuntimeId,\n\tUnpersistedEntityDummyId,\n} from '@contember/binding-common'\nimport type { AccessorErrorManager } from '../AccessorErrorManager.js'\nimport { EventManager } from '../EventManager.js'\nimport { RealmKeyGenerator } from '../RealmKeyGenerator.js'\nimport { EntityListState, getEntityMarker, StateIterator } from '../state/index.js'\nimport type { StateInitializer } from '../StateInitializer.js'\nimport type { TreeStore } from '../TreeStore.js'\nimport { OperationsHelpers } from './OperationsHelpers.js'\nimport { ErrorLocator } from '../exceptions/ErrorLocator.js'\n\nexport class ListOperations {\n\tpublic constructor(\n\t\tprivate readonly accessorErrorManager: AccessorErrorManager,\n\t\tprivate readonly batchUpdatesOptions: BatchUpdatesOptions,\n\t\tprivate readonly eventManager: EventManager,\n\t\tprivate readonly stateInitializer: StateInitializer,\n\t\tprivate readonly treeStore: TreeStore,\n\t) {}\n\n\tpublic addError(listState: EntityListState, error: ErrorAccessor.Error): () => void {\n\t\treturn this.accessorErrorManager.addError(listState, error)\n\t}\n\n\tpublic addEventListener<Type extends keyof EntityListAccessor.RuntimeEntityListEventListenerMap>(\n\t\tstate: EntityListState,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityListAccessor.EntityListEventListenerMap[Type],\n\t) {\n\t\tif (state.eventListeners === undefined) {\n\t\t\tstate.eventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.eventListeners.add(event, listener)\n\t}\n\n\tpublic addChildEventListener<Type extends keyof EntityAccessor.EntityEventListenerMap>(\n\t\tstate: EntityListState,\n\t\tevent: { type: Type; key?: string },\n\t\tlistener: EntityAccessor.EntityEventListenerMap[Type],\n\t) {\n\t\tif (state.childEventListeners === undefined) {\n\t\t\tstate.childEventListeners = new EventListenersStore()\n\t\t}\n\t\treturn state.childEventListeners.add(event, listener)\n\t}\n\n\tpublic batchUpdates(state: EntityListState, performUpdates: EntityListAccessor.BatchUpdatesHandler) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tperformUpdates(state.getAccessor, this.batchUpdatesOptions)\n\t\t})\n\t}\n\n\tpublic connectEntity(outerState: EntityListState, entityToConnect: EntityAccessor) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(outerState)\n\t\t\tconst stateToConnect = OperationsHelpers.resolveAndPrepareEntityToConnect(this.treeStore, entityToConnect)\n\t\t\tconst idToConnect = stateToConnect.entity.id\n\n\t\t\t// TODO disable this at the top-level.\n\t\t\tfor (const state of StateIterator.eachSiblingRealmChild(this.treeStore, outerState)) {\n\t\t\t\tif (state.entityName !== stateToConnect.entity.entityName) {\n\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t`EntityListAccessor.connectEntity: Attempting to connect at an entity of type `\n\t\t\t\t\t\t\t+ `'${stateToConnect.entity.entityName}' but '${state.entityName}' is expected.\\n\\n`\n\t\t\t\t\t\t\t+ `Entity located at: ${ErrorLocator.locateInternalState(outerState)}.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tMarkerComparator.assertEntityMarkersSubsetOf(\n\t\t\t\t\t\tstate.blueprint.marker.fields,\n\t\t\t\t\t\tgetEntityMarker(stateToConnect).fields,\n\t\t\t\t\t)\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (error instanceof LocalizedBindingError) {\n\t\t\t\t\t\tthrow new BindingError(\n\t\t\t\t\t\t\t`Entity list: cannot connect entity with key '${entityToConnect.key}' because its fields are `\n\t\t\t\t\t\t\t\t+ `incompatible with entities found on this list. Make sure both trees are equivalent.\\n\\n`\n\t\t\t\t\t\t\t\t+ `${error.message}\\n\\n`\n\t\t\t\t\t\t\t\t+ (error.markerPath.length > 1\n\t\t\t\t\t\t\t\t\t? `Incompatibility found at: ${ErrorLocator.locateMarkerPath(error.markerPath)}.\\n\\n`\n\t\t\t\t\t\t\t\t\t: '')\n\t\t\t\t\t\t\t\t+ `Entity list located at: ${ErrorLocator.locateInternalState(state)}.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tthrow error\n\t\t\t\t}\n\n\t\t\t\tif (state.children.has(idToConnect.value)) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tthis.stateInitializer.initializeEntityRealm(idToConnect, state.entityName, {\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: state,\n\t\t\t\t}, stateToConnect)\n\n\t\t\t\tstate.plannedRemovals?.delete(idToConnect.value)\n\n\t\t\t\tlet changesDelta = 0\n\n\t\t\t\tif (persistedEntityIds.has(idToConnect.value)) {\n\t\t\t\t\t// It was removed from the list but now we're adding it back.\n\t\t\t\t\tchangesDelta--\n\t\t\t\t} else if (idToConnect.existsOnServer) {\n\t\t\t\t\t// ignoring unpersisted entities\n\t\t\t\t\tchangesDelta++\n\t\t\t\t}\n\t\t\t\tif (stateToConnect.type === 'entityRealm') {\n\t\t\t\t\tchangesDelta += stateToConnect.unpersistedChangesCount\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic disconnectEntity(listState: EntityListState, childEntity: EntityAccessor, options: { noPersist?: boolean }) {\n\t\tthis.eventManager.syncOperation(() => {\n\t\t\t// TODO disable this at the top-level.\n\t\t\tconst persistedEntityIds = this.treeStore.getEntityListPersistedIds(listState)\n\n\t\t\tif (!listState.children.has(childEntity.id)) {\n\t\t\t\tthrow new BindingError(\n\t\t\t\t\t`Entity list doesn't include an entity with id '${childEntity.id}' and so it cannot remove it.`,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealmChild(this.treeStore, listState)) {\n\t\t\t\tconst disconnectedChildIdValue = childEntity.id\n\t\t\t\tconst disconnectedChildRealm = state.children.get(disconnectedChildIdValue)\n\n\t\t\t\tif (disconnectedChildRealm === undefined) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tthis.treeStore.disposeOfRealm(disconnectedChildRealm)\n\t\t\t\tstate.children.delete(disconnectedChildIdValue)\n\n\t\t\t\tlet changesDelta = 0\n\n\t\t\t\tif (persistedEntityIds.has(disconnectedChildIdValue)) {\n\t\t\t\t\tchangesDelta++\n\n\t\t\t\t\tif (state.plannedRemovals === undefined) {\n\t\t\t\t\t\tstate.plannedRemovals = new Map()\n\t\t\t\t\t}\n\t\t\t\t\tif (!options.noPersist) {\n\t\t\t\t\t\tstate.plannedRemovals.set(disconnectedChildIdValue, 'disconnect')\n\t\t\t\t\t}\n\t\t\t\t} else if (disconnectedChildRealm.entity.id.existsOnServer) {\n\t\t\t\t\t// Disconnecting unpersisted entities doesn't constitute a change.\n\n\t\t\t\t\t// It wasn't on the list, then it was, and now we're removing it again.\n\t\t\t\t\tchangesDelta--\n\t\t\t\t}\n\n\t\t\t\tif (disconnectedChildRealm.type === 'entityRealm') {\n\t\t\t\t\t// Undoing whatever changes this child had caused.\n\t\t\t\t\tchangesDelta -= disconnectedChildRealm.unpersistedChangesCount\n\t\t\t\t}\n\n\t\t\t\tthis.eventManager.registerJustUpdated(state, changesDelta)\n\t\t\t}\n\t\t})\n\t}\n\n\tpublic createNewEntity(outerState: EntityListState, initialize: EntityAccessor.BatchUpdatesHandler | undefined): RuntimeId {\n\t\treturn this.eventManager.syncOperation((): RuntimeId => {\n\t\t\t// All siblings need to have the same id.\n\t\t\tconst id = new UnpersistedEntityDummyId()\n\n\t\t\tfor (const state of StateIterator.eachSiblingRealmChild(this.treeStore, outerState)) {\n\t\t\t\tconst newEntity = this.stateInitializer.initializeEntityRealm(id, state.entityName, {\n\t\t\t\t\ttype: 'listEntity',\n\t\t\t\t\tparent: state,\n\t\t\t\t})\n\n\t\t\t\tthis.stateInitializer.runImmediateUserInitialization(newEntity, initialize)\n\n\t\t\t\t// The act of creating the entity doesn't constitute a change unless there's something inside to persist.\n\t\t\t\t// TODO forceCreate\n\t\t\t\t// TODO defaultValue\n\t\t\t\tthis.eventManager.registerJustUpdated(state, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t}\n\t\t\treturn id\n\t\t})\n\t}\n\n\tpublic getChildEntityById(state: EntityListState, id: EntityId) {\n\t\tconst realm = state.children.get(id)\n\t\tif (realm === undefined) {\n\t\t\tconst isRuntimeId = typeof id === 'number' || uuidValidate(id) || UnpersistedEntityDummyId.matchesDummyId(id)\n\t\t\tif (isRuntimeId) {\n\t\t\t\tthrow new BindingError(`EntityList: cannot retrieve an entity with id '${id}' as it is not on the list.`)\n\t\t\t}\n\t\t\tconst looksLikeKey = typeof id === 'string' && (this.treeStore.entityRealmStore.has(id) || RealmKeyGenerator.vaguelyAppearsToBeAKey(id))\n\t\t\tthrow new BindingError(\n\t\t\t\t`EntityList: cannot retrieve an entity with id '${id}' because it's not a valid id.`\n\t\t\t\t\t+ (looksLikeKey\n\t\t\t\t\t\t? `\\nThe supplied value appears to be an entity *key*, not an *id*. List accessors are indexed by ids `\n\t\t\t\t\t\t\t+ `and you can use \\`EntityAccessor.id\\` to obtain one. Keys, on the other hand, are globally unique and `\n\t\t\t\t\t\t\t+ `you can use \\`getEntityByKey\\` to get its EntityAccessor.`\n\t\t\t\t\t\t: ''),\n\t\t\t)\n\t\t}\n\t\treturn realm.getAccessor()\n\t}\n}\n"],"names":["uuidValidate"],"mappings":";;;;;;;;AAoBO,MAAM,eAAe;AAAA,EACpB,YACW,sBACA,qBACA,cACA,kBACA,WAChB;AALgB,SAAA,uBAAA;AACA,SAAA,sBAAA;AACA,SAAA,eAAA;AACA,SAAA,mBAAA;AACA,SAAA,YAAA;AAAA,EACf;AAAA,EAEI,SAAS,WAA4B,OAAwC;AACnF,WAAO,KAAK,qBAAqB,SAAS,WAAW,KAAK;AAAA,EAC3D;AAAA,EAEO,iBACN,OACA,OACA,UACC;AACD,QAAI,MAAM,mBAAmB,QAAW;AACvC,YAAM,iBAAiB,IAAI,oBAAA;AAAA,IAC5B;AACA,WAAO,MAAM,eAAe,IAAI,OAAO,QAAQ;AAAA,EAChD;AAAA,EAEO,sBACN,OACA,OACA,UACC;A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A,aAAK,aAAa,oBAAoB,OAAO,YAAY;AAAA,MAC1D;AAAA,IACD,CAAC;AAAA,EACF;AAAA,EAEO,gBAAgB,YAA6B,YAAuE;AAC1H,WAAO,KAAK,aAAa,cAAc,MAAiB;AAEvD,YAAM,KAAK,IAAI,yBAAA;AAEf,iBAAW,SAAS,cAAc,sBAAsB,KAAK,WAAW,UAAU,GAAG;AACpF,cAAM,YAAY,KAAK,iBAAiB,sBAAsB,IAAI,MAAM,YAAY;AAAA,UACnF,MAAM;AAAA,UACN,QAAQ;AAAA,QAAA,CACR;AAED,aAAK,iBAAiB,+BAA+B,WAAW,UAAU;AAK1E,aAAK,aAAa,oBAAoB,OAAO,aAAa,qBAAqB;AAAA,MAChF;AACA,aAAO;AAAA,IACR,CAAC;AAAA,EACF;AAAA,EAEO,mBAAmB,OAAwB,IAAc;AAC/D,UAAM,QAAQ,MAAM,SAAS,IAAI,EAAE;AACnC,QAAI,UAAU,QAAW;AACxB,YAAM,cAAc,OAAO,OAAO,YAAYA,SAAa,EAAE,KAAK,yBAAyB,eAAe,EAAE;AAC5G,UAAI,aAAa;AAChB,cAAM,IAAI,aAAa,kDAAkD,EAAE,6BAA6B;AAAA,MACzG;AACA,YAAM,eAAe,OAAO,OAAO,aAAa,KAAK,UAAU,iBAAiB,IAAI,EAAE,KAAK,kBAAkB,uBAAuB,EAAE;AACtI,YAAM,IAAI;AAAA,QACT,kDAAkD,EAAE,oCAChD,eACA;AAAA,oQAGA;AAAA,MAAA;AAAA,IAEN;AACA,WAAO,MAAM,YAAA;AAAA,EACd;AACD;"}
@@ -1 +1 @@
1
- {"version":3,"file":"OperationsHelpers.cjs","sources":["../../../../../../packages/binding-legacy/src/core/operations/OperationsHelpers.ts"],"sourcesContent":["import type { EntityAccessor } from '@contember/binding-common'\nimport type { RuntimeId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { PRIMARY_KEY_NAME } from '@contember/binding-common'\nimport { EventManager } from '../EventManager'\nimport { RealmKeyGenerator } from '../RealmKeyGenerator'\nimport type { EntityRealmState, EntityRealmStateStub, EntityState } from '../state'\nimport type { StateInitializer } from '../StateInitializer'\nimport type { TreeStore } from '../TreeStore'\n\nexport class OperationsHelpers {\n\tpublic static rejectInvalidAccessorTree(): never {\n\t\tthrow new BindingError(\n\t\t\t`The accessor tree does not correspond to the MarkerTree. This should absolutely never happen.`,\n\t\t)\n\t}\n\n\tpublic static resolveAndPrepareEntityToConnect(\n\t\ttreeStore: TreeStore,\n\t\tentityToConnect: EntityAccessor,\n\t): EntityRealmState | EntityRealmStateStub {\n\t\tconst entityToConnectKey = entityToConnect.key\n\t\tif (!entityToConnectKey) {\n\t\t\tthrow new BindingError(`Attempting to connect an entity without a key.`)\n\t\t}\n\t\tconst stateToConnect = treeStore.entityRealmStore.get(entityToConnectKey)\n\n\t\tif (stateToConnect === undefined) {\n\t\t\tthrow new BindingError(`Attempting to connect an entity with key '${entityToConnectKey}' but it doesn't exist.`)\n\t\t}\n\t\t// TODO This is commented out for now in order to at least somewhat mitigate the limitations of dealing with\n\t\t// \t\tinverse relations. However, once that has been addressed systemically, this code needs to be re-enabled.\n\t\t// if (!stateToConnect.entity.id.existsOnServer) {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Attempting to connect an entity with key '${stateToConnect.realmKey}' that ` +\n\t\t// \t\t\t`doesn't exist on server. That is currently impossible.`, // At least for now.\n\t\t// \t)\n\t\t// }\n\n\t\t// TODO\n\t\t// if (stateToConnect.entity.isScheduledForDeletion) {\n\t\t// \t// As far as the other realms are concerned, this entity is deleted. We don't want to just make it re-appear\n\t\t// \t// for them just because some other random relation decided to connect it.\n\t\t// \tstateToConnect.entity.realms.clear()\n\t\t// \tstateToConnect.entity.isScheduledForDeletion = false\n\t\t// }\n\n\t\treturn stateToConnect\n\t}\n\n\tpublic static changeEntityId(\n\t\ttreeStore: TreeStore,\n\t\teventManager: EventManager,\n\t\tstateInitializer: StateInitializer,\n\t\tentity: EntityState,\n\t\tnewId: RuntimeId,\n\t) {\n\t\tconst previousId = entity.id\n\n\t\ttreeStore.entityStore.delete(previousId.uniqueValue)\n\t\ttreeStore.entityStore.set(newId.uniqueValue, entity)\n\n\t\tlet counter = 0\n\t\tfor (const realm of entity.realms.values()) {\n\t\t\tthis.changeRealmId(treeStore, eventManager, stateInitializer, realm, newId)\n\n\t\t\tif (++counter === entity.realms.size) {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\tentity.hasIdSetInStone = true\n\t\tentity.id = newId\n\t}\n\n\tpublic static changeRealmId(\n\t\ttreeStore: TreeStore,\n\t\teventManager: EventManager,\n\t\tstateInitializer: StateInitializer,\n\t\trealm: EntityRealmState | EntityRealmStateStub,\n\t\tnewId: RuntimeId,\n\t) {\n\t\tconst oldEntity = realm.entity\n\t\tconst oldRealmKey = realm.realmKey\n\t\tconst oldId = oldEntity.id\n\n\t\tconst realmBlueprint = realm.blueprint\n\n\t\tconst newEntity = stateInitializer.initializeEntityState(newId, oldEntity.entityName)\n\t\tconst newRealmKey = RealmKeyGenerator.getRealmKey(newId, realmBlueprint)\n\n\t\trealm.realmKey = newRealmKey\n\t\trealm.entity = newEntity\n\n\t\t// This may still actually be necessary if we're going from a client to server generated uuid.\n\t\t// They both may have the same value but still be different.\n\t\tnewEntity.id = newId\n\t\toldEntity.realms.delete(oldRealmKey)\n\t\tnewEntity.realms.set(newRealmKey, realm)\n\n\t\tif (realm.type === 'entityRealm') {\n\t\t\tconst childIdState = realm.children.get(PRIMARY_KEY_NAME)\n\t\t\tif (childIdState?.type === 'field') {\n\t\t\t\tchildIdState.value = newId.value\n\t\t\t\teventManager.registerJustUpdated(childIdState, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t}\n\n\t\t\tthis.purgeStaleListenersAfterIdChange(realm)\n\t\t}\n\n\t\tif (oldEntity.realms.size === 0) {\n\t\t\ttreeStore.disposeOfEntity(oldEntity)\n\t\t}\n\n\t\ttreeStore.entityRealmStore.delete(oldRealmKey)\n\t\ttreeStore.entityRealmStore.set(newRealmKey, realm)\n\n\t\tif (realmBlueprint.type === 'listEntity') {\n\t\t\tconst list = realmBlueprint.parent\n\t\t\tlist.children.changeKey(oldId.value, newId.value) // 😎\n\t\t} else if (realmBlueprint.type === 'hasOne') {\n\t\t\teventManager.registerUpdatedConnection(realmBlueprint.parent, realmBlueprint.marker.placeholderName)\n\t\t}\n\t\tconst parent = realmBlueprint.parent\n\t\tif (parent) {\n\t\t\teventManager.registerJustUpdated(parent, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t}\n\t\tif (realm.type === 'entityRealm') {\n\t\t\teventManager.registerJustUpdated(realm, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t}\n\t}\n\n\tpublic static purgeStaleListenersAfterIdChange(realm: EntityRealmState) {\n\t\t// Version 1.\n\t\t// The listeners subscribed to a particular entity key so we no longer want to call these.\n\t\t// The only positionally associated listeners are in the blueprint so we re-initialize those.\n\t\t// realm.eventListeners = stateInitializer.initializeEntityEventListenerStore(realmBlueprint)\n\n\t\t// Version 2.\n\t\t// This is version two of this terrible hack. We want to preserve e.g. persistSuccess handlers.\n\n\t\t// Version 3.\n\t\t// The saga continues. We add a condition to exempt top-level realms. That way they can start\n\t\t// propagating changes from the top.\n\t\tconst listeners = realm.eventListeners\n\t\tif (listeners) {\n\t\t\tif (realm.blueprint.parent) {\n\t\t\t\tlisteners.delete({ type: 'update' })\n\t\t\t}\n\t\t\tlisteners.deleteByType('connectionUpdate')\n\t\t}\n\t}\n}\n"],"names":["BindingError","RealmKeyGenerator","PRIMARY_KEY_NAME","EventManager"],"mappings":";;;;;AAUO,MAAM,kBAAkB;AAAA,EAC9B,OAAc,4BAAmC;AAChD,UAAM,IAAIA,cAAAA;AAAAA,MACT;AAAA,IAAA;AAAA,EAEF;AAAA,EAEA,OAAc,iCACb,WACA,iBAC0C;AAC1C,UAAM,qBAAqB,gBAAgB;AAC3C,QAAI,CAAC,oBAAoB;AACxB,YAAM,IAAIA,cAAAA,aAA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AChC,gBAAU,gBAAgB,SAAS;AAAA,IACpC;AAEA,cAAU,iBAAiB,OAAO,WAAW;AAC7C,cAAU,iBAAiB,IAAI,aAAa,KAAK;AAEjD,QAAI,eAAe,SAAS,cAAc;AACzC,YAAM,OAAO,eAAe;AAC5B,WAAK,SAAS,UAAU,MAAM,OAAO,MAAM,KAAK;AAAA,IACjD,WAAW,eAAe,SAAS,UAAU;AAC5C,mBAAa,0BAA0B,eAAe,QAAQ,eAAe,OAAO,eAAe;AAAA,IACpG;AACA,UAAM,SAAS,eAAe;AAC9B,QAAI,QAAQ;AACX,mBAAa,oBAAoB,QAAQA,aAAAA,aAAa,qBAAqB;AAAA,IAC5E;AACA,QAAI,MAAM,SAAS,eAAe;AACjC,mBAAa,oBAAoB,OAAOA,aAAAA,aAAa,qBAAqB;AAAA,IAC3E;AAAA,EACD;AAAA,EAEA,OAAc,iCAAiC,OAAyB;AAYvE,UAAM,YAAY,MAAM;AACxB,QAAI,WAAW;AACd,UAAI,MAAM,UAAU,QAAQ;AAC3B,kBAAU,OAAO,EAAE,MAAM,SAAA,CAAU;AAAA,MACpC;AACA,gBAAU,aAAa,kBAAkB;AAAA,IAC1C;AAAA,EACD;AACD;;"}
1
+ {"version":3,"file":"OperationsHelpers.cjs","sources":["../../../../../../packages/binding-legacy/src/core/operations/OperationsHelpers.ts"],"sourcesContent":["import type { EntityAccessor } from '@contember/binding-common'\nimport type { RuntimeId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { PRIMARY_KEY_NAME } from '@contember/binding-common'\nimport { EventManager } from '../EventManager.js'\nimport { RealmKeyGenerator } from '../RealmKeyGenerator.js'\nimport type { EntityRealmState, EntityRealmStateStub, EntityState } from '../state/index.js'\nimport type { StateInitializer } from '../StateInitializer.js'\nimport type { TreeStore } from '../TreeStore.js'\n\nexport class OperationsHelpers {\n\tpublic static rejectInvalidAccessorTree(): never {\n\t\tthrow new BindingError(\n\t\t\t`The accessor tree does not correspond to the MarkerTree. This should absolutely never happen.`,\n\t\t)\n\t}\n\n\tpublic static resolveAndPrepareEntityToConnect(\n\t\ttreeStore: TreeStore,\n\t\tentityToConnect: EntityAccessor,\n\t): EntityRealmState | EntityRealmStateStub {\n\t\tconst entityToConnectKey = entityToConnect.key\n\t\tif (!entityToConnectKey) {\n\t\t\tthrow new BindingError(`Attempting to connect an entity without a key.`)\n\t\t}\n\t\tconst stateToConnect = treeStore.entityRealmStore.get(entityToConnectKey)\n\n\t\tif (stateToConnect === undefined) {\n\t\t\tthrow new BindingError(`Attempting to connect an entity with key '${entityToConnectKey}' but it doesn't exist.`)\n\t\t}\n\t\t// TODO This is commented out for now in order to at least somewhat mitigate the limitations of dealing with\n\t\t// \t\tinverse relations. However, once that has been addressed systemically, this code needs to be re-enabled.\n\t\t// if (!stateToConnect.entity.id.existsOnServer) {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Attempting to connect an entity with key '${stateToConnect.realmKey}' that ` +\n\t\t// \t\t\t`doesn't exist on server. That is currently impossible.`, // At least for now.\n\t\t// \t)\n\t\t// }\n\n\t\t// TODO\n\t\t// if (stateToConnect.entity.isScheduledForDeletion) {\n\t\t// \t// As far as the other realms are concerned, this entity is deleted. We don't want to just make it re-appear\n\t\t// \t// for them just because some other random relation decided to connect it.\n\t\t// \tstateToConnect.entity.realms.clear()\n\t\t// \tstateToConnect.entity.isScheduledForDeletion = false\n\t\t// }\n\n\t\treturn stateToConnect\n\t}\n\n\tpublic static changeEntityId(\n\t\ttreeStore: TreeStore,\n\t\teventManager: EventManager,\n\t\tstateInitializer: StateInitializer,\n\t\tentity: EntityState,\n\t\tnewId: RuntimeId,\n\t) {\n\t\tconst previousId = entity.id\n\n\t\ttreeStore.entityStore.delete(previousId.uniqueValue)\n\t\ttreeStore.entityStore.set(newId.uniqueValue, entity)\n\n\t\tlet counter = 0\n\t\tfor (const realm of entity.realms.values()) {\n\t\t\tthis.changeRealmId(treeStore, eventManager, stateInitializer, realm, newId)\n\n\t\t\tif (++counter === entity.realms.size) {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\tentity.hasIdSetInStone = true\n\t\tentity.id = newId\n\t}\n\n\tpublic static changeRealmId(\n\t\ttreeStore: TreeStore,\n\t\teventManager: EventManager,\n\t\tstateInitializer: StateInitializer,\n\t\trealm: EntityRealmState | EntityRealmStateStub,\n\t\tnewId: RuntimeId,\n\t) {\n\t\tconst oldEntity = realm.entity\n\t\tconst oldRealmKey = realm.realmKey\n\t\tconst oldId = oldEntity.id\n\n\t\tconst realmBlueprint = realm.blueprint\n\n\t\tconst newEntity = stateInitializer.initializeEntityState(newId, oldEntity.entityName)\n\t\tconst newRealmKey = RealmKeyGenerator.getRealmKey(newId, realmBlueprint)\n\n\t\trealm.realmKey = newRealmKey\n\t\trealm.entity = newEntity\n\n\t\t// This may still actually be necessary if we're going from a client to server generated uuid.\n\t\t// They both may have the same value but still be different.\n\t\tnewEntity.id = newId\n\t\toldEntity.realms.delete(oldRealmKey)\n\t\tnewEntity.realms.set(newRealmKey, realm)\n\n\t\tif (realm.type === 'entityRealm') {\n\t\t\tconst childIdState = realm.children.get(PRIMARY_KEY_NAME)\n\t\t\tif (childIdState?.type === 'field') {\n\t\t\t\tchildIdState.value = newId.value\n\t\t\t\teventManager.registerJustUpdated(childIdState, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t}\n\n\t\t\tthis.purgeStaleListenersAfterIdChange(realm)\n\t\t}\n\n\t\tif (oldEntity.realms.size === 0) {\n\t\t\ttreeStore.disposeOfEntity(oldEntity)\n\t\t}\n\n\t\ttreeStore.entityRealmStore.delete(oldRealmKey)\n\t\ttreeStore.entityRealmStore.set(newRealmKey, realm)\n\n\t\tif (realmBlueprint.type === 'listEntity') {\n\t\t\tconst list = realmBlueprint.parent\n\t\t\tlist.children.changeKey(oldId.value, newId.value) // 😎\n\t\t} else if (realmBlueprint.type === 'hasOne') {\n\t\t\teventManager.registerUpdatedConnection(realmBlueprint.parent, realmBlueprint.marker.placeholderName)\n\t\t}\n\t\tconst parent = realmBlueprint.parent\n\t\tif (parent) {\n\t\t\teventManager.registerJustUpdated(parent, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t}\n\t\tif (realm.type === 'entityRealm') {\n\t\t\teventManager.registerJustUpdated(realm, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t}\n\t}\n\n\tpublic static purgeStaleListenersAfterIdChange(realm: EntityRealmState) {\n\t\t// Version 1.\n\t\t// The listeners subscribed to a particular entity key so we no longer want to call these.\n\t\t// The only positionally associated listeners are in the blueprint so we re-initialize those.\n\t\t// realm.eventListeners = stateInitializer.initializeEntityEventListenerStore(realmBlueprint)\n\n\t\t// Version 2.\n\t\t// This is version two of this terrible hack. We want to preserve e.g. persistSuccess handlers.\n\n\t\t// Version 3.\n\t\t// The saga continues. We add a condition to exempt top-level realms. That way they can start\n\t\t// propagating changes from the top.\n\t\tconst listeners = realm.eventListeners\n\t\tif (listeners) {\n\t\t\tif (realm.blueprint.parent) {\n\t\t\t\tlisteners.delete({ type: 'update' })\n\t\t\t}\n\t\t\tlisteners.deleteByType('connectionUpdate')\n\t\t}\n\t}\n}\n"],"names":["BindingError","RealmKeyGenerator","PRIMARY_KEY_NAME","EventManager"],"mappings":";;;;;AAUO,MAAM,kBAAkB;AAAA,EAC9B,OAAc,4BAAmC;AAChD,UAAM,IAAIA,cAAAA;AAAAA,MACT;AAAA,IAAA;AAAA,EAEF;AAAA,EAEA,OAAc,iCACb,WACA,iBAC0C;AAC1C,UAAM,qBAAqB,gBAAgB;AAC3C,QAAI,CAAC,oBAAoB;AACxB,YAAM,IAAIA,cAAAA,aAAa,gDAAgD;AAAA,IACxE;AACA,UAAM,iBAAiB,UAAU,iBAAiB,IAAI,kBAAkB;AAExE,QAAI,mBAAmB,QAAW;AACjC,YAAM,IAAIA,cAAAA,aAAa,6CAA6C,kBAAkB,yBAAyB;AAAA,IAChH;AAkBA,WAAO;AAAA,EACR;AAAA,EAEA,OAAc,eACb,WACA,cACA,k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D,WAAW,eAAe,SAAS,UAAU;AAC5C,mBAAa,0BAA0B,eAAe,QAAQ,eAAe,OAAO,eAAe;AAAA,IACpG;AACA,UAAM,SAAS,eAAe;AAC9B,QAAI,QAAQ;AACX,mBAAa,oBAAoB,QAAQA,aAAAA,aAAa,qBAAqB;AAAA,IAC5E;AACA,QAAI,MAAM,SAAS,eAAe;AACjC,mBAAa,oBAAoB,OAAOA,aAAAA,aAAa,qBAAqB;AAAA,IAC3E;AAAA,EACD;AAAA,EAEA,OAAc,iCAAiC,OAAyB;AAYvE,UAAM,YAAY,MAAM;AACxB,QAAI,WAAW;AACd,UAAI,MAAM,UAAU,QAAQ;AAC3B,kBAAU,OAAO,EAAE,MAAM,SAAA,CAAU;AAAA,MACpC;AACA,gBAAU,aAAa,kBAAkB;AAAA,IAC1C;AAAA,EACD;AACD;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"OperationsHelpers.js","sources":["../../../../../../packages/binding-legacy/src/core/operations/OperationsHelpers.ts"],"sourcesContent":["import type { EntityAccessor } from '@contember/binding-common'\nimport type { RuntimeId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { PRIMARY_KEY_NAME } from '@contember/binding-common'\nimport { EventManager } from '../EventManager'\nimport { RealmKeyGenerator } from '../RealmKeyGenerator'\nimport type { EntityRealmState, EntityRealmStateStub, EntityState } from '../state'\nimport type { StateInitializer } from '../StateInitializer'\nimport type { TreeStore } from '../TreeStore'\n\nexport class OperationsHelpers {\n\tpublic static rejectInvalidAccessorTree(): never {\n\t\tthrow new BindingError(\n\t\t\t`The accessor tree does not correspond to the MarkerTree. This should absolutely never happen.`,\n\t\t)\n\t}\n\n\tpublic static resolveAndPrepareEntityToConnect(\n\t\ttreeStore: TreeStore,\n\t\tentityToConnect: EntityAccessor,\n\t): EntityRealmState | EntityRealmStateStub {\n\t\tconst entityToConnectKey = entityToConnect.key\n\t\tif (!entityToConnectKey) {\n\t\t\tthrow new BindingError(`Attempting to connect an entity without a key.`)\n\t\t}\n\t\tconst stateToConnect = treeStore.entityRealmStore.get(entityToConnectKey)\n\n\t\tif (stateToConnect === undefined) {\n\t\t\tthrow new BindingError(`Attempting to connect an entity with key '${entityToConnectKey}' but it doesn't exist.`)\n\t\t}\n\t\t// TODO This is commented out for now in order to at least somewhat mitigate the limitations of dealing with\n\t\t// \t\tinverse relations. However, once that has been addressed systemically, this code needs to be re-enabled.\n\t\t// if (!stateToConnect.entity.id.existsOnServer) {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Attempting to connect an entity with key '${stateToConnect.realmKey}' that ` +\n\t\t// \t\t\t`doesn't exist on server. That is currently impossible.`, // At least for now.\n\t\t// \t)\n\t\t// }\n\n\t\t// TODO\n\t\t// if (stateToConnect.entity.isScheduledForDeletion) {\n\t\t// \t// As far as the other realms are concerned, this entity is deleted. We don't want to just make it re-appear\n\t\t// \t// for them just because some other random relation decided to connect it.\n\t\t// \tstateToConnect.entity.realms.clear()\n\t\t// \tstateToConnect.entity.isScheduledForDeletion = false\n\t\t// }\n\n\t\treturn stateToConnect\n\t}\n\n\tpublic static changeEntityId(\n\t\ttreeStore: TreeStore,\n\t\teventManager: EventManager,\n\t\tstateInitializer: StateInitializer,\n\t\tentity: EntityState,\n\t\tnewId: RuntimeId,\n\t) {\n\t\tconst previousId = entity.id\n\n\t\ttreeStore.entityStore.delete(previousId.uniqueValue)\n\t\ttreeStore.entityStore.set(newId.uniqueValue, entity)\n\n\t\tlet counter = 0\n\t\tfor (const realm of entity.realms.values()) {\n\t\t\tthis.changeRealmId(treeStore, eventManager, stateInitializer, realm, newId)\n\n\t\t\tif (++counter === entity.realms.size) {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\tentity.hasIdSetInStone = true\n\t\tentity.id = newId\n\t}\n\n\tpublic static changeRealmId(\n\t\ttreeStore: TreeStore,\n\t\teventManager: EventManager,\n\t\tstateInitializer: StateInitializer,\n\t\trealm: EntityRealmState | EntityRealmStateStub,\n\t\tnewId: RuntimeId,\n\t) {\n\t\tconst oldEntity = realm.entity\n\t\tconst oldRealmKey = realm.realmKey\n\t\tconst oldId = oldEntity.id\n\n\t\tconst realmBlueprint = realm.blueprint\n\n\t\tconst newEntity = stateInitializer.initializeEntityState(newId, oldEntity.entityName)\n\t\tconst newRealmKey = RealmKeyGenerator.getRealmKey(newId, realmBlueprint)\n\n\t\trealm.realmKey = newRealmKey\n\t\trealm.entity = newEntity\n\n\t\t// This may still actually be necessary if we're going from a client to server generated uuid.\n\t\t// They both may have the same value but still be different.\n\t\tnewEntity.id = newId\n\t\toldEntity.realms.delete(oldRealmKey)\n\t\tnewEntity.realms.set(newRealmKey, realm)\n\n\t\tif (realm.type === 'entityRealm') {\n\t\t\tconst childIdState = realm.children.get(PRIMARY_KEY_NAME)\n\t\t\tif (childIdState?.type === 'field') {\n\t\t\t\tchildIdState.value = newId.value\n\t\t\t\teventManager.registerJustUpdated(childIdState, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t}\n\n\t\t\tthis.purgeStaleListenersAfterIdChange(realm)\n\t\t}\n\n\t\tif (oldEntity.realms.size === 0) {\n\t\t\ttreeStore.disposeOfEntity(oldEntity)\n\t\t}\n\n\t\ttreeStore.entityRealmStore.delete(oldRealmKey)\n\t\ttreeStore.entityRealmStore.set(newRealmKey, realm)\n\n\t\tif (realmBlueprint.type === 'listEntity') {\n\t\t\tconst list = realmBlueprint.parent\n\t\t\tlist.children.changeKey(oldId.value, newId.value) // 😎\n\t\t} else if (realmBlueprint.type === 'hasOne') {\n\t\t\teventManager.registerUpdatedConnection(realmBlueprint.parent, realmBlueprint.marker.placeholderName)\n\t\t}\n\t\tconst parent = realmBlueprint.parent\n\t\tif (parent) {\n\t\t\teventManager.registerJustUpdated(parent, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t}\n\t\tif (realm.type === 'entityRealm') {\n\t\t\teventManager.registerJustUpdated(realm, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t}\n\t}\n\n\tpublic static purgeStaleListenersAfterIdChange(realm: EntityRealmState) {\n\t\t// Version 1.\n\t\t// The listeners subscribed to a particular entity key so we no longer want to call these.\n\t\t// The only positionally associated listeners are in the blueprint so we re-initialize those.\n\t\t// realm.eventListeners = stateInitializer.initializeEntityEventListenerStore(realmBlueprint)\n\n\t\t// Version 2.\n\t\t// This is version two of this terrible hack. We want to preserve e.g. persistSuccess handlers.\n\n\t\t// Version 3.\n\t\t// The saga continues. We add a condition to exempt top-level realms. That way they can start\n\t\t// propagating changes from the top.\n\t\tconst listeners = realm.eventListeners\n\t\tif (listeners) {\n\t\t\tif (realm.blueprint.parent) {\n\t\t\t\tlisteners.delete({ type: 'update' })\n\t\t\t}\n\t\t\tlisteners.deleteByType('connectionUpdate')\n\t\t}\n\t}\n}\n"],"names":[],"mappings":";;;AAUO,MAAM,kBAAkB;AAAA,EAC9B,OAAc,4BAAmC;AAChD,UAAM,IAAI;AAAA,MACT;AAAA,IAAA;AAAA,EAEF;AAAA,EAEA,OAAc,iCACb,WACA,iBAC0C;AAC1C,UAAM,qBAAqB,gBAAgB;AAC3C,QAAI,CAAC,oBAAoB;AACxB,YAAM,IAAI,aAAa,gDAAgD;AAAA,IACxE;AACA,UAAM,iBAAiB,UAAU,iBAAiB,IAAI,kBAAkB;AAExE,QAAI,mBAAmB,QAAW;AACjC,YAAM,IAAI,aAAa,6CAA6C,kBAAkB,yBAAyB;AAAA,IAChH;AAkBA,WAAO;AAAA,EACR;AAAA,EAEA,OAAc,eACb,WACA,cACA,kBACA,QACA,OACC;AACD,UAAM,aAAa,OAAO;AAE1B,cAAU,YAAY,OAAO,WAAW,WAAW;AACnD,cAAU,YAAY,IAAI,MAAM,aAAa,MAAM;AAEnD,QAAI,UAAU;AACd,eAAW,SAAS,OAAO,OAAO,OAAA,GAAU;AAC3C,WAAK,cAAc,WAAW,cAAc,kBAAkB,OAAO,KAAK;AAE1E,UAAI,EAAE,YAAY,OAAO,OAAO,MAAM;AACrC;AAAA,MACD;AAAA,IACD;A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UAAM,YAAY,MAAM;AACxB,QAAI,WAAW;AACd,UAAI,MAAM,UAAU,QAAQ;AAC3B,kBAAU,OAAO,EAAE,MAAM,SAAA,CAAU;AAAA,MACpC;AACA,gBAAU,aAAa,kBAAkB;AAAA,IAC1C;AAAA,EACD;AACD;"}
1
+ {"version":3,"file":"OperationsHelpers.js","sources":["../../../../../../packages/binding-legacy/src/core/operations/OperationsHelpers.ts"],"sourcesContent":["import type { EntityAccessor } from '@contember/binding-common'\nimport type { RuntimeId } from '@contember/binding-common'\nimport { BindingError } from '@contember/binding-common'\nimport { PRIMARY_KEY_NAME } from '@contember/binding-common'\nimport { EventManager } from '../EventManager.js'\nimport { RealmKeyGenerator } from '../RealmKeyGenerator.js'\nimport type { EntityRealmState, EntityRealmStateStub, EntityState } from '../state/index.js'\nimport type { StateInitializer } from '../StateInitializer.js'\nimport type { TreeStore } from '../TreeStore.js'\n\nexport class OperationsHelpers {\n\tpublic static rejectInvalidAccessorTree(): never {\n\t\tthrow new BindingError(\n\t\t\t`The accessor tree does not correspond to the MarkerTree. This should absolutely never happen.`,\n\t\t)\n\t}\n\n\tpublic static resolveAndPrepareEntityToConnect(\n\t\ttreeStore: TreeStore,\n\t\tentityToConnect: EntityAccessor,\n\t): EntityRealmState | EntityRealmStateStub {\n\t\tconst entityToConnectKey = entityToConnect.key\n\t\tif (!entityToConnectKey) {\n\t\t\tthrow new BindingError(`Attempting to connect an entity without a key.`)\n\t\t}\n\t\tconst stateToConnect = treeStore.entityRealmStore.get(entityToConnectKey)\n\n\t\tif (stateToConnect === undefined) {\n\t\t\tthrow new BindingError(`Attempting to connect an entity with key '${entityToConnectKey}' but it doesn't exist.`)\n\t\t}\n\t\t// TODO This is commented out for now in order to at least somewhat mitigate the limitations of dealing with\n\t\t// \t\tinverse relations. However, once that has been addressed systemically, this code needs to be re-enabled.\n\t\t// if (!stateToConnect.entity.id.existsOnServer) {\n\t\t// \tthrow new BindingError(\n\t\t// \t\t`Attempting to connect an entity with key '${stateToConnect.realmKey}' that ` +\n\t\t// \t\t\t`doesn't exist on server. That is currently impossible.`, // At least for now.\n\t\t// \t)\n\t\t// }\n\n\t\t// TODO\n\t\t// if (stateToConnect.entity.isScheduledForDeletion) {\n\t\t// \t// As far as the other realms are concerned, this entity is deleted. We don't want to just make it re-appear\n\t\t// \t// for them just because some other random relation decided to connect it.\n\t\t// \tstateToConnect.entity.realms.clear()\n\t\t// \tstateToConnect.entity.isScheduledForDeletion = false\n\t\t// }\n\n\t\treturn stateToConnect\n\t}\n\n\tpublic static changeEntityId(\n\t\ttreeStore: TreeStore,\n\t\teventManager: EventManager,\n\t\tstateInitializer: StateInitializer,\n\t\tentity: EntityState,\n\t\tnewId: RuntimeId,\n\t) {\n\t\tconst previousId = entity.id\n\n\t\ttreeStore.entityStore.delete(previousId.uniqueValue)\n\t\ttreeStore.entityStore.set(newId.uniqueValue, entity)\n\n\t\tlet counter = 0\n\t\tfor (const realm of entity.realms.values()) {\n\t\t\tthis.changeRealmId(treeStore, eventManager, stateInitializer, realm, newId)\n\n\t\t\tif (++counter === entity.realms.size) {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\tentity.hasIdSetInStone = true\n\t\tentity.id = newId\n\t}\n\n\tpublic static changeRealmId(\n\t\ttreeStore: TreeStore,\n\t\teventManager: EventManager,\n\t\tstateInitializer: StateInitializer,\n\t\trealm: EntityRealmState | EntityRealmStateStub,\n\t\tnewId: RuntimeId,\n\t) {\n\t\tconst oldEntity = realm.entity\n\t\tconst oldRealmKey = realm.realmKey\n\t\tconst oldId = oldEntity.id\n\n\t\tconst realmBlueprint = realm.blueprint\n\n\t\tconst newEntity = stateInitializer.initializeEntityState(newId, oldEntity.entityName)\n\t\tconst newRealmKey = RealmKeyGenerator.getRealmKey(newId, realmBlueprint)\n\n\t\trealm.realmKey = newRealmKey\n\t\trealm.entity = newEntity\n\n\t\t// This may still actually be necessary if we're going from a client to server generated uuid.\n\t\t// They both may have the same value but still be different.\n\t\tnewEntity.id = newId\n\t\toldEntity.realms.delete(oldRealmKey)\n\t\tnewEntity.realms.set(newRealmKey, realm)\n\n\t\tif (realm.type === 'entityRealm') {\n\t\t\tconst childIdState = realm.children.get(PRIMARY_KEY_NAME)\n\t\t\tif (childIdState?.type === 'field') {\n\t\t\t\tchildIdState.value = newId.value\n\t\t\t\teventManager.registerJustUpdated(childIdState, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t\t}\n\n\t\t\tthis.purgeStaleListenersAfterIdChange(realm)\n\t\t}\n\n\t\tif (oldEntity.realms.size === 0) {\n\t\t\ttreeStore.disposeOfEntity(oldEntity)\n\t\t}\n\n\t\ttreeStore.entityRealmStore.delete(oldRealmKey)\n\t\ttreeStore.entityRealmStore.set(newRealmKey, realm)\n\n\t\tif (realmBlueprint.type === 'listEntity') {\n\t\t\tconst list = realmBlueprint.parent\n\t\t\tlist.children.changeKey(oldId.value, newId.value) // 😎\n\t\t} else if (realmBlueprint.type === 'hasOne') {\n\t\t\teventManager.registerUpdatedConnection(realmBlueprint.parent, realmBlueprint.marker.placeholderName)\n\t\t}\n\t\tconst parent = realmBlueprint.parent\n\t\tif (parent) {\n\t\t\teventManager.registerJustUpdated(parent, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t}\n\t\tif (realm.type === 'entityRealm') {\n\t\t\teventManager.registerJustUpdated(realm, EventManager.NO_CHANGES_DIFFERENCE)\n\t\t}\n\t}\n\n\tpublic static purgeStaleListenersAfterIdChange(realm: EntityRealmState) {\n\t\t// Version 1.\n\t\t// The listeners subscribed to a particular entity key so we no longer want to call these.\n\t\t// The only positionally associated listeners are in the blueprint so we re-initialize those.\n\t\t// realm.eventListeners = stateInitializer.initializeEntityEventListenerStore(realmBlueprint)\n\n\t\t// Version 2.\n\t\t// This is version two of this terrible hack. We want to preserve e.g. persistSuccess handlers.\n\n\t\t// Version 3.\n\t\t// The saga continues. We add a condition to exempt top-level realms. That way they can start\n\t\t// propagating changes from the top.\n\t\tconst listeners = realm.eventListeners\n\t\tif (listeners) {\n\t\t\tif (realm.blueprint.parent) {\n\t\t\t\tlisteners.delete({ type: 'update' })\n\t\t\t}\n\t\t\tlisteners.deleteByType('connectionUpdate')\n\t\t}\n\t}\n}\n"],"names":[],"mappings":";;;AAUO,MAAM,kBAAkB;AAAA,EAC9B,OAAc,4BAAmC;AAChD,UAAM,IAAI;AAAA,MACT;AAAA,IAAA;AAAA,EAEF;AAAA,EAEA,OAAc,iCACb,WACA,iBAC0C;AAC1C,UAAM,qBAAqB,gBAAgB;AAC3C,QAAI,CAAC,oBAAoB;AACxB,YAAM,IAAI,aAAa,gDAAgD;AAAA,IACxE;AACA,UAAM,iBAAiB,UAAU,iBAAiB,IAAI,kBAAkB;AAExE,QAAI,mBAAmB,QAAW;AACjC,YAAM,IAAI,aAAa,6CAA6C,kBAAkB,yBAAyB;AAAA,IAChH;AAkBA,WAAO;AAAA,EACR;AAAA,EAEA,OAAc,eACb,WACA,cACA,kBACA,QACA,OACC;AACD,UAAM,aAAa,OAAO;AAE1B,cAAU,YAAY,OAAO,WAAW,WAAW;AACnD,cAAU,YAAY,IAAI,MAAM,aAAa,MAAM;AAEnD,QAAI,UAAU;AACd,eAAW,SAAS,OAAO,OAAO,OAAA,GAAU;AAC3C,WAAK,cAAc,WAAW,cAAc,kBAAkB,OAAO,KAAK;AAE1E,UAAI,EAAE,YAAY,OAAO,OAAO,MAAM;AACrC;AAAA,MACD;AAAA,IACD;AAEA,WAAO,kBAAkB;AACzB,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,OAAc,cACb,WACA,cACA,kBACA,OACA,OACC;AACD,UAAM,YAAY,MAAM;AACxB,UAAM,cAAc,MAAM;AAC1B,UAAM,QAAQ,UAAU;AAExB,UAAM,iBAAiB,MAAM;AAE7B,UAAM,YAAY,iBAAiB,sBAAsB,OAAO,UAAU,UAAU;AACpF,UAAM,cAAc,kBAAkB,YAAY,OAAO,cAAc;AAEvE,UAAM,WAAW;AACjB,UAAM,SAAS;AAIf,cAAU,KAAK;AACf,cAAU,OAAO,OAAO,WAAW;AACnC,cAAU,OAAO,IAAI,aAAa,KAAK;AAEvC,QAAI,MAAM,SAAS,eAAe;AACjC,YAAM,eAAe,MAAM,SAAS,IAAI,gBAAgB;AACxD,UAAI,cAAc,SAAS,SAAS;AACnC,qBAAa,QAAQ,MAAM;AAC3B,qBAAa,oBAAoB,cAAc,aAAa,qBAAqB;AAAA,MAClF;AAEA,WAAK,iCAAiC,KAAK;AAAA,IAC5C;AAEA,QAAI,UAAU,OAAO,SAAS,GAAG;AAChC,gBAAU,gBAAgB,SAAS;AAAA,IACpC;AAEA,cAAU,iBAAiB,OAAO,WAAW;AAC7C,cAAU,iBAAiB,IAAI,aAAa,KAAK;AAEjD,QAAI,eAAe,SAAS,cAAc;AACzC,YAAM,OAAO,eAAe;AAC5B,WAAK,SAAS,UAAU,MAAM,OAAO,MAAM,KAAK;AAAA,IACjD,WAAW,eAAe,SAAS,UAAU;AAC5C,mBAAa,0BAA0B,eAAe,QAAQ,eAAe,OAAO,eAAe;AAAA,IACpG;AACA,UAAM,SAAS,eAAe;AAC9B,QAAI,QAAQ;AACX,mBAAa,oBAAoB,QAAQ,aAAa,qBAAqB;AAAA,IAC5E;AACA,QAAI,MAAM,SAAS,eAAe;AACjC,mBAAa,oBAAoB,OAAO,aAAa,qBAAqB;AAAA,IAC3E;AAAA,EACD;AAAA,EAEA,OAAc,iCAAiC,OAAyB;AAYvE,UAAM,YAAY,MAAM;AACxB,QAAI,WAAW;AACd,UAAI,MAAM,UAAU,QAAQ;AAC3B,kBAAU,OAAO,EAAE,MAAM,SAAA,CAAU;AAAA,MACpC;AACA,gBAAU,aAAa,kBAAkB;AAAA,IAC1C;AAAA,EACD;AACD;"}
@@ -1 +1 @@
1
- {"version":3,"file":"StateIterator.cjs","sources":["../../../../../../packages/binding-legacy/src/core/state/StateIterator.ts"],"sourcesContent":["import type { FieldValue, PlaceholderName } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { TreeStore } from '../TreeStore'\nimport type { EntityListState } from './EntityListState'\nimport type { EntityRealmState } from './EntityRealmState'\nimport type { FieldState } from './FieldState'\nimport { getEntityMarker } from './getEntityMarker'\nimport type { RootStateNode, StateINode, StateNode } from './StateNode'\n\nexport class StateIterator {\n\tpublic static *eachSiblingRealm(\n\t\tstate: EntityRealmState,\n\t\trelevantPlaceholder?: PlaceholderName,\n\t): IterableIterator<EntityRealmState> {\n\t\tfor (let [realmKey, realm] of state.entity.realms) {\n\t\t\tif (realm.type === 'entityRealmStub') {\n\t\t\t\tif (relevantPlaceholder === undefined || getEntityMarker(realm).fields.placeholders.has(relevantPlaceholder)) {\n\t\t\t\t\trealm.getAccessor()\n\t\t\t\t\trealm = state.entity.realms.get(realmKey) as EntityRealmState\n\t\t\t\t} else {\n\t\t\t\t\t// This realm is irrelevant. No need to force-initialize it.\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t}\n\t\t\tyield realm\n\t\t}\n\t}\n\n\t// Note that this only yields siblings with the same placeholderName!\n\tpublic static *eachSiblingRealmChild<Value extends FieldValue, S extends EntityListState | FieldState<Value>>(\n\t\ttreeStore: TreeStore,\n\t\tstate: S & StateNode<Value>,\n\t): IterableIterator<S> {\n\t\tif (state.type === 'entityList' && state.blueprint.parent === undefined) {\n\t\t\t// Top-level entity list\n\t\t\tfor (const [, rootStates] of treeStore.subTreeStatesByRoot) {\n\t\t\t\tconst rootList = rootStates.get(state.blueprint.marker.placeholderName)\n\t\t\t\tif (rootList?.type === 'entityList') {\n\t\t\t\t\tyield rootList as S\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn\n\t\t}\n\n\t\tlet closestEntityRealm: EntityRealmState\n\t\tlet placeholderName: PlaceholderName\n\n\t\tif (state.type === 'field') {\n\t\t\tclosestEntityRealm = state.parent\n\t\t\tplaceholderName = state.placeholderName\n\t\t} else if (state.type === 'entityList') {\n\t\t\tclosestEntityRealm = state.blueprint.parent! // The parent-less case is handled above. Hence the assertion.\n\t\t\tplaceholderName = state.blueprint.marker.placeholderName\n\t\t} else {\n\t\t\treturn assertNever(state)\n\t\t}\n\n\t\tfor (const realm of this.eachSiblingRealm(closestEntityRealm, placeholderName)) {\n\t\t\tconst child = realm.children.get(placeholderName)\n\t\t\tif (child === undefined) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (child.type === state.type) {\n\t\t\t\tyield child as S\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic static *depthFirstAllNodes(root: StateNode): IterableIterator<StateNode> {\n\t\tswitch (root.type) {\n\t\t\tcase 'entityRealm':\n\t\t\tcase 'entityList':\n\t\t\t\tfor (const childState of root.children.values()) {\n\t\t\t\t\tswitch (childState.type) {\n\t\t\t\t\t\tcase 'field':\n\t\t\t\t\t\tcase 'entityRealm':\n\t\t\t\t\t\tcase 'entityList':\n\t\t\t\t\t\t\tyield* this.depthFirstAllNodes(childState)\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\tcase 'entityRealmStub':\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\treturn assertNever(childState)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak\n\t\t\tcase 'field':\n\t\t\t\tbreak\n\t\t\tdefault:\n\t\t\t\treturn assertNever(root)\n\t\t}\n\t\tyield root\n\t}\n\n\tpublic static *depthFirstINodes(\n\t\troot: StateINode,\n\t\tmatch?: (iNode: StateINode) => boolean,\n\t): IterableIterator<StateINode> {\n\t\tswitch (root.type) {\n\t\t\tcase 'entityRealm':\n\t\t\tcase 'entityList':\n\t\t\t\tfor (const childState of root.children.values()) {\n\t\t\t\t\tif (childState.type === 'entityRealm' || childState.type === 'entityList') {\n\t\t\t\t\t\tyield* this.depthFirstINodes(childState, match)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak\n\t\t\tdefault:\n\t\t\t\treturn assertNever(root)\n\t\t}\n\t\tif (match === undefined || match(root)) {\n\t\t\tyield root\n\t\t}\n\t}\n\n\tpublic static *eachRootState(treeStore: TreeStore): IterableIterator<[PlaceholderName, RootStateNode]> {\n\t\tfor (const rootStates of treeStore.subTreeStatesByRoot.values()) {\n\t\t\tyield* rootStates.entries()\n\t\t}\n\t}\n}\n"],"names":["getEntityMarker","assertNever"],"mappings":";;;;AASO,MAAM,cAAc;AAAA,EAC1B,QAAe,iBACd,OACA,qBACqC;AACrC,aAAS,CAAC,UAAU,KAAK,KAAK,MAAM,OAAO,QAAQ;AAClD,UAAI,MAAM,SAAS,mBAAmB;AACrC,YAAI,wBAAwB,UAAaA,gBAAAA,gBAAgB,KAAK,EAAE,OAAO,aAAa,IAAI,mBAAmB,GAAG;AAC7G,gBAAM,YAAA;AACN,kBAAQ,MAAM,OAAO,OAAO,IAAI,QAAQ;AAAA,QACzC,OAAO;AAEN;AAAA,QACD;AAAA,MACD;AACA,YAAM;AAAA,IACP;AAAA,EACD;AAAA;AAAA,EAGA,QAAe,sBACd,WACA,OACsB;AACtB,QAAI,MAAM,SAAS,gBAAgB,MAAM,UAAU,WAAW,QAAW;AAExE,iBAAW,CAAA,EAAG,UAAU,KAAK,UAAU,qBAAqB;AAC3D,cAAM,WAAW,WAAW,IAAI,MAAM,UAAU,OAAO,eAAe;AACtE,YAAI,UAAU,SAAS,cAAc;AACpC,gBAAM;AAAA,QACP;AAAA,MACD;AACA;AAAA,IACD;AAEA,QAAI;AACJ,QAAI;AAEJ,QAAI,MAAM,SAAS,SAAS;AAC3B,2BAAqB,MAAM;AAC3B,wBAAkB,MAAM;AAAA,IACzB,WAAW,MAAM,SAAS,cAAc;AACvC,2BAAqB,MAAM,UAAU;AACrC,wBAAkB,MAAM,UAAU,OAAO;AAAA,IAC1C,OAAO;AACN,aAAOC,cAAAA,YAAY,KAAK;AAAA,IACzB;AAEA,eAAW,SAAS,KAAK,iBAAiB,oBAAoB,eAAe,GAAG;AAC/E,YAAM,QAAQ,MAAM,SAAS,IAAI,eAAe;AAChD,UAAI,UAAU,QAAW;AACxB;AAAA,MACD;AACA,UAAI,MAAM,SAAS,MAAM,MAAM;AAC9B,cAAM;AAAA,MACP;AAAA,IACD;AAAA,EACD;AAAA,EAEA,QAAe,mBAAmB,MAA8C;AAC/E,YAAQ,KAAK,MAAA;AAAA,MACZ,KAAK;AAAA,MACL,KAAK;AACJ,mBAAW,cAAc,KAAK,SAAS,OAAA,GAAU;AAChD,kBAAQ,WAAW,MAAA;AAAA,YAClB,KAAK;AAAA,YACL,KAAK;AAAA,YACL,KAAK;AACJ,qBAAO,KAAK,mBAAmB,UAAU;AACzC;AAAA,YACD,KAAK;AACJ;AAAA,YACD;AACC,qBAAOA,cAAAA,YAAY,UAAU;AAAA,UAAA;AAAA,QAEhC;AACA;AAAA,MACD,KAAK;AACJ;AAAA,MACD;AACC,eAAOA,cAAAA,YAAY,IAAI;AAAA,IAAA;AAEzB,UAAM;AAAA,EACP;AAAA,EAEA,QAAe,iBACd,MACA,OAC+B;AAC/B,YAAQ,KAAK,MAAA;AAAA,MACZ,KAAK;AAAA,MACL,KAAK;AACJ,mBAAW,cAAc,KAAK,SAAS,OAAA,GAAU;AAChD,cAAI,WAAW,SAAS,iBAAiB,WAAW,SAAS,cAAc;AAC1E,mBAAO,KAAK,iBAAiB,YAAY,KAAK;AAAA,UAC/C;AAAA,QACD;AACA;AAAA,MACD;AACC,eAAOA,cAAAA,YAAY,IAAI;AAAA,IAAA;AAEzB,QAAI,UAAU,UAAa,MAAM,IAAI,GAAG;AACvC,YAAM;AAAA,IACP;AAAA,EACD;AAAA,EAEA,QAAe,cAAc,WAA0E;AACtG,eAAW,cAAc,UAAU,oBAAoB,OAAA,GAAU;AAChE,aAAO,WAAW,QAAA;AAAA,IACnB;AAAA,EACD;AACD;;"}
1
+ {"version":3,"file":"StateIterator.cjs","sources":["../../../../../../packages/binding-legacy/src/core/state/StateIterator.ts"],"sourcesContent":["import type { FieldValue, PlaceholderName } from '@contember/binding-common'\nimport { assertNever } from '@contember/binding-common'\nimport type { TreeStore } from '../TreeStore.js'\nimport type { EntityListState } from './EntityListState.js'\nimport type { EntityRealmState } from './EntityRealmState.js'\nimport type { FieldState } from './FieldState.js'\nimport { getEntityMarker } from './getEntityMarker.js'\nimport type { RootStateNode, StateINode, StateNode } from './StateNode.js'\n\nexport class StateIterator {\n\tpublic static *eachSiblingRealm(\n\t\tstate: EntityRealmState,\n\t\trelevantPlaceholder?: PlaceholderName,\n\t): IterableIterator<EntityRealmState> {\n\t\tfor (let [realmKey, realm] of state.entity.realms) {\n\t\t\tif (realm.type === 'entityRealmStub') {\n\t\t\t\tif (relevantPlaceholder === undefined || getEntityMarker(realm).fields.placeholders.has(relevantPlaceholder)) {\n\t\t\t\t\trealm.getAccessor()\n\t\t\t\t\trealm = state.entity.realms.get(realmKey) as EntityRealmState\n\t\t\t\t} else {\n\t\t\t\t\t// This realm is irrelevant. No need to force-initialize it.\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t}\n\t\t\tyield realm\n\t\t}\n\t}\n\n\t// Note that this only yields siblings with the same placeholderName!\n\tpublic static *eachSiblingRealmChild<Value extends FieldValue, S extends EntityListState | FieldState<Value>>(\n\t\ttreeStore: TreeStore,\n\t\tstate: S & StateNode<Value>,\n\t): IterableIterator<S> {\n\t\tif (state.type === 'entityList' && state.blueprint.parent === undefined) {\n\t\t\t// Top-level entity list\n\t\t\tfor (const [, rootStates] of treeStore.subTreeStatesByRoot) {\n\t\t\t\tconst rootList = rootStates.get(state.blueprint.marker.placeholderName)\n\t\t\t\tif (rootList?.type === 'entityList') {\n\t\t\t\t\tyield rootList as S\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn\n\t\t}\n\n\t\tlet closestEntityRealm: EntityRealmState\n\t\tlet placeholderName: PlaceholderName\n\n\t\tif (state.type === 'field') {\n\t\t\tclosestEntityRealm = state.parent\n\t\t\tplaceholderName = state.placeholderName\n\t\t} else if (state.type === 'entityList') {\n\t\t\tclosestEntityRealm = state.blueprint.parent! // The parent-less case is handled above. Hence the assertion.\n\t\t\tplaceholderName = state.blueprint.marker.placeholderName\n\t\t} else {\n\t\t\treturn assertNever(state)\n\t\t}\n\n\t\tfor (const realm of this.eachSiblingRealm(closestEntityRealm, placeholderName)) {\n\t\t\tconst child = realm.children.get(placeholderName)\n\t\t\tif (child === undefined) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (child.type === state.type) {\n\t\t\t\tyield child as S\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic static *depthFirstAllNodes(root: StateNode): IterableIterator<StateNode> {\n\t\tswitch (root.type) {\n\t\t\tcase 'entityRealm':\n\t\t\tcase 'entityList':\n\t\t\t\tfor (const childState of root.children.values()) {\n\t\t\t\t\tswitch (childState.type) {\n\t\t\t\t\t\tcase 'field':\n\t\t\t\t\t\tcase 'entityRealm':\n\t\t\t\t\t\tcase 'entityList':\n\t\t\t\t\t\t\tyield* this.depthFirstAllNodes(childState)\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\tcase 'entityRealmStub':\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\treturn assertNever(childState)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak\n\t\t\tcase 'field':\n\t\t\t\tbreak\n\t\t\tdefault:\n\t\t\t\treturn assertNever(root)\n\t\t}\n\t\tyield root\n\t}\n\n\tpublic static *depthFirstINodes(\n\t\troot: StateINode,\n\t\tmatch?: (iNode: StateINode) => boolean,\n\t): IterableIterator<StateINode> {\n\t\tswitch (root.type) {\n\t\t\tcase 'entityRealm':\n\t\t\tcase 'entityList':\n\t\t\t\tfor (const childState of root.children.values()) {\n\t\t\t\t\tif (childState.type === 'entityRealm' || childState.type === 'entityList') {\n\t\t\t\t\t\tyield* this.depthFirstINodes(childState, match)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak\n\t\t\tdefault:\n\t\t\t\treturn assertNever(root)\n\t\t}\n\t\tif (match === undefined || match(root)) {\n\t\t\tyield root\n\t\t}\n\t}\n\n\tpublic static *eachRootState(treeStore: TreeStore): IterableIterator<[PlaceholderName, RootStateNode]> {\n\t\tfor (const rootStates of treeStore.subTreeStatesByRoot.values()) {\n\t\t\tyield* rootStates.entries()\n\t\t}\n\t}\n}\n"],"names":["getEntityMarker","assertNever"],"mappings":";;;;AASO,MAAM,cAAc;AAAA,EAC1B,QAAe,iBACd,OACA,qBACqC;AACrC,aAAS,CAAC,UAAU,KAAK,KAAK,MAAM,OAAO,QAAQ;AAClD,UAAI,MAAM,SAAS,mBAAmB;AACrC,YAAI,wBAAwB,UAAaA,gBAAAA,gBAAgB,KAAK,EAAE,OAAO,aAAa,IAAI,mBAAmB,GAAG;AAC7G,gBAAM,YAAA;AACN,kBAAQ,MAAM,OAAO,OAAO,IAAI,QAAQ;AAAA,QACzC,OAAO;AAEN;AAAA,QACD;AAAA,MACD;AACA,YAAM;AAAA,IACP;AAAA,EACD;AAAA;AAAA,EAGA,QAAe,sBACd,WACA,OACsB;AACtB,QAAI,MAAM,SAAS,gBAAgB,MAAM,UAAU,WAAW,QAAW;AAExE,iBAAW,CAAA,EAAG,UAAU,KAAK,UAAU,qBAAqB;AAC3D,cAAM,WAAW,WAAW,IAAI,MAAM,UAAU,OAAO,eAAe;AACtE,YAAI,UAAU,SAAS,cAAc;AACpC,gBAAM;AAAA,QACP;AAAA,MACD;AACA;AAAA,IACD;AAEA,QAAI;AACJ,QAAI;AAEJ,QAAI,MAAM,SAAS,SAAS;AAC3B,2BAAqB,MAAM;AAC3B,wBAAkB,MAAM;AAAA,IACzB,WAAW,MAAM,SAAS,cAAc;AACvC,2BAAqB,MAAM,UAAU;AACrC,wBAAkB,MAAM,UAAU,OAAO;AAAA,IAC1C,OAAO;AACN,aAAOC,cAAAA,YAAY,KAAK;AAAA,IACzB;AAEA,eAAW,SAAS,KAAK,iBAAiB,oBAAoB,eAAe,GAAG;AAC/E,YAAM,QAAQ,MAAM,SAAS,IAAI,eAAe;AAChD,UAAI,UAAU,QAAW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