@dxos/echo 0.8.4-main.bc674ce → 0.8.4-main.bd9b33e6c8

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 (536) hide show
  1. package/README.md +2 -2
  2. package/dist/lib/neutral/Annotation.mjs +8 -3
  3. package/dist/lib/neutral/Database.mjs +34 -6
  4. package/dist/lib/neutral/Entity.mjs +18 -10
  5. package/dist/lib/neutral/Err.mjs +3 -1
  6. package/dist/lib/neutral/Extension.mjs +18 -0
  7. package/dist/lib/neutral/Extension.mjs.map +7 -0
  8. package/dist/lib/neutral/Feed.mjs +45 -0
  9. package/dist/lib/neutral/Feed.mjs.map +7 -0
  10. package/dist/lib/neutral/Filter.mjs +17 -10
  11. package/dist/lib/neutral/Format.mjs +3 -3
  12. package/dist/lib/neutral/JsonSchema.mjs +9 -9
  13. package/dist/lib/neutral/Obj.mjs +35 -15
  14. package/dist/lib/neutral/Order.mjs +1 -1
  15. package/dist/lib/neutral/Query.mjs +19 -9
  16. package/dist/lib/neutral/Ref.mjs +9 -7
  17. package/dist/lib/neutral/Relation.mjs +20 -11
  18. package/dist/lib/neutral/SchemaRegistry.mjs +1 -1
  19. package/dist/lib/neutral/Tag.mjs +13 -10
  20. package/dist/lib/neutral/Type.mjs +13 -23
  21. package/dist/lib/neutral/{chunk-HMOXCO27.mjs → chunk-35O3JYTV.mjs} +8 -3
  22. package/dist/lib/neutral/chunk-35O3JYTV.mjs.map +7 -0
  23. package/dist/lib/neutral/chunk-4BT7QVIT.mjs +34 -0
  24. package/dist/lib/neutral/chunk-4BT7QVIT.mjs.map +7 -0
  25. package/dist/lib/neutral/chunk-4O4AV6CL.mjs +787 -0
  26. package/dist/lib/neutral/chunk-4O4AV6CL.mjs.map +7 -0
  27. package/dist/lib/neutral/{chunk-62DWJ6CE.mjs → chunk-B5OXLWZL.mjs} +14 -14
  28. package/dist/lib/neutral/chunk-B5OXLWZL.mjs.map +7 -0
  29. package/dist/lib/neutral/{chunk-NWUO5FKZ.mjs → chunk-B7HTUIZA.mjs} +8 -2
  30. package/dist/lib/neutral/chunk-B7HTUIZA.mjs.map +7 -0
  31. package/dist/lib/neutral/{chunk-4L6DOFXP.mjs → chunk-BNCCGLJN.mjs} +1 -1
  32. package/dist/lib/neutral/{chunk-4L6DOFXP.mjs.map → chunk-BNCCGLJN.mjs.map} +1 -1
  33. package/dist/lib/neutral/{chunk-3ZEAZF7T.mjs → chunk-EIX4KLES.mjs} +8 -4
  34. package/dist/lib/neutral/chunk-EIX4KLES.mjs.map +7 -0
  35. package/dist/lib/neutral/chunk-FLSFS6UB.mjs +117 -0
  36. package/dist/lib/neutral/chunk-FLSFS6UB.mjs.map +7 -0
  37. package/dist/lib/neutral/{chunk-VR4A4WGG.mjs → chunk-FXQ52LDX.mjs} +72 -23
  38. package/dist/lib/neutral/chunk-FXQ52LDX.mjs.map +7 -0
  39. package/dist/lib/neutral/{chunk-4TT624YA.mjs → chunk-FZO7LQO7.mjs} +2 -2
  40. package/dist/lib/neutral/{chunk-4TT624YA.mjs.map → chunk-FZO7LQO7.mjs.map} +1 -1
  41. package/dist/lib/neutral/chunk-HIMJ3MXL.mjs +283 -0
  42. package/dist/lib/neutral/chunk-HIMJ3MXL.mjs.map +7 -0
  43. package/dist/lib/neutral/{chunk-VB5HVDCA.mjs → chunk-HITCYIP2.mjs} +47 -5
  44. package/dist/lib/neutral/chunk-HITCYIP2.mjs.map +7 -0
  45. package/dist/lib/neutral/{chunk-SU2LVM7J.mjs → chunk-JJTRVYRD.mjs} +1596 -2730
  46. package/dist/lib/neutral/chunk-JJTRVYRD.mjs.map +7 -0
  47. package/dist/lib/neutral/{chunk-6QPSGMEF.mjs → chunk-JX5K6AY3.mjs} +32 -8
  48. package/dist/lib/neutral/chunk-JX5K6AY3.mjs.map +7 -0
  49. package/dist/lib/neutral/{chunk-IP2GHXWA.mjs → chunk-PRQVBGFG.mjs} +5 -5
  50. package/dist/lib/neutral/chunk-PRQVBGFG.mjs.map +7 -0
  51. package/dist/lib/neutral/chunk-RUYH5REG.mjs +130 -0
  52. package/dist/lib/neutral/chunk-RUYH5REG.mjs.map +7 -0
  53. package/dist/lib/neutral/{chunk-2MTHJ5UW.mjs → chunk-T74ABBRN.mjs} +134 -24
  54. package/dist/lib/neutral/chunk-T74ABBRN.mjs.map +7 -0
  55. package/dist/lib/neutral/{chunk-XMIDBDE6.mjs → chunk-TJRQTGXR.mjs} +11 -56
  56. package/dist/lib/neutral/chunk-TJRQTGXR.mjs.map +7 -0
  57. package/dist/lib/neutral/{chunk-RPB6V4BE.mjs → chunk-UBEZSGXY.mjs} +14 -1
  58. package/dist/lib/neutral/chunk-UBEZSGXY.mjs.map +7 -0
  59. package/dist/lib/neutral/{chunk-ROKO4RKJ.mjs → chunk-UI6MWK5W.mjs} +2 -2
  60. package/dist/lib/neutral/chunk-UI6MWK5W.mjs.map +7 -0
  61. package/dist/lib/neutral/chunk-VF2IHN6I.mjs +396 -0
  62. package/dist/lib/neutral/chunk-VF2IHN6I.mjs.map +7 -0
  63. package/dist/lib/neutral/{chunk-YQYMWZAD.mjs → chunk-WYOKA6AE.mjs} +4 -4
  64. package/dist/lib/neutral/chunk-WYOKA6AE.mjs.map +7 -0
  65. package/dist/lib/neutral/{chunk-CUDIBUM4.mjs → chunk-XEBKJROJ.mjs} +104 -25
  66. package/dist/lib/neutral/chunk-XEBKJROJ.mjs.map +7 -0
  67. package/dist/lib/neutral/chunk-YS6Q3XAD.mjs +50 -0
  68. package/dist/lib/neutral/chunk-YS6Q3XAD.mjs.map +7 -0
  69. package/dist/lib/neutral/chunk-ZZEIMPNC.mjs +159 -0
  70. package/dist/lib/neutral/chunk-ZZEIMPNC.mjs.map +7 -0
  71. package/dist/lib/neutral/index.mjs +42 -25
  72. package/dist/lib/neutral/internal/index.mjs +52 -38
  73. package/dist/lib/neutral/meta.json +1 -1
  74. package/dist/lib/neutral/testing/index.mjs +42 -34
  75. package/dist/lib/neutral/testing/index.mjs.map +3 -3
  76. package/dist/types/src/Annotation.d.ts +23 -1
  77. package/dist/types/src/Annotation.d.ts.map +1 -1
  78. package/dist/types/src/Collection.d.ts +16 -0
  79. package/dist/types/src/Collection.d.ts.map +1 -0
  80. package/dist/types/src/Database.d.ts +79 -84
  81. package/dist/types/src/Database.d.ts.map +1 -1
  82. package/dist/types/src/Dataset.d.ts +19 -0
  83. package/dist/types/src/Dataset.d.ts.map +1 -0
  84. package/dist/types/src/Entity.d.ts +41 -15
  85. package/dist/types/src/Entity.d.ts.map +1 -1
  86. package/dist/types/src/Err.d.ts +43 -0
  87. package/dist/types/src/Err.d.ts.map +1 -1
  88. package/dist/types/src/Extension.d.ts +80 -0
  89. package/dist/types/src/Extension.d.ts.map +1 -0
  90. package/dist/types/src/Extension.test.d.ts +2 -0
  91. package/dist/types/src/Extension.test.d.ts.map +1 -0
  92. package/dist/types/src/Feed.d.ts +163 -0
  93. package/dist/types/src/Feed.d.ts.map +1 -0
  94. package/dist/types/src/Filter.d.ts +13 -1
  95. package/dist/types/src/Filter.d.ts.map +1 -1
  96. package/dist/types/src/Filter.test.d.ts +2 -0
  97. package/dist/types/src/Filter.test.d.ts.map +1 -0
  98. package/dist/types/src/Format.d.ts +3 -3
  99. package/dist/types/src/Format.d.ts.map +1 -1
  100. package/dist/types/src/Hypergraph.d.ts +8 -3
  101. package/dist/types/src/Hypergraph.d.ts.map +1 -1
  102. package/dist/types/src/JsonSchema.d.ts +11 -4
  103. package/dist/types/src/JsonSchema.d.ts.map +1 -1
  104. package/dist/types/src/Obj.d.ts +158 -43
  105. package/dist/types/src/Obj.d.ts.map +1 -1
  106. package/dist/types/src/Order.d.ts.map +1 -1
  107. package/dist/types/src/Query.d.ts +101 -14
  108. package/dist/types/src/Query.d.ts.map +1 -1
  109. package/dist/types/src/Ref.d.ts +58 -10
  110. package/dist/types/src/Ref.d.ts.map +1 -1
  111. package/dist/types/src/Relation.d.ts +75 -28
  112. package/dist/types/src/Relation.d.ts.map +1 -1
  113. package/dist/types/src/SchemaRegistry.d.ts +4 -4
  114. package/dist/types/src/SchemaRegistry.d.ts.map +1 -1
  115. package/dist/types/src/Tag.d.ts +4 -3
  116. package/dist/types/src/Tag.d.ts.map +1 -1
  117. package/dist/types/src/Type.d.ts +51 -180
  118. package/dist/types/src/Type.d.ts.map +1 -1
  119. package/dist/types/src/View.d.ts +68 -0
  120. package/dist/types/src/View.d.ts.map +1 -0
  121. package/dist/types/src/hierarchy.test.d.ts +2 -0
  122. package/dist/types/src/hierarchy.test.d.ts.map +1 -0
  123. package/dist/types/src/index.d.ts +5 -0
  124. package/dist/types/src/index.d.ts.map +1 -1
  125. package/dist/types/src/internal/{annotations → Annotation}/annotations.d.ts +69 -8
  126. package/dist/types/src/internal/Annotation/annotations.d.ts.map +1 -0
  127. package/dist/types/src/internal/Annotation/annotations.test.d.ts.map +1 -0
  128. package/dist/types/src/internal/Annotation/index.d.ts +4 -0
  129. package/dist/types/src/internal/Annotation/index.d.ts.map +1 -0
  130. package/dist/types/src/internal/{api → Annotation}/sorting.d.ts +1 -1
  131. package/dist/types/src/internal/Annotation/sorting.d.ts.map +1 -0
  132. package/dist/types/src/internal/{annotations → Annotation}/util.d.ts +13 -1
  133. package/dist/types/src/internal/Annotation/util.d.ts.map +1 -0
  134. package/dist/types/src/internal/{api/entity.d.ts → Entity/api.d.ts} +2 -2
  135. package/dist/types/src/internal/Entity/api.d.ts.map +1 -0
  136. package/dist/types/src/internal/{entities → Entity}/entity.d.ts +3 -3
  137. package/dist/types/src/internal/Entity/entity.d.ts.map +1 -0
  138. package/dist/types/src/internal/{entities → Entity}/index.d.ts +2 -0
  139. package/dist/types/src/internal/Entity/index.d.ts.map +1 -0
  140. package/dist/types/src/internal/{entities → Entity}/model.d.ts +4 -26
  141. package/dist/types/src/internal/Entity/model.d.ts.map +1 -0
  142. package/dist/types/src/internal/{entities → Entity}/object.d.ts +2 -2
  143. package/dist/types/src/internal/Entity/object.d.ts.map +1 -0
  144. package/dist/types/src/internal/{entities → Entity}/relation.d.ts +3 -30
  145. package/dist/types/src/internal/Entity/relation.d.ts.map +1 -0
  146. package/dist/types/src/internal/Entity/util.d.ts.map +1 -0
  147. package/dist/types/src/internal/{api → Entity}/version.d.ts +1 -1
  148. package/dist/types/src/internal/Entity/version.d.ts.map +1 -0
  149. package/dist/types/src/internal/Format/date.d.ts.map +1 -0
  150. package/dist/types/src/internal/Format/date.test.d.ts.map +1 -0
  151. package/dist/types/src/internal/Format/format.d.ts.map +1 -0
  152. package/dist/types/src/internal/Format/format.test.d.ts.map +1 -0
  153. package/dist/types/src/internal/Format/index.d.ts.map +1 -0
  154. package/dist/types/src/internal/Format/number.d.ts.map +1 -0
  155. package/dist/types/src/internal/Format/object.d.ts.map +1 -0
  156. package/dist/types/src/internal/Format/select.d.ts.map +1 -0
  157. package/dist/types/src/internal/Format/string.d.ts.map +1 -0
  158. package/dist/types/src/internal/{formats → Format}/types.d.ts +1 -1
  159. package/dist/types/src/internal/Format/types.d.ts.map +1 -0
  160. package/dist/types/src/internal/{json-schema → JsonSchema}/annotations.d.ts +1 -1
  161. package/dist/types/src/internal/JsonSchema/annotations.d.ts.map +1 -0
  162. package/dist/types/src/internal/JsonSchema/effect-schema.test.d.ts.map +1 -0
  163. package/dist/types/src/internal/JsonSchema/index.d.ts.map +1 -0
  164. package/dist/types/src/internal/JsonSchema/json-schema-normalize.d.ts.map +1 -0
  165. package/dist/types/src/internal/{json-schema → JsonSchema}/json-schema-type.d.ts +19 -19
  166. package/dist/types/src/internal/JsonSchema/json-schema-type.d.ts.map +1 -0
  167. package/dist/types/src/internal/JsonSchema/json-schema.d.ts.map +1 -0
  168. package/dist/types/src/internal/JsonSchema/json-schema.test.d.ts.map +1 -0
  169. package/dist/types/src/internal/Obj/clone.d.ts.map +1 -0
  170. package/dist/types/src/internal/Obj/common.d.ts.map +1 -0
  171. package/dist/types/src/internal/{object → Obj}/create-object.d.ts +2 -2
  172. package/dist/types/src/internal/Obj/create-object.d.ts.map +1 -0
  173. package/dist/types/src/internal/Obj/create-object.test.d.ts.map +1 -0
  174. package/dist/types/src/internal/{object → Obj}/deleted.d.ts +1 -1
  175. package/dist/types/src/internal/Obj/deleted.d.ts.map +1 -0
  176. package/dist/types/src/internal/Obj/ids.d.ts.map +1 -0
  177. package/dist/types/src/internal/Obj/index.d.ts.map +1 -0
  178. package/dist/types/src/internal/Obj/inspect.d.ts.map +1 -0
  179. package/dist/types/src/internal/{object → Obj}/json-serializer.d.ts +12 -5
  180. package/dist/types/src/internal/Obj/json-serializer.d.ts.map +1 -0
  181. package/dist/types/src/internal/Obj/json-serializer.test.d.ts.map +1 -0
  182. package/dist/types/src/internal/Obj/schema-validator.d.ts +2 -0
  183. package/dist/types/src/internal/Obj/schema-validator.d.ts.map +1 -0
  184. package/dist/types/src/internal/Obj/schema-validator.test.d.ts.map +1 -0
  185. package/dist/types/src/internal/{object → Obj}/set-value.d.ts +1 -1
  186. package/dist/types/src/internal/Obj/set-value.d.ts.map +1 -0
  187. package/dist/types/src/internal/Obj/set-value.test.d.ts.map +1 -0
  188. package/dist/types/src/internal/Obj/snapshot.d.ts.map +1 -0
  189. package/dist/types/src/internal/{object → Obj}/typed-object.d.ts +4 -4
  190. package/dist/types/src/internal/Obj/typed-object.d.ts.map +1 -0
  191. package/dist/types/src/internal/Obj/typed-object.test.d.ts.map +1 -0
  192. package/dist/types/src/internal/Ref/index.d.ts.map +1 -0
  193. package/dist/types/src/internal/{ref → Ref}/ref-array.d.ts +1 -1
  194. package/dist/types/src/internal/Ref/ref-array.d.ts.map +1 -0
  195. package/dist/types/src/internal/{ref → Ref}/ref.d.ts +16 -3
  196. package/dist/types/src/internal/Ref/ref.d.ts.map +1 -0
  197. package/dist/types/src/internal/Ref/ref.test.d.ts.map +1 -0
  198. package/dist/types/src/internal/{schema → Type}/compose.d.ts +1 -1
  199. package/dist/types/src/internal/Type/compose.d.ts.map +1 -0
  200. package/dist/types/src/internal/Type/compose.test.d.ts.map +1 -0
  201. package/dist/types/src/internal/{schema → Type}/echo-schema.d.ts +8 -7
  202. package/dist/types/src/internal/Type/echo-schema.d.ts.map +1 -0
  203. package/dist/types/src/internal/Type/index.d.ts.map +1 -0
  204. package/dist/types/src/internal/Type/manipulation.d.ts.map +1 -0
  205. package/dist/types/src/internal/{schema → Type}/persistent-schema.d.ts +2 -2
  206. package/dist/types/src/internal/Type/persistent-schema.d.ts.map +1 -0
  207. package/dist/types/src/internal/{api → common/api}/index.d.ts +0 -4
  208. package/dist/types/src/internal/common/api/index.d.ts.map +1 -0
  209. package/dist/types/src/internal/common/api/meta.d.ts.map +1 -0
  210. package/dist/types/src/internal/common/index.d.ts +4 -0
  211. package/dist/types/src/internal/common/index.d.ts.map +1 -0
  212. package/dist/types/src/internal/common/proxy/change-context.d.ts.map +1 -0
  213. package/dist/types/src/internal/common/proxy/change.test.d.ts.map +1 -0
  214. package/dist/types/src/internal/common/proxy/define-hidden-property.d.ts.map +1 -0
  215. package/dist/types/src/internal/common/proxy/errors.d.ts.map +1 -0
  216. package/dist/types/src/internal/common/proxy/event-batch.d.ts.map +1 -0
  217. package/dist/types/src/internal/common/proxy/handler.test.d.ts.map +1 -0
  218. package/dist/types/src/internal/common/proxy/index.d.ts.map +1 -0
  219. package/dist/types/src/internal/common/proxy/json-serializer.d.ts.map +1 -0
  220. package/dist/types/src/internal/common/proxy/make-object.d.ts.map +1 -0
  221. package/dist/types/src/internal/common/proxy/ownership.d.ts.map +1 -0
  222. package/dist/types/src/internal/common/proxy/proxy-types.d.ts.map +1 -0
  223. package/dist/types/src/internal/common/proxy/proxy-utils.d.ts.map +1 -0
  224. package/dist/types/src/internal/common/proxy/reactive-array.d.ts.map +1 -0
  225. package/dist/types/src/internal/{proxy → common/proxy}/reactive.d.ts +1 -1
  226. package/dist/types/src/internal/common/proxy/reactive.d.ts.map +1 -0
  227. package/dist/types/src/internal/common/proxy/schema-validator.d.ts.map +1 -0
  228. package/dist/types/src/internal/common/proxy/schema.test.d.ts.map +1 -0
  229. package/dist/types/src/internal/common/proxy/symbols.d.ts.map +1 -0
  230. package/dist/types/src/internal/{proxy → common/proxy}/typed-handler.d.ts +2 -1
  231. package/dist/types/src/internal/common/proxy/typed-handler.d.ts.map +1 -0
  232. package/dist/types/src/internal/common/proxy/typed-handler.test.d.ts.map +1 -0
  233. package/dist/types/src/internal/common/proxy/typed-object.test.d.ts.map +1 -0
  234. package/dist/types/src/internal/{types → common/types}/base.d.ts +1 -0
  235. package/dist/types/src/internal/common/types/base.d.ts.map +1 -0
  236. package/dist/types/src/internal/{types → common/types}/entity.d.ts +2 -2
  237. package/dist/types/src/internal/common/types/entity.d.ts.map +1 -0
  238. package/dist/types/src/internal/{types → common/types}/index.d.ts +1 -0
  239. package/dist/types/src/internal/common/types/index.d.ts.map +1 -0
  240. package/dist/types/src/internal/{types → common/types}/meta.d.ts +2 -1
  241. package/dist/types/src/internal/common/types/meta.d.ts.map +1 -0
  242. package/dist/types/src/internal/common/types/model-symbols.d.ts +54 -0
  243. package/dist/types/src/internal/common/types/model-symbols.d.ts.map +1 -0
  244. package/dist/types/src/internal/{types → common/types}/typename.d.ts +8 -0
  245. package/dist/types/src/internal/common/types/typename.d.ts.map +1 -0
  246. package/dist/types/src/internal/{types → common/types}/version.d.ts +2 -2
  247. package/dist/types/src/internal/common/types/version.d.ts.map +1 -0
  248. package/dist/types/src/internal/index.d.ts +8 -10
  249. package/dist/types/src/internal/index.d.ts.map +1 -1
  250. package/dist/types/src/testing/test-schema.d.ts +19 -18
  251. package/dist/types/src/testing/test-schema.d.ts.map +1 -1
  252. package/dist/types/tsconfig.tsbuildinfo +1 -1
  253. package/package.json +24 -14
  254. package/src/Annotation.ts +31 -1
  255. package/src/Collection.ts +37 -0
  256. package/src/Database.ts +154 -173
  257. package/src/Dataset.ts +26 -0
  258. package/src/Entity.ts +73 -54
  259. package/src/Err.ts +22 -0
  260. package/src/Extension.test.ts +235 -0
  261. package/src/Extension.ts +122 -0
  262. package/src/Feed.ts +267 -0
  263. package/src/Filter.test.ts +90 -0
  264. package/src/Filter.ts +47 -4
  265. package/src/Format.ts +3 -3
  266. package/src/Hypergraph.ts +9 -4
  267. package/src/JsonSchema.ts +14 -4
  268. package/src/Obj.test.ts +69 -8
  269. package/src/Obj.ts +285 -140
  270. package/src/Order.ts +3 -1
  271. package/src/Query.test.ts +190 -15
  272. package/src/Query.ts +249 -23
  273. package/src/Ref.ts +64 -11
  274. package/src/Relation.test.ts +8 -2
  275. package/src/Relation.ts +165 -131
  276. package/src/SchemaRegistry.ts +5 -4
  277. package/src/Tag.ts +6 -5
  278. package/src/Type.test.ts +10 -10
  279. package/src/Type.ts +70 -303
  280. package/src/View.ts +107 -0
  281. package/src/hierarchy.test.ts +33 -0
  282. package/src/index.ts +5 -0
  283. package/src/internal/{annotations → Annotation}/annotations.test.ts +56 -6
  284. package/src/internal/{annotations → Annotation}/annotations.ts +128 -10
  285. package/src/internal/{annotations → Annotation}/index.ts +1 -0
  286. package/src/internal/{api → Annotation}/sorting.ts +2 -3
  287. package/src/internal/{annotations → Annotation}/util.ts +14 -1
  288. package/src/internal/{api/entity.ts → Entity/api.ts} +4 -2
  289. package/src/internal/{entities → Entity}/entity.ts +6 -4
  290. package/src/internal/{entities → Entity}/index.ts +2 -0
  291. package/src/internal/{entities → Entity}/model.ts +17 -42
  292. package/src/internal/{entities → Entity}/object.ts +2 -2
  293. package/src/internal/{entities → Entity}/relation.ts +19 -36
  294. package/src/internal/{api → Entity}/version.ts +3 -2
  295. package/src/internal/{formats → Format}/date.test.ts +1 -1
  296. package/src/internal/{formats → Format}/format.test.ts +1 -1
  297. package/src/internal/{formats → Format}/types.ts +3 -3
  298. package/src/internal/{json-schema → JsonSchema}/annotations.ts +3 -3
  299. package/src/internal/{json-schema → JsonSchema}/json-schema-type.ts +4 -4
  300. package/src/internal/{json-schema → JsonSchema}/json-schema.test.ts +48 -48
  301. package/src/internal/{json-schema → JsonSchema}/json-schema.ts +6 -4
  302. package/src/internal/{object → Obj}/clone.ts +3 -3
  303. package/src/internal/{object → Obj}/common.ts +2 -2
  304. package/src/internal/{object → Obj}/create-object.test.ts +4 -4
  305. package/src/internal/{object → Obj}/create-object.ts +5 -5
  306. package/src/internal/{object → Obj}/deleted.ts +2 -2
  307. package/src/internal/{object → Obj}/inspect.ts +2 -2
  308. package/src/internal/{object → Obj}/json-serializer.test.ts +31 -8
  309. package/src/internal/{object → Obj}/json-serializer.ts +42 -15
  310. package/src/internal/{object → Obj}/schema-validator.ts +1 -1
  311. package/src/internal/{object → Obj}/set-value.test.ts +24 -24
  312. package/src/internal/{object → Obj}/set-value.ts +2 -2
  313. package/src/internal/Obj/snapshot.ts +105 -0
  314. package/src/internal/{object → Obj}/typed-object.test.ts +3 -3
  315. package/src/internal/{object → Obj}/typed-object.ts +4 -4
  316. package/src/internal/{ref → Ref}/ref-array.ts +1 -1
  317. package/src/internal/{ref → Ref}/ref.test.ts +4 -4
  318. package/src/internal/{ref → Ref}/ref.ts +27 -4
  319. package/src/internal/{schema → Type}/compose.test.ts +5 -5
  320. package/src/internal/{schema → Type}/compose.ts +1 -1
  321. package/src/internal/{schema → Type}/echo-schema.ts +11 -10
  322. package/src/internal/{schema → Type}/manipulation.ts +1 -1
  323. package/src/internal/{schema → Type}/persistent-schema.ts +10 -4
  324. package/src/internal/{README.md → common/README.md} +1 -1
  325. package/src/internal/{api → common/api}/index.ts +0 -4
  326. package/src/internal/common/index.ts +7 -0
  327. package/src/internal/{proxy → common/proxy}/change.test.ts +58 -58
  328. package/src/internal/{proxy → common/proxy}/handler.test.ts +2 -2
  329. package/src/internal/{proxy → common/proxy}/json-serializer.ts +6 -3
  330. package/src/internal/{proxy → common/proxy}/make-object.ts +13 -3
  331. package/src/internal/{proxy → common/proxy}/reactive.ts +1 -1
  332. package/src/internal/{proxy → common/proxy}/schema.test.ts +10 -10
  333. package/src/internal/{proxy → common/proxy}/typed-handler.test.ts +6 -6
  334. package/src/internal/{proxy → common/proxy}/typed-handler.ts +10 -3
  335. package/src/internal/{proxy → common/proxy}/typed-object.test.ts +4 -4
  336. package/src/internal/{types → common/types}/base.ts +1 -1
  337. package/src/internal/{types → common/types}/entity.ts +2 -2
  338. package/src/internal/{types → common/types}/index.ts +1 -0
  339. package/src/internal/{types → common/types}/meta.ts +4 -1
  340. package/src/internal/common/types/model-symbols.ts +69 -0
  341. package/src/internal/{types → common/types}/typename.ts +10 -0
  342. package/src/internal/{types → common/types}/version.ts +2 -3
  343. package/src/internal/index.ts +8 -31
  344. package/src/testing/api.test.ts +4 -4
  345. package/src/testing/test-schema.ts +20 -18
  346. package/dist/lib/neutral/chunk-2MTHJ5UW.mjs.map +0 -7
  347. package/dist/lib/neutral/chunk-3ZEAZF7T.mjs.map +0 -7
  348. package/dist/lib/neutral/chunk-62DWJ6CE.mjs.map +0 -7
  349. package/dist/lib/neutral/chunk-6QPSGMEF.mjs.map +0 -7
  350. package/dist/lib/neutral/chunk-CUDIBUM4.mjs.map +0 -7
  351. package/dist/lib/neutral/chunk-HMOXCO27.mjs.map +0 -7
  352. package/dist/lib/neutral/chunk-IP2GHXWA.mjs.map +0 -7
  353. package/dist/lib/neutral/chunk-KRHDMXSU.mjs +0 -132
  354. package/dist/lib/neutral/chunk-KRHDMXSU.mjs.map +0 -7
  355. package/dist/lib/neutral/chunk-NWUO5FKZ.mjs.map +0 -7
  356. package/dist/lib/neutral/chunk-ROKO4RKJ.mjs.map +0 -7
  357. package/dist/lib/neutral/chunk-RPB6V4BE.mjs.map +0 -7
  358. package/dist/lib/neutral/chunk-SU2LVM7J.mjs.map +0 -7
  359. package/dist/lib/neutral/chunk-VB5HVDCA.mjs.map +0 -7
  360. package/dist/lib/neutral/chunk-VR4A4WGG.mjs.map +0 -7
  361. package/dist/lib/neutral/chunk-X2MPMYYN.mjs +0 -13
  362. package/dist/lib/neutral/chunk-X2MPMYYN.mjs.map +0 -7
  363. package/dist/lib/neutral/chunk-XK5OUUBY.mjs +0 -157
  364. package/dist/lib/neutral/chunk-XK5OUUBY.mjs.map +0 -7
  365. package/dist/lib/neutral/chunk-XMIDBDE6.mjs.map +0 -7
  366. package/dist/lib/neutral/chunk-YQYMWZAD.mjs.map +0 -7
  367. package/dist/types/src/internal/annotations/annotations.d.ts.map +0 -1
  368. package/dist/types/src/internal/annotations/annotations.test.d.ts.map +0 -1
  369. package/dist/types/src/internal/annotations/index.d.ts +0 -3
  370. package/dist/types/src/internal/annotations/index.d.ts.map +0 -1
  371. package/dist/types/src/internal/annotations/util.d.ts.map +0 -1
  372. package/dist/types/src/internal/api/annotations.d.ts +0 -23
  373. package/dist/types/src/internal/api/annotations.d.ts.map +0 -1
  374. package/dist/types/src/internal/api/entity.d.ts.map +0 -1
  375. package/dist/types/src/internal/api/index.d.ts.map +0 -1
  376. package/dist/types/src/internal/api/meta.d.ts.map +0 -1
  377. package/dist/types/src/internal/api/sorting.d.ts.map +0 -1
  378. package/dist/types/src/internal/api/version.d.ts.map +0 -1
  379. package/dist/types/src/internal/entities/entity.d.ts.map +0 -1
  380. package/dist/types/src/internal/entities/index.d.ts.map +0 -1
  381. package/dist/types/src/internal/entities/model.d.ts.map +0 -1
  382. package/dist/types/src/internal/entities/object.d.ts.map +0 -1
  383. package/dist/types/src/internal/entities/relation.d.ts.map +0 -1
  384. package/dist/types/src/internal/entities/util.d.ts.map +0 -1
  385. package/dist/types/src/internal/formats/date.d.ts.map +0 -1
  386. package/dist/types/src/internal/formats/date.test.d.ts.map +0 -1
  387. package/dist/types/src/internal/formats/format.d.ts.map +0 -1
  388. package/dist/types/src/internal/formats/format.test.d.ts.map +0 -1
  389. package/dist/types/src/internal/formats/index.d.ts.map +0 -1
  390. package/dist/types/src/internal/formats/number.d.ts.map +0 -1
  391. package/dist/types/src/internal/formats/object.d.ts.map +0 -1
  392. package/dist/types/src/internal/formats/select.d.ts.map +0 -1
  393. package/dist/types/src/internal/formats/string.d.ts.map +0 -1
  394. package/dist/types/src/internal/formats/types.d.ts.map +0 -1
  395. package/dist/types/src/internal/json-schema/annotations.d.ts.map +0 -1
  396. package/dist/types/src/internal/json-schema/effect-schema.test.d.ts.map +0 -1
  397. package/dist/types/src/internal/json-schema/index.d.ts.map +0 -1
  398. package/dist/types/src/internal/json-schema/json-schema-normalize.d.ts.map +0 -1
  399. package/dist/types/src/internal/json-schema/json-schema-type.d.ts.map +0 -1
  400. package/dist/types/src/internal/json-schema/json-schema.d.ts.map +0 -1
  401. package/dist/types/src/internal/json-schema/json-schema.test.d.ts.map +0 -1
  402. package/dist/types/src/internal/object/clone.d.ts.map +0 -1
  403. package/dist/types/src/internal/object/common.d.ts.map +0 -1
  404. package/dist/types/src/internal/object/create-object.d.ts.map +0 -1
  405. package/dist/types/src/internal/object/create-object.test.d.ts.map +0 -1
  406. package/dist/types/src/internal/object/deleted.d.ts.map +0 -1
  407. package/dist/types/src/internal/object/ids.d.ts.map +0 -1
  408. package/dist/types/src/internal/object/index.d.ts.map +0 -1
  409. package/dist/types/src/internal/object/inspect.d.ts.map +0 -1
  410. package/dist/types/src/internal/object/json-serializer.d.ts.map +0 -1
  411. package/dist/types/src/internal/object/json-serializer.test.d.ts.map +0 -1
  412. package/dist/types/src/internal/object/schema-validator.d.ts +0 -2
  413. package/dist/types/src/internal/object/schema-validator.d.ts.map +0 -1
  414. package/dist/types/src/internal/object/schema-validator.test.d.ts.map +0 -1
  415. package/dist/types/src/internal/object/set-value.d.ts.map +0 -1
  416. package/dist/types/src/internal/object/set-value.test.d.ts.map +0 -1
  417. package/dist/types/src/internal/object/snapshot.d.ts.map +0 -1
  418. package/dist/types/src/internal/object/typed-object.d.ts.map +0 -1
  419. package/dist/types/src/internal/object/typed-object.test.d.ts.map +0 -1
  420. package/dist/types/src/internal/proxy/change-context.d.ts.map +0 -1
  421. package/dist/types/src/internal/proxy/change.test.d.ts.map +0 -1
  422. package/dist/types/src/internal/proxy/define-hidden-property.d.ts.map +0 -1
  423. package/dist/types/src/internal/proxy/errors.d.ts.map +0 -1
  424. package/dist/types/src/internal/proxy/event-batch.d.ts.map +0 -1
  425. package/dist/types/src/internal/proxy/handler.test.d.ts.map +0 -1
  426. package/dist/types/src/internal/proxy/index.d.ts.map +0 -1
  427. package/dist/types/src/internal/proxy/json-serializer.d.ts.map +0 -1
  428. package/dist/types/src/internal/proxy/make-object.d.ts.map +0 -1
  429. package/dist/types/src/internal/proxy/ownership.d.ts.map +0 -1
  430. package/dist/types/src/internal/proxy/proxy-types.d.ts.map +0 -1
  431. package/dist/types/src/internal/proxy/proxy-utils.d.ts.map +0 -1
  432. package/dist/types/src/internal/proxy/reactive-array.d.ts.map +0 -1
  433. package/dist/types/src/internal/proxy/reactive.d.ts.map +0 -1
  434. package/dist/types/src/internal/proxy/schema-validator.d.ts.map +0 -1
  435. package/dist/types/src/internal/proxy/schema.test.d.ts.map +0 -1
  436. package/dist/types/src/internal/proxy/symbols.d.ts.map +0 -1
  437. package/dist/types/src/internal/proxy/typed-handler.d.ts.map +0 -1
  438. package/dist/types/src/internal/proxy/typed-handler.test.d.ts.map +0 -1
  439. package/dist/types/src/internal/proxy/typed-object.test.d.ts.map +0 -1
  440. package/dist/types/src/internal/ref/index.d.ts.map +0 -1
  441. package/dist/types/src/internal/ref/ref-array.d.ts.map +0 -1
  442. package/dist/types/src/internal/ref/ref.d.ts.map +0 -1
  443. package/dist/types/src/internal/ref/ref.test.d.ts.map +0 -1
  444. package/dist/types/src/internal/schema/compose.d.ts.map +0 -1
  445. package/dist/types/src/internal/schema/compose.test.d.ts.map +0 -1
  446. package/dist/types/src/internal/schema/echo-schema.d.ts.map +0 -1
  447. package/dist/types/src/internal/schema/index.d.ts.map +0 -1
  448. package/dist/types/src/internal/schema/manipulation.d.ts.map +0 -1
  449. package/dist/types/src/internal/schema/persistent-schema.d.ts.map +0 -1
  450. package/dist/types/src/internal/types/base.d.ts.map +0 -1
  451. package/dist/types/src/internal/types/entity.d.ts.map +0 -1
  452. package/dist/types/src/internal/types/index.d.ts.map +0 -1
  453. package/dist/types/src/internal/types/meta.d.ts.map +0 -1
  454. package/dist/types/src/internal/types/typename.d.ts.map +0 -1
  455. package/dist/types/src/internal/types/version.d.ts.map +0 -1
  456. package/src/internal/api/annotations.ts +0 -60
  457. package/src/internal/object/snapshot.ts +0 -70
  458. /package/dist/types/src/internal/{annotations → Annotation}/annotations.test.d.ts +0 -0
  459. /package/dist/types/src/internal/{entities → Entity}/util.d.ts +0 -0
  460. /package/dist/types/src/internal/{formats → Format}/date.d.ts +0 -0
  461. /package/dist/types/src/internal/{formats → Format}/date.test.d.ts +0 -0
  462. /package/dist/types/src/internal/{formats → Format}/format.d.ts +0 -0
  463. /package/dist/types/src/internal/{formats → Format}/format.test.d.ts +0 -0
  464. /package/dist/types/src/internal/{formats → Format}/index.d.ts +0 -0
  465. /package/dist/types/src/internal/{formats → Format}/number.d.ts +0 -0
  466. /package/dist/types/src/internal/{formats → Format}/object.d.ts +0 -0
  467. /package/dist/types/src/internal/{formats → Format}/select.d.ts +0 -0
  468. /package/dist/types/src/internal/{formats → Format}/string.d.ts +0 -0
  469. /package/dist/types/src/internal/{json-schema → JsonSchema}/effect-schema.test.d.ts +0 -0
  470. /package/dist/types/src/internal/{json-schema → JsonSchema}/index.d.ts +0 -0
  471. /package/dist/types/src/internal/{json-schema → JsonSchema}/json-schema-normalize.d.ts +0 -0
  472. /package/dist/types/src/internal/{json-schema → JsonSchema}/json-schema.d.ts +0 -0
  473. /package/dist/types/src/internal/{json-schema → JsonSchema}/json-schema.test.d.ts +0 -0
  474. /package/dist/types/src/internal/{object → Obj}/clone.d.ts +0 -0
  475. /package/dist/types/src/internal/{object → Obj}/common.d.ts +0 -0
  476. /package/dist/types/src/internal/{object → Obj}/create-object.test.d.ts +0 -0
  477. /package/dist/types/src/internal/{object → Obj}/ids.d.ts +0 -0
  478. /package/dist/types/src/internal/{object → Obj}/index.d.ts +0 -0
  479. /package/dist/types/src/internal/{object → Obj}/inspect.d.ts +0 -0
  480. /package/dist/types/src/internal/{object → Obj}/json-serializer.test.d.ts +0 -0
  481. /package/dist/types/src/internal/{object → Obj}/schema-validator.test.d.ts +0 -0
  482. /package/dist/types/src/internal/{object → Obj}/set-value.test.d.ts +0 -0
  483. /package/dist/types/src/internal/{object → Obj}/snapshot.d.ts +0 -0
  484. /package/dist/types/src/internal/{object → Obj}/typed-object.test.d.ts +0 -0
  485. /package/dist/types/src/internal/{ref → Ref}/index.d.ts +0 -0
  486. /package/dist/types/src/internal/{ref → Ref}/ref.test.d.ts +0 -0
  487. /package/dist/types/src/internal/{schema → Type}/compose.test.d.ts +0 -0
  488. /package/dist/types/src/internal/{schema → Type}/index.d.ts +0 -0
  489. /package/dist/types/src/internal/{schema → Type}/manipulation.d.ts +0 -0
  490. /package/dist/types/src/internal/{api → common/api}/meta.d.ts +0 -0
  491. /package/dist/types/src/internal/{proxy → common/proxy}/change-context.d.ts +0 -0
  492. /package/dist/types/src/internal/{proxy → common/proxy}/change.test.d.ts +0 -0
  493. /package/dist/types/src/internal/{proxy → common/proxy}/define-hidden-property.d.ts +0 -0
  494. /package/dist/types/src/internal/{proxy → common/proxy}/errors.d.ts +0 -0
  495. /package/dist/types/src/internal/{proxy → common/proxy}/event-batch.d.ts +0 -0
  496. /package/dist/types/src/internal/{proxy → common/proxy}/handler.test.d.ts +0 -0
  497. /package/dist/types/src/internal/{proxy → common/proxy}/index.d.ts +0 -0
  498. /package/dist/types/src/internal/{proxy → common/proxy}/json-serializer.d.ts +0 -0
  499. /package/dist/types/src/internal/{proxy → common/proxy}/make-object.d.ts +0 -0
  500. /package/dist/types/src/internal/{proxy → common/proxy}/ownership.d.ts +0 -0
  501. /package/dist/types/src/internal/{proxy → common/proxy}/proxy-types.d.ts +0 -0
  502. /package/dist/types/src/internal/{proxy → common/proxy}/proxy-utils.d.ts +0 -0
  503. /package/dist/types/src/internal/{proxy → common/proxy}/reactive-array.d.ts +0 -0
  504. /package/dist/types/src/internal/{proxy → common/proxy}/schema-validator.d.ts +0 -0
  505. /package/dist/types/src/internal/{proxy → common/proxy}/schema.test.d.ts +0 -0
  506. /package/dist/types/src/internal/{proxy → common/proxy}/symbols.d.ts +0 -0
  507. /package/dist/types/src/internal/{proxy → common/proxy}/typed-handler.test.d.ts +0 -0
  508. /package/dist/types/src/internal/{proxy → common/proxy}/typed-object.test.d.ts +0 -0
  509. /package/src/internal/{entities → Entity}/util.ts +0 -0
  510. /package/src/internal/{formats → Format}/date.ts +0 -0
  511. /package/src/internal/{formats → Format}/format.ts +0 -0
  512. /package/src/internal/{formats → Format}/index.ts +0 -0
  513. /package/src/internal/{formats → Format}/number.ts +0 -0
  514. /package/src/internal/{formats → Format}/object.ts +0 -0
  515. /package/src/internal/{formats → Format}/select.ts +0 -0
  516. /package/src/internal/{formats → Format}/string.ts +0 -0
  517. /package/src/internal/{json-schema → JsonSchema}/effect-schema.test.ts +0 -0
  518. /package/src/internal/{json-schema → JsonSchema}/index.ts +0 -0
  519. /package/src/internal/{json-schema → JsonSchema}/json-schema-normalize.ts +0 -0
  520. /package/src/internal/{object → Obj}/ids.ts +0 -0
  521. /package/src/internal/{object → Obj}/index.ts +0 -0
  522. /package/src/internal/{object → Obj}/schema-validator.test.ts +0 -0
  523. /package/src/internal/{ref → Ref}/index.ts +0 -0
  524. /package/src/internal/{schema → Type}/index.ts +0 -0
  525. /package/src/internal/{api → common/api}/meta.ts +0 -0
  526. /package/src/internal/{proxy → common/proxy}/change-context.ts +0 -0
  527. /package/src/internal/{proxy → common/proxy}/define-hidden-property.ts +0 -0
  528. /package/src/internal/{proxy → common/proxy}/errors.ts +0 -0
  529. /package/src/internal/{proxy → common/proxy}/event-batch.ts +0 -0
  530. /package/src/internal/{proxy → common/proxy}/index.ts +0 -0
  531. /package/src/internal/{proxy → common/proxy}/ownership.ts +0 -0
  532. /package/src/internal/{proxy → common/proxy}/proxy-types.ts +0 -0
  533. /package/src/internal/{proxy → common/proxy}/proxy-utils.ts +0 -0
  534. /package/src/internal/{proxy → common/proxy}/reactive-array.ts +0 -0
  535. /package/src/internal/{proxy → common/proxy}/schema-validator.ts +0 -0
  536. /package/src/internal/{proxy → common/proxy}/symbols.ts +0 -0
@@ -1,18 +1,20 @@
1
1
  import {
2
- CurrencyAnnotationId,
3
- FormatAnnotation,
4
- FormatAnnotationId,
5
- TypeFormat
6
- } from "./chunk-62DWJ6CE.mjs";
2
+ JsonSchemaFields,
3
+ JsonSchemaType,
4
+ toEffectSchema,
5
+ toJsonSchema
6
+ } from "./chunk-4O4AV6CL.mjs";
7
7
  import {
8
- Service
9
- } from "./chunk-KRHDMXSU.mjs";
8
+ Ref,
9
+ refFromEncodedReference,
10
+ setRefResolver
11
+ } from "./chunk-VF2IHN6I.mjs";
10
12
  import {
13
+ ATTR_PARENT,
11
14
  ATTR_TYPE,
12
- GeneratorAnnotationId,
13
- LabelAnnotationId,
14
- PropertyMetaAnnotationId,
15
- ReferenceAnnotationId,
15
+ IconAnnotation,
16
+ LabelAnnotation,
17
+ ParentId,
16
18
  SchemaId,
17
19
  SchemaMetaSymbol,
18
20
  TypeAnnotationId,
@@ -20,9 +22,8 @@ import {
20
22
  TypeIdentifierAnnotationId,
21
23
  TypenameSchema,
22
24
  VersionSchema,
23
- getDescriptionWithSchema,
24
25
  getEntityKind,
25
- getLabelWithSchema,
26
+ getLabel,
26
27
  getSchema,
27
28
  getSchemaDXN,
28
29
  getSchemaTypename,
@@ -30,26 +31,21 @@ import {
30
31
  getTypeDXN,
31
32
  getTypeIdentifierAnnotation,
32
33
  getTypename,
33
- setDescriptionWithSchema,
34
- setLabelWithSchema,
35
34
  setSchema,
36
35
  setTypename
37
- } from "./chunk-CUDIBUM4.mjs";
36
+ } from "./chunk-XEBKJROJ.mjs";
38
37
  import {
39
- ANY_OBJECT_TYPENAME,
40
- ANY_OBJECT_VERSION,
41
38
  EntityKind,
42
- EntityKindSchema,
43
39
  KindId,
44
40
  SchemaKindId,
45
41
  SnapshotKindId,
46
42
  makeTypeJsonSchemaAnnotation
47
- } from "./chunk-YQYMWZAD.mjs";
43
+ } from "./chunk-WYOKA6AE.mjs";
48
44
 
49
45
  // src/internal/index.ts
50
- import { JsonPath as JsonPath2, JsonProp, getValue, splitJsonPath } from "@dxos/effect";
46
+ import { JsonPath, JsonProp, getValue, splitJsonPath } from "@dxos/effect";
51
47
 
52
- // src/internal/types/base.ts
48
+ // src/internal/common/types/base.ts
53
49
  import * as Schema from "effect/Schema";
54
50
  import * as SchemaAST from "effect/SchemaAST";
55
51
  var RawObject = (schema) => {
@@ -58,14 +54,14 @@ var RawObject = (schema) => {
58
54
  ]));
59
55
  };
60
56
 
61
- // src/internal/types/meta.ts
57
+ // src/internal/common/types/meta.ts
62
58
  import * as Schema2 from "effect/Schema";
63
59
  import { ForeignKey } from "@dxos/echo-protocol";
64
60
  import { invariant } from "@dxos/invariant";
65
61
  import { intersection } from "@dxos/util";
66
- var __dxlog_file = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/types/meta.ts";
62
+ var __dxlog_file = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/common/types/meta.ts";
67
63
  var ATTR_META = "@meta";
68
- var MetaId = Symbol.for("@dxos/echo/Meta");
64
+ var MetaId = /* @__PURE__ */ Symbol.for("@dxos/echo/Meta");
69
65
  var ObjectMetaSchema = Schema2.Struct({
70
66
  keys: Schema2.Array(ForeignKey),
71
67
  /**
@@ -75,13 +71,14 @@ var ObjectMetaSchema = Schema2.Struct({
75
71
  */
76
72
  // TODO(dmaretskyi): Has to be optional for compatibility with old data.
77
73
  // Defaulting to an empty array is possible but requires a bit more work.
74
+ // TODO(dmaretskyi): In automerge this should be a map of { [tag]: boolean } for uniqueness and conflict resolution.
78
75
  tags: Schema2.optional(Schema2.Array(Schema2.String))
79
76
  });
80
77
  var getMeta = (obj) => {
81
78
  const metadata = obj[MetaId];
82
79
  invariant(metadata, "ObjectMeta not found.", {
83
80
  F: __dxlog_file,
84
- L: 52,
81
+ L: 55,
85
82
  S: void 0,
86
83
  A: [
87
84
  "metadata",
@@ -97,2766 +94,1631 @@ var foreignKey = (source, id) => ({
97
94
  var foreignKeyEquals = (a, b) => a.source === b.source && a.id === b.id;
98
95
  var compareForeignKeys = (a, b) => intersection(getMeta(a).keys, getMeta(b).keys, foreignKeyEquals).length > 0;
99
96
 
100
- // src/internal/types/version.ts
101
- var VersionTypeId = Symbol.for("@dxos/echo/VersionTypeId");
97
+ // src/internal/common/types/model-symbols.ts
98
+ var ATTR_SELF_DXN = "@dxn";
99
+ var SelfDXNId = /* @__PURE__ */ Symbol.for("@dxos/echo/DXN");
100
+ var ATTR_DELETED = "@deleted";
101
+ var ObjectDeletedId = /* @__PURE__ */ Symbol.for("@dxos/echo/Deleted");
102
+ var ObjectVersionId = /* @__PURE__ */ Symbol.for("@dxos/echo/Version");
103
+ var ObjectDatabaseId = /* @__PURE__ */ Symbol.for("@dxos/echo/Database");
104
+ var ATTR_RELATION_SOURCE = "@relationSource";
105
+ var RelationSourceId = /* @__PURE__ */ Symbol.for("@dxos/echo/RelationSource");
106
+ var RelationSourceDXNId = /* @__PURE__ */ Symbol.for("@dxos/echo/RelationSourceDXN");
107
+ var ATTR_RELATION_TARGET = "@relationTarget";
108
+ var RelationTargetId = /* @__PURE__ */ Symbol.for("@dxos/echo/RelationTarget");
109
+ var RelationTargetDXNId = /* @__PURE__ */ Symbol.for("@dxos/echo/RelationTargetDXN");
110
+
111
+ // src/internal/common/types/version.ts
112
+ var VersionTypeId = "~@dxos/echo/VersionTypeId";
102
113
 
103
- // src/internal/api/annotations.ts
104
- var getLabel = (entity) => {
105
- const schema = getSchema(entity);
106
- if (schema != null) {
107
- return getLabelWithSchema(schema, entity);
114
+ // src/internal/Annotation/sorting.ts
115
+ var compare = (a, b) => {
116
+ if (a == null) {
117
+ return b == null ? 0 : 1;
108
118
  }
109
- };
110
- var setLabel = (entity, label) => {
111
- const schema = getSchema(entity);
112
- if (schema != null) {
113
- setLabelWithSchema(schema, entity, label);
119
+ if (b == null) {
120
+ return -1;
114
121
  }
122
+ return a.localeCompare(b);
115
123
  };
116
- var getDescription = (entity) => {
117
- const schema = getSchema(entity);
118
- if (schema != null) {
119
- return getDescriptionWithSchema(schema, entity);
124
+ var sortByLabel = (a, b) => compare(getLabel(a), getLabel(b));
125
+ var sortByTypename = (a, b) => compare(getTypename(a), getTypename(b));
126
+ var sort = (...comparators) => {
127
+ return (a, b) => {
128
+ for (const comparator of comparators) {
129
+ const result = comparator(a, b);
130
+ if (result !== 0) {
131
+ return result;
132
+ }
133
+ }
134
+ return 0;
135
+ };
136
+ };
137
+
138
+ // src/internal/common/api/meta.ts
139
+ import { assertArgument, invariant as invariant2 } from "@dxos/invariant";
140
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/common/api/meta.ts";
141
+ function getMetaChecked(entity) {
142
+ assertArgument(entity, "entity", "Should be an entity.");
143
+ const meta = getMeta(entity);
144
+ invariant2(meta != null, "Invalid entity.", {
145
+ F: __dxlog_file2,
146
+ L: 34,
147
+ S: this,
148
+ A: [
149
+ "meta != null",
150
+ "'Invalid entity.'"
151
+ ]
152
+ });
153
+ return meta;
154
+ }
155
+ var getKeys = (entity, source) => {
156
+ assertArgument(entity, "entity", "Should be an entity.");
157
+ const meta = getMetaChecked(entity);
158
+ invariant2(meta != null, "Invalid entity.", {
159
+ F: __dxlog_file2,
160
+ L: 45,
161
+ S: void 0,
162
+ A: [
163
+ "meta != null",
164
+ "'Invalid entity.'"
165
+ ]
166
+ });
167
+ return meta.keys.filter((key) => key.source === source);
168
+ };
169
+ var deleteKeys = (entity, source) => {
170
+ const meta = getMetaChecked(entity);
171
+ for (let i = 0; i < meta.keys.length; i++) {
172
+ if (meta.keys[i].source === source) {
173
+ meta.keys.splice(i, 1);
174
+ i--;
175
+ }
120
176
  }
121
177
  };
122
- var setDescription = (entity, description) => {
123
- const schema = getSchema(entity);
124
- if (schema != null) {
125
- setDescriptionWithSchema(schema, entity, description);
178
+ var addTag = (entity, tag) => {
179
+ const meta = getMetaChecked(entity);
180
+ meta.tags ??= [];
181
+ meta.tags.push(tag);
182
+ };
183
+ var removeTag = (entity, tag) => {
184
+ const meta = getMetaChecked(entity);
185
+ if (!meta.tags) {
186
+ return;
187
+ }
188
+ for (let i = 0; i < meta.tags.length; i++) {
189
+ if (meta.tags[i] === tag) {
190
+ meta.tags.splice(i, 1);
191
+ i--;
192
+ }
126
193
  }
127
194
  };
128
195
 
129
- // src/internal/api/entity.ts
130
- import { invariant as invariant6 } from "@dxos/invariant";
131
- import { assumeType as assumeType3 } from "@dxos/util";
196
+ // src/internal/common/proxy/symbols.ts
197
+ var EventId = /* @__PURE__ */ Symbol.for("@dxos/live-object/EventId");
198
+ var ChangeId = /* @__PURE__ */ Symbol.for("@dxos/live-object/ChangeId");
132
199
 
133
- // src/internal/entities/entity.ts
134
- import * as Schema3 from "effect/Schema";
135
- var makeEchoTypeSchema = (fields, ast, typename, version2, kind) => {
136
- return class EchoObjectSchemaClass extends Schema3.make(ast) {
137
- static typename = typename;
138
- static version = version2;
139
- static [SchemaKindId] = kind;
140
- static fields = fields;
141
- static annotations(annotations4) {
142
- const schema = Schema3.make(ast).annotations(annotations4);
143
- return makeEchoTypeSchema(fields, schema.ast, typename, version2, kind);
144
- }
145
- // static make(
146
- // props: RequiredKeys<Schema.TypeLiteral.Constructor<Fields, []>> extends never
147
- // ? void | Simplify<Schema.TypeLiteral.Constructor<Fields, []>>
148
- // : Simplify<Schema.TypeLiteral.Constructor<Fields, []>>,
149
- // options?: MakeProps,
150
- // ): Simplify<Schema.TypeLiteral.Type<Fields, []>> {
151
- // const propsWithDefaults: any = _lazilyMergeDefaults(fields, { ...(props as any) });
152
- // return _getDisableValidationMakeOption(options)
153
- // ? propsWithDefaults
154
- // : ParseResult.validateSync(this)(propsWithDefaults);
155
- // }
156
- static instanceOf(value2) {
157
- return Schema3.is(this)(value2);
200
+ // src/internal/common/proxy/event-batch.ts
201
+ var eventBatchDepth = 0;
202
+ var pendingEventTargets = /* @__PURE__ */ new Set();
203
+ var batchEvents = (callback) => {
204
+ eventBatchDepth++;
205
+ try {
206
+ callback();
207
+ } finally {
208
+ eventBatchDepth--;
209
+ if (eventBatchDepth === 0) {
210
+ for (const target of pendingEventTargets) {
211
+ target[EventId]?.emit();
212
+ }
213
+ pendingEventTargets.clear();
158
214
  }
159
- };
215
+ }
216
+ };
217
+ var emitEvent = (target) => {
218
+ if (eventBatchDepth > 0) {
219
+ pendingEventTargets.add(target);
220
+ } else {
221
+ target[EventId]?.emit();
222
+ }
160
223
  };
161
224
 
162
- // src/internal/entities/model.ts
163
- import { invariant as invariant3 } from "@dxos/invariant";
164
- import { DXN as DXN2, ObjectId } from "@dxos/keys";
165
- import { assumeType } from "@dxos/util";
166
-
167
- // src/internal/entities/relation.ts
168
- import * as Schema4 from "effect/Schema";
169
- import * as SchemaAST2 from "effect/SchemaAST";
170
- import { raise } from "@dxos/debug";
171
- import { assertArgument, invariant as invariant2 } from "@dxos/invariant";
172
- import { DXN } from "@dxos/keys";
173
- var __dxlog_file2 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/entities/relation.ts";
174
- var ATTR_RELATION_SOURCE = "@relationSource";
175
- var RelationSourceId = Symbol.for("@dxos/echo/RelationSource");
176
- var RelationSourceDXNId = Symbol.for("@dxos/echo/RelationSourceDXN");
177
- var ATTR_RELATION_TARGET = "@relationTarget";
178
- var RelationTargetId = Symbol.for("@dxos/echo/RelationTarget");
179
- var RelationTargetDXNId = Symbol.for("@dxos/echo/RelationTargetDXN");
180
- var EchoRelationSchema = ({ source, target, typename, version: version2 }) => {
181
- assertArgument(Schema4.isSchema(source), "source");
182
- assertArgument(Schema4.isSchema(target), "target");
183
- const sourceDXN = getDXNForRelationSchemaRef(source);
184
- const targetDXN = getDXNForRelationSchemaRef(target);
185
- if (getEntityKind(source) !== EntityKind.Object) {
186
- raise(new Error("Source schema must be an echo object schema."));
225
+ // src/internal/common/proxy/change-context.ts
226
+ var currentChangeContext = null;
227
+ var pendingNotificationKey = null;
228
+ var pendingOwnerNotifications = /* @__PURE__ */ new Set();
229
+ var enterChangeContext = (key) => {
230
+ currentChangeContext = key;
231
+ return () => {
232
+ currentChangeContext = null;
233
+ };
234
+ };
235
+ var isInChangeContext = (key) => {
236
+ return currentChangeContext === key;
237
+ };
238
+ var queueNotification = (key) => {
239
+ if (currentChangeContext === key) {
240
+ pendingNotificationKey = key;
187
241
  }
188
- if (getEntityKind(target) !== EntityKind.Object) {
189
- raise(new Error("Target schema must be an echo object schema."));
242
+ };
243
+ var queueOwnerNotification = (target) => {
244
+ if (currentChangeContext !== null && target !== currentChangeContext) {
245
+ pendingOwnerNotifications.add(target);
190
246
  }
191
- return (self) => {
192
- invariant2(SchemaAST2.isTypeLiteral(self.ast), "Schema must be a TypeLiteral.", {
193
- F: __dxlog_file2,
194
- L: 127,
195
- S: void 0,
196
- A: [
197
- "SchemaAST.isTypeLiteral(self.ast)",
198
- "'Schema must be a TypeLiteral.'"
199
- ]
200
- });
201
- const fields = self.fields ?? {};
202
- const schemaWithId = Schema4.extend(self, Schema4.Struct({
203
- id: Schema4.String
204
- }));
205
- const ast = SchemaAST2.annotations(schemaWithId.ast, {
206
- // TODO(dmaretskyi): `extend` kills the annotations.
207
- ...self.ast.annotations,
208
- [TypeAnnotationId]: {
209
- kind: EntityKind.Relation,
210
- typename,
211
- version: version2,
212
- sourceSchema: sourceDXN,
213
- targetSchema: targetDXN
214
- },
215
- // TODO(dmaretskyi): TypeIdentifierAnnotationId?
216
- [SchemaAST2.JSONSchemaAnnotationId]: makeTypeJsonSchemaAnnotation({
217
- kind: EntityKind.Relation,
218
- typename,
219
- version: version2,
220
- relationSource: sourceDXN,
221
- relationTarget: targetDXN
222
- })
223
- });
224
- return makeEchoTypeSchema(fields, ast, typename, version2, EntityKind.Relation);
225
- };
226
247
  };
227
- var getDXNForRelationSchemaRef = (schema) => {
228
- assertArgument(Schema4.isSchema(schema), "schema");
229
- const identifier = getTypeIdentifierAnnotation(schema);
230
- if (identifier) {
231
- return identifier;
248
+ var hasPendingNotifications = (key) => {
249
+ return pendingNotificationKey === key;
250
+ };
251
+ var clearPendingNotifications = (key) => {
252
+ if (pendingNotificationKey === key) {
253
+ pendingNotificationKey = null;
232
254
  }
233
- const typename = getSchemaTypename(schema);
234
- if (!typename) {
235
- throw new Error("Schema must have a typename");
255
+ };
256
+ var executeChange = (contextKey, eventTarget, proxy, callback) => {
257
+ const exitContext = enterChangeContext(contextKey);
258
+ try {
259
+ batchEvents(() => callback(proxy));
260
+ } finally {
261
+ exitContext();
262
+ if (hasPendingNotifications(contextKey)) {
263
+ clearPendingNotifications(contextKey);
264
+ eventTarget[EventId]?.emit();
265
+ }
266
+ for (const ownerTarget of pendingOwnerNotifications) {
267
+ ownerTarget[EventId]?.emit();
268
+ }
269
+ pendingOwnerNotifications.clear();
236
270
  }
237
- return DXN.fromTypename(typename).toString();
238
271
  };
239
272
 
240
- // src/internal/entities/model.ts
241
- var __dxlog_file3 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/entities/model.ts";
242
- var ATTR_SELF_DXN = "@dxn";
243
- var SelfDXNId = Symbol.for("@dxos/echo/DXN");
244
- var ATTR_DELETED = "@deleted";
245
- var ObjectDeletedId = Symbol.for("@dxos/echo/Deleted");
246
- var ObjectVersionId = Symbol.for("@dxos/echo/Version");
247
- var ObjectDatabaseId = Symbol.for("@dxos/echo/Database");
248
- function assertObjectModel(obj) {
249
- invariant3(typeof obj === "object" && obj !== null, "Invalid object model: not an object", {
250
- F: __dxlog_file3,
251
- L: 119,
252
- S: this,
253
- A: [
254
- "typeof obj === 'object' && obj !== null",
255
- "'Invalid object model: not an object'"
256
- ]
257
- });
258
- assumeType(obj);
259
- invariant3(ObjectId.isValid(obj.id), "Invalid object model: invalid id", {
260
- F: __dxlog_file3,
261
- L: 121,
262
- S: this,
263
- A: [
264
- "ObjectId.isValid(obj.id)",
265
- "'Invalid object model: invalid id'"
266
- ]
267
- });
268
- invariant3(obj[TypeId] === void 0 || obj[TypeId] instanceof DXN2, "Invalid object model: invalid type", {
269
- F: __dxlog_file3,
270
- L: 122,
271
- S: this,
272
- A: [
273
- "obj[TypeId] === undefined || obj[TypeId] instanceof DXN",
274
- "'Invalid object model: invalid type'"
275
- ]
276
- });
277
- invariant3(obj[KindId] === EntityKind.Object || obj[KindId] === EntityKind.Relation, "Invalid object model: invalid entity kind", {
278
- F: __dxlog_file3,
279
- L: 123,
280
- S: this,
281
- A: [
282
- "obj[KindId] === EntityKind.Object || obj[KindId] === EntityKind.Relation",
283
- "'Invalid object model: invalid entity kind'"
284
- ]
273
+ // src/internal/common/proxy/define-hidden-property.ts
274
+ var defineHiddenProperty = (object, key, value2) => {
275
+ Object.defineProperty(object, key, {
276
+ enumerable: false,
277
+ configurable: true,
278
+ value: value2
285
279
  });
286
- if (obj[KindId] === EntityKind.Relation) {
287
- invariant3(obj[RelationSourceDXNId] instanceof DXN2, "Invalid object model: invalid relation source", {
288
- F: __dxlog_file3,
289
- L: 129,
290
- S: this,
291
- A: [
292
- "obj[RelationSourceDXNId] instanceof DXN",
293
- "'Invalid object model: invalid relation source'"
294
- ]
295
- });
296
- invariant3(obj[RelationTargetDXNId] instanceof DXN2, "Invalid object model: invalid relation target", {
297
- F: __dxlog_file3,
298
- L: 130,
299
- S: this,
300
- A: [
301
- "obj[RelationTargetDXNId] instanceof DXN",
302
- "'Invalid object model: invalid relation target'"
303
- ]
304
- });
305
- invariant3(!(obj[RelationSourceId] instanceof DXN2), "Invalid object model: source pointer is a DXN", {
280
+ };
281
+
282
+ // src/internal/common/proxy/errors.ts
283
+ var MutationOutsideChangeContextError = class extends Error {
284
+ constructor(operation, suggestion) {
285
+ super(`Cannot ${operation} outside of Obj.change(). Use Obj.change(obj, (mutableObj) => { ${suggestion} }) instead.`);
286
+ this.name = "MutationOutsideChangeContextError";
287
+ }
288
+ };
289
+ var createPropertySetError = (prop) => {
290
+ return new MutationOutsideChangeContextError(`modify object property "${String(prop)}"`, `mutableObj.${String(prop)} = value;`);
291
+ };
292
+ var createPropertyDeleteError = (prop) => {
293
+ return new MutationOutsideChangeContextError(`delete object property "${String(prop)}"`, `delete mutableObj.${String(prop)};`);
294
+ };
295
+ var createArrayMethodError = (method) => {
296
+ return new MutationOutsideChangeContextError(`call array.${method}()`, `mutableObj.array.${method}(...);`);
297
+ };
298
+
299
+ // src/internal/common/proxy/json-serializer.ts
300
+ import { invariant as invariant3 } from "@dxos/invariant";
301
+ import { DXN } from "@dxos/keys";
302
+ import { deepMapValues } from "@dxos/util";
303
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/common/proxy/json-serializer.ts";
304
+ var attachTypedJsonSerializer = (obj) => {
305
+ const descriptor = Object.getOwnPropertyDescriptor(obj, "toJSON");
306
+ if (descriptor) {
307
+ return;
308
+ }
309
+ Object.defineProperty(obj, "toJSON", {
310
+ value: typedJsonSerializer,
311
+ writable: false,
312
+ enumerable: false,
313
+ // Setting `configurable` to false breaks proxy invariants, should be fixable.
314
+ configurable: true
315
+ });
316
+ };
317
+ var typedJsonSerializer = function() {
318
+ const { id, ...rest } = this;
319
+ const result = {
320
+ id
321
+ };
322
+ if (this[TypeId]) {
323
+ result[ATTR_TYPE] = this[TypeId].toString();
324
+ }
325
+ if (this[MetaId]) {
326
+ result[ATTR_META] = serializeMeta(this[MetaId]);
327
+ }
328
+ if (this[SelfDXNId]) {
329
+ result[ATTR_SELF_DXN] = this[SelfDXNId].toString();
330
+ }
331
+ if (this[RelationSourceDXNId]) {
332
+ const sourceDXN = this[RelationSourceDXNId];
333
+ invariant3(sourceDXN instanceof DXN, void 0, {
306
334
  F: __dxlog_file3,
307
- L: 131,
335
+ L: 64,
308
336
  S: this,
309
337
  A: [
310
- "!(obj[RelationSourceId] instanceof DXN)",
311
- "'Invalid object model: source pointer is a DXN'"
338
+ "sourceDXN instanceof DXN",
339
+ ""
312
340
  ]
313
341
  });
314
- invariant3(!(obj[RelationTargetId] instanceof DXN2), "Invalid object model: target pointer is a DXN", {
342
+ result[ATTR_RELATION_SOURCE] = sourceDXN.toString();
343
+ }
344
+ if (this[RelationTargetDXNId]) {
345
+ const targetDXN = this[RelationTargetDXNId];
346
+ invariant3(targetDXN instanceof DXN, void 0, {
315
347
  F: __dxlog_file3,
316
- L: 132,
348
+ L: 69,
317
349
  S: this,
318
350
  A: [
319
- "!(obj[RelationTargetId] instanceof DXN)",
320
- "'Invalid object model: target pointer is a DXN'"
351
+ "targetDXN instanceof DXN",
352
+ ""
321
353
  ]
322
354
  });
355
+ result[ATTR_RELATION_TARGET] = targetDXN.toString();
323
356
  }
324
- }
325
-
326
- // src/internal/entities/object.ts
327
- import * as Schema5 from "effect/Schema";
328
- import * as SchemaAST3 from "effect/SchemaAST";
329
- import { invariant as invariant4 } from "@dxos/invariant";
330
- var __dxlog_file4 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/entities/object.ts";
331
- var EchoObjectSchema = ({ typename, version: version2 }) => {
332
- return (self) => {
333
- invariant4(typeof TypeAnnotationId === "symbol", "Sanity.", {
334
- F: __dxlog_file4,
335
- L: 37,
336
- S: void 0,
337
- A: [
338
- "typeof TypeAnnotationId === 'symbol'",
339
- "'Sanity.'"
340
- ]
341
- });
342
- invariant4(SchemaAST3.isTypeLiteral(self.ast), "Schema must be a TypeLiteral.", {
343
- F: __dxlog_file4,
344
- L: 38,
345
- S: void 0,
346
- A: [
347
- "SchemaAST.isTypeLiteral(self.ast)",
348
- "'Schema must be a TypeLiteral.'"
349
- ]
350
- });
351
- const fields = self.fields ?? {};
352
- const schemaWithId = Schema5.extend(self, Schema5.Struct({
353
- id: Schema5.String
354
- }));
355
- const ast = SchemaAST3.annotations(schemaWithId.ast, {
356
- // TODO(dmaretskyi): `extend` kills the annotations.
357
- ...self.ast.annotations,
358
- [TypeAnnotationId]: {
359
- kind: EntityKind.Object,
360
- typename,
361
- version: version2
362
- },
363
- // TODO(dmaretskyi): TypeIdentifierAnnotationId?
364
- [SchemaAST3.JSONSchemaAnnotationId]: makeTypeJsonSchemaAnnotation({
365
- kind: EntityKind.Object,
366
- typename,
367
- version: version2
368
- })
369
- });
370
- return makeEchoTypeSchema(fields, ast, typename, version2, EntityKind.Object);
371
- };
357
+ Object.assign(result, serializeData(rest));
358
+ return result;
372
359
  };
373
-
374
- // src/internal/entities/util.ts
375
- import * as Schema6 from "effect/Schema";
376
- import { assertArgument as assertArgument2, invariant as invariant5 } from "@dxos/invariant";
377
- import { DXN as DXN3, ObjectId as ObjectId2 } from "@dxos/keys";
378
- import { assumeType as assumeType2 } from "@dxos/util";
379
- var __dxlog_file5 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/entities/util.ts";
380
- var getObjectDXN = (object) => {
381
- invariant5(!Schema6.isSchema(object), "schema not allowed in this function", {
382
- F: __dxlog_file5,
383
- L: 19,
384
- S: void 0,
385
- A: [
386
- "!Schema.isSchema(object)",
387
- "'schema not allowed in this function'"
388
- ]
360
+ var serializeData = (data) => {
361
+ return deepMapValues(data, (value2, recurse) => {
362
+ if (Ref.isRef(value2)) {
363
+ return value2.noInline().encode();
364
+ }
365
+ return recurse(value2);
389
366
  });
390
- assertArgument2(typeof object === "object" && object != null, "object", "expected object");
391
- assumeType2(object);
392
- if (object[SelfDXNId]) {
393
- invariant5(object[SelfDXNId] instanceof DXN3, "Invalid object model: invalid self dxn", {
394
- F: __dxlog_file5,
395
- L: 24,
396
- S: void 0,
397
- A: [
398
- "object[SelfDXNId] instanceof DXN",
399
- "'Invalid object model: invalid self dxn'"
400
- ]
401
- });
402
- return object[SelfDXNId];
367
+ };
368
+ var serializeMeta = (meta) => {
369
+ return deepMapValues(meta, (value2, recurse) => recurse(value2));
370
+ };
371
+
372
+ // src/internal/common/proxy/make-object.ts
373
+ import { ObjectId } from "@dxos/keys";
374
+
375
+ // src/internal/common/proxy/proxy-utils.ts
376
+ import { invariant as invariant4 } from "@dxos/invariant";
377
+ var __dxlog_file4 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/common/proxy/proxy-utils.ts";
378
+ var symbolIsProxy = /* @__PURE__ */ Symbol.for("@dxos/schema/Proxy");
379
+ var isProxy = (value2) => !!value2?.[symbolIsProxy];
380
+ var isValidProxyTarget = (value2) => {
381
+ if (value2 == null || value2[symbolIsProxy]) {
382
+ return false;
403
383
  }
404
- if (!ObjectId2.isValid(object.id)) {
405
- throw new TypeError("Object id is not valid.");
384
+ if (Array.isArray(value2)) {
385
+ return true;
406
386
  }
407
- return DXN3.fromLocalObjectId(object.id);
387
+ return typeof value2 === "object" && Object.getPrototypeOf(value2) === Object.prototype;
408
388
  };
409
-
410
- // src/internal/api/entity.ts
411
- var __dxlog_file6 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/api/entity.ts";
412
- var getDXN = (entity) => {
413
- const dxn = getObjectDXN(entity);
414
- invariant6(dxn != null, "Invalid entity.", {
415
- F: __dxlog_file6,
416
- L: 18,
389
+ var getProxySlot = (proxy) => {
390
+ const value2 = proxy[symbolIsProxy];
391
+ invariant4(value2 instanceof ProxyHandlerSlot, void 0, {
392
+ F: __dxlog_file4,
393
+ L: 32,
417
394
  S: void 0,
418
395
  A: [
419
- "dxn != null",
420
- "'Invalid entity.'"
396
+ "value instanceof ProxyHandlerSlot",
397
+ ""
421
398
  ]
422
399
  });
423
- return dxn;
424
- };
425
- var getDatabase = (entity) => {
426
- assumeType3(entity);
427
- return entity[ObjectDatabaseId];
400
+ return value2;
428
401
  };
429
-
430
- // src/internal/api/meta.ts
431
- import { assertArgument as assertArgument3, invariant as invariant7 } from "@dxos/invariant";
432
- var __dxlog_file7 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/api/meta.ts";
433
- function getMetaChecked(entity) {
434
- assertArgument3(entity, "entity", "Should be an entity.");
435
- const meta = getMeta(entity);
436
- invariant7(meta != null, "Invalid entity.", {
437
- F: __dxlog_file7,
438
- L: 34,
439
- S: this,
440
- A: [
441
- "meta != null",
442
- "'Invalid entity.'"
443
- ]
444
- });
445
- return meta;
446
- }
447
- var getKeys = (entity, source) => {
448
- assertArgument3(entity, "entity", "Should be an entity.");
449
- const meta = getMetaChecked(entity);
450
- invariant7(meta != null, "Invalid entity.", {
451
- F: __dxlog_file7,
452
- L: 45,
453
- S: void 0,
454
- A: [
455
- "meta != null",
456
- "'Invalid entity.'"
457
- ]
458
- });
459
- return meta.keys.filter((key) => key.source === source);
402
+ var getProxyTarget = (proxy) => {
403
+ return getProxySlot(proxy).target;
460
404
  };
461
- var deleteKeys = (entity, source) => {
462
- const meta = getMetaChecked(entity);
463
- for (let i = 0; i < meta.keys.length; i++) {
464
- if (meta.keys[i].source === source) {
465
- meta.keys.splice(i, 1);
466
- i--;
467
- }
468
- }
405
+ var getProxyHandler = (proxy) => {
406
+ return getProxySlot(proxy).handler;
469
407
  };
470
- var addTag = (entity, tag) => {
471
- const meta = getMetaChecked(entity);
472
- meta.tags ??= [];
473
- meta.tags.push(tag);
408
+ var dangerouslySetProxyId = (obj, id) => {
409
+ getProxySlot(obj).target.id = id;
474
410
  };
475
- var removeTag = (entity, tag) => {
476
- const meta = getMetaChecked(entity);
477
- if (!meta.tags) {
478
- return;
479
- }
480
- for (let i = 0; i < meta.tags.length; i++) {
481
- if (meta.tags[i] === tag) {
482
- meta.tags.splice(i, 1);
483
- i--;
484
- }
411
+ var createProxy = (target, handler) => {
412
+ const existingProxy = handler._proxyMap.get(target);
413
+ if (existingProxy) {
414
+ return existingProxy;
485
415
  }
416
+ const proxy = new Proxy(target, new ProxyHandlerSlot(target, handler));
417
+ handler.init(target);
418
+ handler._proxyMap.set(target, proxy);
419
+ return proxy;
486
420
  };
487
-
488
- // src/internal/api/sorting.ts
489
- var compare = (a, b) => {
490
- if (a == null) {
491
- return b == null ? 0 : 1;
492
- }
493
- if (b == null) {
494
- return -1;
421
+ var ProxyHandlerSlot = class {
422
+ target;
423
+ _handler;
424
+ /**
425
+ * @param target Original object.
426
+ * @param _handler Handles intercepted operations.
427
+ */
428
+ constructor(target, _handler) {
429
+ this.target = target;
430
+ this._handler = _handler;
495
431
  }
496
- return a.localeCompare(b);
497
- };
498
- var sortByLabel = (a, b) => compare(getLabel(a), getLabel(b));
499
- var sortByTypename = (a, b) => compare(getTypename(a), getTypename(b));
500
- var sort = (...comparators) => {
501
- return (a, b) => {
502
- for (const comparator of comparators) {
503
- const result = comparator(a, b);
504
- if (result !== 0) {
505
- return result;
432
+ get handler() {
433
+ invariant4(this._handler, void 0, {
434
+ F: __dxlog_file4,
435
+ L: 91,
436
+ S: this,
437
+ A: [
438
+ "this._handler",
439
+ ""
440
+ ]
441
+ });
442
+ return this._handler;
443
+ }
444
+ // TODO(burdon): Requires comment.
445
+ setHandler(handler) {
446
+ this._handler = handler;
447
+ }
448
+ /**
449
+ * Get value.
450
+ */
451
+ get(target, prop, receiver) {
452
+ if (prop === symbolIsProxy) {
453
+ return this;
454
+ }
455
+ if (!this._handler || !this._handler.get) {
456
+ return Reflect.get(target, prop, receiver);
457
+ }
458
+ return this._handler.get(target, prop, receiver);
459
+ }
460
+ static {
461
+ const TRAPS = [
462
+ "apply",
463
+ "construct",
464
+ "defineProperty",
465
+ "deleteProperty",
466
+ "get",
467
+ "getOwnPropertyDescriptor",
468
+ "getPrototypeOf",
469
+ "has",
470
+ "isExtensible",
471
+ "ownKeys",
472
+ "preventExtensions",
473
+ "set",
474
+ "setPrototypeOf"
475
+ ];
476
+ for (const trap of TRAPS) {
477
+ if (trap === "get") {
478
+ continue;
506
479
  }
480
+ Object.defineProperty(this.prototype, trap, {
481
+ enumerable: false,
482
+ value: function(...args) {
483
+ if (!this._handler || !this._handler[trap]) {
484
+ return Reflect[trap](...args);
485
+ }
486
+ return this._handler[trap].apply(this._handler, args);
487
+ }
488
+ });
507
489
  }
508
- return 0;
509
- };
490
+ }
510
491
  };
511
492
 
512
- // src/internal/api/version.ts
513
- import { assertArgument as assertArgument4 } from "@dxos/invariant";
514
- var unversioned = {
515
- [VersionTypeId]: {},
516
- versioned: false
517
- };
518
- var isVersion = (entity) => {
519
- return entity != null && typeof entity === "object" && VersionTypeId in entity;
493
+ // src/internal/common/proxy/typed-handler.ts
494
+ import * as Schema4 from "effect/Schema";
495
+ import * as SchemaAST3 from "effect/SchemaAST";
496
+ import { Event } from "@dxos/async";
497
+ import { inspectCustom } from "@dxos/debug";
498
+ import { invariant as invariant7 } from "@dxos/invariant";
499
+
500
+ // src/internal/common/proxy/ownership.ts
501
+ import { invariant as invariant5 } from "@dxos/invariant";
502
+ var __dxlog_file5 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/common/proxy/ownership.ts";
503
+ var EchoOwner = /* @__PURE__ */ Symbol.for("@dxos/echo/Owner");
504
+ var getRawTarget = (value2) => {
505
+ return isProxy(value2) ? getProxyTarget(value2) : value2;
520
506
  };
521
- var version = (entity) => {
522
- const ver = entity[ObjectVersionId];
523
- if (ver === void 0) {
524
- return unversioned;
525
- }
526
- return ver;
507
+ var getOwner = (value2) => {
508
+ return value2?.[EchoOwner];
527
509
  };
528
- var versionValid = (ver) => {
529
- assertArgument4(isVersion(ver), "version", "Invalid version object");
530
- return !!ver.versioned;
510
+ var setMetaOwner = (metaTarget, parent) => {
511
+ defineHiddenProperty(metaTarget, EchoOwner, parent);
531
512
  };
532
- var compareVersions = (version1, version2) => {
533
- assertArgument4(isVersion(version1), "version1", "Invalid version object");
534
- assertArgument4(isVersion(version2), "version2", "Invalid version object");
535
- if (!versionValid(version1) || !versionValid(version2)) {
536
- return "unversioned";
513
+ var setOwnerRecursive = (value2, owner, options = {}) => {
514
+ const { visited = /* @__PURE__ */ new Set(), depth = 0, allowedPreviousOwner } = options;
515
+ if (value2 == null || typeof value2 !== "object") {
516
+ return;
537
517
  }
538
- if (version1.automergeHeads?.length !== version2.automergeHeads?.length) {
539
- return "different";
518
+ const actualValue = getRawTarget(value2);
519
+ if (visited.has(actualValue)) {
520
+ return;
540
521
  }
541
- if (version1.automergeHeads?.some((head) => !version2.automergeHeads?.includes(head))) {
542
- return "different";
522
+ visited.add(actualValue);
523
+ const existingOwner = getOwner(actualValue);
524
+ const isRootEchoObject = EventId in actualValue;
525
+ let newAllowedPreviousOwner = allowedPreviousOwner;
526
+ if (isRootEchoObject && depth === 0) {
527
+ newAllowedPreviousOwner = actualValue;
543
528
  }
544
- return "equal";
545
- };
546
- var encodeVersion = (ver) => {
547
- return JSON.stringify(ver);
548
- };
549
- var decodeVersion = (ver) => {
550
- const parsed = JSON.parse(ver);
551
- parsed[VersionTypeId] = {};
552
- return parsed;
553
- };
554
-
555
- // src/internal/json-schema/annotations.ts
556
- import * as SchemaAST4 from "effect/SchemaAST";
557
- var CustomAnnotations = {
558
- format: FormatAnnotationId,
559
- currency: CurrencyAnnotationId
560
- };
561
- var DecodedAnnotations = {
562
- title: SchemaAST4.TitleAnnotationId,
563
- description: SchemaAST4.DescriptionAnnotationId
564
- };
565
- var EchoAnnotations = {
566
- // TODO(dmaretskyi): `FieldLookupAnnotationId` might go here, but lets remove it entirely and use LabelAnnotation instead.
567
- meta: PropertyMetaAnnotationId,
568
- generator: GeneratorAnnotationId,
569
- labelProp: LabelAnnotationId
570
- };
571
-
572
- // src/internal/json-schema/json-schema-type.ts
573
- import * as Schema7 from "effect/Schema";
574
- import { JsonPath } from "@dxos/effect";
575
- var SimpleTypes = Schema7.Literal("array", "boolean", "integer", "null", "number", "object", "string");
576
- var NonNegativeInteger = Schema7.Number.pipe(Schema7.greaterThanOrEqualTo(0));
577
- var StringArray = Schema7.Array(Schema7.String);
578
- var JsonSchemaOrBoolean = Schema7.Union(Schema7.suspend(() => JsonSchemaType), Schema7.Boolean);
579
- var JsonSchemaEchoAnnotations = Schema7.Struct({
580
- /**
581
- * Label for this schema.
582
- * Mapped from {@link LabelAnnotationId}.
583
- */
584
- labelProp: Schema7.optional(Schema7.Union(JsonPath, Schema7.Array(JsonPath))),
585
- /**
586
- * Generator function for this schema.
587
- * Mapped from {@link GeneratorAnnotationId}.
588
- */
589
- generator: Schema7.optional(Schema7.Union(Schema7.String, Schema7.Tuple(Schema7.String, Schema7.Number))),
590
- /**
591
- * {@link PropertyMeta} annotations get serialized here.
592
- */
593
- meta: Schema7.optional(Schema7.Record({
594
- key: Schema7.String,
595
- value: Schema7.Any
596
- })),
597
- /**
598
- * @deprecated
599
- */
600
- // TODO(dmaretskyi): We risk old schema not passing validation due to the extra fields. Remove when we are sure this is safe.
601
- type: Schema7.optional(Schema7.Struct({
602
- typename: Schema7.String,
603
- version: Schema7.String,
604
- // Not used.
605
- schemaId: Schema7.optional(Schema7.String)
606
- })),
607
- /**
608
- * @deprecated Superseded by `meta`.
609
- */
610
- annotations: Schema7.optional(Schema7.Record({
611
- key: Schema7.String,
612
- value: Schema7.Any
613
- }))
614
- });
615
- var _JsonSchemaType = Schema7.Struct({
616
- /**
617
- * Identifier for this schema.
618
- * This schema might be referenced by $ref clause in other schemas.
619
- */
620
- // TODO(dmaretskyi): Specify how the ids are generated.
621
- // TODO(dmaretskyi): For type dxns, should this include the version?
622
- $id: Schema7.optional(Schema7.String),
623
- /**
624
- * Schema of this schema.
625
- * Set to "https://json-schema.org/draft-07/schema".
626
- */
627
- $schema: Schema7.optional(Schema7.String),
628
- /**
629
- * Reference to another schema.
630
- */
631
- $ref: Schema7.optional(Schema7.String),
632
- /**
633
- * Comments are ignored when interpreting the schema.
634
- */
635
- $comment: Schema7.optional(Schema7.String),
636
- /**
637
- * Defines whether this schema is an object schema or a relation schema.
638
- */
639
- entityKind: Schema7.optional(EntityKindSchema),
640
- /**
641
- * Typename of this schema.
642
- * Only on schema representing an ECHO object.
643
- *
644
- * @example 'example.com/type/MyType'
645
- */
646
- typename: Schema7.optional(Schema7.String),
647
- /**
648
- * Version of this schema.
649
- * Custom dialect for ECHO.
650
- */
651
- version: Schema7.optional(Schema7.String),
652
- /**
653
- * Target of this relation.
654
- * Only for relation schemas.
655
- * The referenced schema must be an object schema.
656
- */
657
- relationTarget: Schema7.optional(Schema7.suspend(() => JsonSchemaType)),
658
- /**
659
- * Source of this relation.
660
- * Only for relation schemas.
661
- * The referenced schema must be an object schema.
662
- */
663
- relationSource: Schema7.optional(Schema7.suspend(() => JsonSchemaType)),
664
- /**
665
- * Title of this schema.
666
- */
667
- title: Schema7.optional(Schema7.String),
668
- /**
669
- * Description of this schema.
670
- */
671
- description: Schema7.optional(Schema7.String),
672
- /**
673
- * Whether this schema is read-only.
674
- */
675
- readOnly: Schema7.optional(Schema7.Boolean),
676
- /**
677
- * Whether this schema is write-only.
678
- */
679
- writeOnly: Schema7.optional(Schema7.Boolean),
680
- /**
681
- * Examples of instances of this schema.
682
- */
683
- examples: Schema7.optional(Schema7.Array(Schema7.Any)),
684
- /**
685
- * Default value for this schema.
686
- */
687
- default: Schema7.optional(Schema7.Any),
688
- /**
689
- * This schema only matches values that are equal to this value.
690
- */
691
- const: Schema7.optional(Schema7.Any),
692
- /**
693
- * This schema only matches one of the values in this array.
694
- */
695
- enum: Schema7.optional(Schema7.Array(Schema7.Any)),
696
- /**
697
- * Base type of the schema.
698
- */
699
- type: Schema7.optional(Schema7.Union(SimpleTypes, Schema7.Array(SimpleTypes))),
700
- //
701
- // Numbers.
702
- //
703
- multipleOf: Schema7.optional(Schema7.Number.pipe(Schema7.greaterThan(0))),
704
- maximum: Schema7.optional(Schema7.Number),
705
- exclusiveMaximum: Schema7.optional(Schema7.Number),
706
- minimum: Schema7.optional(Schema7.Number),
707
- exclusiveMinimum: Schema7.optional(Schema7.Number),
708
- //
709
- // Strings.
710
- //
711
- maxLength: Schema7.optional(NonNegativeInteger),
712
- /**
713
- * Regex pattern for strings.
714
- */
715
- pattern: Schema7.optional(Schema7.String.pipe(FormatAnnotation.set(TypeFormat.Regex))),
716
- /**
717
- * Serialized from {@link FormatAnnotationId}.
718
- */
719
- format: Schema7.optional(Schema7.String),
720
- //
721
- // Arrays
722
- //
723
- minLength: Schema7.optional(NonNegativeInteger),
724
- items: Schema7.optional(Schema7.Union(Schema7.suspend(() => JsonSchemaType), Schema7.Array(Schema7.suspend(() => JsonSchemaType)))),
725
- additionalItems: Schema7.optional(Schema7.Union(Schema7.suspend(() => JsonSchemaType), Schema7.Boolean)),
726
- maxItems: Schema7.optional(NonNegativeInteger),
727
- minItems: Schema7.optional(NonNegativeInteger),
728
- uniqueItems: Schema7.optional(Schema7.Boolean),
729
- contains: Schema7.optional(Schema7.suspend(() => JsonSchemaType)),
730
- //
731
- // Objects
732
- //
733
- maxProperties: Schema7.optional(NonNegativeInteger),
734
- minProperties: Schema7.optional(NonNegativeInteger),
735
- required: Schema7.optional(StringArray),
736
- /**
737
- * Non-standard JSON Schema extension.
738
- * Defines the order of properties in the object.
739
- * The unmentioned properties are placed at the end.
740
- *
741
- * Related: https://github.com/json-schema/json-schema/issues/119
742
- */
743
- propertyOrder: Schema7.optional(StringArray),
744
- additionalProperties: Schema7.optional(JsonSchemaOrBoolean),
745
- properties: Schema7.optional(Schema7.Record({
746
- key: Schema7.String,
747
- value: Schema7.suspend(() => JsonSchemaType)
748
- })),
749
- patternProperties: Schema7.optional(Schema7.Record({
750
- key: Schema7.String,
751
- value: Schema7.suspend(() => JsonSchemaType)
752
- })),
753
- propertyNames: Schema7.optional(Schema7.suspend(() => JsonSchemaType)),
754
- definitions: Schema7.optional(Schema7.Record({
755
- key: Schema7.String,
756
- value: Schema7.suspend(() => JsonSchemaType)
757
- })),
758
- dependencies: Schema7.optional(Schema7.Record({
759
- key: Schema7.String,
760
- value: Schema7.suspend(() => Schema7.Union(Schema7.String, StringArray, JsonSchemaType)).annotations({
761
- identifier: "dependency",
762
- description: "Dependency"
763
- })
764
- })),
765
- contentMediaType: Schema7.optional(Schema7.String),
766
- contentEncoding: Schema7.optional(Schema7.String),
767
- if: Schema7.optional(Schema7.suspend(() => JsonSchemaType)),
768
- then: Schema7.optional(Schema7.suspend(() => JsonSchemaType)),
769
- else: Schema7.optional(Schema7.suspend(() => JsonSchemaType)),
770
- allOf: Schema7.optional(Schema7.Array(Schema7.suspend(() => JsonSchemaType))),
771
- anyOf: Schema7.optional(Schema7.Array(Schema7.suspend(() => JsonSchemaType))),
772
- oneOf: Schema7.optional(Schema7.Array(Schema7.suspend(() => JsonSchemaType))),
773
- not: Schema7.optional(Schema7.suspend(() => JsonSchemaType)),
774
- $defs: Schema7.optional(Schema7.Record({
775
- key: Schema7.String,
776
- value: Schema7.suspend(() => JsonSchemaType)
777
- })),
778
- //
779
- // ECHO extensions.
780
- //
781
- currency: Schema7.optional(Schema7.String),
782
- reference: Schema7.optional(Schema7.Struct({
783
- schema: Schema7.suspend(() => JsonSchemaType),
784
- schemaVersion: Schema7.optional(Schema7.String),
785
- schemaObject: Schema7.optional(Schema7.String)
786
- })),
787
- /**
788
- * ECHO-specific annotations.
789
- */
790
- // TODO(dmaretskyi): Since we are adding a lot of new extensions to the JSON Schema, it is safer to namespace them here.
791
- annotations: Schema7.optional(JsonSchemaEchoAnnotations),
792
- /**
793
- * @deprecated Use `annotations` instead.
794
- */
795
- echo: Schema7.optional(JsonSchemaEchoAnnotations)
796
- }).annotations({
797
- identifier: "jsonSchema",
798
- description: "JSON Schema"
799
- });
800
- var JsonSchemaFields = Object.keys(_JsonSchemaType.fields);
801
- var JsonSchemaType = _JsonSchemaType;
802
- var getSchemaProperty = (schema, property) => {
803
- return schema.properties?.[property];
804
- };
805
- var setSchemaProperty = (schema, property, value2) => {
806
- schema.properties ??= {};
807
- schema.properties[property] = value2;
808
- return schema;
809
- };
810
- var ECHO_ANNOTATIONS_NS_DEPRECATED_KEY = "echo";
811
- var ECHO_ANNOTATIONS_NS_KEY = "annotations";
812
- var getNormalizedEchoAnnotations = (obj) => {
813
- if (obj[ECHO_ANNOTATIONS_NS_KEY] != null && obj[ECHO_ANNOTATIONS_NS_DEPRECATED_KEY] != null) {
814
- return normalizeEchoAnnotations({
815
- ...obj[ECHO_ANNOTATIONS_NS_DEPRECATED_KEY],
816
- ...obj[ECHO_ANNOTATIONS_NS_KEY]
529
+ if (!isRootEchoObject) {
530
+ const ownershipAllowed = existingOwner == null || existingOwner === owner || existingOwner === newAllowedPreviousOwner;
531
+ invariant5(ownershipAllowed, "Cannot reassign ownership of a nested record to a different ECHO object. Use deep copy first.", {
532
+ F: __dxlog_file5,
533
+ L: 102,
534
+ S: void 0,
535
+ A: [
536
+ "ownershipAllowed",
537
+ "'Cannot reassign ownership of a nested record to a different ECHO object. Use deep copy first.'"
538
+ ]
817
539
  });
818
- } else if (obj[ECHO_ANNOTATIONS_NS_KEY] != null) {
819
- return normalizeEchoAnnotations(obj[ECHO_ANNOTATIONS_NS_KEY]);
820
- } else if (obj[ECHO_ANNOTATIONS_NS_DEPRECATED_KEY] != null) {
821
- return normalizeEchoAnnotations(obj[ECHO_ANNOTATIONS_NS_DEPRECATED_KEY]);
822
- } else {
823
- return void 0;
824
540
  }
825
- };
826
- var normalizeEchoAnnotations = (obj) => {
827
- if (!obj.annotations) {
828
- return obj;
541
+ defineHiddenProperty(actualValue, EchoOwner, owner);
542
+ const recursiveOptions = {
543
+ visited,
544
+ depth: depth + 1,
545
+ allowedPreviousOwner: newAllowedPreviousOwner
546
+ };
547
+ if (Array.isArray(actualValue)) {
548
+ for (const item of actualValue) {
549
+ if (isValidProxyTarget(item) || isProxy(item)) {
550
+ setOwnerRecursive(item, owner, recursiveOptions);
551
+ }
552
+ }
829
553
  } else {
830
- const res = {
831
- ...obj,
832
- meta: {
833
- ...obj.annotations,
834
- ...obj.meta ?? {}
554
+ for (const key in actualValue) {
555
+ if (Object.prototype.hasOwnProperty.call(actualValue, key)) {
556
+ const nested = actualValue[key];
557
+ if (isValidProxyTarget(nested) || isProxy(nested)) {
558
+ setOwnerRecursive(nested, owner, recursiveOptions);
559
+ }
835
560
  }
836
- };
837
- delete res.annotations;
838
- return res;
561
+ }
839
562
  }
840
563
  };
841
-
842
- // src/internal/json-schema/json-schema-normalize.ts
843
- var normalizeSchema = (schema) => {
844
- const copy = structuredClone(schema);
845
- go(copy);
846
- return copy;
847
- };
848
- var go = (schema) => {
849
- if (typeof schema !== "object" || schema === null) {
850
- return;
851
- }
852
- if (schema.exclusiveMaximum === true) {
853
- schema.exclusiveMaximum = schema.maximum;
854
- delete schema.exclusiveMaximum;
855
- } else if (schema.exclusiveMaximum === false) {
856
- delete schema.exclusiveMaximum;
564
+ var traverseObjectGraph = (value2, visitor, visited = /* @__PURE__ */ new Set()) => {
565
+ if (value2 == null || typeof value2 !== "object") {
566
+ return false;
857
567
  }
858
- if (schema.exclusiveMinimum === true) {
859
- schema.exclusiveMinimum = schema.minimum;
860
- delete schema.exclusiveMinimum;
861
- } else if (schema.exclusiveMinimum === false) {
862
- delete schema.exclusiveMinimum;
568
+ const actualValue = getRawTarget(value2);
569
+ if (visited.has(actualValue)) {
570
+ return false;
863
571
  }
864
- for (const key of Object.keys(schema)) {
865
- if (!JsonSchemaFields.includes(key)) {
866
- delete schema[key];
867
- }
868
- }
869
- if (schema.properties) {
870
- goOnRecord(schema.properties);
871
- }
872
- if (schema.patternProperties) {
873
- goOnRecord(schema.patternProperties);
874
- }
875
- if (schema.propertyNames) {
876
- go(schema.propertyNames);
877
- }
878
- if (schema.definitions) {
879
- goOnRecord(schema.definitions);
880
- }
881
- if (schema.items) {
882
- maybeGoOnArray(schema.items);
883
- }
884
- if (schema.additionalItems) {
885
- maybeGoOnArray(schema.additionalItems);
886
- }
887
- if (schema.contains) {
888
- go(schema.contains);
889
- }
890
- if (schema.if) {
891
- go(schema.if);
892
- }
893
- if (schema.then) {
894
- go(schema.then);
895
- }
896
- if (schema.else) {
897
- go(schema.else);
898
- }
899
- if (schema.allOf) {
900
- maybeGoOnArray(schema.allOf);
901
- }
902
- if (schema.anyOf) {
903
- maybeGoOnArray(schema.anyOf);
904
- }
905
- if (schema.oneOf) {
906
- maybeGoOnArray(schema.oneOf);
907
- }
908
- if (schema.not) {
909
- go(schema.not);
910
- }
911
- if (schema.$defs) {
912
- goOnRecord(schema.$defs);
913
- }
914
- if (schema.reference) {
915
- go(schema.reference.schema);
572
+ visited.add(actualValue);
573
+ if (visitor(actualValue)) {
574
+ return true;
916
575
  }
917
- };
918
- var maybeGoOnArray = (value2) => {
919
- if (Array.isArray(value2)) {
920
- for (const item of value2) {
921
- go(item);
576
+ if (Array.isArray(actualValue)) {
577
+ for (const item of actualValue) {
578
+ if (traverseObjectGraph(item, visitor, visited)) {
579
+ return true;
580
+ }
922
581
  }
923
- } else if (typeof value2 === "object" && value2 !== null) {
924
- go(value2);
925
- }
926
- };
927
- var goOnRecord = (record) => {
928
- for (const key of Object.keys(record)) {
929
- go(record[key]);
930
- }
931
- };
932
-
933
- // src/internal/json-schema/json-schema.ts
934
- import * as Array4 from "effect/Array";
935
- import * as Function from "effect/Function";
936
- import * as JSONSchema from "effect/JSONSchema";
937
- import * as Option2 from "effect/Option";
938
- import * as Schema9 from "effect/Schema";
939
- import * as SchemaAST6 from "effect/SchemaAST";
940
- import { raise as raise2 } from "@dxos/debug";
941
- import { mapAst } from "@dxos/effect";
942
- import { assertArgument as assertArgument6, invariant as invariant9 } from "@dxos/invariant";
943
- import { DXN as DXN5, ObjectId as ObjectId4 } from "@dxos/keys";
944
- import { log } from "@dxos/log";
945
- import { clearUndefined, orderKeys, removeProperties } from "@dxos/util";
946
-
947
- // src/internal/ref/ref.ts
948
- import * as Effect from "effect/Effect";
949
- import * as Option from "effect/Option";
950
- import * as ParseResult from "effect/ParseResult";
951
- import * as Schema8 from "effect/Schema";
952
- import * as SchemaAST5 from "effect/SchemaAST";
953
- import { Event } from "@dxos/async";
954
- import { inspectCustom } from "@dxos/debug";
955
- import { EncodedReference } from "@dxos/echo-protocol";
956
- import { assertArgument as assertArgument5, invariant as invariant8 } from "@dxos/invariant";
957
- import { DXN as DXN4, ObjectId as ObjectId3 } from "@dxos/keys";
958
- var __dxlog_file8 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/ref/ref.ts";
959
- var JSON_SCHEMA_ECHO_REF_ID = "/schemas/echo/ref";
960
- var getSchemaReference = (property) => {
961
- const { $id, reference: { schema: { $ref } = {} } = {} } = property;
962
- if ($id === JSON_SCHEMA_ECHO_REF_ID && $ref) {
963
- return {
964
- typename: DXN4.parse($ref).typename
965
- };
966
- }
967
- };
968
- var createSchemaReference = (typename) => {
969
- return {
970
- $id: JSON_SCHEMA_ECHO_REF_ID,
971
- reference: {
972
- schema: {
973
- $ref: DXN4.fromTypename(typename).toString()
582
+ } else {
583
+ for (const key in actualValue) {
584
+ if (Object.prototype.hasOwnProperty.call(actualValue, key)) {
585
+ if (traverseObjectGraph(actualValue[key], visitor, visited)) {
586
+ return true;
587
+ }
974
588
  }
975
589
  }
976
- };
977
- };
978
- var getReferenceAst = (ast) => {
979
- if (ast._tag !== "Declaration" || !ast.annotations[ReferenceAnnotationId]) {
980
- return void 0;
981
590
  }
982
- return {
983
- typename: ast.annotations[ReferenceAnnotationId].typename,
984
- version: ast.annotations[ReferenceAnnotationId].version
985
- };
591
+ return false;
986
592
  };
987
- var RefTypeId = Symbol("@dxos/echo/internal/Ref");
988
- var Ref = (schema) => {
989
- assertArgument5(Schema8.isSchema(schema), "schema", "Must call with an instance of effect-schema");
990
- const annotation = getTypeAnnotation(schema);
991
- if (annotation == null) {
992
- throw new Error("Reference target must be an ECHO schema.");
593
+ var wouldCreateCycle = (targetRoot, value2) => traverseObjectGraph(value2, (v) => v === targetRoot);
594
+ var hasForeignOwner = (value2, target) => traverseObjectGraph(value2, (v) => {
595
+ const owner = getOwner(v);
596
+ if (owner != null && owner !== target) {
597
+ return true;
993
598
  }
994
- return createEchoReferenceSchema(getTypeIdentifierAnnotation(schema), annotation.typename, annotation.version);
995
- };
996
- Ref.isRef = (obj) => {
997
- return obj && typeof obj === "object" && RefTypeId in obj;
998
- };
999
- Ref.hasObjectId = (id) => (ref) => ref.dxn.isLocalObjectId() && ref.dxn.parts[1] === id;
1000
- Ref.isRefSchema = (schema) => {
1001
- return Ref.isRefSchemaAST(schema.ast);
1002
- };
1003
- Ref.isRefSchemaAST = (ast) => {
1004
- return SchemaAST5.getAnnotation(ast, ReferenceAnnotationId).pipe(Option.isSome);
1005
- };
1006
- Ref.make = (obj) => {
1007
- if (typeof obj !== "object" || obj === null) {
1008
- throw new TypeError("Expected: ECHO object.");
599
+ if (EventId in v && v !== target) {
600
+ return true;
1009
601
  }
1010
- const id = obj.id;
1011
- invariant8(ObjectId3.isValid(id), "Invalid object ID", {
1012
- F: __dxlog_file8,
1013
- L: 222,
602
+ return false;
603
+ });
604
+ var MAX_OWNER_DEPTH = 100;
605
+ var getEchoRoot = (target, depth = 0) => {
606
+ invariant5(depth < MAX_OWNER_DEPTH, "Owner chain too deep - possible circular ownership", {
607
+ F: __dxlog_file5,
608
+ L: 221,
1014
609
  S: void 0,
1015
610
  A: [
1016
- "ObjectId.isValid(id)",
1017
- "'Invalid object ID'"
611
+ "depth < MAX_OWNER_DEPTH",
612
+ "'Owner chain too deep - possible circular ownership'"
1018
613
  ]
1019
614
  });
1020
- const dxn = DXN4.fromLocalObjectId(id);
1021
- return new RefImpl(dxn, obj);
1022
- };
1023
- Ref.fromDXN = (dxn) => {
1024
- assertArgument5(dxn instanceof DXN4, "dxn", "Expected DXN");
1025
- return new RefImpl(dxn);
615
+ if (KindId in target) {
616
+ return target;
617
+ }
618
+ const owner = getOwner(target);
619
+ if (owner) {
620
+ return getEchoRoot(owner, depth + 1);
621
+ }
622
+ return target;
1026
623
  };
1027
- var createEchoReferenceSchema = (echoId, typename, version2) => {
1028
- if (!echoId && !typename) {
1029
- throw new TypeError("Either echoId or typename must be provided.");
624
+ var notifyOwnerChain = (target) => {
625
+ const owner = getOwner(target);
626
+ if (owner) {
627
+ queueOwnerNotification(getEchoRoot(owner));
628
+ notifyOwnerChain(owner);
1030
629
  }
1031
- const referenceInfo = {
1032
- schema: {
1033
- // TODO(dmaretskyi): Include version?
1034
- $ref: echoId ?? DXN4.fromTypename(typename).toString()
1035
- },
1036
- schemaVersion: version2
1037
- };
1038
- const refSchema = Schema8.declare([], {
1039
- encode: () => {
1040
- return (value2) => Effect.gen(function* () {
1041
- if (Ref.isRef(value2)) {
1042
- return EncodedReference.fromDXN(value2.dxn);
1043
- } else if (EncodedReference.isEncodedReference(value2)) {
1044
- return value2;
1045
- }
1046
- throw new Error("Invalid reference");
1047
- });
1048
- },
1049
- decode: () => {
1050
- return (value2) => Effect.gen(function* () {
1051
- const dbService = yield* Effect.serviceOption(Service);
1052
- if (Ref.isRef(value2)) {
1053
- if (Option.isSome(dbService)) {
1054
- return dbService.value.db.makeRef(value2.dxn);
1055
- } else {
1056
- return value2;
1057
- }
1058
- }
1059
- if (!EncodedReference.isEncodedReference(value2)) {
1060
- return yield* Effect.fail(new ParseResult.Unexpected(value2, "reference"));
1061
- }
1062
- if (Option.isSome(dbService)) {
1063
- return dbService.value.db.makeRef(EncodedReference.toDXN(value2));
1064
- } else {
1065
- return Ref.fromDXN(EncodedReference.toDXN(value2));
1066
- }
1067
- });
1068
- }
1069
- }, {
1070
- jsonSchema: {
1071
- // TODO(dmaretskyi): We should remove `$id` and keep `$ref` with a fully qualified name.
1072
- $id: JSON_SCHEMA_ECHO_REF_ID,
1073
- $ref: JSON_SCHEMA_ECHO_REF_ID,
1074
- reference: referenceInfo
1075
- },
1076
- [ReferenceAnnotationId]: {
1077
- typename: typename ?? "",
1078
- version: version2
1079
- }
1080
- });
1081
- return refSchema;
1082
630
  };
1083
- var RefImpl = class _RefImpl {
1084
- #dxn;
1085
- #resolver = void 0;
1086
- #resolved = new Event();
1087
- /**
1088
- * Target is set when the reference is created from a specific object.
1089
- * In this case, the target might not be in the database.
1090
- */
1091
- #target = void 0;
1092
- /**
1093
- * Callback to issue a reactive notification when object is resolved.
1094
- */
1095
- #resolverCallback = () => {
1096
- this.#resolved.emit();
1097
- };
1098
- constructor(dxn, target) {
1099
- this.#dxn = dxn;
1100
- this.#target = target;
631
+
632
+ // src/internal/common/proxy/proxy-types.ts
633
+ var objectData = /* @__PURE__ */ Symbol.for("@dxos/live-object/objectData");
634
+
635
+ // src/internal/common/proxy/reactive-array.ts
636
+ var checkArrayMutationAllowed = (arr, method) => {
637
+ const target = isProxy(arr) ? getProxyTarget(arr) : arr;
638
+ const echoRoot = getEchoRoot(target);
639
+ const isInitialized = echoRoot[ChangeId] === true || EventId in echoRoot;
640
+ if (!isInitialized) {
641
+ return;
1101
642
  }
1102
- /**
1103
- * @inheritdoc
1104
- */
1105
- get dxn() {
1106
- return this.#dxn;
643
+ if (!isInChangeContext(echoRoot)) {
644
+ throw createArrayMethodError(method);
1107
645
  }
1108
- /**
1109
- * @inheritdoc
1110
- */
1111
- get isAvailable() {
1112
- return this.#target !== void 0 || this.#resolver !== void 0;
646
+ };
647
+ var ReactiveArray = class extends Array {
648
+ static get [Symbol.species]() {
649
+ return Array;
1113
650
  }
1114
- /**
1115
- * @inheritdoc
1116
- */
1117
- get target() {
1118
- if (this.#target) {
1119
- return this.#target;
651
+ static {
652
+ const BATCHED_METHODS = [
653
+ "push",
654
+ "pop",
655
+ "shift",
656
+ "unshift",
657
+ "splice",
658
+ "sort",
659
+ "reverse"
660
+ ];
661
+ for (const method of BATCHED_METHODS) {
662
+ Object.defineProperty(this.prototype, method, {
663
+ enumerable: false,
664
+ value: function(...args) {
665
+ checkArrayMutationAllowed(this, method);
666
+ let result;
667
+ batchEvents(() => {
668
+ result = Array.prototype[method].apply(this, args);
669
+ });
670
+ return result;
671
+ }
672
+ });
1120
673
  }
1121
- invariant8(this.#resolver, "Resolver is not set", {
1122
- F: __dxlog_file8,
1123
- L: 390,
1124
- S: this,
1125
- A: [
1126
- "this.#resolver",
1127
- "'Resolver is not set'"
1128
- ]
1129
- });
1130
- return this.#resolver.resolveSync(this.#dxn, true, this.#resolverCallback);
1131
674
  }
675
+ };
676
+
677
+ // src/internal/common/proxy/schema-validator.ts
678
+ import * as Schema3 from "effect/Schema";
679
+ import * as SchemaAST2 from "effect/SchemaAST";
680
+ import { invariant as invariant6 } from "@dxos/invariant";
681
+ import { log } from "@dxos/log";
682
+ var __dxlog_file6 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/common/proxy/schema-validator.ts";
683
+ var SchemaValidator = class {
1132
684
  /**
1133
- * @inheritdoc
685
+ * Recursively check that schema specifies constructions we can handle.
686
+ * Validates there are no ambiguous discriminated union types.
1134
687
  */
1135
- async load() {
1136
- if (this.#target) {
1137
- return this.#target;
1138
- }
1139
- invariant8(this.#resolver, "Resolver is not set", {
1140
- F: __dxlog_file8,
1141
- L: 401,
1142
- S: this,
1143
- A: [
1144
- "this.#resolver",
1145
- "'Resolver is not set'"
1146
- ]
1147
- });
1148
- const obj = await this.#resolver.resolve(this.#dxn);
1149
- if (obj == null) {
1150
- throw new Error("Object not found");
688
+ static validateSchema(schema) {
689
+ const visitAll = (nodes) => nodes.forEach((node) => this.validateSchema(Schema3.make(node)));
690
+ if (SchemaAST2.isUnion(schema.ast)) {
691
+ const typeAstList = schema.ast.types.filter((type) => SchemaAST2.isTypeLiteral(type));
692
+ if (typeAstList.length > 1) {
693
+ getTypeDiscriminators(typeAstList);
694
+ }
695
+ visitAll(typeAstList);
696
+ } else if (SchemaAST2.isTupleType(schema.ast)) {
697
+ const positionalTypes = schema.ast.elements.map((t) => t.type);
698
+ const allTypes = positionalTypes.concat(schema.ast.rest.map((t) => t.type));
699
+ visitAll(allTypes);
700
+ } else if (SchemaAST2.isTypeLiteral(schema.ast)) {
701
+ visitAll(SchemaAST2.getPropertySignatures(schema.ast).map((p) => p.type));
1151
702
  }
1152
- return obj;
1153
703
  }
1154
- /**
1155
- * @inheritdoc
1156
- */
1157
- async tryLoad() {
1158
- if (this.#target) {
1159
- return this.#target;
704
+ static hasTypeAnnotation(rootObjectSchema, property, annotation) {
705
+ try {
706
+ let type = this.getPropertySchema(rootObjectSchema, [
707
+ property
708
+ ]);
709
+ if (SchemaAST2.isTupleType(type.ast)) {
710
+ type = this.getPropertySchema(rootObjectSchema, [
711
+ property,
712
+ "0"
713
+ ]);
714
+ }
715
+ return type.ast.annotations[annotation] != null;
716
+ } catch {
717
+ return false;
1160
718
  }
1161
- invariant8(this.#resolver, "Resolver is not set", {
1162
- F: __dxlog_file8,
1163
- L: 416,
1164
- S: this,
1165
- A: [
1166
- "this.#resolver",
1167
- "'Resolver is not set'"
1168
- ]
1169
- });
1170
- return await this.#resolver.resolve(this.#dxn);
1171
- }
1172
- /**
1173
- * Do not inline the target object in the reference.
1174
- * Makes .target unavailable unless the reference is connected to a database context.
1175
- * Clones the reference object.
1176
- */
1177
- noInline() {
1178
- const ref = new _RefImpl(this.#dxn, void 0);
1179
- ref.#resolver = this.#resolver;
1180
- return ref;
1181
- }
1182
- encode() {
1183
- return {
1184
- "/": this.#dxn.toString(),
1185
- ...this.#target ? {
1186
- target: this.#target
1187
- } : {}
1188
- };
1189
719
  }
1190
- /**
1191
- * Serializes the reference to a JSON object.
1192
- * The serialization format is compatible with the IPLD-style encoded references.
1193
- * When a reference has a saved target (i.e. the target or object holding the reference is not in the database),
1194
- * the target is included in the serialized object.
1195
- */
1196
- toJSON() {
1197
- return this.encode();
1198
- }
1199
- toString() {
1200
- if (this.#target) {
1201
- return `Ref(${this.#target.toString()})`;
1202
- }
1203
- return `Ref(${this.#dxn.toString()})`;
1204
- }
1205
- [inspectCustom] = (depth, options, inspect) => {
1206
- return this.toString();
1207
- };
1208
- [RefTypeId] = refVariance;
1209
- /**
1210
- * Internal method to set the resolver.
1211
- *
1212
- * @internal
1213
- */
1214
- _setResolver(resolver) {
1215
- this.#resolver = resolver;
1216
- }
1217
- /**
1218
- * @internal
1219
- */
1220
- _getSavedTarget() {
1221
- return this.#target;
1222
- }
1223
- };
1224
- var setRefResolver = (ref, resolver) => {
1225
- invariant8(ref instanceof RefImpl, "Ref is not an instance of RefImpl", {
1226
- F: __dxlog_file8,
1227
- L: 483,
1228
- S: void 0,
1229
- A: [
1230
- "ref instanceof RefImpl",
1231
- "'Ref is not an instance of RefImpl'"
1232
- ]
1233
- });
1234
- ref._setResolver(resolver);
1235
- };
1236
- var getRefSavedTarget = (ref) => {
1237
- invariant8(ref instanceof RefImpl, "Ref is not an instance of RefImpl", {
1238
- F: __dxlog_file8,
1239
- L: 491,
1240
- S: void 0,
1241
- A: [
1242
- "ref instanceof RefImpl",
1243
- "'Ref is not an instance of RefImpl'"
1244
- ]
1245
- });
1246
- return ref._getSavedTarget();
1247
- };
1248
- var refVariance = {
1249
- _T: null
1250
- };
1251
- var refFromEncodedReference = (encodedReference, resolver) => {
1252
- const dxn = DXN4.parse(encodedReference["/"]);
1253
- const ref = new RefImpl(dxn);
1254
- if (resolver) {
1255
- setRefResolver(ref, resolver);
1256
- }
1257
- return ref;
1258
- };
1259
- var StaticRefResolver = class {
1260
- objects = /* @__PURE__ */ new Map();
1261
- schemas = /* @__PURE__ */ new Map();
1262
- addObject(obj) {
1263
- this.objects.set(obj.id, obj);
1264
- return this;
720
+ static getPropertySchema(rootObjectSchema, propertyPath, getProperty = () => null) {
721
+ let schema = rootObjectSchema;
722
+ for (let i = 0; i < propertyPath.length; i++) {
723
+ const propertyName = propertyPath[i];
724
+ const tupleAst = unwrapArray(schema.ast);
725
+ if (tupleAst != null) {
726
+ schema = getArrayElementSchema(tupleAst, propertyName);
727
+ } else {
728
+ const propertyType = getPropertyType(schema.ast, propertyName.toString(), (propertyName2) => getProperty([
729
+ ...propertyPath.slice(0, i),
730
+ propertyName2
731
+ ]));
732
+ if (propertyType == null) {
733
+ log.warn("unknown property", {
734
+ path: propertyPath,
735
+ property: propertyName
736
+ }, {
737
+ F: __dxlog_file6,
738
+ L: 71,
739
+ S: this,
740
+ C: (f, a) => f(...a)
741
+ });
742
+ continue;
743
+ }
744
+ schema = Schema3.make(propertyType).annotations(propertyType.annotations);
745
+ }
746
+ }
747
+ return schema;
1265
748
  }
1266
- addSchema(schema) {
1267
- const dxn = getSchemaDXN(schema);
1268
- invariant8(dxn, "Schema has no DXN", {
1269
- F: __dxlog_file8,
1270
- L: 523,
749
+ static getTargetPropertySchema(target, prop) {
750
+ const schema = target[SchemaId];
751
+ invariant6(schema, "target has no schema", {
752
+ F: __dxlog_file6,
753
+ L: 84,
1271
754
  S: this,
1272
755
  A: [
1273
- "dxn",
1274
- "'Schema has no DXN'"
756
+ "schema",
757
+ "'target has no schema'"
1275
758
  ]
1276
759
  });
1277
- this.schemas.set(dxn.toString(), schema);
1278
- return this;
1279
- }
1280
- resolveSync(dxn, _load, _onLoad) {
1281
- const id = dxn?.asEchoDXN()?.echoId;
1282
- if (id == null) {
1283
- return void 0;
1284
- }
1285
- return this.objects.get(id);
1286
- }
1287
- async resolve(dxn) {
1288
- const id = dxn?.asEchoDXN()?.echoId;
1289
- if (id == null) {
1290
- return void 0;
1291
- }
1292
- return this.objects.get(id);
1293
- }
1294
- async resolveSchema(dxn) {
1295
- return this.schemas.get(dxn.toString());
1296
- }
1297
- };
1298
-
1299
- // src/internal/ref/ref-array.ts
1300
- import { isNonNullable } from "@dxos/util";
1301
- var RefArray = Object.freeze({
1302
- /**
1303
- * @returns all resolved targets.
1304
- */
1305
- targets: (refs) => {
1306
- return refs.map((ref) => ref.target).filter(isNonNullable);
1307
- },
1308
- /**
1309
- * Load all referenced objects.
1310
- */
1311
- loadAll: (refs) => {
1312
- return Promise.all(refs.map((ref) => ref.load()));
1313
- },
1314
- /**
1315
- * Removes the ref with the given id.
1316
- */
1317
- removeById: (refs, id) => {
1318
- const index = refs.findIndex(Ref.hasObjectId(id));
1319
- if (index >= 0) {
1320
- refs.splice(index, 1);
760
+ const arrayAst = unwrapArray(schema.ast);
761
+ if (arrayAst != null) {
762
+ return getArrayElementSchema(arrayAst, prop);
1321
763
  }
1322
- }
1323
- });
1324
-
1325
- // src/internal/json-schema/json-schema.ts
1326
- var __dxlog_file9 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/json-schema/json-schema.ts";
1327
- var PropType = /* @__PURE__ */ (function(PropType2) {
1328
- PropType2[PropType2["NONE"] = 0] = "NONE";
1329
- PropType2[PropType2["STRING"] = 1] = "STRING";
1330
- PropType2[PropType2["NUMBER"] = 2] = "NUMBER";
1331
- PropType2[PropType2["BOOLEAN"] = 3] = "BOOLEAN";
1332
- PropType2[PropType2["DATE"] = 4] = "DATE";
1333
- PropType2[PropType2["REF"] = 5] = "REF";
1334
- PropType2[PropType2["RECORD"] = 6] = "RECORD";
1335
- PropType2[PropType2["ENUM"] = 7] = "ENUM";
1336
- return PropType2;
1337
- })({});
1338
- var toPropType = (type) => {
1339
- switch (type) {
1340
- case 1:
1341
- return "string";
1342
- case 2:
1343
- return "number";
1344
- case 3:
1345
- return "boolean";
1346
- case 4:
1347
- return "date";
1348
- case 5:
1349
- return "ref";
1350
- case 6:
1351
- return "object";
1352
- default:
1353
- throw new Error(`Invalid type: ${type}`);
1354
- }
1355
- };
1356
- var JSON_SCHEMA_URL = "http://json-schema.org/draft-07/schema#";
1357
- var toJsonSchema = (schema, options = {}) => {
1358
- assertArgument6(Schema9.isSchema(schema), "schema");
1359
- let jsonSchema = _toJsonSchemaAST(schema.ast);
1360
- if (options.strict) {
1361
- jsonSchema = removeProperties(jsonSchema, (key, value2) => {
1362
- if (key === "$id" && value2 === "/schemas/any") {
1363
- return true;
1364
- }
1365
- if (key === "$ref" && value2 === "#/$defs/dependency") {
1366
- return true;
1367
- }
1368
- if (key === "$ref" && value2 === "#/$defs/jsonSchema") {
1369
- return true;
1370
- }
1371
- return false;
1372
- });
1373
- }
1374
- return jsonSchema;
1375
- };
1376
- var _toJsonSchemaAST = (ast) => {
1377
- const withRefinements = withEchoRefinements(ast, "#");
1378
- const jsonSchema = JSONSchema.fromAST(withRefinements, {
1379
- definitions: {}
1380
- });
1381
- return normalizeJsonSchema(jsonSchema);
1382
- };
1383
- var withEchoRefinements = (ast, path, suspendCache = /* @__PURE__ */ new Map()) => {
1384
- if (path) {
1385
- suspendCache.set(ast, path);
1386
- }
1387
- let recursiveResult;
1388
- if (SchemaAST6.isSuspend(ast)) {
1389
- const suspendedAst = ast.f();
1390
- const cachedPath = suspendCache.get(suspendedAst);
1391
- if (cachedPath) {
1392
- recursiveResult = new SchemaAST6.Suspend(() => withEchoRefinements(suspendedAst, path, suspendCache), {
1393
- [SchemaAST6.JSONSchemaAnnotationId]: {
1394
- $ref: cachedPath
1395
- }
1396
- });
1397
- } else {
1398
- const jsonSchema = _toJsonSchemaAST(suspendedAst);
1399
- recursiveResult = new SchemaAST6.Suspend(() => withEchoRefinements(suspendedAst, path, suspendCache), {
1400
- [SchemaAST6.JSONSchemaAnnotationId]: jsonSchema
1401
- });
764
+ const propertyType = getPropertyType(schema.ast, prop.toString(), (prop2) => target[prop2]);
765
+ if (propertyType == null) {
766
+ return Schema3.Any;
1402
767
  }
1403
- } else if (SchemaAST6.isTypeLiteral(ast)) {
1404
- recursiveResult = mapAst(ast, (ast2, key) => withEchoRefinements(ast2, path && typeof key === "string" ? `${path}/${key}` : void 0, suspendCache));
1405
- recursiveResult = addJsonSchemaFields(recursiveResult, {
1406
- propertyOrder: [
1407
- ...ast.propertySignatures.map((p) => p.name)
768
+ invariant6(propertyType, `invalid property: ${prop.toString()}`, {
769
+ F: __dxlog_file6,
770
+ L: 95,
771
+ S: this,
772
+ A: [
773
+ "propertyType",
774
+ "`invalid property: ${prop.toString()}`"
1408
775
  ]
1409
776
  });
1410
- } else if (SchemaAST6.isUndefinedKeyword(ast)) {
1411
- return ast;
1412
- } else {
1413
- recursiveResult = mapAst(ast, (ast2, key) => withEchoRefinements(ast2, path && (typeof key === "string" || typeof key === "number") ? `${path}/${key}` : void 0, suspendCache));
777
+ return Schema3.make(propertyType);
1414
778
  }
1415
- const annotationFields = annotations_toJsonSchemaFields(ast.annotations);
1416
- if (Object.keys(annotationFields).length === 0) {
1417
- return recursiveResult;
1418
- } else {
1419
- return addJsonSchemaFields(recursiveResult, annotationFields);
1420
- }
1421
- };
1422
- var toEffectSchema = (root, _defs) => {
1423
- const defs = root.$defs ? {
1424
- ..._defs,
1425
- ...root.$defs
1426
- } : _defs ?? {};
1427
- if ("type" in root && root.type === "object") {
1428
- return objectToEffectSchema(root, defs);
1429
- }
1430
- let result = Schema9.Unknown;
1431
- if ("$ref" in root) {
1432
- switch (root.$ref) {
1433
- case "/schemas/echo/ref": {
1434
- result = refToEffectSchema(root);
1435
- break;
1436
- }
1437
- }
1438
- } else if ("$id" in root) {
1439
- switch (root.$id) {
1440
- case "/schemas/any": {
1441
- result = anyToEffectSchema(root);
1442
- break;
1443
- }
1444
- case "/schemas/unknown": {
1445
- result = Schema9.Unknown;
1446
- break;
1447
- }
1448
- case "/schemas/{}":
1449
- case "/schemas/object": {
1450
- result = Schema9.Object;
1451
- break;
1452
- }
1453
- // Custom ECHO object reference.
1454
- case "/schemas/echo/ref": {
1455
- result = refToEffectSchema(root);
1456
- break;
1457
- }
1458
- }
1459
- } else if ("enum" in root) {
1460
- result = Schema9.Union(...root.enum.map((e) => Schema9.Literal(e)));
1461
- } else if ("oneOf" in root) {
1462
- result = Schema9.Union(...root.oneOf.map((v) => toEffectSchema(v, defs)));
1463
- } else if ("anyOf" in root) {
1464
- result = Schema9.Union(...root.anyOf.map((v) => toEffectSchema(v, defs)));
1465
- } else if ("allOf" in root) {
1466
- if (root.allOf.length === 1) {
1467
- result = toEffectSchema(root.allOf[0], defs);
1468
- } else {
1469
- log.warn("allOf with multiple schemas is not supported", void 0, {
1470
- F: __dxlog_file9,
1471
- L: 224,
1472
- S: void 0,
1473
- C: (f, a) => f(...a)
1474
- });
1475
- result = Schema9.Unknown;
1476
- }
1477
- } else if ("type" in root) {
1478
- switch (root.type) {
1479
- case "string": {
1480
- result = Schema9.String;
1481
- if (root.pattern) {
1482
- result = result.pipe(Schema9.pattern(new RegExp(root.pattern)));
1483
- }
1484
- break;
1485
- }
1486
- case "number": {
1487
- result = Schema9.Number;
1488
- break;
1489
- }
1490
- case "integer": {
1491
- result = Schema9.Number.pipe(Schema9.int());
1492
- break;
1493
- }
1494
- case "boolean": {
1495
- result = Schema9.Boolean;
1496
- break;
1497
- }
1498
- case "array": {
1499
- if (Array4.isArray(root.items)) {
1500
- const [required, optional5] = Function.pipe(root.items, Array4.map((v) => toEffectSchema(v, defs)), Array4.splitAt(root.minItems ?? root.items.length));
1501
- result = Schema9.Tuple(...required, ...optional5.map(Schema9.optionalElement));
1502
- } else {
1503
- invariant9(root.items, void 0, {
1504
- F: __dxlog_file9,
1505
- L: 257,
1506
- S: void 0,
1507
- A: [
1508
- "root.items",
1509
- ""
1510
- ]
1511
- });
1512
- const items = root.items;
1513
- result = Array4.isArray(items) ? Schema9.Tuple(...items.map((v) => toEffectSchema(v, defs))) : Schema9.Array(toEffectSchema(items, defs));
1514
- }
1515
- break;
1516
- }
1517
- case "null": {
1518
- result = Schema9.Null;
1519
- break;
1520
- }
1521
- }
1522
- } else if ("$ref" in root) {
1523
- const refSegments = root.$ref.split("/");
1524
- const jsonSchema = defs[refSegments[refSegments.length - 1]];
1525
- invariant9(jsonSchema, `missing definition for ${root.$ref}`, {
1526
- F: __dxlog_file9,
1527
- L: 273,
779
+ };
780
+ var getArrayElementSchema = (tupleAst, property) => {
781
+ const elementIndex = typeof property === "string" ? parseInt(property, 10) : Number.NaN;
782
+ if (Number.isNaN(elementIndex)) {
783
+ invariant6(property === "length", `invalid array property: ${String(property)}`, {
784
+ F: __dxlog_file6,
785
+ L: 111,
1528
786
  S: void 0,
1529
787
  A: [
1530
- "jsonSchema",
1531
- "`missing definition for ${root.$ref}`"
788
+ "property === 'length'",
789
+ "`invalid array property: ${String(property)}`"
1532
790
  ]
1533
791
  });
1534
- result = toEffectSchema(jsonSchema, defs).pipe(Schema9.annotations({
1535
- identifier: refSegments[refSegments.length - 1]
1536
- }));
792
+ return Schema3.Number;
1537
793
  }
1538
- const annotations4 = jsonSchemaFieldsToAnnotations(root);
1539
- result = result.annotations(annotations4);
1540
- return result;
794
+ if (elementIndex < tupleAst.elements.length) {
795
+ const elementType = tupleAst.elements[elementIndex].type;
796
+ return Schema3.make(elementType).annotations(elementType.annotations);
797
+ }
798
+ const restType = tupleAst.rest;
799
+ return Schema3.make(restType[0].type).annotations(restType[0].annotations);
1541
800
  };
1542
- var objectToEffectSchema = (root, defs) => {
1543
- invariant9("type" in root && root.type === "object", `not an object: ${root}`, {
1544
- F: __dxlog_file9,
1545
- L: 288,
801
+ var flattenUnion = (typeAst) => SchemaAST2.isUnion(typeAst) ? typeAst.types.flatMap(flattenUnion) : [
802
+ typeAst
803
+ ];
804
+ var getProperties = (typeAst, getTargetPropertyFn) => {
805
+ const astCandidates = flattenUnion(typeAst);
806
+ const typeAstList = astCandidates.filter((type) => SchemaAST2.isTypeLiteral(type));
807
+ if (typeAstList.length === 0) {
808
+ return [];
809
+ }
810
+ if (typeAstList.length === 1) {
811
+ return SchemaAST2.getPropertySignatures(typeAstList[0]);
812
+ }
813
+ const typeDiscriminators = getTypeDiscriminators(typeAstList);
814
+ const targetPropertyValue = getTargetPropertyFn(String(typeDiscriminators[0].name));
815
+ const typeIndex = typeDiscriminators.findIndex((p) => targetPropertyValue === p.type.literal);
816
+ invariant6(typeIndex !== -1, "discriminator field not set on target", {
817
+ F: __dxlog_file6,
818
+ L: 142,
1546
819
  S: void 0,
1547
820
  A: [
1548
- "'type' in root && root.type === 'object'",
1549
- "`not an object: ${root}`"
821
+ "typeIndex !== -1",
822
+ "'discriminator field not set on target'"
1550
823
  ]
1551
824
  });
1552
- const echoRefinement = root[ECHO_ANNOTATIONS_NS_DEPRECATED_KEY];
1553
- const isEchoObject = echoRefinement != null || "$id" in root && typeof root.$id === "string" && root.$id.startsWith("dxn:");
1554
- let fields = {};
1555
- const propertyList = Object.entries(root.properties ?? {});
1556
- let immutableIdField;
1557
- for (const [key, value2] of propertyList) {
1558
- if (isEchoObject && key === "id") {
1559
- immutableIdField = toEffectSchema(value2, defs);
1560
- } else {
1561
- fields[key] = root.required?.includes(key) ? toEffectSchema(value2, defs) : Schema9.optional(toEffectSchema(value2, defs));
1562
- }
1563
- }
1564
- if (root.propertyOrder) {
1565
- fields = orderKeys(fields, root.propertyOrder);
825
+ return SchemaAST2.getPropertySignatures(typeAstList[typeIndex]);
826
+ };
827
+ var getPropertyType = (ast, propertyName, getTargetPropertyFn) => {
828
+ const anyOrObject = unwrapAst(ast, (candidate) => SchemaAST2.isAnyKeyword(candidate) || SchemaAST2.isObjectKeyword(candidate));
829
+ if (anyOrObject != null) {
830
+ return ast;
1566
831
  }
1567
- let schema;
1568
- if (root.patternProperties) {
1569
- invariant9(propertyList.length === 0, "pattern properties mixed with regular properties are not supported", {
1570
- F: __dxlog_file9,
1571
- L: 314,
1572
- S: void 0,
1573
- A: [
1574
- "propertyList.length === 0",
1575
- "'pattern properties mixed with regular properties are not supported'"
1576
- ]
1577
- });
1578
- invariant9(Object.keys(root.patternProperties).length === 1 && Object.keys(root.patternProperties)[0] === "", "only one pattern property is supported", {
1579
- F: __dxlog_file9,
1580
- L: 315,
1581
- S: void 0,
1582
- A: [
1583
- "Object.keys(root.patternProperties).length === 1 && Object.keys(root.patternProperties)[0] === ''",
1584
- "'only one pattern property is supported'"
1585
- ]
1586
- });
1587
- schema = Schema9.Record({
1588
- key: Schema9.String,
1589
- value: toEffectSchema(root.patternProperties[""], defs)
1590
- });
1591
- } else if (typeof root.additionalProperties !== "object") {
1592
- schema = Schema9.Struct(fields);
1593
- } else {
1594
- const indexValue = toEffectSchema(root.additionalProperties, defs);
1595
- if (propertyList.length > 0) {
1596
- schema = Schema9.Struct(fields, {
1597
- key: Schema9.String,
1598
- value: indexValue
1599
- });
1600
- } else {
1601
- schema = Schema9.Record({
1602
- key: Schema9.String,
1603
- value: indexValue
1604
- });
1605
- }
832
+ const typeOrDiscriminatedUnion = unwrapAst(ast, (t) => {
833
+ return SchemaAST2.isTypeLiteral(t) || SchemaAST2.isUnion(t) && t.types.some((t2) => SchemaAST2.isTypeLiteral(t2));
834
+ });
835
+ if (typeOrDiscriminatedUnion == null) {
836
+ return null;
1606
837
  }
1607
- if (immutableIdField) {
1608
- schema = Schema9.extend(schema, Schema9.Struct({
1609
- id: immutableIdField
1610
- }));
838
+ const targetProperty = getProperties(typeOrDiscriminatedUnion, getTargetPropertyFn).find((p) => p.name === propertyName);
839
+ if (targetProperty != null) {
840
+ return unwrapAst(targetProperty.type);
1611
841
  }
1612
- const annotations4 = jsonSchemaFieldsToAnnotations(root);
1613
- return schema.annotations(annotations4);
1614
- };
1615
- var anyToEffectSchema = (root) => {
1616
- const echoRefinement = root[ECHO_ANNOTATIONS_NS_DEPRECATED_KEY];
1617
- if (echoRefinement?.reference != null) {
1618
- const echoId = root.$id.startsWith("dxn:echo:") ? root.$id : void 0;
1619
- return createEchoReferenceSchema(echoId, echoRefinement.reference.typename, echoRefinement.reference.version);
842
+ const indexSignatureType = unwrapAst(ast, SchemaAST2.isTypeLiteral);
843
+ if (indexSignatureType && SchemaAST2.isTypeLiteral(indexSignatureType) && indexSignatureType.indexSignatures.length > 0) {
844
+ return unwrapAst(indexSignatureType.indexSignatures[0].type);
1620
845
  }
1621
- return Schema9.Any;
846
+ return null;
1622
847
  };
1623
- var refToEffectSchema = (root) => {
1624
- if (!("reference" in root)) {
1625
- return createEchoReferenceSchema(void 0, ANY_OBJECT_TYPENAME, ANY_OBJECT_VERSION);
1626
- }
1627
- const reference = root.reference;
1628
- if (typeof reference !== "object") {
1629
- throw new Error("Invalid reference field in ref schema");
1630
- }
1631
- const targetSchemaDXN = DXN5.parse(reference.schema.$ref);
1632
- invariant9(targetSchemaDXN.kind === DXN5.kind.TYPE, void 0, {
1633
- F: __dxlog_file9,
1634
- L: 368,
848
+ var getTypeDiscriminators = (typeAstList) => {
849
+ const discriminatorPropCandidates = typeAstList.flatMap(SchemaAST2.getPropertySignatures).filter((p) => SchemaAST2.isLiteral(p.type));
850
+ const propertyName = discriminatorPropCandidates[0].name;
851
+ const isValidDiscriminator = discriminatorPropCandidates.every((p) => p.name === propertyName && !p.isOptional);
852
+ const everyTypeHasDiscriminator = discriminatorPropCandidates.length === typeAstList.length;
853
+ const isDiscriminatedUnion = isValidDiscriminator && everyTypeHasDiscriminator;
854
+ invariant6(isDiscriminatedUnion, "type ambiguity: every type in a union must have a single unique-literal field", {
855
+ F: __dxlog_file6,
856
+ L: 193,
1635
857
  S: void 0,
1636
858
  A: [
1637
- "targetSchemaDXN.kind === DXN.kind.TYPE",
1638
- ""
859
+ "isDiscriminatedUnion",
860
+ "'type ambiguity: every type in a union must have a single unique-literal field'"
1639
861
  ]
1640
862
  });
1641
- return createEchoReferenceSchema(targetSchemaDXN.toString(), targetSchemaDXN.kind === DXN5.kind.TYPE ? targetSchemaDXN.parts[0] : void 0, reference.schemaVersion);
863
+ return discriminatorPropCandidates;
1642
864
  };
1643
- var annotations_toJsonSchemaFields = (annotations4) => {
1644
- const schemaFields = {};
1645
- const echoAnnotations = {};
1646
- for (const [key, annotationId] of Object.entries(EchoAnnotations)) {
1647
- if (annotations4[annotationId] != null) {
1648
- echoAnnotations[key] = annotations4[annotationId];
865
+ var unwrapAst = (rootAst, predicate) => {
866
+ let ast = rootAst;
867
+ while (ast != null) {
868
+ if (predicate?.(ast)) {
869
+ return ast;
1649
870
  }
1650
- }
1651
- if (Object.keys(echoAnnotations).length > 0) {
1652
- schemaFields[ECHO_ANNOTATIONS_NS_KEY] = echoAnnotations;
1653
- }
1654
- const echoIdentifier = annotations4[TypeIdentifierAnnotationId];
1655
- if (echoIdentifier) {
1656
- schemaFields[ECHO_ANNOTATIONS_NS_KEY] ??= {};
1657
- schemaFields[ECHO_ANNOTATIONS_NS_KEY].schemaId = echoIdentifier;
1658
- }
1659
- for (const [key, annotationId] of Object.entries(CustomAnnotations)) {
1660
- const value2 = annotations4[annotationId];
1661
- if (value2 != null) {
1662
- schemaFields[key] = value2;
871
+ if (SchemaAST2.isUnion(ast)) {
872
+ const next = ast.types.find((t) => predicate != null && predicate(t) || SchemaAST2.isSuspend(t));
873
+ if (next != null) {
874
+ ast = next;
875
+ continue;
876
+ }
1663
877
  }
1664
- }
1665
- return schemaFields;
1666
- };
1667
- var decodeTypeIdentifierAnnotation = (schema) => {
1668
- if (schema.$id && schema.$id.startsWith("dxn:echo:")) {
1669
- return schema.$id;
1670
- } else if (schema.$id && schema.$id.startsWith("dxn:type:") && schema?.echo?.type?.schemaId) {
1671
- const id = schema?.echo?.type?.schemaId;
1672
- if (ObjectId4.isValid(id)) {
1673
- return DXN5.fromLocalObjectId(id).toString();
878
+ if (SchemaAST2.isSuspend(ast)) {
879
+ ast = ast.f();
880
+ } else {
881
+ return predicate == null ? ast : null;
1674
882
  }
1675
883
  }
1676
- return void 0;
884
+ return null;
1677
885
  };
1678
- var decodeTypeAnnotation = (schema) => {
1679
- if (schema.typename) {
1680
- const annotation = {
1681
- // TODO(dmaretskyi): Decoding default.
1682
- kind: schema.entityKind ? Schema9.decodeSync(EntityKindSchema)(schema.entityKind) : EntityKind.Object,
1683
- typename: schema.typename,
1684
- version: schema.version ?? "0.1.0"
1685
- };
1686
- if (annotation.kind === EntityKind.Relation) {
1687
- const source = schema.relationSource?.$ref ?? raise2(new Error("Relation source not set"));
1688
- const target = schema.relationTarget?.$ref ?? raise2(new Error("Relation target not set"));
1689
- annotation.sourceSchema = DXN5.parse(source).toString();
1690
- annotation.targetSchema = DXN5.parse(target).toString();
1691
- }
1692
- return annotation;
1693
- }
1694
- if (!schema.typename && schema?.echo?.type) {
1695
- return {
1696
- kind: EntityKind.Object,
1697
- typename: schema.echo.type.typename,
1698
- version: schema.echo.type.version
1699
- };
886
+ var unwrapArray = (ast) => unwrapAst(ast, SchemaAST2.isTupleType);
887
+ var checkIdNotPresentOnSchema = (schema) => {
888
+ invariant6(SchemaAST2.isTypeLiteral(schema.ast), void 0, {
889
+ F: __dxlog_file6,
890
+ L: 236,
891
+ S: void 0,
892
+ A: [
893
+ "SchemaAST.isTypeLiteral(schema.ast)",
894
+ ""
895
+ ]
896
+ });
897
+ const idProperty = SchemaAST2.getPropertySignatures(schema.ast).find((prop) => prop.name === "id");
898
+ if (idProperty != null) {
899
+ throw new Error('"id" property name is reserved');
1700
900
  }
1701
- return void 0;
1702
901
  };
1703
- var jsonSchemaFieldsToAnnotations = (schema) => {
1704
- const annotations4 = {};
1705
- const echoAnnotations = getNormalizedEchoAnnotations(schema) ?? {};
1706
- if (echoAnnotations) {
1707
- for (const [key, annotationId] of Object.entries(EchoAnnotations)) {
1708
- if (echoAnnotations[key]) {
1709
- annotations4[annotationId] = echoAnnotations[key];
1710
- }
1711
- }
902
+
903
+ // src/internal/common/proxy/typed-handler.ts
904
+ var __dxlog_file7 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/common/proxy/typed-handler.ts";
905
+ var deepCopy = (value2, visited = /* @__PURE__ */ new Map()) => {
906
+ if (value2 == null || typeof value2 !== "object") {
907
+ return value2;
1712
908
  }
1713
- annotations4[TypeIdentifierAnnotationId] = decodeTypeIdentifierAnnotation(schema);
1714
- const typeAnnotation = decodeTypeAnnotation(schema);
1715
- if (typeAnnotation) {
1716
- annotations4[TypeAnnotationId] = typeAnnotation;
1717
- annotations4[SchemaAST6.JSONSchemaAnnotationId] = makeTypeJsonSchemaAnnotation({
1718
- kind: typeAnnotation.kind,
1719
- typename: typeAnnotation.typename,
1720
- version: typeAnnotation.version,
1721
- relationSource: typeAnnotation.sourceSchema,
1722
- relationTarget: typeAnnotation.targetSchema
1723
- });
909
+ const actualValue = getRawTarget(value2);
910
+ if (visited.has(actualValue)) {
911
+ return visited.get(actualValue);
1724
912
  }
1725
- for (const [key, annotationId] of Object.entries({
1726
- ...CustomAnnotations,
1727
- ...DecodedAnnotations
1728
- })) {
1729
- if (key in schema) {
1730
- annotations4[annotationId] = schema[key];
913
+ if (actualValue instanceof ReactiveArray) {
914
+ const copy2 = new ReactiveArray();
915
+ visited.set(actualValue, copy2);
916
+ for (const item of actualValue) {
917
+ copy2.push(deepCopy(item, visited));
1731
918
  }
919
+ copyHiddenProperties(actualValue, copy2);
920
+ return copy2;
1732
921
  }
1733
- return clearUndefined(annotations4);
1734
- };
1735
- var makeAnnotatedRefinement = (ast, annotations4) => {
1736
- return new SchemaAST6.Refinement(ast, () => Option2.none(), annotations4);
1737
- };
1738
- var addJsonSchemaFields = (ast, schema) => makeAnnotatedRefinement(ast, {
1739
- [SchemaAST6.JSONSchemaAnnotationId]: schema
1740
- });
1741
- var normalizeJsonSchema = (jsonSchema) => {
1742
- if (jsonSchema.properties && "id" in jsonSchema.properties) {
1743
- jsonSchema.properties = orderKeys(jsonSchema.properties, [
1744
- "id"
1745
- ]);
1746
- }
1747
- jsonSchema.$schema = JSON_SCHEMA_URL;
1748
- jsonSchema = orderKeys(jsonSchema, [
1749
- "$schema",
1750
- "$id",
1751
- "entityKind",
1752
- "typename",
1753
- "version",
1754
- "relationTarget",
1755
- "relationSource",
1756
- "type",
1757
- "enum",
1758
- "properties",
1759
- "required",
1760
- "propertyOrder",
1761
- "items",
1762
- "additionalProperties",
1763
- "anyOf",
1764
- "oneOf"
1765
- ]);
1766
- return jsonSchema;
1767
- };
1768
-
1769
- // src/internal/object/common.ts
1770
- import * as Schema10 from "effect/Schema";
1771
- var makeTypedEntityClass = (typename, version2, baseSchema) => {
1772
- return class {
1773
- // Implement TypedObject properties.
1774
- static typename = typename;
1775
- static version = version2;
1776
- // Implement Schema.Schema properties.
1777
- // TODO(burdon): Comment required.
1778
- static [Schema10.TypeId] = schemaVariance;
1779
- static ast = baseSchema.ast;
1780
- static annotations = baseSchema.annotations.bind(baseSchema);
1781
- static pipe = baseSchema.pipe.bind(baseSchema);
1782
- // TODO(burdon): Comment required.
1783
- static [Symbol.hasInstance](obj) {
1784
- return obj != null && getTypename(obj) === typename;
1785
- }
1786
- // TODO(burdon): Throw APIError.
1787
- constructor() {
1788
- throw new Error("Use live(Typename, { ...fields }) to instantiate an object.");
1789
- }
1790
- };
1791
- };
1792
- var schemaVariance = {
1793
- _A: (_) => _,
1794
- _I: (_) => _,
1795
- _R: (_) => _
1796
- };
1797
-
1798
- // src/internal/object/create-object.ts
1799
- import { raise as raise4 } from "@dxos/debug";
1800
- import { assertArgument as assertArgument8, failedInvariant } from "@dxos/invariant";
1801
- import { ObjectId as ObjectId7 } from "@dxos/keys";
1802
-
1803
- // src/internal/proxy/symbols.ts
1804
- var EventId = Symbol.for("@dxos/live-object/EventId");
1805
- var ChangeId = Symbol.for("@dxos/live-object/ChangeId");
1806
-
1807
- // src/internal/proxy/event-batch.ts
1808
- var eventBatchDepth = 0;
1809
- var pendingEventTargets = /* @__PURE__ */ new Set();
1810
- var batchEvents = (callback) => {
1811
- eventBatchDepth++;
1812
- try {
1813
- callback();
1814
- } finally {
1815
- eventBatchDepth--;
1816
- if (eventBatchDepth === 0) {
1817
- for (const target of pendingEventTargets) {
1818
- target[EventId]?.emit();
1819
- }
1820
- pendingEventTargets.clear();
1821
- }
1822
- }
1823
- };
1824
- var emitEvent = (target) => {
1825
- if (eventBatchDepth > 0) {
1826
- pendingEventTargets.add(target);
1827
- } else {
1828
- target[EventId]?.emit();
1829
- }
1830
- };
1831
-
1832
- // src/internal/proxy/change-context.ts
1833
- var currentChangeContext = null;
1834
- var pendingNotificationKey = null;
1835
- var pendingOwnerNotifications = /* @__PURE__ */ new Set();
1836
- var enterChangeContext = (key) => {
1837
- currentChangeContext = key;
1838
- return () => {
1839
- currentChangeContext = null;
1840
- };
1841
- };
1842
- var isInChangeContext = (key) => {
1843
- return currentChangeContext === key;
1844
- };
1845
- var queueNotification = (key) => {
1846
- if (currentChangeContext === key) {
1847
- pendingNotificationKey = key;
1848
- }
1849
- };
1850
- var queueOwnerNotification = (target) => {
1851
- if (currentChangeContext !== null && target !== currentChangeContext) {
1852
- pendingOwnerNotifications.add(target);
1853
- }
1854
- };
1855
- var hasPendingNotifications = (key) => {
1856
- return pendingNotificationKey === key;
1857
- };
1858
- var clearPendingNotifications = (key) => {
1859
- if (pendingNotificationKey === key) {
1860
- pendingNotificationKey = null;
922
+ const proto = Object.getPrototypeOf(actualValue);
923
+ if (proto !== Object.prototype && proto !== Array.prototype && proto !== null) {
924
+ return value2;
1861
925
  }
1862
- };
1863
- var executeChange = (contextKey, eventTarget, proxy, callback) => {
1864
- const exitContext = enterChangeContext(contextKey);
1865
- try {
1866
- batchEvents(() => callback(proxy));
1867
- } finally {
1868
- exitContext();
1869
- if (hasPendingNotifications(contextKey)) {
1870
- clearPendingNotifications(contextKey);
1871
- eventTarget[EventId]?.emit();
1872
- }
1873
- for (const ownerTarget of pendingOwnerNotifications) {
1874
- ownerTarget[EventId]?.emit();
926
+ if (Array.isArray(actualValue)) {
927
+ const copy2 = new ReactiveArray();
928
+ visited.set(actualValue, copy2);
929
+ for (const item of actualValue) {
930
+ copy2.push(deepCopy(item, visited));
1875
931
  }
1876
- pendingOwnerNotifications.clear();
1877
- }
1878
- };
1879
-
1880
- // src/internal/proxy/define-hidden-property.ts
1881
- var defineHiddenProperty = (object, key, value2) => {
1882
- Object.defineProperty(object, key, {
1883
- enumerable: false,
1884
- configurable: true,
1885
- value: value2
1886
- });
1887
- };
1888
-
1889
- // src/internal/proxy/errors.ts
1890
- var MutationOutsideChangeContextError = class extends Error {
1891
- constructor(operation, suggestion) {
1892
- super(`Cannot ${operation} outside of Obj.change(). Use Obj.change(obj, (mutableObj) => { ${suggestion} }) instead.`);
1893
- this.name = "MutationOutsideChangeContextError";
932
+ return copy2;
1894
933
  }
1895
- };
1896
- var createPropertySetError = (prop) => {
1897
- return new MutationOutsideChangeContextError(`modify object property "${String(prop)}"`, `mutableObj.${String(prop)} = value;`);
1898
- };
1899
- var createPropertyDeleteError = (prop) => {
1900
- return new MutationOutsideChangeContextError(`delete object property "${String(prop)}"`, `delete mutableObj.${String(prop)};`);
1901
- };
1902
- var createArrayMethodError = (method) => {
1903
- return new MutationOutsideChangeContextError(`call array.${method}()`, `mutableObj.array.${method}(...);`);
1904
- };
1905
-
1906
- // src/internal/proxy/json-serializer.ts
1907
- import { invariant as invariant10 } from "@dxos/invariant";
1908
- import { DXN as DXN6 } from "@dxos/keys";
1909
- import { deepMapValues } from "@dxos/util";
1910
- var __dxlog_file10 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/proxy/json-serializer.ts";
1911
- var attachTypedJsonSerializer = (obj) => {
1912
- const descriptor = Object.getOwnPropertyDescriptor(obj, "toJSON");
1913
- if (descriptor) {
1914
- return;
934
+ const copy = {};
935
+ visited.set(actualValue, copy);
936
+ for (const key of Object.keys(actualValue)) {
937
+ copy[key] = deepCopy(actualValue[key], visited);
1915
938
  }
1916
- Object.defineProperty(obj, "toJSON", {
1917
- value: typedJsonSerializer,
1918
- writable: false,
1919
- enumerable: false,
1920
- // Setting `configurable` to false breaks proxy invariants, should be fixable.
1921
- configurable: true
1922
- });
939
+ copyHiddenProperties(actualValue, copy);
940
+ return copy;
1923
941
  };
1924
- var typedJsonSerializer = function() {
1925
- const { id, ...rest } = this;
1926
- const result = {
1927
- id
1928
- };
1929
- if (this[TypeId]) {
1930
- result[ATTR_TYPE] = this[TypeId].toString();
942
+ var copyHiddenProperties = (source, target) => {
943
+ if (SchemaId in source) {
944
+ defineHiddenProperty(target, SchemaId, source[SchemaId]);
1931
945
  }
1932
- if (this[MetaId]) {
1933
- result[ATTR_META] = serializeMeta(this[MetaId]);
946
+ if (TypeId in source) {
947
+ defineHiddenProperty(target, TypeId, source[TypeId]);
1934
948
  }
1935
- if (this[SelfDXNId]) {
1936
- result[ATTR_SELF_DXN] = this[SelfDXNId].toString();
949
+ };
950
+ var TypedReactiveHandler = class _TypedReactiveHandler {
951
+ static instance = new _TypedReactiveHandler();
952
+ _proxyMap = /* @__PURE__ */ new WeakMap();
953
+ _inSet = false;
954
+ constructor() {
1937
955
  }
1938
- if (this[RelationSourceDXNId]) {
1939
- const sourceDXN = this[RelationSourceDXNId];
1940
- invariant10(sourceDXN instanceof DXN6, void 0, {
1941
- F: __dxlog_file10,
1942
- L: 61,
956
+ init(target) {
957
+ invariant7(typeof target === "object" && target !== null, void 0, {
958
+ F: __dxlog_file7,
959
+ L: 147,
1943
960
  S: this,
1944
961
  A: [
1945
- "sourceDXN instanceof DXN",
962
+ "typeof target === 'object' && target !== null",
1946
963
  ""
1947
964
  ]
1948
965
  });
1949
- result[ATTR_RELATION_SOURCE] = sourceDXN.toString();
1950
- }
1951
- if (this[RelationTargetDXNId]) {
1952
- const targetDXN = this[RelationTargetDXNId];
1953
- invariant10(targetDXN instanceof DXN6, void 0, {
1954
- F: __dxlog_file10,
1955
- L: 66,
966
+ invariant7(SchemaId in target, "Schema is not defined for the target", {
967
+ F: __dxlog_file7,
968
+ L: 148,
1956
969
  S: this,
1957
970
  A: [
1958
- "targetDXN instanceof DXN",
1959
- ""
971
+ "SchemaId in target",
972
+ "'Schema is not defined for the target'"
1960
973
  ]
1961
974
  });
1962
- result[ATTR_RELATION_TARGET] = targetDXN.toString();
1963
- }
1964
- Object.assign(result, serializeData(rest));
1965
- return result;
1966
- };
1967
- var serializeData = (data) => {
1968
- return deepMapValues(data, (value2, recurse) => {
1969
- if (Ref.isRef(value2)) {
1970
- return value2.noInline().encode();
975
+ const hasOwner = !!getOwner(target);
976
+ if (!(EventId in target) && !hasOwner) {
977
+ defineHiddenProperty(target, EventId, new Event());
1971
978
  }
1972
- return recurse(value2);
1973
- });
1974
- };
1975
- var serializeMeta = (meta) => {
1976
- return deepMapValues(meta, (value2, recurse) => recurse(value2));
1977
- };
1978
-
1979
- // src/internal/proxy/make-object.ts
1980
- import { ObjectId as ObjectId5 } from "@dxos/keys";
1981
-
1982
- // src/internal/proxy/proxy-utils.ts
1983
- import { invariant as invariant11 } from "@dxos/invariant";
1984
- var __dxlog_file11 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/proxy/proxy-utils.ts";
1985
- var symbolIsProxy = Symbol.for("@dxos/schema/Proxy");
1986
- var isProxy = (value2) => !!value2?.[symbolIsProxy];
1987
- var isValidProxyTarget = (value2) => {
1988
- if (value2 == null || value2[symbolIsProxy]) {
1989
- return false;
1990
- }
1991
- if (Array.isArray(value2)) {
1992
- return true;
979
+ defineHiddenProperty(target, ObjectDeletedId, false);
980
+ if (!hasOwner && !(ChangeId in target)) {
981
+ defineHiddenProperty(target, ChangeId, true);
982
+ }
983
+ if (!hasOwner) {
984
+ for (const key in target) {
985
+ if (target[symbolIsProxy]) {
986
+ continue;
987
+ }
988
+ let value2 = target[key];
989
+ if (isValidProxyTarget(value2) || isProxy(value2)) {
990
+ if (hasForeignOwner(value2, target)) {
991
+ value2 = deepCopy(value2);
992
+ target[key] = value2;
993
+ }
994
+ setOwnerRecursive(value2, target);
995
+ }
996
+ }
997
+ }
998
+ Object.defineProperty(target, inspectCustom, {
999
+ enumerable: false,
1000
+ configurable: true,
1001
+ value: this._inspect.bind(target)
1002
+ });
1993
1003
  }
1994
- return typeof value2 === "object" && Object.getPrototypeOf(value2) === Object.prototype;
1995
- };
1996
- var getProxySlot = (proxy) => {
1997
- const value2 = proxy[symbolIsProxy];
1998
- invariant11(value2 instanceof ProxyHandlerSlot, void 0, {
1999
- F: __dxlog_file11,
2000
- L: 32,
2001
- S: void 0,
2002
- A: [
2003
- "value instanceof ProxyHandlerSlot",
2004
- ""
2005
- ]
2006
- });
2007
- return value2;
2008
- };
2009
- var getProxyTarget = (proxy) => {
2010
- return getProxySlot(proxy).target;
2011
- };
2012
- var getProxyHandler = (proxy) => {
2013
- return getProxySlot(proxy).handler;
2014
- };
2015
- var dangerouslySetProxyId = (obj, id) => {
2016
- getProxySlot(obj).target.id = id;
2017
- };
2018
- var createProxy = (target, handler) => {
2019
- const existingProxy = handler._proxyMap.get(target);
2020
- if (existingProxy) {
2021
- return existingProxy;
2022
- }
2023
- const proxy = new Proxy(target, new ProxyHandlerSlot(target, handler));
2024
- handler.init(target);
2025
- handler._proxyMap.set(target, proxy);
2026
- return proxy;
2027
- };
2028
- var ProxyHandlerSlot = class {
2029
- target;
2030
- _handler;
2031
- /**
2032
- * @param target Original object.
2033
- * @param _handler Handles intercepted operations.
2034
- */
2035
- constructor(target, _handler) {
2036
- this.target = target;
2037
- this._handler = _handler;
2038
- }
2039
- get handler() {
2040
- invariant11(this._handler, void 0, {
2041
- F: __dxlog_file11,
2042
- L: 91,
2043
- S: this,
2044
- A: [
2045
- "this._handler",
2046
- ""
2047
- ]
2048
- });
2049
- return this._handler;
2050
- }
2051
- // TODO(burdon): Requires comment.
2052
- setHandler(handler) {
2053
- this._handler = handler;
2054
- }
2055
- /**
2056
- * Get value.
2057
- */
2058
1004
  get(target, prop, receiver) {
2059
- if (prop === symbolIsProxy) {
2060
- return this;
1005
+ switch (prop) {
1006
+ // TODO(burdon): Remove?
1007
+ case objectData: {
1008
+ return toJSON(target);
1009
+ }
1010
+ case ChangeId: {
1011
+ if (target[ChangeId] !== true) {
1012
+ return void 0;
1013
+ }
1014
+ return (callback) => executeChange(target, target, receiver, callback);
1015
+ }
2061
1016
  }
2062
- if (!this._handler || !this._handler.get) {
1017
+ if (Object.getOwnPropertyDescriptor(target, prop)?.get) {
2063
1018
  return Reflect.get(target, prop, receiver);
2064
1019
  }
2065
- return this._handler.get(target, prop, receiver);
1020
+ const value2 = Reflect.get(target, prop, receiver);
1021
+ if (isValidProxyTarget(value2)) {
1022
+ return createProxy(value2, this);
1023
+ }
1024
+ return value2;
2066
1025
  }
2067
- static {
2068
- const TRAPS = [
2069
- "apply",
2070
- "construct",
2071
- "defineProperty",
2072
- "deleteProperty",
2073
- "get",
2074
- "getOwnPropertyDescriptor",
2075
- "getPrototypeOf",
2076
- "has",
2077
- "isExtensible",
2078
- "ownKeys",
2079
- "preventExtensions",
2080
- "set",
2081
- "setPrototypeOf"
2082
- ];
2083
- for (const trap of TRAPS) {
2084
- if (trap === "get") {
2085
- continue;
2086
- }
2087
- Object.defineProperty(this.prototype, trap, {
2088
- enumerable: false,
2089
- value: function(...args) {
2090
- if (!this._handler || !this._handler[trap]) {
2091
- return Reflect[trap](...args);
2092
- }
2093
- return this._handler[trap].apply(this._handler, args);
1026
+ set(target, prop, value2, receiver) {
1027
+ const echoRoot = getEchoRoot(target);
1028
+ const isInitialized = ChangeId in echoRoot || EventId in echoRoot;
1029
+ const isSymbolProp = typeof prop === "symbol";
1030
+ if (isInitialized && !isSymbolProp && !isInChangeContext(echoRoot)) {
1031
+ throw new Error(`Cannot modify object property "${String(prop)}" outside of Obj.change(). Use Obj.change(obj, (mutableObj) => { mutableObj.property = value; }) instead.`);
1032
+ }
1033
+ let result = false;
1034
+ this._inSet = true;
1035
+ try {
1036
+ batchEvents(() => {
1037
+ const { echoRoot: _, preparedValue } = this._prepareValueForAssignment(target, prop, value2);
1038
+ result = Reflect.set(target, prop, preparedValue, receiver);
1039
+ if (isInitialized) {
1040
+ queueNotification(echoRoot);
1041
+ notifyOwnerChain(target);
2094
1042
  }
2095
1043
  });
1044
+ } finally {
1045
+ this._inSet = false;
2096
1046
  }
1047
+ return result;
2097
1048
  }
2098
- };
2099
-
2100
- // src/internal/proxy/typed-handler.ts
2101
- import * as Schema12 from "effect/Schema";
2102
- import * as SchemaAST8 from "effect/SchemaAST";
2103
- import { Event as Event2 } from "@dxos/async";
2104
- import { inspectCustom as inspectCustom2 } from "@dxos/debug";
2105
- import { invariant as invariant14 } from "@dxos/invariant";
2106
-
2107
- // src/internal/proxy/ownership.ts
2108
- import { invariant as invariant12 } from "@dxos/invariant";
2109
- var __dxlog_file12 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/proxy/ownership.ts";
2110
- var EchoOwner = Symbol.for("@dxos/echo/Owner");
2111
- var getRawTarget = (value2) => {
2112
- return isProxy(value2) ? getProxyTarget(value2) : value2;
2113
- };
2114
- var getOwner = (value2) => {
2115
- return value2?.[EchoOwner];
2116
- };
2117
- var setMetaOwner = (metaTarget, parent) => {
2118
- defineHiddenProperty(metaTarget, EchoOwner, parent);
2119
- };
2120
- var setOwnerRecursive = (value2, owner, options = {}) => {
2121
- const { visited = /* @__PURE__ */ new Set(), depth = 0, allowedPreviousOwner } = options;
2122
- if (value2 == null || typeof value2 !== "object") {
2123
- return;
2124
- }
2125
- const actualValue = getRawTarget(value2);
2126
- if (visited.has(actualValue)) {
2127
- return;
1049
+ ownKeys(target) {
1050
+ return Reflect.ownKeys(target);
2128
1051
  }
2129
- visited.add(actualValue);
2130
- const existingOwner = getOwner(actualValue);
2131
- const isRootEchoObject = EventId in actualValue;
2132
- let newAllowedPreviousOwner = allowedPreviousOwner;
2133
- if (isRootEchoObject && depth === 0) {
2134
- newAllowedPreviousOwner = actualValue;
1052
+ deleteProperty(target, property) {
1053
+ const echoRoot = getEchoRoot(target);
1054
+ const isInitialized = echoRoot[ChangeId] === true || EventId in echoRoot;
1055
+ const isSymbolProp = typeof property === "symbol";
1056
+ if (isInitialized && !isSymbolProp && !isInChangeContext(echoRoot)) {
1057
+ throw createPropertyDeleteError(property);
1058
+ }
1059
+ const result = Reflect.deleteProperty(target, property);
1060
+ if (isInitialized) {
1061
+ queueNotification(echoRoot);
1062
+ }
1063
+ return result;
2135
1064
  }
2136
- if (!isRootEchoObject) {
2137
- const ownershipAllowed = existingOwner == null || existingOwner === owner || existingOwner === newAllowedPreviousOwner;
2138
- invariant12(ownershipAllowed, "Cannot reassign ownership of a nested record to a different ECHO object. Use deep copy first.", {
2139
- F: __dxlog_file12,
2140
- L: 102,
2141
- S: void 0,
2142
- A: [
2143
- "ownershipAllowed",
2144
- "'Cannot reassign ownership of a nested record to a different ECHO object. Use deep copy first.'"
2145
- ]
1065
+ defineProperty(target, property, attributes) {
1066
+ const echoRoot = getEchoRoot(target);
1067
+ const isInitialized = ChangeId in echoRoot || EventId in echoRoot;
1068
+ const isSymbolProp = typeof property === "symbol";
1069
+ if (isInitialized && !isSymbolProp && !isInChangeContext(echoRoot)) {
1070
+ throw new Error(`Cannot modify object property "${String(property)}" outside of Obj.change(). Use Obj.change(obj, (mutableObj) => { mutableObj.property = value; }) instead.`);
1071
+ }
1072
+ const { echoRoot: _, preparedValue } = this._prepareValueForAssignment(target, property, attributes.value);
1073
+ const result = Reflect.defineProperty(target, property, {
1074
+ ...attributes,
1075
+ value: preparedValue
2146
1076
  });
1077
+ if (!this._inSet && isInitialized) {
1078
+ queueNotification(echoRoot);
1079
+ }
1080
+ return result;
2147
1081
  }
2148
- defineHiddenProperty(actualValue, EchoOwner, owner);
2149
- const recursiveOptions = {
2150
- visited,
2151
- depth: depth + 1,
2152
- allowedPreviousOwner: newAllowedPreviousOwner
2153
- };
2154
- if (Array.isArray(actualValue)) {
2155
- for (const item of actualValue) {
2156
- if (isValidProxyTarget(item) || isProxy(item)) {
2157
- setOwnerRecursive(item, owner, recursiveOptions);
2158
- }
1082
+ /**
1083
+ * Prepare a value for assignment to a typed object property.
1084
+ * Handles cycle detection, copy-on-assign, array conversion, validation, and ownership.
1085
+ */
1086
+ _prepareValueForAssignment(target, prop, value2) {
1087
+ const echoRoot = getEchoRoot(target);
1088
+ if (prop === ParentId) {
1089
+ return {
1090
+ echoRoot,
1091
+ preparedValue: value2
1092
+ };
2159
1093
  }
2160
- } else {
2161
- for (const key in actualValue) {
2162
- if (Object.prototype.hasOwnProperty.call(actualValue, key)) {
2163
- const nested = actualValue[key];
2164
- if (isValidProxyTarget(nested) || isProxy(nested)) {
2165
- setOwnerRecursive(nested, owner, recursiveOptions);
2166
- }
1094
+ if (isValidProxyTarget(value2) || isProxy(value2)) {
1095
+ if (wouldCreateCycle(echoRoot, value2)) {
1096
+ throw new Error("Cannot create cycles in typed object graph. Consider using Ref for circular references.");
2167
1097
  }
2168
1098
  }
2169
- }
2170
- };
2171
- var traverseObjectGraph = (value2, visitor, visited = /* @__PURE__ */ new Set()) => {
2172
- if (value2 == null || typeof value2 !== "object") {
2173
- return false;
2174
- }
2175
- const actualValue = getRawTarget(value2);
2176
- if (visited.has(actualValue)) {
2177
- return false;
2178
- }
2179
- visited.add(actualValue);
2180
- if (visitor(actualValue)) {
2181
- return true;
2182
- }
2183
- if (Array.isArray(actualValue)) {
2184
- for (const item of actualValue) {
2185
- if (traverseObjectGraph(item, visitor, visited)) {
2186
- return true;
1099
+ if (isValidProxyTarget(value2) || isProxy(value2)) {
1100
+ const actualValue = getRawTarget(value2);
1101
+ const isRootEchoObject = EventId in actualValue;
1102
+ if (isRootEchoObject) {
1103
+ throw new Error("Object references must be wrapped with `Ref.make`");
2187
1104
  }
2188
1105
  }
2189
- } else {
2190
- for (const key in actualValue) {
2191
- if (Object.prototype.hasOwnProperty.call(actualValue, key)) {
2192
- if (traverseObjectGraph(actualValue[key], visitor, visited)) {
2193
- return true;
2194
- }
1106
+ if (isValidProxyTarget(value2) || isProxy(value2)) {
1107
+ const actualValue = getRawTarget(value2);
1108
+ const existingOwner = getOwner(actualValue);
1109
+ if (existingOwner != null && existingOwner !== echoRoot) {
1110
+ value2 = deepCopy(value2);
2195
1111
  }
2196
1112
  }
1113
+ if (Array.isArray(value2) && !(value2 instanceof ReactiveArray)) {
1114
+ value2 = ReactiveArray.from(value2);
1115
+ }
1116
+ const validatedValue = this._validateValue(target, prop, value2);
1117
+ if (isValidProxyTarget(validatedValue) || isProxy(validatedValue)) {
1118
+ setOwnerRecursive(validatedValue, echoRoot);
1119
+ }
1120
+ return {
1121
+ echoRoot,
1122
+ preparedValue: validatedValue
1123
+ };
2197
1124
  }
2198
- return false;
2199
- };
2200
- var wouldCreateCycle = (targetRoot, value2) => traverseObjectGraph(value2, (v) => v === targetRoot);
2201
- var hasForeignOwner = (value2, target) => traverseObjectGraph(value2, (v) => {
2202
- const owner = getOwner(v);
2203
- if (owner != null && owner !== target) {
2204
- return true;
2205
- }
2206
- if (EventId in v && v !== target) {
2207
- return true;
2208
- }
2209
- return false;
2210
- });
2211
- var MAX_OWNER_DEPTH = 100;
2212
- var getEchoRoot = (target, depth = 0) => {
2213
- invariant12(depth < MAX_OWNER_DEPTH, "Owner chain too deep - possible circular ownership", {
2214
- F: __dxlog_file12,
2215
- L: 221,
2216
- S: void 0,
2217
- A: [
2218
- "depth < MAX_OWNER_DEPTH",
2219
- "'Owner chain too deep - possible circular ownership'"
2220
- ]
2221
- });
2222
- if (KindId in target) {
2223
- return target;
1125
+ _validateValue(target, prop, value2) {
1126
+ if (prop === ParentId) {
1127
+ return value2;
1128
+ }
1129
+ const schema = SchemaValidator.getTargetPropertySchema(target, prop);
1130
+ const _ = Schema4.asserts(schema)(value2);
1131
+ if (isValidProxyTarget(value2)) {
1132
+ setSchemaProperties(value2, schema);
1133
+ }
1134
+ return value2;
2224
1135
  }
2225
- const owner = getOwner(target);
2226
- if (owner) {
2227
- return getEchoRoot(owner, depth + 1);
1136
+ _inspect(_, options, inspectFn) {
1137
+ const inspected = inspectFn(this, {
1138
+ ...options,
1139
+ showHidden: false,
1140
+ customInspect: false
1141
+ });
1142
+ return `Typed ${inspected}`;
2228
1143
  }
2229
- return target;
2230
1144
  };
2231
- var notifyOwnerChain = (target) => {
2232
- const owner = getOwner(target);
2233
- if (owner) {
2234
- queueOwnerNotification(getEchoRoot(owner));
2235
- notifyOwnerChain(owner);
2236
- }
1145
+ var toJSON = (target) => {
1146
+ return {
1147
+ "@type": "TypedReactiveObject",
1148
+ ...target
1149
+ };
2237
1150
  };
2238
-
2239
- // src/internal/proxy/proxy-types.ts
2240
- var objectData = Symbol.for("@dxos/live-object/objectData");
2241
-
2242
- // src/internal/proxy/reactive-array.ts
2243
- var checkArrayMutationAllowed = (arr, method) => {
2244
- const target = isProxy(arr) ? getProxyTarget(arr) : arr;
2245
- const echoRoot = getEchoRoot(target);
2246
- const isInitialized = echoRoot[ChangeId] === true || EventId in echoRoot;
2247
- if (!isInitialized) {
2248
- return;
1151
+ var setSchemaProperties = (obj, schema) => {
1152
+ const schemaType = getSchemaDXN(schema);
1153
+ if (schemaType != null) {
1154
+ defineHiddenProperty(obj, TypeId, schemaType);
2249
1155
  }
2250
- if (!isInChangeContext(echoRoot)) {
2251
- throw createArrayMethodError(method);
1156
+ defineHiddenProperty(obj, SchemaId, schema);
1157
+ for (const key in obj) {
1158
+ if (isValidProxyTarget(obj[key])) {
1159
+ const elementSchema = SchemaValidator.getTargetPropertySchema(obj, key);
1160
+ if (elementSchema != null) {
1161
+ setSchemaProperties(obj[key], elementSchema);
1162
+ }
1163
+ }
2252
1164
  }
2253
1165
  };
2254
- var ReactiveArray = class extends Array {
2255
- static get [Symbol.species]() {
2256
- return Array;
1166
+ var prepareTypedTarget = (target, schema) => {
1167
+ if (!SchemaAST3.isTypeLiteral(schema.ast)) {
1168
+ throw new Error("schema has to describe an object type");
2257
1169
  }
2258
- static {
2259
- const BATCHED_METHODS = [
2260
- "push",
2261
- "pop",
2262
- "shift",
2263
- "unshift",
2264
- "splice",
2265
- "sort",
2266
- "reverse"
2267
- ];
2268
- for (const method of BATCHED_METHODS) {
2269
- Object.defineProperty(this.prototype, method, {
2270
- enumerable: false,
2271
- value: function(...args) {
2272
- checkArrayMutationAllowed(this, method);
2273
- let result;
2274
- batchEvents(() => {
2275
- result = Array.prototype[method].apply(this, args);
2276
- });
2277
- return result;
2278
- }
2279
- });
1170
+ SchemaValidator.validateSchema(schema);
1171
+ const _ = Schema4.asserts(schema)(target);
1172
+ makeArraysReactive(target);
1173
+ setSchemaProperties(target, schema);
1174
+ };
1175
+ var makeArraysReactive = (target) => {
1176
+ for (const key in target) {
1177
+ if (target[symbolIsProxy]) {
1178
+ continue;
1179
+ }
1180
+ if (Array.isArray(target[key])) {
1181
+ target[key] = ReactiveArray.from(target[key]);
1182
+ }
1183
+ if (typeof target[key] === "object") {
1184
+ makeArraysReactive(target[key]);
2280
1185
  }
2281
1186
  }
2282
1187
  };
2283
1188
 
2284
- // src/internal/proxy/schema-validator.ts
2285
- import * as Schema11 from "effect/Schema";
2286
- import * as SchemaAST7 from "effect/SchemaAST";
2287
- import { invariant as invariant13 } from "@dxos/invariant";
2288
- import { log as log2 } from "@dxos/log";
2289
- var __dxlog_file13 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/proxy/schema-validator.ts";
2290
- var SchemaValidator = class {
2291
- /**
2292
- * Recursively check that schema specifies constructions we can handle.
2293
- * Validates there are no ambiguous discriminated union types.
2294
- */
2295
- static validateSchema(schema) {
2296
- const visitAll = (nodes) => nodes.forEach((node) => this.validateSchema(Schema11.make(node)));
2297
- if (SchemaAST7.isUnion(schema.ast)) {
2298
- const typeAstList = schema.ast.types.filter((type) => SchemaAST7.isTypeLiteral(type));
2299
- if (typeAstList.length > 1) {
2300
- getTypeDiscriminators(typeAstList);
2301
- }
2302
- visitAll(typeAstList);
2303
- } else if (SchemaAST7.isTupleType(schema.ast)) {
2304
- const positionalTypes = schema.ast.elements.map((t) => t.type);
2305
- const allTypes = positionalTypes.concat(schema.ast.rest.map((t) => t.type));
2306
- visitAll(allTypes);
2307
- } else if (SchemaAST7.isTypeLiteral(schema.ast)) {
2308
- visitAll(SchemaAST7.getPropertySignatures(schema.ast).map((p) => p.type));
2309
- }
1189
+ // src/internal/common/proxy/make-object.ts
1190
+ var makeObject = (schema, obj, meta) => {
1191
+ return createReactiveObject(Object.assign({}, obj), meta, schema);
1192
+ };
1193
+ var createReactiveObject = (obj, meta, schema) => {
1194
+ if (!isValidProxyTarget(obj)) {
1195
+ throw new Error("Value cannot be made into a reactive object.");
2310
1196
  }
2311
- static hasTypeAnnotation(rootObjectSchema, property, annotation) {
2312
- try {
2313
- let type = this.getPropertySchema(rootObjectSchema, [
2314
- property
2315
- ]);
2316
- if (SchemaAST7.isTupleType(type.ast)) {
2317
- type = this.getPropertySchema(rootObjectSchema, [
2318
- property,
2319
- "0"
2320
- ]);
2321
- }
2322
- return type.ast.annotations[annotation] != null;
2323
- } catch {
2324
- return false;
2325
- }
1197
+ if (!schema) {
1198
+ throw new Error("Schema is required for reactive objects. Use Atom for untyped reactive state.");
2326
1199
  }
2327
- static getPropertySchema(rootObjectSchema, propertyPath, getProperty = () => null) {
2328
- let schema = rootObjectSchema;
2329
- for (let i = 0; i < propertyPath.length; i++) {
2330
- const propertyName = propertyPath[i];
2331
- const tupleAst = unwrapArray(schema.ast);
2332
- if (tupleAst != null) {
2333
- schema = getArrayElementSchema(tupleAst, propertyName);
2334
- } else {
2335
- const propertyType = getPropertyType(schema.ast, propertyName.toString(), (propertyName2) => getProperty([
2336
- ...propertyPath.slice(0, i),
2337
- propertyName2
2338
- ]));
2339
- if (propertyType == null) {
2340
- log2.warn("unknown property", {
2341
- path: propertyPath,
2342
- property: propertyName
2343
- }, {
2344
- F: __dxlog_file13,
2345
- L: 71,
2346
- S: this,
2347
- C: (f, a) => f(...a)
2348
- });
2349
- continue;
2350
- }
2351
- schema = Schema11.make(propertyType).annotations(propertyType.annotations);
2352
- }
2353
- }
2354
- return schema;
1200
+ const parent = obj[ParentId];
1201
+ if (parent !== void 0) {
1202
+ delete obj[ParentId];
2355
1203
  }
2356
- static getTargetPropertySchema(target, prop) {
2357
- const schema = target[SchemaId];
2358
- invariant13(schema, "target has no schema", {
2359
- F: __dxlog_file13,
2360
- L: 84,
2361
- S: this,
2362
- A: [
2363
- "schema",
2364
- "'target has no schema'"
2365
- ]
2366
- });
2367
- const arrayAst = unwrapArray(schema.ast);
2368
- if (arrayAst != null) {
2369
- return getArrayElementSchema(arrayAst, prop);
1204
+ const annotation = getTypeAnnotation(schema);
1205
+ if (annotation) {
1206
+ setIdOnTarget(obj);
1207
+ defineHiddenProperty(obj, KindId, annotation.kind);
1208
+ }
1209
+ initMeta(obj, meta);
1210
+ if (parent !== void 0) {
1211
+ defineHiddenProperty(obj, ParentId, parent);
1212
+ }
1213
+ prepareTypedTarget(obj, schema);
1214
+ attachTypedJsonSerializer(obj);
1215
+ const proxy = createProxy(obj, TypedReactiveHandler.instance);
1216
+ const metaProxy = obj[MetaId];
1217
+ if (metaProxy) {
1218
+ const metaTarget = getProxyTarget(metaProxy);
1219
+ if (metaTarget) {
1220
+ setMetaOwner(metaTarget, obj);
2370
1221
  }
2371
- const propertyType = getPropertyType(schema.ast, prop.toString(), (prop2) => target[prop2]);
2372
- if (propertyType == null) {
2373
- return Schema11.Any;
1222
+ }
1223
+ return proxy;
1224
+ };
1225
+ var setIdOnTarget = (target) => {
1226
+ if ("id" in target && target.id !== void 0 && target.id !== null) {
1227
+ if (!ObjectId.isValid(target.id)) {
1228
+ throw new Error("Invalid object id format.");
2374
1229
  }
2375
- invariant13(propertyType, `invalid property: ${prop.toString()}`, {
2376
- F: __dxlog_file13,
2377
- L: 95,
2378
- S: this,
2379
- A: [
2380
- "propertyType",
2381
- "`invalid property: ${prop.toString()}`"
2382
- ]
2383
- });
2384
- return Schema11.make(propertyType);
1230
+ } else {
1231
+ target.id = ObjectId.random();
2385
1232
  }
2386
1233
  };
2387
- var getArrayElementSchema = (tupleAst, property) => {
2388
- const elementIndex = typeof property === "string" ? parseInt(property, 10) : Number.NaN;
2389
- if (Number.isNaN(elementIndex)) {
2390
- invariant13(property === "length", `invalid array property: ${String(property)}`, {
2391
- F: __dxlog_file13,
2392
- L: 111,
2393
- S: void 0,
2394
- A: [
2395
- "property === 'length'",
2396
- "`invalid array property: ${String(property)}`"
2397
- ]
2398
- });
2399
- return Schema11.Number;
1234
+ var initMeta = (obj, meta = {
1235
+ keys: []
1236
+ }) => {
1237
+ prepareTypedTarget(meta, ObjectMetaSchema);
1238
+ defineHiddenProperty(obj, MetaId, createProxy(meta, TypedReactiveHandler.instance));
1239
+ };
1240
+
1241
+ // src/internal/common/proxy/reactive.ts
1242
+ var subscribe = (obj, callback) => {
1243
+ const target = getProxyTarget(obj);
1244
+ if (target && EventId in target) {
1245
+ return target[EventId].on(callback);
2400
1246
  }
2401
- if (elementIndex < tupleAst.elements.length) {
2402
- const elementType = tupleAst.elements[elementIndex].type;
2403
- return Schema11.make(elementType).annotations(elementType.annotations);
1247
+ return () => {
1248
+ };
1249
+ };
1250
+ var change = (obj, callback) => {
1251
+ const changeFn = obj[ChangeId];
1252
+ if (changeFn) {
1253
+ changeFn(callback);
1254
+ } else {
1255
+ callback(obj);
2404
1256
  }
2405
- const restType = tupleAst.rest;
2406
- return Schema11.make(restType[0].type).annotations(restType[0].annotations);
2407
1257
  };
2408
- var flattenUnion = (typeAst) => SchemaAST7.isUnion(typeAst) ? typeAst.types.flatMap(flattenUnion) : [
2409
- typeAst
2410
- ];
2411
- var getProperties = (typeAst, getTargetPropertyFn) => {
2412
- const astCandidates = flattenUnion(typeAst);
2413
- const typeAstList = astCandidates.filter((type) => SchemaAST7.isTypeLiteral(type));
2414
- if (typeAstList.length === 0) {
2415
- return [];
2416
- }
2417
- if (typeAstList.length === 1) {
2418
- return SchemaAST7.getPropertySignatures(typeAstList[0]);
2419
- }
2420
- const typeDiscriminators = getTypeDiscriminators(typeAstList);
2421
- const targetPropertyValue = getTargetPropertyFn(String(typeDiscriminators[0].name));
2422
- const typeIndex = typeDiscriminators.findIndex((p) => targetPropertyValue === p.type.literal);
2423
- invariant13(typeIndex !== -1, "discriminator field not set on target", {
2424
- F: __dxlog_file13,
2425
- L: 142,
2426
- S: void 0,
1258
+
1259
+ // src/internal/Entity/api.ts
1260
+ import { invariant as invariant10 } from "@dxos/invariant";
1261
+ import { assumeType as assumeType3 } from "@dxos/util";
1262
+
1263
+ // src/internal/Entity/model.ts
1264
+ import { invariant as invariant8 } from "@dxos/invariant";
1265
+ import { DXN as DXN2, ObjectId as ObjectId2 } from "@dxos/keys";
1266
+ import { assumeType } from "@dxos/util";
1267
+ var __dxlog_file8 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/Entity/model.ts";
1268
+ function assertObjectModel(obj) {
1269
+ invariant8(typeof obj === "object" && obj !== null, "Invalid object model: not an object", {
1270
+ F: __dxlog_file8,
1271
+ L: 94,
1272
+ S: this,
2427
1273
  A: [
2428
- "typeIndex !== -1",
2429
- "'discriminator field not set on target'"
1274
+ "typeof obj === 'object' && obj !== null",
1275
+ "'Invalid object model: not an object'"
2430
1276
  ]
2431
1277
  });
2432
- return SchemaAST7.getPropertySignatures(typeAstList[typeIndex]);
2433
- };
2434
- var getPropertyType = (ast, propertyName, getTargetPropertyFn) => {
2435
- const anyOrObject = unwrapAst(ast, (candidate) => SchemaAST7.isAnyKeyword(candidate) || SchemaAST7.isObjectKeyword(candidate));
2436
- if (anyOrObject != null) {
2437
- return ast;
2438
- }
2439
- const typeOrDiscriminatedUnion = unwrapAst(ast, (t) => {
2440
- return SchemaAST7.isTypeLiteral(t) || SchemaAST7.isUnion(t) && t.types.some((t2) => SchemaAST7.isTypeLiteral(t2));
1278
+ assumeType(obj);
1279
+ invariant8(ObjectId2.isValid(obj.id), "Invalid object model: invalid id", {
1280
+ F: __dxlog_file8,
1281
+ L: 96,
1282
+ S: this,
1283
+ A: [
1284
+ "ObjectId.isValid(obj.id)",
1285
+ "'Invalid object model: invalid id'"
1286
+ ]
2441
1287
  });
2442
- if (typeOrDiscriminatedUnion == null) {
2443
- return null;
2444
- }
2445
- const targetProperty = getProperties(typeOrDiscriminatedUnion, getTargetPropertyFn).find((p) => p.name === propertyName);
2446
- if (targetProperty != null) {
2447
- return unwrapAst(targetProperty.type);
2448
- }
2449
- const indexSignatureType = unwrapAst(ast, SchemaAST7.isTypeLiteral);
2450
- if (indexSignatureType && SchemaAST7.isTypeLiteral(indexSignatureType) && indexSignatureType.indexSignatures.length > 0) {
2451
- return unwrapAst(indexSignatureType.indexSignatures[0].type);
1288
+ invariant8(obj[TypeId] === void 0 || obj[TypeId] instanceof DXN2, "Invalid object model: invalid type", {
1289
+ F: __dxlog_file8,
1290
+ L: 97,
1291
+ S: this,
1292
+ A: [
1293
+ "obj[TypeId] === undefined || obj[TypeId] instanceof DXN",
1294
+ "'Invalid object model: invalid type'"
1295
+ ]
1296
+ });
1297
+ invariant8(obj[KindId] === EntityKind.Object || obj[KindId] === EntityKind.Relation, "Invalid object model: invalid entity kind", {
1298
+ F: __dxlog_file8,
1299
+ L: 98,
1300
+ S: this,
1301
+ A: [
1302
+ "obj[KindId] === EntityKind.Object || obj[KindId] === EntityKind.Relation",
1303
+ "'Invalid object model: invalid entity kind'"
1304
+ ]
1305
+ });
1306
+ if (obj[KindId] === EntityKind.Relation) {
1307
+ invariant8(obj[RelationSourceDXNId] instanceof DXN2, "Invalid object model: invalid relation source", {
1308
+ F: __dxlog_file8,
1309
+ L: 104,
1310
+ S: this,
1311
+ A: [
1312
+ "obj[RelationSourceDXNId] instanceof DXN",
1313
+ "'Invalid object model: invalid relation source'"
1314
+ ]
1315
+ });
1316
+ invariant8(obj[RelationTargetDXNId] instanceof DXN2, "Invalid object model: invalid relation target", {
1317
+ F: __dxlog_file8,
1318
+ L: 105,
1319
+ S: this,
1320
+ A: [
1321
+ "obj[RelationTargetDXNId] instanceof DXN",
1322
+ "'Invalid object model: invalid relation target'"
1323
+ ]
1324
+ });
1325
+ invariant8(!(obj[RelationSourceId] instanceof DXN2), "Invalid object model: source pointer is a DXN", {
1326
+ F: __dxlog_file8,
1327
+ L: 106,
1328
+ S: this,
1329
+ A: [
1330
+ "!(obj[RelationSourceId] instanceof DXN)",
1331
+ "'Invalid object model: source pointer is a DXN'"
1332
+ ]
1333
+ });
1334
+ invariant8(!(obj[RelationTargetId] instanceof DXN2), "Invalid object model: target pointer is a DXN", {
1335
+ F: __dxlog_file8,
1336
+ L: 107,
1337
+ S: this,
1338
+ A: [
1339
+ "!(obj[RelationTargetId] instanceof DXN)",
1340
+ "'Invalid object model: target pointer is a DXN'"
1341
+ ]
1342
+ });
2452
1343
  }
2453
- return null;
2454
- };
2455
- var getTypeDiscriminators = (typeAstList) => {
2456
- const discriminatorPropCandidates = typeAstList.flatMap(SchemaAST7.getPropertySignatures).filter((p) => SchemaAST7.isLiteral(p.type));
2457
- const propertyName = discriminatorPropCandidates[0].name;
2458
- const isValidDiscriminator = discriminatorPropCandidates.every((p) => p.name === propertyName && !p.isOptional);
2459
- const everyTypeHasDiscriminator = discriminatorPropCandidates.length === typeAstList.length;
2460
- const isDiscriminatedUnion = isValidDiscriminator && everyTypeHasDiscriminator;
2461
- invariant13(isDiscriminatedUnion, "type ambiguity: every type in a union must have a single unique-literal field", {
2462
- F: __dxlog_file13,
2463
- L: 193,
1344
+ }
1345
+
1346
+ // src/internal/Entity/util.ts
1347
+ import * as Schema5 from "effect/Schema";
1348
+ import { assertArgument as assertArgument2, invariant as invariant9 } from "@dxos/invariant";
1349
+ import { DXN as DXN3, ObjectId as ObjectId3 } from "@dxos/keys";
1350
+ import { assumeType as assumeType2 } from "@dxos/util";
1351
+ var __dxlog_file9 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/Entity/util.ts";
1352
+ var getObjectDXN = (object) => {
1353
+ invariant9(!Schema5.isSchema(object), "schema not allowed in this function", {
1354
+ F: __dxlog_file9,
1355
+ L: 19,
2464
1356
  S: void 0,
2465
1357
  A: [
2466
- "isDiscriminatedUnion",
2467
- "'type ambiguity: every type in a union must have a single unique-literal field'"
1358
+ "!Schema.isSchema(object)",
1359
+ "'schema not allowed in this function'"
2468
1360
  ]
2469
1361
  });
2470
- return discriminatorPropCandidates;
2471
- };
2472
- var unwrapAst = (rootAst, predicate) => {
2473
- let ast = rootAst;
2474
- while (ast != null) {
2475
- if (predicate?.(ast)) {
2476
- return ast;
2477
- }
2478
- if (SchemaAST7.isUnion(ast)) {
2479
- const next = ast.types.find((t) => predicate != null && predicate(t) || SchemaAST7.isSuspend(t));
2480
- if (next != null) {
2481
- ast = next;
2482
- continue;
2483
- }
2484
- }
2485
- if (SchemaAST7.isSuspend(ast)) {
2486
- ast = ast.f();
2487
- } else {
2488
- return predicate == null ? ast : null;
2489
- }
1362
+ assertArgument2(typeof object === "object" && object != null, "object", "expected object");
1363
+ assumeType2(object);
1364
+ if (object[SelfDXNId]) {
1365
+ invariant9(object[SelfDXNId] instanceof DXN3, "Invalid object model: invalid self dxn", {
1366
+ F: __dxlog_file9,
1367
+ L: 24,
1368
+ S: void 0,
1369
+ A: [
1370
+ "object[SelfDXNId] instanceof DXN",
1371
+ "'Invalid object model: invalid self dxn'"
1372
+ ]
1373
+ });
1374
+ return object[SelfDXNId];
2490
1375
  }
2491
- return null;
1376
+ if (!ObjectId3.isValid(object.id)) {
1377
+ throw new TypeError("Object id is not valid.");
1378
+ }
1379
+ return DXN3.fromLocalObjectId(object.id);
2492
1380
  };
2493
- var unwrapArray = (ast) => unwrapAst(ast, SchemaAST7.isTupleType);
2494
- var checkIdNotPresentOnSchema = (schema) => {
2495
- invariant13(SchemaAST7.isTypeLiteral(schema.ast), void 0, {
2496
- F: __dxlog_file13,
2497
- L: 236,
1381
+
1382
+ // src/internal/Entity/api.ts
1383
+ var __dxlog_file10 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/Entity/api.ts";
1384
+ var getDXN = (entity) => {
1385
+ const dxn = getObjectDXN(entity);
1386
+ invariant10(dxn != null, "Invalid entity.", {
1387
+ F: __dxlog_file10,
1388
+ L: 20,
2498
1389
  S: void 0,
2499
1390
  A: [
2500
- "SchemaAST.isTypeLiteral(schema.ast)",
2501
- ""
1391
+ "dxn != null",
1392
+ "'Invalid entity.'"
2502
1393
  ]
2503
1394
  });
2504
- const idProperty = SchemaAST7.getPropertySignatures(schema.ast).find((prop) => prop.name === "id");
2505
- if (idProperty != null) {
2506
- throw new Error('"id" property name is reserved');
2507
- }
1395
+ return dxn;
1396
+ };
1397
+ var getDatabase = (entity) => {
1398
+ assumeType3(entity);
1399
+ return entity[ObjectDatabaseId];
2508
1400
  };
2509
1401
 
2510
- // src/internal/proxy/typed-handler.ts
2511
- var __dxlog_file14 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/proxy/typed-handler.ts";
2512
- var deepCopy = (value2, visited = /* @__PURE__ */ new Map()) => {
2513
- if (value2 == null || typeof value2 !== "object") {
2514
- return value2;
2515
- }
2516
- const actualValue = getRawTarget(value2);
2517
- if (visited.has(actualValue)) {
2518
- return visited.get(actualValue);
2519
- }
2520
- if (actualValue instanceof ReactiveArray) {
2521
- const copy2 = new ReactiveArray();
2522
- visited.set(actualValue, copy2);
2523
- for (const item of actualValue) {
2524
- copy2.push(deepCopy(item, visited));
1402
+ // src/internal/Entity/entity.ts
1403
+ import * as Schema6 from "effect/Schema";
1404
+ var makeEchoTypeSchema = (fields, ast, typename, version2, kind) => {
1405
+ return class EchoObjectSchemaClass extends Schema6.make(ast) {
1406
+ static typename = typename;
1407
+ static version = version2;
1408
+ static [SchemaKindId] = kind;
1409
+ static fields = fields;
1410
+ static annotations(annotations3) {
1411
+ const schema = Schema6.make(ast).annotations(annotations3);
1412
+ return makeEchoTypeSchema(fields, schema.ast, typename, version2, kind);
2525
1413
  }
2526
- copyHiddenProperties(actualValue, copy2);
2527
- return copy2;
2528
- }
2529
- const proto = Object.getPrototypeOf(actualValue);
2530
- if (proto !== Object.prototype && proto !== Array.prototype && proto !== null) {
2531
- return value2;
2532
- }
2533
- if (Array.isArray(actualValue)) {
2534
- const copy2 = new ReactiveArray();
2535
- visited.set(actualValue, copy2);
2536
- for (const item of actualValue) {
2537
- copy2.push(deepCopy(item, visited));
1414
+ // static make(
1415
+ // props: RequiredKeys<Schema.TypeLiteral.Constructor<Fields, []>> extends never
1416
+ // ? void | Simplify<Schema.TypeLiteral.Constructor<Fields, []>>
1417
+ // : Simplify<Schema.TypeLiteral.Constructor<Fields, []>>,
1418
+ // options?: MakeProps,
1419
+ // ): Simplify<Schema.TypeLiteral.Type<Fields, []>> {
1420
+ // const propsWithDefaults: any = _lazilyMergeDefaults(fields, { ...(props as any) });
1421
+ // return _getDisableValidationMakeOption(options)
1422
+ // ? propsWithDefaults
1423
+ // : ParseResult.validateSync(this)(propsWithDefaults);
1424
+ // }
1425
+ static instanceOf(value2) {
1426
+ return Schema6.is(this)(value2);
2538
1427
  }
2539
- return copy2;
2540
- }
2541
- const copy = {};
2542
- visited.set(actualValue, copy);
2543
- for (const key of Object.keys(actualValue)) {
2544
- copy[key] = deepCopy(actualValue[key], visited);
2545
- }
2546
- copyHiddenProperties(actualValue, copy);
2547
- return copy;
2548
- };
2549
- var copyHiddenProperties = (source, target) => {
2550
- if (SchemaId in source) {
2551
- defineHiddenProperty(target, SchemaId, source[SchemaId]);
2552
- }
2553
- if (TypeId in source) {
2554
- defineHiddenProperty(target, TypeId, source[TypeId]);
2555
- }
1428
+ };
2556
1429
  };
2557
- var TypedReactiveHandler = class _TypedReactiveHandler {
2558
- static instance = new _TypedReactiveHandler();
2559
- _proxyMap = /* @__PURE__ */ new WeakMap();
2560
- _inSet = false;
2561
- constructor() {
2562
- }
2563
- init(target) {
2564
- invariant14(typeof target === "object" && target !== null, void 0, {
2565
- F: __dxlog_file14,
2566
- L: 147,
2567
- S: this,
1430
+
1431
+ // src/internal/Entity/object.ts
1432
+ import * as Schema7 from "effect/Schema";
1433
+ import * as SchemaAST4 from "effect/SchemaAST";
1434
+ import { invariant as invariant11 } from "@dxos/invariant";
1435
+ var __dxlog_file11 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/Entity/object.ts";
1436
+ var EchoObjectSchema = ({ typename, version: version2 }) => {
1437
+ return (self) => {
1438
+ invariant11(typeof TypeAnnotationId === "symbol", "Sanity.", {
1439
+ F: __dxlog_file11,
1440
+ L: 37,
1441
+ S: void 0,
2568
1442
  A: [
2569
- "typeof target === 'object' && target !== null",
2570
- ""
1443
+ "typeof TypeAnnotationId === 'symbol'",
1444
+ "'Sanity.'"
2571
1445
  ]
2572
1446
  });
2573
- invariant14(SchemaId in target, "Schema is not defined for the target", {
2574
- F: __dxlog_file14,
2575
- L: 148,
2576
- S: this,
1447
+ invariant11(SchemaAST4.isTypeLiteral(self.ast), "Schema must be a TypeLiteral.", {
1448
+ F: __dxlog_file11,
1449
+ L: 38,
1450
+ S: void 0,
2577
1451
  A: [
2578
- "SchemaId in target",
2579
- "'Schema is not defined for the target'"
1452
+ "SchemaAST.isTypeLiteral(self.ast)",
1453
+ "'Schema must be a TypeLiteral.'"
2580
1454
  ]
2581
1455
  });
2582
- const hasOwner = !!getOwner(target);
2583
- if (!(EventId in target) && !hasOwner) {
2584
- defineHiddenProperty(target, EventId, new Event2());
2585
- }
2586
- defineHiddenProperty(target, ObjectDeletedId, false);
2587
- if (!hasOwner && !(ChangeId in target)) {
2588
- defineHiddenProperty(target, ChangeId, true);
2589
- }
2590
- if (!hasOwner) {
2591
- for (const key in target) {
2592
- if (target[symbolIsProxy]) {
2593
- continue;
2594
- }
2595
- let value2 = target[key];
2596
- if (isValidProxyTarget(value2) || isProxy(value2)) {
2597
- if (hasForeignOwner(value2, target)) {
2598
- value2 = deepCopy(value2);
2599
- target[key] = value2;
2600
- }
2601
- setOwnerRecursive(value2, target);
2602
- }
2603
- }
2604
- }
2605
- Object.defineProperty(target, inspectCustom2, {
2606
- enumerable: false,
2607
- configurable: true,
2608
- value: this._inspect.bind(target)
1456
+ const fields = self.fields ?? {};
1457
+ const schemaWithId = Schema7.extend(self, Schema7.Struct({
1458
+ id: Schema7.String
1459
+ }));
1460
+ const ast = SchemaAST4.annotations(schemaWithId.ast, {
1461
+ // TODO(dmaretskyi): `extend` kills the annotations.
1462
+ ...self.ast.annotations,
1463
+ [TypeAnnotationId]: {
1464
+ kind: EntityKind.Object,
1465
+ typename,
1466
+ version: version2
1467
+ },
1468
+ // TODO(dmaretskyi): TypeIdentifierAnnotationId?
1469
+ [SchemaAST4.JSONSchemaAnnotationId]: makeTypeJsonSchemaAnnotation({
1470
+ kind: EntityKind.Object,
1471
+ typename,
1472
+ version: version2
1473
+ })
2609
1474
  });
1475
+ return makeEchoTypeSchema(fields, ast, typename, version2, EntityKind.Object);
1476
+ };
1477
+ };
1478
+
1479
+ // src/internal/Entity/relation.ts
1480
+ import * as Schema8 from "effect/Schema";
1481
+ import * as SchemaAST5 from "effect/SchemaAST";
1482
+ import { raise } from "@dxos/debug";
1483
+ import { assertArgument as assertArgument3, invariant as invariant12 } from "@dxos/invariant";
1484
+ import { DXN as DXN4 } from "@dxos/keys";
1485
+ var __dxlog_file12 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/Entity/relation.ts";
1486
+ var EchoRelationSchema = ({ source, target, typename, version: version2 }) => {
1487
+ assertArgument3(Schema8.isSchema(source), "source");
1488
+ assertArgument3(Schema8.isSchema(target), "target");
1489
+ const sourceDXN = getDXNForRelationSchemaRef(source);
1490
+ const targetDXN = getDXNForRelationSchemaRef(target);
1491
+ if (getEntityKind(source) !== EntityKind.Object) {
1492
+ raise(new Error("Source schema must be an echo object schema."));
2610
1493
  }
2611
- get(target, prop, receiver) {
2612
- switch (prop) {
2613
- // TODO(burdon): Remove?
2614
- case objectData: {
2615
- return toJSON(target);
2616
- }
2617
- case ChangeId: {
2618
- if (target[ChangeId] !== true) {
2619
- return void 0;
2620
- }
2621
- return (callback) => executeChange(target, target, receiver, callback);
2622
- }
2623
- }
2624
- if (Object.getOwnPropertyDescriptor(target, prop)?.get) {
2625
- return Reflect.get(target, prop, receiver);
2626
- }
2627
- const value2 = Reflect.get(target, prop, receiver);
2628
- if (isValidProxyTarget(value2)) {
2629
- return createProxy(value2, this);
2630
- }
2631
- return value2;
1494
+ if (getEntityKind(target) !== EntityKind.Object) {
1495
+ raise(new Error("Target schema must be an echo object schema."));
2632
1496
  }
2633
- set(target, prop, value2, receiver) {
2634
- const echoRoot = getEchoRoot(target);
2635
- const isInitialized = ChangeId in echoRoot || EventId in echoRoot;
2636
- const isSymbolProp = typeof prop === "symbol";
2637
- if (isInitialized && !isSymbolProp && !isInChangeContext(echoRoot)) {
2638
- throw new Error(`Cannot modify object property "${String(prop)}" outside of Obj.change(). Use Obj.change(obj, (mutableObj) => { mutableObj.property = value; }) instead.`);
2639
- }
2640
- let result = false;
2641
- this._inSet = true;
2642
- try {
2643
- batchEvents(() => {
2644
- const { echoRoot: _, preparedValue } = this._prepareValueForAssignment(target, prop, value2);
2645
- result = Reflect.set(target, prop, preparedValue, receiver);
2646
- if (isInitialized) {
2647
- queueNotification(echoRoot);
2648
- notifyOwnerChain(target);
2649
- }
2650
- });
2651
- } finally {
2652
- this._inSet = false;
2653
- }
2654
- return result;
1497
+ return (self) => {
1498
+ invariant12(SchemaAST5.isTypeLiteral(self.ast), "Schema must be a TypeLiteral.", {
1499
+ F: __dxlog_file12,
1500
+ L: 110,
1501
+ S: void 0,
1502
+ A: [
1503
+ "SchemaAST.isTypeLiteral(self.ast)",
1504
+ "'Schema must be a TypeLiteral.'"
1505
+ ]
1506
+ });
1507
+ const fields = self.fields ?? {};
1508
+ const schemaWithId = Schema8.extend(self, Schema8.Struct({
1509
+ id: Schema8.String
1510
+ }));
1511
+ const ast = SchemaAST5.annotations(schemaWithId.ast, {
1512
+ // TODO(dmaretskyi): `extend` kills the annotations.
1513
+ ...self.ast.annotations,
1514
+ [TypeAnnotationId]: {
1515
+ kind: EntityKind.Relation,
1516
+ typename,
1517
+ version: version2,
1518
+ sourceSchema: sourceDXN,
1519
+ targetSchema: targetDXN
1520
+ },
1521
+ // TODO(dmaretskyi): TypeIdentifierAnnotationId?
1522
+ [SchemaAST5.JSONSchemaAnnotationId]: makeTypeJsonSchemaAnnotation({
1523
+ kind: EntityKind.Relation,
1524
+ typename,
1525
+ version: version2,
1526
+ relationSource: sourceDXN,
1527
+ relationTarget: targetDXN
1528
+ })
1529
+ });
1530
+ return makeEchoTypeSchema(fields, ast, typename, version2, EntityKind.Relation);
1531
+ };
1532
+ };
1533
+ var getDXNForRelationSchemaRef = (schema) => {
1534
+ assertArgument3(Schema8.isSchema(schema), "schema");
1535
+ const identifier = getTypeIdentifierAnnotation(schema);
1536
+ if (identifier) {
1537
+ return identifier;
2655
1538
  }
2656
- ownKeys(target) {
2657
- return Reflect.ownKeys(target);
1539
+ const typename = getSchemaTypename(schema);
1540
+ if (!typename) {
1541
+ throw new Error("Schema must have a typename");
2658
1542
  }
2659
- deleteProperty(target, property) {
2660
- const echoRoot = getEchoRoot(target);
2661
- const isInitialized = echoRoot[ChangeId] === true || EventId in echoRoot;
2662
- const isSymbolProp = typeof property === "symbol";
2663
- if (isInitialized && !isSymbolProp && !isInChangeContext(echoRoot)) {
2664
- throw createPropertyDeleteError(property);
2665
- }
2666
- const result = Reflect.deleteProperty(target, property);
2667
- if (isInitialized) {
2668
- queueNotification(echoRoot);
2669
- }
2670
- return result;
1543
+ return DXN4.fromTypename(typename).toString();
1544
+ };
1545
+
1546
+ // src/internal/Entity/version.ts
1547
+ import { assertArgument as assertArgument4 } from "@dxos/invariant";
1548
+ var unversioned = {
1549
+ [VersionTypeId]: {},
1550
+ versioned: false
1551
+ };
1552
+ var isVersion = (entity) => {
1553
+ return entity != null && typeof entity === "object" && VersionTypeId in entity;
1554
+ };
1555
+ var version = (entity) => {
1556
+ const ver = entity[ObjectVersionId];
1557
+ if (ver === void 0) {
1558
+ return unversioned;
2671
1559
  }
2672
- defineProperty(target, property, attributes) {
2673
- const echoRoot = getEchoRoot(target);
2674
- const isInitialized = ChangeId in echoRoot || EventId in echoRoot;
2675
- const isSymbolProp = typeof property === "symbol";
2676
- if (isInitialized && !isSymbolProp && !isInChangeContext(echoRoot)) {
2677
- throw new Error(`Cannot modify object property "${String(property)}" outside of Obj.change(). Use Obj.change(obj, (mutableObj) => { mutableObj.property = value; }) instead.`);
2678
- }
2679
- const { echoRoot: _, preparedValue } = this._prepareValueForAssignment(target, property, attributes.value);
2680
- const result = Reflect.defineProperty(target, property, {
2681
- ...attributes,
2682
- value: preparedValue
2683
- });
2684
- if (!this._inSet && isInitialized) {
2685
- queueNotification(echoRoot);
2686
- }
2687
- return result;
1560
+ return ver;
1561
+ };
1562
+ var versionValid = (ver) => {
1563
+ assertArgument4(isVersion(ver), "version", "Invalid version object");
1564
+ return !!ver.versioned;
1565
+ };
1566
+ var compareVersions = (version1, version2) => {
1567
+ assertArgument4(isVersion(version1), "version1", "Invalid version object");
1568
+ assertArgument4(isVersion(version2), "version2", "Invalid version object");
1569
+ if (!versionValid(version1) || !versionValid(version2)) {
1570
+ return "unversioned";
2688
1571
  }
2689
- /**
2690
- * Prepare a value for assignment to a typed object property.
2691
- * Handles cycle detection, copy-on-assign, array conversion, validation, and ownership.
2692
- */
2693
- _prepareValueForAssignment(target, prop, value2) {
2694
- const echoRoot = getEchoRoot(target);
2695
- if (isValidProxyTarget(value2) || isProxy(value2)) {
2696
- if (wouldCreateCycle(echoRoot, value2)) {
2697
- throw new Error("Cannot create cycles in typed object graph. Consider using Ref for circular references.");
2698
- }
2699
- }
2700
- if (isValidProxyTarget(value2) || isProxy(value2)) {
2701
- const actualValue = getRawTarget(value2);
2702
- const isRootEchoObject = EventId in actualValue;
2703
- if (isRootEchoObject) {
2704
- throw new Error("Object references must be wrapped with `Ref.make`");
2705
- }
2706
- }
2707
- if (isValidProxyTarget(value2) || isProxy(value2)) {
2708
- const actualValue = getRawTarget(value2);
2709
- const existingOwner = getOwner(actualValue);
2710
- if (existingOwner != null && existingOwner !== echoRoot) {
2711
- value2 = deepCopy(value2);
2712
- }
2713
- }
2714
- if (Array.isArray(value2) && !(value2 instanceof ReactiveArray)) {
2715
- value2 = ReactiveArray.from(value2);
2716
- }
2717
- const validatedValue = this._validateValue(target, prop, value2);
2718
- if (isValidProxyTarget(validatedValue) || isProxy(validatedValue)) {
2719
- setOwnerRecursive(validatedValue, echoRoot);
2720
- }
2721
- return {
2722
- echoRoot,
2723
- preparedValue: validatedValue
2724
- };
1572
+ if (version1.automergeHeads?.length !== version2.automergeHeads?.length) {
1573
+ return "different";
2725
1574
  }
2726
- _validateValue(target, prop, value2) {
2727
- const schema = SchemaValidator.getTargetPropertySchema(target, prop);
2728
- const _ = Schema12.asserts(schema)(value2);
2729
- if (isValidProxyTarget(value2)) {
2730
- setSchemaProperties(value2, schema);
1575
+ if (version1.automergeHeads?.some((head) => !version2.automergeHeads?.includes(head))) {
1576
+ return "different";
1577
+ }
1578
+ return "equal";
1579
+ };
1580
+ var encodeVersion = (ver) => {
1581
+ return JSON.stringify(ver);
1582
+ };
1583
+ var decodeVersion = (ver) => {
1584
+ const parsed = JSON.parse(ver);
1585
+ parsed[VersionTypeId] = {};
1586
+ return parsed;
1587
+ };
1588
+
1589
+ // src/internal/JsonSchema/json-schema-normalize.ts
1590
+ var normalizeSchema = (schema) => {
1591
+ const copy = structuredClone(schema);
1592
+ go(copy);
1593
+ return copy;
1594
+ };
1595
+ var go = (schema) => {
1596
+ if (typeof schema !== "object" || schema === null) {
1597
+ return;
1598
+ }
1599
+ if (schema.exclusiveMaximum === true) {
1600
+ schema.exclusiveMaximum = schema.maximum;
1601
+ delete schema.exclusiveMaximum;
1602
+ } else if (schema.exclusiveMaximum === false) {
1603
+ delete schema.exclusiveMaximum;
1604
+ }
1605
+ if (schema.exclusiveMinimum === true) {
1606
+ schema.exclusiveMinimum = schema.minimum;
1607
+ delete schema.exclusiveMinimum;
1608
+ } else if (schema.exclusiveMinimum === false) {
1609
+ delete schema.exclusiveMinimum;
1610
+ }
1611
+ for (const key of Object.keys(schema)) {
1612
+ if (!JsonSchemaFields.includes(key)) {
1613
+ delete schema[key];
2731
1614
  }
2732
- return value2;
2733
1615
  }
2734
- _inspect(_, options, inspectFn) {
2735
- const inspected = inspectFn(this, {
2736
- ...options,
2737
- showHidden: false,
2738
- customInspect: false
2739
- });
2740
- return `Typed ${inspected}`;
1616
+ if (schema.properties) {
1617
+ goOnRecord(schema.properties);
1618
+ }
1619
+ if (schema.patternProperties) {
1620
+ goOnRecord(schema.patternProperties);
1621
+ }
1622
+ if (schema.propertyNames) {
1623
+ go(schema.propertyNames);
1624
+ }
1625
+ if (schema.definitions) {
1626
+ goOnRecord(schema.definitions);
1627
+ }
1628
+ if (schema.items) {
1629
+ maybeGoOnArray(schema.items);
1630
+ }
1631
+ if (schema.additionalItems) {
1632
+ maybeGoOnArray(schema.additionalItems);
2741
1633
  }
2742
- };
2743
- var toJSON = (target) => {
2744
- return {
2745
- "@type": "TypedReactiveObject",
2746
- ...target
2747
- };
2748
- };
2749
- var setSchemaProperties = (obj, schema) => {
2750
- const schemaType = getSchemaDXN(schema);
2751
- if (schemaType != null) {
2752
- defineHiddenProperty(obj, TypeId, schemaType);
1634
+ if (schema.contains) {
1635
+ go(schema.contains);
2753
1636
  }
2754
- defineHiddenProperty(obj, SchemaId, schema);
2755
- for (const key in obj) {
2756
- if (isValidProxyTarget(obj[key])) {
2757
- const elementSchema = SchemaValidator.getTargetPropertySchema(obj, key);
2758
- if (elementSchema != null) {
2759
- setSchemaProperties(obj[key], elementSchema);
2760
- }
2761
- }
1637
+ if (schema.if) {
1638
+ go(schema.if);
2762
1639
  }
2763
- };
2764
- var prepareTypedTarget = (target, schema) => {
2765
- if (!SchemaAST8.isTypeLiteral(schema.ast)) {
2766
- throw new Error("schema has to describe an object type");
1640
+ if (schema.then) {
1641
+ go(schema.then);
2767
1642
  }
2768
- SchemaValidator.validateSchema(schema);
2769
- const _ = Schema12.asserts(schema)(target);
2770
- makeArraysReactive(target);
2771
- setSchemaProperties(target, schema);
2772
- };
2773
- var makeArraysReactive = (target) => {
2774
- for (const key in target) {
2775
- if (target[symbolIsProxy]) {
2776
- continue;
2777
- }
2778
- if (Array.isArray(target[key])) {
2779
- target[key] = ReactiveArray.from(target[key]);
2780
- }
2781
- if (typeof target[key] === "object") {
2782
- makeArraysReactive(target[key]);
2783
- }
1643
+ if (schema.else) {
1644
+ go(schema.else);
2784
1645
  }
2785
- };
2786
-
2787
- // src/internal/proxy/make-object.ts
2788
- var makeObject = (schema, obj, meta) => {
2789
- return createReactiveObject({
2790
- ...obj
2791
- }, meta, schema);
2792
- };
2793
- var createReactiveObject = (obj, meta, schema) => {
2794
- if (!isValidProxyTarget(obj)) {
2795
- throw new Error("Value cannot be made into a reactive object.");
1646
+ if (schema.allOf) {
1647
+ maybeGoOnArray(schema.allOf);
2796
1648
  }
2797
- if (!schema) {
2798
- throw new Error("Schema is required for reactive objects. Use Atom for untyped reactive state.");
1649
+ if (schema.anyOf) {
1650
+ maybeGoOnArray(schema.anyOf);
2799
1651
  }
2800
- const annotation = getTypeAnnotation(schema);
2801
- if (annotation) {
2802
- setIdOnTarget(obj);
2803
- defineHiddenProperty(obj, KindId, annotation.kind);
1652
+ if (schema.oneOf) {
1653
+ maybeGoOnArray(schema.oneOf);
2804
1654
  }
2805
- initMeta(obj, meta);
2806
- prepareTypedTarget(obj, schema);
2807
- attachTypedJsonSerializer(obj);
2808
- const proxy = createProxy(obj, TypedReactiveHandler.instance);
2809
- const metaProxy = obj[MetaId];
2810
- if (metaProxy) {
2811
- const metaTarget = getProxyTarget(metaProxy);
2812
- if (metaTarget) {
2813
- setMetaOwner(metaTarget, obj);
2814
- }
1655
+ if (schema.not) {
1656
+ go(schema.not);
1657
+ }
1658
+ if (schema.$defs) {
1659
+ goOnRecord(schema.$defs);
1660
+ }
1661
+ if (schema.reference) {
1662
+ go(schema.reference.schema);
2815
1663
  }
2816
- return proxy;
2817
1664
  };
2818
- var setIdOnTarget = (target) => {
2819
- if ("id" in target && target.id !== void 0 && target.id !== null) {
2820
- if (!ObjectId5.isValid(target.id)) {
2821
- throw new Error("Invalid object id format.");
1665
+ var maybeGoOnArray = (value2) => {
1666
+ if (Array.isArray(value2)) {
1667
+ for (const item of value2) {
1668
+ go(item);
2822
1669
  }
2823
- } else {
2824
- target.id = ObjectId5.random();
1670
+ } else if (typeof value2 === "object" && value2 !== null) {
1671
+ go(value2);
2825
1672
  }
2826
1673
  };
2827
- var initMeta = (obj, meta = {
2828
- keys: []
2829
- }) => {
2830
- prepareTypedTarget(meta, ObjectMetaSchema);
2831
- defineHiddenProperty(obj, MetaId, createProxy(meta, TypedReactiveHandler.instance));
1674
+ var goOnRecord = (record) => {
1675
+ for (const key of Object.keys(record)) {
1676
+ go(record[key]);
1677
+ }
2832
1678
  };
2833
1679
 
2834
- // src/internal/proxy/reactive.ts
2835
- var subscribe = (obj, callback) => {
2836
- const target = getProxyTarget(obj);
2837
- if (target && EventId in target) {
2838
- return target[EventId].on(callback);
2839
- }
2840
- return () => {
1680
+ // src/internal/Obj/common.ts
1681
+ import * as Schema9 from "effect/Schema";
1682
+ var makeTypedEntityClass = (typename, version2, baseSchema) => {
1683
+ return class {
1684
+ // Implement TypedObject properties.
1685
+ static typename = typename;
1686
+ static version = version2;
1687
+ // Implement Schema.Schema properties.
1688
+ // TODO(burdon): Comment required.
1689
+ static [Schema9.TypeId] = schemaVariance;
1690
+ static ast = baseSchema.ast;
1691
+ static annotations = baseSchema.annotations.bind(baseSchema);
1692
+ static pipe = baseSchema.pipe.bind(baseSchema);
1693
+ // TODO(burdon): Comment required.
1694
+ static [Symbol.hasInstance](obj) {
1695
+ return obj != null && getTypename(obj) === typename;
1696
+ }
1697
+ // TODO(burdon): Throw APIError.
1698
+ constructor() {
1699
+ throw new Error("Use live(Typename, { ...fields }) to instantiate an object.");
1700
+ }
2841
1701
  };
2842
1702
  };
2843
- var change = (obj, callback) => {
2844
- const changeFn = obj[ChangeId];
2845
- if (changeFn) {
2846
- changeFn(callback);
2847
- } else {
2848
- callback(obj);
2849
- }
1703
+ var schemaVariance = {
1704
+ _A: (_) => _,
1705
+ _I: (_) => _,
1706
+ _R: (_) => _
2850
1707
  };
2851
1708
 
2852
- // src/internal/object/inspect.ts
2853
- import { inspectCustom as inspectCustom3 } from "@dxos/debug";
1709
+ // src/internal/Obj/create-object.ts
1710
+ import { raise as raise3 } from "@dxos/debug";
1711
+ import { assertArgument as assertArgument6, failedInvariant } from "@dxos/invariant";
1712
+ import { ObjectId as ObjectId5 } from "@dxos/keys";
1713
+
1714
+ // src/internal/Obj/inspect.ts
1715
+ import { inspectCustom as inspectCustom2 } from "@dxos/debug";
2854
1716
  var attachedTypedObjectInspector = (obj) => {
2855
- const descriptor = Object.getOwnPropertyDescriptor(obj, inspectCustom3);
1717
+ const descriptor = Object.getOwnPropertyDescriptor(obj, inspectCustom2);
2856
1718
  if (descriptor) {
2857
1719
  return;
2858
1720
  }
2859
- Object.defineProperty(obj, inspectCustom3, {
1721
+ Object.defineProperty(obj, inspectCustom2, {
2860
1722
  value: typedObjectInspectFunction,
2861
1723
  writable: false,
2862
1724
  enumerable: false,
@@ -2873,19 +1735,19 @@ var typedObjectInspectFunction = function(depth, options, inspect) {
2873
1735
  }, options);
2874
1736
  };
2875
1737
 
2876
- // src/internal/object/json-serializer.ts
2877
- import * as Schema13 from "effect/Schema";
2878
- import { raise as raise3 } from "@dxos/debug";
1738
+ // src/internal/Obj/json-serializer.ts
1739
+ import * as Schema10 from "effect/Schema";
1740
+ import { raise as raise2 } from "@dxos/debug";
2879
1741
  import { ObjectStructure, isEncodedReference } from "@dxos/echo-protocol";
2880
- import { assertArgument as assertArgument7, invariant as invariant15 } from "@dxos/invariant";
2881
- import { DXN as DXN7, ObjectId as ObjectId6 } from "@dxos/keys";
1742
+ import { assertArgument as assertArgument5, invariant as invariant13 } from "@dxos/invariant";
1743
+ import { DXN as DXN5, ObjectId as ObjectId4 } from "@dxos/keys";
2882
1744
  import { assumeType as assumeType4, deepMapValues as deepMapValues2, visitValues } from "@dxos/util";
2883
- var __dxlog_file15 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/object/json-serializer.ts";
1745
+ var __dxlog_file13 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/Obj/json-serializer.ts";
2884
1746
  var objectToJSON = (obj) => {
2885
1747
  const typename = getTypeDXN(obj)?.toString();
2886
- invariant15(typename && typeof typename === "string", void 0, {
2887
- F: __dxlog_file15,
2888
- L: 62,
1748
+ invariant13(typename && typeof typename === "string", void 0, {
1749
+ F: __dxlog_file13,
1750
+ L: 66,
2889
1751
  S: void 0,
2890
1752
  A: [
2891
1753
  "typename && typeof typename === 'string'",
@@ -2894,36 +1756,37 @@ var objectToJSON = (obj) => {
2894
1756
  });
2895
1757
  return typedJsonSerializer.call(obj);
2896
1758
  };
2897
- var objectFromJSON = async (jsonData, { refResolver, dxn } = {}) => {
1759
+ var objectFromJSON = async (jsonData, { refResolver, dxn, database } = {}) => {
2898
1760
  assumeType4(jsonData);
2899
- assertArgument7(typeof jsonData === "object" && jsonData !== null, "jsonData", "expect object");
2900
- assertArgument7(typeof jsonData[ATTR_TYPE] === "string", "jsonData[ATTR_TYPE]", "expected object to have a type");
2901
- assertArgument7(typeof jsonData.id === "string", "jsonData.id", "expected object to have an id");
2902
- const type = DXN7.parse(jsonData[ATTR_TYPE]);
1761
+ assertArgument5(typeof jsonData === "object" && jsonData !== null, "jsonData", "expect object");
1762
+ assertArgument5(typeof jsonData[ATTR_TYPE] === "string", "jsonData[ATTR_TYPE]", "expected object to have a type");
1763
+ assertArgument5(typeof jsonData.id === "string", "jsonData.id", "expected object to have an id");
1764
+ const type = DXN5.parse(jsonData[ATTR_TYPE]);
2903
1765
  const schema = await refResolver?.resolveSchema(type);
2904
- invariant15(schema === void 0 || Schema13.isSchema(schema), void 0, {
2905
- F: __dxlog_file15,
2906
- L: 83,
1766
+ invariant13(schema === void 0 || Schema10.isSchema(schema), void 0, {
1767
+ F: __dxlog_file13,
1768
+ L: 92,
2907
1769
  S: void 0,
2908
1770
  A: [
2909
1771
  "schema === undefined || Schema.isSchema(schema)",
2910
1772
  ""
2911
1773
  ]
2912
1774
  });
1775
+ const decodedInput = stripInternalJsonKeys(jsonData);
2913
1776
  let obj;
2914
1777
  if (schema != null) {
2915
- obj = await schema.pipe(Schema13.decodeUnknownPromise)(jsonData);
1778
+ obj = await schema.pipe(Schema10.decodeUnknownPromise)(decodedInput);
2916
1779
  if (refResolver) {
2917
1780
  setRefResolverOnData(obj, refResolver);
2918
1781
  }
2919
1782
  } else {
2920
- obj = decodeGeneric(jsonData, {
1783
+ obj = decodeGeneric(decodedInput, {
2921
1784
  refResolver
2922
1785
  });
2923
1786
  }
2924
- invariant15(ObjectId6.isValid(obj.id), "Invalid object id", {
2925
- F: __dxlog_file15,
2926
- L: 95,
1787
+ invariant13(ObjectId4.isValid(obj.id), "Invalid object id", {
1788
+ F: __dxlog_file13,
1789
+ L: 105,
2927
1790
  S: void 0,
2928
1791
  A: [
2929
1792
  "ObjectId.isValid(obj.id)",
@@ -2936,8 +1799,8 @@ var objectFromJSON = async (jsonData, { refResolver, dxn } = {}) => {
2936
1799
  }
2937
1800
  const isRelation = typeof jsonData[ATTR_RELATION_SOURCE] === "string" || typeof jsonData[ATTR_RELATION_TARGET] === "string";
2938
1801
  if (isRelation) {
2939
- const sourceDxn = DXN7.parse(jsonData[ATTR_RELATION_SOURCE] ?? raise3(new TypeError("Missing relation source")));
2940
- const targetDxn = DXN7.parse(jsonData[ATTR_RELATION_TARGET] ?? raise3(new TypeError("Missing relation target")));
1802
+ const sourceDxn = DXN5.parse(jsonData[ATTR_RELATION_SOURCE] ?? raise2(new TypeError("Missing relation source")));
1803
+ const targetDxn = DXN5.parse(jsonData[ATTR_RELATION_TARGET] ?? raise2(new TypeError("Missing relation target")));
2941
1804
  const source = await refResolver?.resolve(sourceDxn);
2942
1805
  const target = await refResolver?.resolve(targetDxn);
2943
1806
  defineHiddenProperty(obj, KindId, EntityKind.Relation);
@@ -2949,10 +1812,10 @@ var objectFromJSON = async (jsonData, { refResolver, dxn } = {}) => {
2949
1812
  defineHiddenProperty(obj, KindId, EntityKind.Object);
2950
1813
  }
2951
1814
  if (typeof jsonData[ATTR_META] === "object") {
2952
- const meta = await ObjectMetaSchema.pipe(Schema13.decodeUnknownPromise)(jsonData[ATTR_META]);
2953
- invariant15(Array.isArray(meta.keys), void 0, {
2954
- F: __dxlog_file15,
2955
- L: 121,
1815
+ const meta = await ObjectMetaSchema.pipe(Schema10.decodeUnknownPromise)(jsonData[ATTR_META]);
1816
+ invariant13(Array.isArray(meta.keys), void 0, {
1817
+ F: __dxlog_file13,
1818
+ L: 131,
2956
1819
  S: void 0,
2957
1820
  A: [
2958
1821
  "Array.isArray(meta.keys)",
@@ -2965,58 +1828,66 @@ var objectFromJSON = async (jsonData, { refResolver, dxn } = {}) => {
2965
1828
  keys: []
2966
1829
  });
2967
1830
  }
1831
+ if (jsonData[ATTR_PARENT]) {
1832
+ const parentDxn = DXN5.parse(jsonData[ATTR_PARENT]);
1833
+ const parent = await refResolver?.resolve(parentDxn);
1834
+ defineHiddenProperty(obj, ParentId, parent);
1835
+ }
2968
1836
  if (dxn) {
2969
1837
  defineHiddenProperty(obj, SelfDXNId, dxn);
2970
1838
  }
1839
+ if (database) {
1840
+ defineHiddenProperty(obj, ObjectDatabaseId, database);
1841
+ }
2971
1842
  assertObjectModel(obj);
2972
- invariant15(obj[ATTR_TYPE] === void 0, "Invalid object model", {
2973
- F: __dxlog_file15,
2974
- L: 134,
1843
+ invariant13(obj[ATTR_TYPE] === void 0, "Invalid object model", {
1844
+ F: __dxlog_file13,
1845
+ L: 154,
2975
1846
  S: void 0,
2976
1847
  A: [
2977
1848
  "(obj as any)[ATTR_TYPE] === undefined",
2978
1849
  "'Invalid object model'"
2979
1850
  ]
2980
1851
  });
2981
- invariant15(obj[ATTR_META] === void 0, "Invalid object model", {
2982
- F: __dxlog_file15,
2983
- L: 135,
1852
+ invariant13(obj[ATTR_META] === void 0, "Invalid object model", {
1853
+ F: __dxlog_file13,
1854
+ L: 155,
2984
1855
  S: void 0,
2985
1856
  A: [
2986
1857
  "(obj as any)[ATTR_META] === undefined",
2987
1858
  "'Invalid object model'"
2988
1859
  ]
2989
1860
  });
2990
- invariant15(obj[ATTR_DELETED] === void 0, "Invalid object model", {
2991
- F: __dxlog_file15,
2992
- L: 136,
1861
+ invariant13(obj[ATTR_DELETED] === void 0, "Invalid object model", {
1862
+ F: __dxlog_file13,
1863
+ L: 156,
2993
1864
  S: void 0,
2994
1865
  A: [
2995
1866
  "(obj as any)[ATTR_DELETED] === undefined",
2996
1867
  "'Invalid object model'"
2997
1868
  ]
2998
1869
  });
2999
- invariant15(obj[ATTR_SELF_DXN] === void 0, "Invalid object model", {
3000
- F: __dxlog_file15,
3001
- L: 137,
1870
+ invariant13(obj[ATTR_SELF_DXN] === void 0, "Invalid object model", {
1871
+ F: __dxlog_file13,
1872
+ L: 157,
3002
1873
  S: void 0,
3003
1874
  A: [
3004
1875
  "(obj as any)[ATTR_SELF_DXN] === undefined",
3005
1876
  "'Invalid object model'"
3006
1877
  ]
3007
1878
  });
3008
- invariant15(obj[ATTR_RELATION_SOURCE] === void 0, "Invalid object model", {
3009
- F: __dxlog_file15,
3010
- L: 138,
1879
+ invariant13(obj[ATTR_RELATION_SOURCE] === void 0, "Invalid object model", {
1880
+ F: __dxlog_file13,
1881
+ L: 158,
3011
1882
  S: void 0,
3012
1883
  A: [
3013
1884
  "(obj as any)[ATTR_RELATION_SOURCE] === undefined",
3014
1885
  "'Invalid object model'"
3015
1886
  ]
3016
1887
  });
3017
- invariant15(obj[ATTR_RELATION_TARGET] === void 0, "Invalid object model", {
3018
- F: __dxlog_file15,
3019
- L: 139,
1888
+ invariant13(obj[ATTR_RELATION_TARGET] === void 0, "Invalid object model", {
1889
+ F: __dxlog_file13,
1890
+ L: 159,
3020
1891
  S: void 0,
3021
1892
  A: [
3022
1893
  "(obj as any)[ATTR_RELATION_TARGET] === undefined",
@@ -3026,7 +1897,7 @@ var objectFromJSON = async (jsonData, { refResolver, dxn } = {}) => {
3026
1897
  return obj;
3027
1898
  };
3028
1899
  var decodeGeneric = (jsonData, options) => {
3029
- const { [ATTR_TYPE]: _type, [ATTR_META]: _meta, [ATTR_DELETED]: _deleted, [ATTR_SELF_DXN]: _selfDxn, [ATTR_RELATION_SOURCE]: _relationSource, [ATTR_RELATION_TARGET]: _relationTarget, ...props } = jsonData;
1900
+ const props = stripInternalJsonKeys(jsonData);
3030
1901
  return deepMapValues2(props, (value2, visitor) => {
3031
1902
  if (isEncodedReference(value2)) {
3032
1903
  return refFromEncodedReference(value2, options.refResolver);
@@ -3034,6 +1905,10 @@ var decodeGeneric = (jsonData, options) => {
3034
1905
  return visitor(value2);
3035
1906
  });
3036
1907
  };
1908
+ var stripInternalJsonKeys = (jsonData) => {
1909
+ const { [ATTR_TYPE]: _type, [ATTR_META]: _meta, [ATTR_DELETED]: _deleted, [ATTR_SELF_DXN]: _selfDxn, [ATTR_RELATION_SOURCE]: _relationSource, [ATTR_RELATION_TARGET]: _relationTarget, ...props } = jsonData;
1910
+ return props;
1911
+ };
3037
1912
  var setRefResolverOnData = (obj, refResolver) => {
3038
1913
  const visitor = (value2) => {
3039
1914
  if (Ref.isRef(value2)) {
@@ -3050,24 +1925,25 @@ var objectStructureToJson = (objectId, structure) => {
3050
1925
  id: objectId,
3051
1926
  [ATTR_TYPE]: ObjectStructure.getTypeReference(structure)?.["/"] ?? "",
3052
1927
  [ATTR_DELETED]: ObjectStructure.isDeleted(structure),
1928
+ [ATTR_PARENT]: ObjectStructure.getParent(structure)?.["/"],
3053
1929
  [ATTR_RELATION_SOURCE]: ObjectStructure.getRelationSource(structure)?.["/"],
3054
1930
  [ATTR_RELATION_TARGET]: ObjectStructure.getRelationTarget(structure)?.["/"]
3055
1931
  };
3056
1932
  };
3057
1933
 
3058
- // src/internal/object/create-object.ts
1934
+ // src/internal/Obj/create-object.ts
3059
1935
  var createObject = (schema, props) => {
3060
1936
  const annotation = getTypeAnnotation(schema);
3061
1937
  if (!annotation) {
3062
1938
  throw new Error("Schema is not an ECHO schema");
3063
1939
  }
3064
- assertArgument8(!("@type" in props), "data", "@type is not allowed");
3065
- assertArgument8(!(RelationSourceDXNId in props), "data", "Relation source DXN is not allowed in the constructor");
3066
- assertArgument8(!(RelationTargetDXNId in props), "data", "Relation target DXN is not allowed in the constructor");
3067
- assertArgument8(RelationSourceId in props === RelationTargetId in props, "data", "Relation source and target must be provided together");
1940
+ assertArgument6(!("@type" in props), "data", "@type is not allowed");
1941
+ assertArgument6(!(RelationSourceDXNId in props), "data", "Relation source DXN is not allowed in the constructor");
1942
+ assertArgument6(!(RelationTargetDXNId in props), "data", "Relation target DXN is not allowed in the constructor");
1943
+ assertArgument6(RelationSourceId in props === RelationTargetId in props, "data", "Relation source and target must be provided together");
3068
1944
  const obj = {
3069
1945
  ...props,
3070
- id: props.id ?? ObjectId7.random()
1946
+ id: props.id ?? ObjectId5.random()
3071
1947
  };
3072
1948
  const kind = RelationSourceId in props ? EntityKind.Relation : EntityKind.Object;
3073
1949
  defineHiddenProperty(obj, KindId, kind);
@@ -3079,8 +1955,8 @@ var createObject = (schema, props) => {
3079
1955
  attachTypedJsonSerializer(obj);
3080
1956
  attachedTypedObjectInspector(obj);
3081
1957
  if (kind === EntityKind.Relation) {
3082
- const sourceDXN = getObjectDXN(props[RelationSourceId]) ?? raise4(new Error("Unresolved relation source"));
3083
- const targetDXN = getObjectDXN(props[RelationTargetId]) ?? raise4(new Error("Unresolved relation target"));
1958
+ const sourceDXN = getObjectDXN(props[RelationSourceId]) ?? raise3(new Error("Unresolved relation source"));
1959
+ const targetDXN = getObjectDXN(props[RelationTargetId]) ?? raise3(new Error("Unresolved relation target"));
3084
1960
  defineHiddenProperty(obj, RelationSourceDXNId, sourceDXN);
3085
1961
  defineHiddenProperty(obj, RelationTargetDXNId, targetDXN);
3086
1962
  }
@@ -3088,7 +1964,7 @@ var createObject = (schema, props) => {
3088
1964
  return obj;
3089
1965
  };
3090
1966
 
3091
- // src/internal/object/deleted.ts
1967
+ // src/internal/Obj/deleted.ts
3092
1968
  var isDeleted = (obj) => {
3093
1969
  if (obj[ObjectDeletedId] === void 0) {
3094
1970
  return false;
@@ -3096,24 +1972,24 @@ var isDeleted = (obj) => {
3096
1972
  return obj[ObjectDeletedId] ?? false;
3097
1973
  };
3098
1974
 
3099
- // src/internal/object/ids.ts
3100
- import { DXN as DXN8, ObjectId as ObjectId8, QueueSubspaceTags, SpaceId } from "@dxos/keys";
3101
- var createQueueDXN = (spaceId = SpaceId.random(), queueId = ObjectId8.random()) => new DXN8(DXN8.kind.QUEUE, [
1975
+ // src/internal/Obj/ids.ts
1976
+ import { DXN as DXN6, ObjectId as ObjectId6, QueueSubspaceTags, SpaceId } from "@dxos/keys";
1977
+ var createQueueDXN = (spaceId = SpaceId.random(), queueId = ObjectId6.random()) => new DXN6(DXN6.kind.QUEUE, [
3102
1978
  QueueSubspaceTags.DATA,
3103
1979
  spaceId,
3104
1980
  queueId
3105
1981
  ]);
3106
1982
 
3107
- // src/internal/object/set-value.ts
1983
+ // src/internal/Obj/set-value.ts
3108
1984
  import * as Match from "effect/Match";
3109
- import * as Option3 from "effect/Option";
3110
- import * as SchemaAST9 from "effect/SchemaAST";
1985
+ import * as Option from "effect/Option";
1986
+ import * as SchemaAST6 from "effect/SchemaAST";
3111
1987
  import { getArrayElementType, getBaseType, getProperties as getProperties2, isArrayType, isNestedType } from "@dxos/effect";
3112
- import { invariant as invariant16 } from "@dxos/invariant";
3113
- var __dxlog_file16 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/object/set-value.ts";
1988
+ import { invariant as invariant14 } from "@dxos/invariant";
1989
+ var __dxlog_file14 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/Obj/set-value.ts";
3114
1990
  var setValue = (obj, path, value2) => {
3115
- invariant16(path.length > 0, "Path must not be empty", {
3116
- F: __dxlog_file16,
1991
+ invariant14(path.length > 0, "Path must not be empty", {
1992
+ F: __dxlog_file14,
3117
1993
  L: 27,
3118
1994
  S: void 0,
3119
1995
  A: [
@@ -3122,8 +1998,8 @@ var setValue = (obj, path, value2) => {
3122
1998
  ]
3123
1999
  });
3124
2000
  const schema = getSchema(obj);
3125
- invariant16(schema != null, "Object must have a schema", {
3126
- F: __dxlog_file16,
2001
+ invariant14(schema != null, "Object must have a schema", {
2002
+ F: __dxlog_file14,
3127
2003
  L: 30,
3128
2004
  S: void 0,
3129
2005
  A: [
@@ -3184,8 +2060,8 @@ var getDefaultValueForType = (ast) => {
3184
2060
  if (!ast) {
3185
2061
  return void 0;
3186
2062
  }
3187
- const defaultValue = SchemaAST9.getDefaultAnnotation(ast);
3188
- if (Option3.isSome(defaultValue)) {
2063
+ const defaultValue = SchemaAST6.getDefaultAnnotation(ast);
2064
+ if (Option.isSome(defaultValue)) {
3189
2065
  return defaultValue.value;
3190
2066
  }
3191
2067
  return Match.value(ast).pipe(Match.when({
@@ -3213,12 +2089,20 @@ var createObjectWithDefaults = (ast) => {
3213
2089
  return obj;
3214
2090
  };
3215
2091
 
3216
- // src/internal/object/snapshot.ts
2092
+ // src/internal/Obj/snapshot.ts
2093
+ import { assertArgument as assertArgument7 } from "@dxos/invariant";
3217
2094
  import { deepMapValues as deepMapValues3 } from "@dxos/util";
3218
- var copySymbolProperty = (source, target, symbol) => {
3219
- if (symbol in source) {
2095
+ var copySymbolProperty = (source, target, symbol, transform) => {
2096
+ let value2;
2097
+ try {
2098
+ value2 = source[symbol];
2099
+ } catch {
2100
+ return;
2101
+ }
2102
+ if (value2 !== void 0) {
2103
+ const finalValue = transform ? transform(value2) : value2;
3220
2104
  Object.defineProperty(target, symbol, {
3221
- value: source[symbol],
2105
+ value: finalValue,
3222
2106
  writable: false,
3223
2107
  enumerable: false,
3224
2108
  configurable: false
@@ -3226,6 +2110,7 @@ var copySymbolProperty = (source, target, symbol) => {
3226
2110
  }
3227
2111
  };
3228
2112
  var getSnapshot = (obj) => {
2113
+ assertArgument7(typeof obj === "object" && obj !== null && KindId in obj, "obj", "must be an entity");
3229
2114
  const snapshot = deepMapValues3(obj, (value2, recurse) => {
3230
2115
  if (typeof value2 === "object" && value2 !== null && Object.getPrototypeOf(value2) !== Object.prototype && !Array.isArray(value2)) {
3231
2116
  return value2;
@@ -3235,28 +2120,36 @@ var getSnapshot = (obj) => {
3235
2120
  if (obj != null && typeof obj === "object" && KindId in obj) {
3236
2121
  snapshot[SnapshotKindId] = obj[KindId];
3237
2122
  }
3238
- if (obj != null && typeof obj === "object") {
3239
- const source = obj;
3240
- copySymbolProperty(source, snapshot, TypeId);
3241
- copySymbolProperty(source, snapshot, SchemaId);
3242
- copySymbolProperty(source, snapshot, MetaId);
3243
- copySymbolProperty(source, snapshot, RelationSourceDXNId);
3244
- copySymbolProperty(source, snapshot, RelationTargetDXNId);
3245
- copySymbolProperty(source, snapshot, RelationSourceId);
3246
- copySymbolProperty(source, snapshot, RelationTargetId);
3247
- }
2123
+ const source = obj;
2124
+ copySymbolProperty(source, snapshot, TypeId);
2125
+ copySymbolProperty(source, snapshot, SchemaId);
2126
+ copySymbolProperty(source, snapshot, SelfDXNId);
2127
+ copySymbolProperty(source, snapshot, ObjectDatabaseId);
2128
+ copySymbolProperty(source, snapshot, ObjectDeletedId);
2129
+ copySymbolProperty(source, snapshot, MetaId, (meta) => ({
2130
+ keys: [
2131
+ ...meta?.keys ?? []
2132
+ ],
2133
+ tags: [
2134
+ ...meta?.tags ?? []
2135
+ ]
2136
+ }));
2137
+ copySymbolProperty(source, snapshot, RelationSourceDXNId);
2138
+ copySymbolProperty(source, snapshot, RelationTargetDXNId);
2139
+ copySymbolProperty(source, snapshot, RelationSourceId);
2140
+ copySymbolProperty(source, snapshot, RelationTargetId);
3248
2141
  return Object.freeze(snapshot);
3249
2142
  };
3250
2143
 
3251
- // src/internal/object/clone.ts
3252
- import { invariant as invariant17 } from "@dxos/invariant";
2144
+ // src/internal/Obj/clone.ts
2145
+ import { invariant as invariant15 } from "@dxos/invariant";
3253
2146
  import { deepMapValues as deepMapValues4 } from "@dxos/util";
3254
- var __dxlog_file17 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/object/clone.ts";
2147
+ var __dxlog_file15 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/Obj/clone.ts";
3255
2148
  var clone = (obj, opts) => {
3256
2149
  const { id, ...data } = obj;
3257
2150
  const schema = getSchema(obj);
3258
- invariant17(schema != null, "Object should have a schema", {
3259
- F: __dxlog_file17,
2151
+ invariant15(schema != null, "Object should have a schema", {
2152
+ F: __dxlog_file15,
3260
2153
  L: 21,
3261
2154
  S: void 0,
3262
2155
  A: [
@@ -3288,25 +2181,25 @@ var clone = (obj, opts) => {
3288
2181
  return makeObject(schema, props, meta);
3289
2182
  };
3290
2183
 
3291
- // src/internal/schema/echo-schema.ts
3292
- import * as Schema16 from "effect/Schema";
3293
- import * as SchemaAST11 from "effect/SchemaAST";
3294
- import { invariant as invariant19 } from "@dxos/invariant";
2184
+ // src/internal/Type/echo-schema.ts
2185
+ import * as Schema13 from "effect/Schema";
2186
+ import * as SchemaAST8 from "effect/SchemaAST";
2187
+ import { invariant as invariant17 } from "@dxos/invariant";
3295
2188
 
3296
- // src/internal/schema/manipulation.ts
3297
- import * as Schema14 from "effect/Schema";
3298
- import * as SchemaAST10 from "effect/SchemaAST";
3299
- import { invariant as invariant18 } from "@dxos/invariant";
3300
- import { DXN as DXN9 } from "@dxos/keys";
3301
- var __dxlog_file18 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/schema/manipulation.ts";
2189
+ // src/internal/Type/manipulation.ts
2190
+ import * as Schema11 from "effect/Schema";
2191
+ import * as SchemaAST7 from "effect/SchemaAST";
2192
+ import { invariant as invariant16 } from "@dxos/invariant";
2193
+ import { DXN as DXN7 } from "@dxos/keys";
2194
+ var __dxlog_file16 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/Type/manipulation.ts";
3302
2195
  var addFieldsToSchema = (schema, fields) => {
3303
- const schemaExtension = Schema14.partial(Schema14.Struct(fields));
3304
- return Schema14.extend(schema, schemaExtension).annotations(schema.ast.annotations);
2196
+ const schemaExtension = Schema11.partial(Schema11.Struct(fields));
2197
+ return Schema11.extend(schema, schemaExtension).annotations(schema.ast.annotations);
3305
2198
  };
3306
2199
  var updateFieldsInSchema = (schema, fields) => {
3307
2200
  const ast = schema.ast;
3308
- invariant18(SchemaAST10.isTypeLiteral(ast), void 0, {
3309
- F: __dxlog_file18,
2201
+ invariant16(SchemaAST7.isTypeLiteral(ast), void 0, {
2202
+ F: __dxlog_file16,
3310
2203
  L: 29,
3311
2204
  S: void 0,
3312
2205
  A: [
@@ -3317,7 +2210,7 @@ var updateFieldsInSchema = (schema, fields) => {
3317
2210
  const updatedProperties = [
3318
2211
  ...ast.propertySignatures
3319
2212
  ];
3320
- const propertiesToUpdate = Schema14.partial(Schema14.Struct(fields)).ast.propertySignatures;
2213
+ const propertiesToUpdate = Schema11.partial(Schema11.Struct(fields)).ast.propertySignatures;
3321
2214
  for (const property of propertiesToUpdate) {
3322
2215
  const index = updatedProperties.findIndex((p) => p.name === property.name);
3323
2216
  if (index !== -1) {
@@ -3326,15 +2219,15 @@ var updateFieldsInSchema = (schema, fields) => {
3326
2219
  updatedProperties.push(property);
3327
2220
  }
3328
2221
  }
3329
- return Schema14.make(new SchemaAST10.TypeLiteral(updatedProperties, ast.indexSignatures, ast.annotations));
2222
+ return Schema11.make(new SchemaAST7.TypeLiteral(updatedProperties, ast.indexSignatures, ast.annotations));
3330
2223
  };
3331
2224
  var removeFieldsFromSchema = (schema, fieldNames) => {
3332
- return Schema14.make(SchemaAST10.omit(schema.ast, fieldNames)).annotations(schema.ast.annotations);
2225
+ return Schema11.make(SchemaAST7.omit(schema.ast, fieldNames)).annotations(schema.ast.annotations);
3333
2226
  };
3334
2227
  var updateFieldNameInSchema = (schema, { before, after }) => {
3335
2228
  const ast = schema.ast;
3336
- invariant18(SchemaAST10.isTypeLiteral(ast), void 0, {
3337
- F: __dxlog_file18,
2229
+ invariant16(SchemaAST7.isTypeLiteral(ast), void 0, {
2230
+ F: __dxlog_file16,
3338
2231
  L: 57,
3339
2232
  S: void 0,
3340
2233
  A: [
@@ -3342,12 +2235,12 @@ var updateFieldNameInSchema = (schema, { before, after }) => {
3342
2235
  ""
3343
2236
  ]
3344
2237
  });
3345
- return Schema14.make(new SchemaAST10.TypeLiteral(ast.propertySignatures.map((p) => p.name === before ? new SchemaAST10.PropertySignature(after, p.type, p.isOptional, p.isReadonly, p.annotations) : p), ast.indexSignatures, ast.annotations));
2238
+ return Schema11.make(new SchemaAST7.TypeLiteral(ast.propertySignatures.map((p) => p.name === before ? new SchemaAST7.PropertySignature(after, p.type, p.isOptional, p.isReadonly, p.annotations) : p), ast.indexSignatures, ast.annotations));
3346
2239
  };
3347
2240
  var setTypenameInSchema = (schema, typename) => {
3348
2241
  const existingAnnotation = schema.ast.annotations[TypeAnnotationId];
3349
- invariant18(existingAnnotation, `Missing ${String(TypeAnnotationId)}`, {
3350
- F: __dxlog_file18,
2242
+ invariant16(existingAnnotation, `Missing ${String(TypeAnnotationId)}`, {
2243
+ F: __dxlog_file16,
3351
2244
  L: 77,
3352
2245
  S: void 0,
3353
2246
  A: [
@@ -3362,37 +2255,42 @@ var setTypenameInSchema = (schema, typename) => {
3362
2255
  typename,
3363
2256
  version: existingAnnotation.version
3364
2257
  },
3365
- [SchemaAST10.JSONSchemaAnnotationId]: {
3366
- ...schema.ast.annotations[SchemaAST10.JSONSchemaAnnotationId] ?? {},
3367
- $id: schema.ast.annotations[TypeIdentifierAnnotationId] ?? DXN9.fromTypename(typename).toString(),
2258
+ [SchemaAST7.JSONSchemaAnnotationId]: {
2259
+ ...schema.ast.annotations[SchemaAST7.JSONSchemaAnnotationId] ?? {},
2260
+ $id: schema.ast.annotations[TypeIdentifierAnnotationId] ?? DXN7.fromTypename(typename).toString(),
3368
2261
  typename
3369
2262
  }
3370
2263
  });
3371
2264
  };
3372
2265
 
3373
- // src/internal/schema/persistent-schema.ts
3374
- import * as Schema15 from "effect/Schema";
3375
- var PersistentSchema = Schema15.Struct({
3376
- name: Schema15.optional(Schema15.String),
2266
+ // src/internal/Type/persistent-schema.ts
2267
+ import * as Schema12 from "effect/Schema";
2268
+ var PersistentSchema = Schema12.Struct({
2269
+ name: Schema12.optional(Schema12.String),
3377
2270
  typename: TypenameSchema,
3378
2271
  version: VersionSchema,
3379
2272
  jsonSchema: JsonSchemaType
3380
2273
  }).pipe(EchoObjectSchema({
3381
- typename: "dxos.org/type/Schema",
2274
+ typename: "org.dxos.type.schema",
3382
2275
  version: "0.1.0"
2276
+ }), LabelAnnotation.set([
2277
+ "name"
2278
+ ]), IconAnnotation.set({
2279
+ icon: "ph--database--regular",
2280
+ hue: "green"
3383
2281
  }));
3384
2282
 
3385
- // src/internal/schema/echo-schema.ts
3386
- var __dxlog_file19 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/schema/echo-schema.ts";
2283
+ // src/internal/Type/echo-schema.ts
2284
+ var __dxlog_file17 = "/__w/dxos/dxos/packages/core/echo/echo/src/internal/Type/echo-schema.ts";
3387
2285
  var ImmutableSchema = class {
3388
2286
  _schema;
3389
2287
  _objectAnnotation;
3390
2288
  constructor(_schema) {
3391
2289
  this._schema = _schema;
3392
2290
  this._objectAnnotation = getTypeAnnotation(this._schema);
3393
- invariant19(this._objectAnnotation, void 0, {
3394
- F: __dxlog_file19,
3395
- L: 48,
2291
+ invariant17(this._objectAnnotation, void 0, {
2292
+ F: __dxlog_file17,
2293
+ L: 49,
3396
2294
  S: this,
3397
2295
  A: [
3398
2296
  "this._objectAnnotation",
@@ -3403,7 +2301,7 @@ var ImmutableSchema = class {
3403
2301
  //
3404
2302
  // Effect Schema (push to abstract base class).
3405
2303
  //
3406
- get [Schema16.TypeId]() {
2304
+ get [Schema13.TypeId]() {
3407
2305
  return schemaVariance2;
3408
2306
  }
3409
2307
  get Type() {
@@ -3456,9 +2354,9 @@ var ImmutableSchema = class {
3456
2354
  var EchoSchemaConstructor = () => {
3457
2355
  return class {
3458
2356
  static get _schema() {
3459
- return Schema16.Union(PersistentSchema, Schema16.instanceOf(EchoSchema)).annotations(PersistentSchema.ast.annotations);
2357
+ return Schema13.Union(PersistentSchema, Schema13.instanceOf(EchoSchema)).annotations(PersistentSchema.ast.annotations);
3460
2358
  }
3461
- static [Schema16.TypeId] = schemaVariance2;
2359
+ static [Schema13.TypeId] = schemaVariance2;
3462
2360
  static get ast() {
3463
2361
  const schema = this._schema;
3464
2362
  return schema.ast;
@@ -3487,7 +2385,7 @@ var EchoSchema = class extends EchoSchemaConstructor() {
3487
2385
  _isDirty = true;
3488
2386
  /**
3489
2387
  * Schema kind key that marks this as an ECHO schema.
3490
- * Makes EchoSchema satisfy the Type.Obj.Any type.
2388
+ * Makes EchoSchema satisfy the Type.AnyObj type.
3491
2389
  */
3492
2390
  [SchemaKindId] = EntityKind.Object;
3493
2391
  constructor(_persistentSchema) {
@@ -3496,7 +2394,7 @@ var EchoSchema = class extends EchoSchemaConstructor() {
3496
2394
  //
3497
2395
  // Effect Schema (push to abstract base class).
3498
2396
  //
3499
- get [Schema16.TypeId]() {
2397
+ get [Schema13.TypeId]() {
3500
2398
  return schemaVariance2;
3501
2399
  }
3502
2400
  get Type() {
@@ -3552,9 +2450,9 @@ var EchoSchema = class extends EchoSchemaConstructor() {
3552
2450
  * Returns a mutable schema.
3553
2451
  */
3554
2452
  get mutable() {
3555
- invariant19(!this.readonly, "Schema is not mutable", {
3556
- F: __dxlog_file19,
3557
- L: 278,
2453
+ invariant17(!this.readonly, "Schema is not mutable", {
2454
+ F: __dxlog_file17,
2455
+ L: 279,
3558
2456
  S: this,
3559
2457
  A: [
3560
2458
  "!this.readonly",
@@ -3593,9 +2491,9 @@ var EchoSchema = class extends EchoSchemaConstructor() {
3593
2491
  }
3594
2492
  getProperties() {
3595
2493
  const ast = this._getSchema().ast;
3596
- invariant19(SchemaAST11.isTypeLiteral(ast), void 0, {
3597
- F: __dxlog_file19,
3598
- L: 313,
2494
+ invariant17(SchemaAST8.isTypeLiteral(ast), void 0, {
2495
+ F: __dxlog_file17,
2496
+ L: 314,
3599
2497
  S: this,
3600
2498
  A: [
3601
2499
  "SchemaAST.isTypeLiteral(ast)",
@@ -3685,7 +2583,7 @@ var EchoSchema = class extends EchoSchemaConstructor() {
3685
2583
  */
3686
2584
  _rebuild() {
3687
2585
  if (this._isDirty || this._schema == null) {
3688
- this._schema = toEffectSchema(getSnapshot(this._persistentSchema.jsonSchema));
2586
+ this._schema = toEffectSchema(getSnapshot(this._persistentSchema).jsonSchema);
3689
2587
  this._isDirty = false;
3690
2588
  }
3691
2589
  }
@@ -3695,12 +2593,12 @@ var EchoSchema = class extends EchoSchemaConstructor() {
3695
2593
  }
3696
2594
  };
3697
2595
  var unwrapOptionality = (property) => {
3698
- if (!SchemaAST11.isUnion(property.type)) {
2596
+ if (!SchemaAST8.isUnion(property.type)) {
3699
2597
  return property;
3700
2598
  }
3701
2599
  return {
3702
2600
  ...property,
3703
- type: property.type.types.find((type) => !SchemaAST11.isUndefinedKeyword(type))
2601
+ type: property.type.types.find((type) => !SchemaAST8.isUndefinedKeyword(type))
3704
2602
  };
3705
2603
  };
3706
2604
 
@@ -3713,75 +2611,28 @@ export {
3713
2611
  foreignKey,
3714
2612
  foreignKeyEquals,
3715
2613
  compareForeignKeys,
3716
- VersionTypeId,
3717
- getLabel,
3718
- setLabel,
3719
- getDescription,
3720
- setDescription,
3721
- makeEchoTypeSchema,
3722
- ATTR_RELATION_SOURCE,
3723
- RelationSourceId,
3724
- RelationSourceDXNId,
3725
- ATTR_RELATION_TARGET,
3726
- RelationTargetId,
3727
- RelationTargetDXNId,
3728
- EchoRelationSchema,
3729
2614
  ATTR_SELF_DXN,
3730
2615
  SelfDXNId,
3731
2616
  ATTR_DELETED,
3732
2617
  ObjectDeletedId,
3733
2618
  ObjectVersionId,
3734
2619
  ObjectDatabaseId,
3735
- assertObjectModel,
3736
- EchoObjectSchema,
3737
- getObjectDXN,
3738
- getDXN,
3739
- getDatabase,
2620
+ ATTR_RELATION_SOURCE,
2621
+ RelationSourceId,
2622
+ RelationSourceDXNId,
2623
+ ATTR_RELATION_TARGET,
2624
+ RelationTargetId,
2625
+ RelationTargetDXNId,
2626
+ VersionTypeId,
2627
+ compare,
2628
+ sortByLabel,
2629
+ sortByTypename,
2630
+ sort,
3740
2631
  getMetaChecked,
3741
2632
  getKeys,
3742
2633
  deleteKeys,
3743
2634
  addTag,
3744
2635
  removeTag,
3745
- compare,
3746
- sortByLabel,
3747
- sortByTypename,
3748
- sort,
3749
- isVersion,
3750
- version,
3751
- versionValid,
3752
- compareVersions,
3753
- encodeVersion,
3754
- decodeVersion,
3755
- CustomAnnotations,
3756
- DecodedAnnotations,
3757
- EchoAnnotations,
3758
- JsonSchemaEchoAnnotations,
3759
- JsonSchemaFields,
3760
- JsonSchemaType,
3761
- getSchemaProperty,
3762
- setSchemaProperty,
3763
- ECHO_ANNOTATIONS_NS_DEPRECATED_KEY,
3764
- ECHO_ANNOTATIONS_NS_KEY,
3765
- getNormalizedEchoAnnotations,
3766
- normalizeSchema,
3767
- JSON_SCHEMA_ECHO_REF_ID,
3768
- getSchemaReference,
3769
- createSchemaReference,
3770
- getReferenceAst,
3771
- RefTypeId,
3772
- Ref,
3773
- createEchoReferenceSchema,
3774
- RefImpl,
3775
- setRefResolver,
3776
- getRefSavedTarget,
3777
- refFromEncodedReference,
3778
- StaticRefResolver,
3779
- RefArray,
3780
- PropType,
3781
- toPropType,
3782
- toJsonSchema,
3783
- toEffectSchema,
3784
- makeTypedEntityClass,
3785
2636
  EventId,
3786
2637
  ChangeId,
3787
2638
  batchEvents,
@@ -3819,6 +2670,21 @@ export {
3819
2670
  makeObject,
3820
2671
  subscribe,
3821
2672
  change,
2673
+ assertObjectModel,
2674
+ getObjectDXN,
2675
+ getDXN,
2676
+ getDatabase,
2677
+ makeEchoTypeSchema,
2678
+ EchoObjectSchema,
2679
+ EchoRelationSchema,
2680
+ isVersion,
2681
+ version,
2682
+ versionValid,
2683
+ compareVersions,
2684
+ encodeVersion,
2685
+ decodeVersion,
2686
+ normalizeSchema,
2687
+ makeTypedEntityClass,
3822
2688
  objectToJSON,
3823
2689
  objectFromJSON,
3824
2690
  setRefResolverOnData,
@@ -3838,9 +2704,9 @@ export {
3838
2704
  ImmutableSchema,
3839
2705
  isMutable,
3840
2706
  EchoSchema,
3841
- JsonPath2 as JsonPath,
2707
+ JsonPath,
3842
2708
  JsonProp,
3843
2709
  getValue,
3844
2710
  splitJsonPath
3845
2711
  };
3846
- //# sourceMappingURL=chunk-SU2LVM7J.mjs.map
2712
+ //# sourceMappingURL=chunk-JJTRVYRD.mjs.map