@warp-drive/core 5.9.0-beta.0 → 5.9.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (436) hide show
  1. package/declarations/aliases-BWYW5EhX.d.ts +17 -0
  2. package/declarations/aliases-BWYW5EhX.d.ts.map +1 -0
  3. package/declarations/build-config/babel-macros.d.ts +2 -1
  4. package/declarations/build-config/canary-features.d.ts +1 -1
  5. package/declarations/build-config/debugging.d.ts +1 -1
  6. package/declarations/build-config/deprecations.d.ts +1 -1
  7. package/declarations/build-config/env.d.ts +2 -1
  8. package/declarations/build-config/macros.d.ts +2 -1
  9. package/declarations/build-config.d.ts +2 -18
  10. package/declarations/change-C7kqv2kW.d.ts +41 -0
  11. package/declarations/change-C7kqv2kW.d.ts.map +1 -0
  12. package/declarations/concepts-LHsLJZMl.d.ts +77 -0
  13. package/declarations/concepts-LHsLJZMl.d.ts.map +1 -0
  14. package/declarations/configure-B_F639tN.d.ts +134 -0
  15. package/declarations/configure-B_F639tN.d.ts.map +1 -0
  16. package/declarations/configure.d.ts +2 -7
  17. package/declarations/document-BEn91SwT.d.ts +129 -0
  18. package/declarations/document-BEn91SwT.d.ts.map +1 -0
  19. package/declarations/error-50OSEhsF.d.ts +77 -0
  20. package/declarations/error-50OSEhsF.d.ts.map +1 -0
  21. package/declarations/fields-FM7peCiM.d.ts +2327 -0
  22. package/declarations/fields-FM7peCiM.d.ts.map +1 -0
  23. package/declarations/graph/-private.d.ts +2 -13
  24. package/declarations/graph-DLqYXP2z.d.ts +186 -0
  25. package/declarations/graph-DLqYXP2z.d.ts.map +1 -0
  26. package/declarations/identifier-DQnqOSJK.d.ts +112 -0
  27. package/declarations/identifier-DQnqOSJK.d.ts.map +1 -0
  28. package/declarations/index.d.ts +2 -96
  29. package/declarations/internal-Cz-893zD.d.ts +8690 -0
  30. package/declarations/internal-Cz-893zD.d.ts.map +1 -0
  31. package/declarations/json-api-raw-C5APJHHB.d.ts +378 -0
  32. package/declarations/json-api-raw-C5APJHHB.d.ts.map +1 -0
  33. package/declarations/mutations-CqtoGB4t.d.ts +165 -0
  34. package/declarations/mutations-CqtoGB4t.d.ts.map +1 -0
  35. package/declarations/operations-D1qA6r0_.d.ts +244 -0
  36. package/declarations/operations-D1qA6r0_.d.ts.map +1 -0
  37. package/declarations/params-DrEx3bf8.d.ts +41 -0
  38. package/declarations/params-DrEx3bf8.d.ts.map +1 -0
  39. package/declarations/raw-D5hnjiq7.d.ts +31 -0
  40. package/declarations/raw-D5hnjiq7.d.ts.map +1 -0
  41. package/declarations/reactive/-private.d.ts +2 -2
  42. package/declarations/reactive.d.ts +2 -286
  43. package/declarations/record-D3QQcaIR.d.ts +254 -0
  44. package/declarations/record-D3QQcaIR.d.ts.map +1 -0
  45. package/declarations/relationship-CmHzMPKf.d.ts +65 -0
  46. package/declarations/relationship-CmHzMPKf.d.ts.map +1 -0
  47. package/declarations/request.d.ts +2 -52
  48. package/declarations/signals/-leaked.d.ts +63 -2
  49. package/declarations/signals/-leaked.d.ts.map +1 -0
  50. package/declarations/store/-private.d.ts +2 -24
  51. package/declarations/store.d.ts +8358 -2
  52. package/declarations/store.d.ts.map +1 -0
  53. package/declarations/symbols-DjRDCeX0.d.ts +88 -0
  54. package/declarations/symbols-DjRDCeX0.d.ts.map +1 -0
  55. package/declarations/types/-private.d.ts +71 -17
  56. package/declarations/types/-private.d.ts.map +1 -0
  57. package/declarations/types/cache/aliases.d.ts +2 -1
  58. package/declarations/types/cache/change.d.ts +2 -6
  59. package/declarations/types/cache/mutations.d.ts +2 -38
  60. package/declarations/types/cache/operations.d.ts +2 -168
  61. package/declarations/types/cache/relationship.d.ts +2 -13
  62. package/declarations/types/cache.d.ts +2 -429
  63. package/declarations/types/graph.d.ts +2 -47
  64. package/declarations/types/identifier.d.ts +2 -86
  65. package/declarations/types/json/raw.d.ts +2 -6
  66. package/declarations/types/params.d.ts +2 -8
  67. package/declarations/types/record.d.ts +2 -269
  68. package/declarations/types/request.d.ts +2 -367
  69. package/declarations/types/runtime.d.ts +53 -12
  70. package/declarations/types/runtime.d.ts.map +1 -0
  71. package/declarations/types/schema/concepts.d.ts +2 -23
  72. package/declarations/types/schema/fields.d.ts +2 -2200
  73. package/declarations/types/schema/fields.type-test.d.ts +1 -1
  74. package/declarations/types/schema/schema-service.d.ts +2 -380
  75. package/declarations/types/spec/document.d.ts +2 -67
  76. package/declarations/types/spec/error.d.ts +2 -18
  77. package/declarations/types/spec/json-api-raw.d.ts +2 -139
  78. package/declarations/types/symbols.d.ts +2 -78
  79. package/declarations/types/utils.d.ts +2 -5
  80. package/declarations/types.d.ts +3 -11
  81. package/declarations/utils/string.d.ts +51 -44
  82. package/declarations/utils/string.d.ts.map +1 -0
  83. package/declarations/utils-BtURiy8l.d.ts +24 -0
  84. package/declarations/utils-BtURiy8l.d.ts.map +1 -0
  85. package/dist/-private-DMzvF_Q6.js +120 -0
  86. package/dist/-private-DMzvF_Q6.js.map +1 -0
  87. package/dist/-private-HjaQ1wp1.js +1695 -0
  88. package/dist/-private-HjaQ1wp1.js.map +1 -0
  89. package/dist/-private-b1g4dhl0.js +22 -0
  90. package/dist/-private-b1g4dhl0.js.map +1 -0
  91. package/dist/build-config/babel-macros.js +3 -1
  92. package/dist/build-config/canary-features.js +1 -1
  93. package/dist/build-config/debugging.js +1 -1
  94. package/dist/build-config/deprecations.js +1 -1
  95. package/dist/build-config/env.js +3 -1
  96. package/dist/build-config/macros.js +3 -1
  97. package/dist/build-config.js +3 -1
  98. package/dist/configure.js +3 -2
  99. package/dist/graph/-private.js +2183 -3192
  100. package/dist/graph/-private.js.map +1 -0
  101. package/dist/index.js +3 -8
  102. package/dist/reactive/-private.js +3 -1
  103. package/dist/reactive.js +5 -4
  104. package/dist/request--vRFALbQ.js +766 -0
  105. package/dist/request--vRFALbQ.js.map +1 -0
  106. package/dist/request.js +2 -48
  107. package/dist/signals/-leaked.js +3 -1
  108. package/dist/src-Dex_phWI.js +9088 -0
  109. package/dist/src-Dex_phWI.js.map +1 -0
  110. package/dist/store/-private.js +3 -1
  111. package/dist/store-DNF9Rgoh.js +462 -0
  112. package/dist/store-DNF9Rgoh.js.map +1 -0
  113. package/dist/store.js +3 -1
  114. package/dist/types/-private.js +1 -77
  115. package/dist/types/identifier.js +60 -52
  116. package/dist/types/identifier.js.map +1 -0
  117. package/dist/types/record.js +211 -181
  118. package/dist/types/record.js.map +1 -0
  119. package/dist/types/request.js +138 -69
  120. package/dist/types/request.js.map +1 -0
  121. package/dist/types/runtime.js +54 -26
  122. package/dist/types/runtime.js.map +1 -0
  123. package/dist/types/schema/fields.js +501 -495
  124. package/dist/types/schema/fields.js.map +1 -0
  125. package/dist/types/schema/fields.type-test.js +79 -0
  126. package/dist/types/schema/fields.type-test.js.map +1 -0
  127. package/dist/types/symbols.js +84 -77
  128. package/dist/types/symbols.js.map +1 -0
  129. package/dist/unpkg/dev/-private-2RfQ2761.js +111 -0
  130. package/dist/unpkg/dev/-private-2RfQ2761.js.map +1 -0
  131. package/dist/unpkg/dev/-private-CM1zxNws.js +1684 -0
  132. package/dist/unpkg/dev/-private-CM1zxNws.js.map +1 -0
  133. package/dist/unpkg/dev/-private-DL4tfrv9.js +22 -0
  134. package/dist/unpkg/dev/-private-DL4tfrv9.js.map +1 -0
  135. package/dist/unpkg/dev/build-config/babel-macros.js +3 -1
  136. package/dist/unpkg/dev/build-config/canary-features.js +1 -1
  137. package/dist/unpkg/dev/build-config/debugging.js +1 -1
  138. package/dist/unpkg/dev/build-config/deprecations.js +1 -1
  139. package/dist/unpkg/dev/build-config/env.js +3 -1
  140. package/dist/unpkg/dev/build-config/macros.js +3 -1
  141. package/dist/unpkg/dev/build-config.js +3 -1
  142. package/dist/unpkg/dev/configure.js +3 -1
  143. package/dist/unpkg/dev/graph/-private.js +1941 -2949
  144. package/dist/unpkg/dev/graph/-private.js.map +1 -0
  145. package/dist/unpkg/dev/index.js +3 -6
  146. package/dist/unpkg/dev/reactive/-private.js +3 -1
  147. package/dist/unpkg/dev/reactive.js +5 -3
  148. package/dist/unpkg/dev/request-DAuXbfYi.js +756 -0
  149. package/dist/unpkg/dev/request-DAuXbfYi.js.map +1 -0
  150. package/dist/unpkg/dev/request.js +2 -48
  151. package/dist/unpkg/dev/runtime-EoVKAhTv.js +110 -0
  152. package/dist/unpkg/dev/runtime-EoVKAhTv.js.map +1 -0
  153. package/dist/unpkg/dev/signals/-leaked.js +3 -1
  154. package/dist/unpkg/dev/src-Bxlr_vue.js +8192 -0
  155. package/dist/unpkg/dev/src-Bxlr_vue.js.map +1 -0
  156. package/dist/unpkg/dev/store/-private.js +3 -55
  157. package/dist/unpkg/dev/store-DmiFvT1B.js +427 -0
  158. package/dist/unpkg/dev/store-DmiFvT1B.js.map +1 -0
  159. package/dist/unpkg/dev/store.js +1 -556
  160. package/dist/unpkg/dev/types/-private.js +1 -67
  161. package/dist/unpkg/dev/types/identifier.js +63 -56
  162. package/dist/unpkg/dev/types/identifier.js.map +1 -0
  163. package/dist/unpkg/dev/types/record.js +211 -181
  164. package/dist/unpkg/dev/types/record.js.map +1 -0
  165. package/dist/unpkg/dev/types/request.js +138 -69
  166. package/dist/unpkg/dev/types/request.js.map +1 -0
  167. package/dist/unpkg/dev/types/runtime.js +54 -26
  168. package/dist/unpkg/dev/types/runtime.js.map +1 -0
  169. package/dist/unpkg/dev/types/schema/fields.js +501 -495
  170. package/dist/unpkg/dev/types/schema/fields.js.map +1 -0
  171. package/dist/unpkg/dev/types/schema/fields.type-test.js +79 -0
  172. package/dist/unpkg/dev/types/schema/fields.type-test.js.map +1 -0
  173. package/dist/unpkg/dev/types/symbols.js +84 -77
  174. package/dist/unpkg/dev/types/symbols.js.map +1 -0
  175. package/dist/unpkg/dev/utils/string.js +79 -85
  176. package/dist/unpkg/dev/utils/string.js.map +1 -0
  177. package/dist/unpkg/dev-deprecated/-private-2RfQ2761.js +111 -0
  178. package/dist/unpkg/dev-deprecated/-private-2RfQ2761.js.map +1 -0
  179. package/dist/unpkg/dev-deprecated/-private-CM1zxNws.js +1684 -0
  180. package/dist/unpkg/dev-deprecated/-private-CM1zxNws.js.map +1 -0
  181. package/dist/unpkg/dev-deprecated/-private-DL4tfrv9.js +22 -0
  182. package/dist/unpkg/dev-deprecated/-private-DL4tfrv9.js.map +1 -0
  183. package/dist/unpkg/dev-deprecated/build-config/babel-macros.js +3 -1
  184. package/dist/unpkg/dev-deprecated/build-config/canary-features.js +1 -1
  185. package/dist/unpkg/dev-deprecated/build-config/debugging.js +1 -1
  186. package/dist/unpkg/dev-deprecated/build-config/deprecations.js +1 -1
  187. package/dist/unpkg/dev-deprecated/build-config/env.js +3 -1
  188. package/dist/unpkg/dev-deprecated/build-config/macros.js +3 -1
  189. package/dist/unpkg/dev-deprecated/build-config.js +3 -1
  190. package/dist/unpkg/dev-deprecated/configure.js +3 -1
  191. package/dist/unpkg/dev-deprecated/graph/-private.js +2060 -3143
  192. package/dist/unpkg/dev-deprecated/graph/-private.js.map +1 -0
  193. package/dist/unpkg/dev-deprecated/index.js +3 -5
  194. package/dist/unpkg/dev-deprecated/reactive/-private.js +3 -1
  195. package/dist/unpkg/dev-deprecated/reactive.js +5 -3
  196. package/dist/unpkg/dev-deprecated/request-DAuXbfYi.js +756 -0
  197. package/dist/unpkg/dev-deprecated/request-DAuXbfYi.js.map +1 -0
  198. package/dist/unpkg/dev-deprecated/request.js +2 -48
  199. package/dist/unpkg/dev-deprecated/runtime-BacZMq1r.js +110 -0
  200. package/dist/unpkg/dev-deprecated/runtime-BacZMq1r.js.map +1 -0
  201. package/dist/unpkg/dev-deprecated/signals/-leaked.js +3 -1
  202. package/dist/unpkg/dev-deprecated/src-DEl2Ca3T.js +8745 -0
  203. package/dist/unpkg/dev-deprecated/src-DEl2Ca3T.js.map +1 -0
  204. package/dist/unpkg/dev-deprecated/store/-private.js +3 -1
  205. package/dist/unpkg/dev-deprecated/store-TcP2H4Af.js +427 -0
  206. package/dist/unpkg/dev-deprecated/store-TcP2H4Af.js.map +1 -0
  207. package/dist/unpkg/dev-deprecated/store.js +1 -556
  208. package/dist/unpkg/dev-deprecated/types/-private.js +1 -67
  209. package/dist/unpkg/dev-deprecated/types/identifier.js +63 -56
  210. package/dist/unpkg/dev-deprecated/types/identifier.js.map +1 -0
  211. package/dist/unpkg/dev-deprecated/types/record.js +211 -181
  212. package/dist/unpkg/dev-deprecated/types/record.js.map +1 -0
  213. package/dist/unpkg/dev-deprecated/types/request.js +138 -69
  214. package/dist/unpkg/dev-deprecated/types/request.js.map +1 -0
  215. package/dist/unpkg/dev-deprecated/types/runtime.js +54 -26
  216. package/dist/unpkg/dev-deprecated/types/runtime.js.map +1 -0
  217. package/dist/unpkg/dev-deprecated/types/schema/fields.js +501 -495
  218. package/dist/unpkg/dev-deprecated/types/schema/fields.js.map +1 -0
  219. package/dist/unpkg/dev-deprecated/types/schema/fields.type-test.js +79 -0
  220. package/dist/unpkg/dev-deprecated/types/schema/fields.type-test.js.map +1 -0
  221. package/dist/unpkg/dev-deprecated/types/symbols.js +84 -77
  222. package/dist/unpkg/dev-deprecated/types/symbols.js.map +1 -0
  223. package/dist/unpkg/dev-deprecated/utils/string.js +79 -85
  224. package/dist/unpkg/dev-deprecated/utils/string.js.map +1 -0
  225. package/dist/unpkg/prod/-private-BK71-uXR.js +99 -0
  226. package/dist/unpkg/prod/-private-BK71-uXR.js.map +1 -0
  227. package/dist/unpkg/prod/-private-TNaS5Piv.js +1494 -0
  228. package/dist/unpkg/prod/-private-TNaS5Piv.js.map +1 -0
  229. package/dist/unpkg/prod/-private-Z9i0icpE.js +22 -0
  230. package/dist/unpkg/prod/-private-Z9i0icpE.js.map +1 -0
  231. package/dist/unpkg/prod/build-config/babel-macros.js +3 -1
  232. package/dist/unpkg/prod/build-config/canary-features.js +1 -1
  233. package/dist/unpkg/prod/build-config/debugging.js +1 -1
  234. package/dist/unpkg/prod/build-config/deprecations.js +1 -1
  235. package/dist/unpkg/prod/build-config/env.js +3 -1
  236. package/dist/unpkg/prod/build-config/macros.js +3 -1
  237. package/dist/unpkg/prod/build-config.js +3 -1
  238. package/dist/unpkg/prod/configure.js +3 -2
  239. package/dist/unpkg/prod/graph/-private.js +1475 -2095
  240. package/dist/unpkg/prod/graph/-private.js.map +1 -0
  241. package/dist/unpkg/prod/index.js +2 -482
  242. package/dist/unpkg/prod/reactive/-private.js +3 -1
  243. package/dist/unpkg/prod/reactive.js +5 -30
  244. package/dist/unpkg/prod/request-BcJNzSgv.js +480 -0
  245. package/dist/unpkg/prod/request-BcJNzSgv.js.map +1 -0
  246. package/dist/unpkg/prod/request.js +3 -2
  247. package/dist/unpkg/prod/signals/-leaked.js +3 -1
  248. package/dist/unpkg/prod/src-Dzz-j6pp.js +6799 -0
  249. package/dist/unpkg/prod/src-Dzz-j6pp.js.map +1 -0
  250. package/dist/unpkg/prod/store/-private.js +3 -126
  251. package/dist/unpkg/prod/store-BWhmb7Ob.js +359 -0
  252. package/dist/unpkg/prod/store-BWhmb7Ob.js.map +1 -0
  253. package/dist/unpkg/prod/store.js +1 -435
  254. package/dist/unpkg/prod/types/-private.js +1 -47
  255. package/dist/unpkg/prod/types/identifier.js +63 -56
  256. package/dist/unpkg/prod/types/identifier.js.map +1 -0
  257. package/dist/unpkg/prod/types/record.js +211 -181
  258. package/dist/unpkg/prod/types/record.js.map +1 -0
  259. package/dist/unpkg/prod/types/request.js +138 -69
  260. package/dist/unpkg/prod/types/request.js.map +1 -0
  261. package/dist/unpkg/prod/types/runtime.js +54 -26
  262. package/dist/unpkg/prod/types/runtime.js.map +1 -0
  263. package/dist/unpkg/prod/types/schema/fields.js +501 -495
  264. package/dist/unpkg/prod/types/schema/fields.js.map +1 -0
  265. package/dist/unpkg/prod/types/schema/fields.type-test.js +79 -0
  266. package/dist/unpkg/prod/types/schema/fields.type-test.js.map +1 -0
  267. package/dist/unpkg/prod/types/symbols.js +84 -77
  268. package/dist/unpkg/prod/types/symbols.js.map +1 -0
  269. package/dist/unpkg/prod/utils/string.js +70 -66
  270. package/dist/unpkg/prod/utils/string.js.map +1 -0
  271. package/dist/unpkg/prod-deprecated/-private-BK71-uXR.js +99 -0
  272. package/dist/unpkg/prod-deprecated/-private-BK71-uXR.js.map +1 -0
  273. package/dist/unpkg/prod-deprecated/-private-TNaS5Piv.js +1494 -0
  274. package/dist/unpkg/prod-deprecated/-private-TNaS5Piv.js.map +1 -0
  275. package/dist/unpkg/prod-deprecated/-private-Z9i0icpE.js +22 -0
  276. package/dist/unpkg/prod-deprecated/-private-Z9i0icpE.js.map +1 -0
  277. package/dist/unpkg/prod-deprecated/build-config/babel-macros.js +3 -1
  278. package/dist/unpkg/prod-deprecated/build-config/canary-features.js +1 -1
  279. package/dist/unpkg/prod-deprecated/build-config/debugging.js +1 -1
  280. package/dist/unpkg/prod-deprecated/build-config/deprecations.js +1 -1
  281. package/dist/unpkg/prod-deprecated/build-config/env.js +3 -1
  282. package/dist/unpkg/prod-deprecated/build-config/macros.js +3 -1
  283. package/dist/unpkg/prod-deprecated/build-config.js +3 -1
  284. package/dist/unpkg/prod-deprecated/configure.js +3 -2
  285. package/dist/unpkg/prod-deprecated/graph/-private.js +1586 -2266
  286. package/dist/unpkg/prod-deprecated/graph/-private.js.map +1 -0
  287. package/dist/unpkg/prod-deprecated/index.js +2 -482
  288. package/dist/unpkg/prod-deprecated/reactive/-private.js +3 -1
  289. package/dist/unpkg/prod-deprecated/reactive.js +5 -5
  290. package/dist/unpkg/prod-deprecated/request-BcJNzSgv.js +480 -0
  291. package/dist/unpkg/prod-deprecated/request-BcJNzSgv.js.map +1 -0
  292. package/dist/unpkg/prod-deprecated/request.js +3 -2
  293. package/dist/unpkg/prod-deprecated/signals/-leaked.js +3 -1
  294. package/dist/unpkg/prod-deprecated/src-BOifHFQ5.js +7232 -0
  295. package/dist/unpkg/prod-deprecated/src-BOifHFQ5.js.map +1 -0
  296. package/dist/unpkg/prod-deprecated/store/-private.js +3 -88
  297. package/dist/unpkg/prod-deprecated/store-BWhmb7Ob.js +359 -0
  298. package/dist/unpkg/prod-deprecated/store-BWhmb7Ob.js.map +1 -0
  299. package/dist/unpkg/prod-deprecated/store.js +1 -435
  300. package/dist/unpkg/prod-deprecated/types/-private.js +1 -47
  301. package/dist/unpkg/prod-deprecated/types/identifier.js +63 -56
  302. package/dist/unpkg/prod-deprecated/types/identifier.js.map +1 -0
  303. package/dist/unpkg/prod-deprecated/types/record.js +211 -181
  304. package/dist/unpkg/prod-deprecated/types/record.js.map +1 -0
  305. package/dist/unpkg/prod-deprecated/types/request.js +138 -69
  306. package/dist/unpkg/prod-deprecated/types/request.js.map +1 -0
  307. package/dist/unpkg/prod-deprecated/types/runtime.js +54 -26
  308. package/dist/unpkg/prod-deprecated/types/runtime.js.map +1 -0
  309. package/dist/unpkg/prod-deprecated/types/schema/fields.js +501 -495
  310. package/dist/unpkg/prod-deprecated/types/schema/fields.js.map +1 -0
  311. package/dist/unpkg/prod-deprecated/types/schema/fields.type-test.js +79 -0
  312. package/dist/unpkg/prod-deprecated/types/schema/fields.type-test.js.map +1 -0
  313. package/dist/unpkg/prod-deprecated/types/symbols.js +84 -77
  314. package/dist/unpkg/prod-deprecated/types/symbols.js.map +1 -0
  315. package/dist/unpkg/prod-deprecated/utils/string.js +70 -66
  316. package/dist/unpkg/prod-deprecated/utils/string.js.map +1 -0
  317. package/dist/utils/string.js +84 -85
  318. package/dist/utils/string.js.map +1 -0
  319. package/package.json +9 -9
  320. package/declarations/graph/-private/-diff.d.ts +0 -19
  321. package/declarations/graph/-private/-edge-definition.d.ts +0 -149
  322. package/declarations/graph/-private/-state.d.ts +0 -9
  323. package/declarations/graph/-private/-utils.d.ts +0 -25
  324. package/declarations/graph/-private/coerce-id.d.ts +0 -4
  325. package/declarations/graph/-private/debug/assert-polymorphic-type.d.ts +0 -6
  326. package/declarations/graph/-private/edges/collection.d.ts +0 -39
  327. package/declarations/graph/-private/edges/implicit.d.ts +0 -43
  328. package/declarations/graph/-private/edges/resource.d.ts +0 -23
  329. package/declarations/graph/-private/graph.d.ts +0 -56
  330. package/declarations/graph/-private/normalize-link.d.ts +0 -2
  331. package/declarations/graph/-private/operations/add-to-related-records.d.ts +0 -4
  332. package/declarations/graph/-private/operations/merge-identifier.d.ts +0 -3
  333. package/declarations/graph/-private/operations/remove-from-related-records.d.ts +0 -4
  334. package/declarations/graph/-private/operations/replace-related-record.d.ts +0 -3
  335. package/declarations/graph/-private/operations/replace-related-records.d.ts +0 -7
  336. package/declarations/graph/-private/operations/update-relationship.d.ts +0 -9
  337. package/declarations/reactive/-private/default-mode.d.ts +0 -73
  338. package/declarations/reactive/-private/document.d.ts +0 -142
  339. package/declarations/reactive/-private/fields/extension.d.ts +0 -8
  340. package/declarations/reactive/-private/fields/get-field-key.d.ts +0 -8
  341. package/declarations/reactive/-private/fields/managed-array.d.ts +0 -18
  342. package/declarations/reactive/-private/fields/managed-object.d.ts +0 -17
  343. package/declarations/reactive/-private/fields/many-array-manager.d.ts +0 -19
  344. package/declarations/reactive/-private/hooks.d.ts +0 -5
  345. package/declarations/reactive/-private/kind/alias-field.d.ts +0 -4
  346. package/declarations/reactive/-private/kind/array-field.d.ts +0 -4
  347. package/declarations/reactive/-private/kind/attribute-field.d.ts +0 -4
  348. package/declarations/reactive/-private/kind/belongs-to-field.d.ts +0 -4
  349. package/declarations/reactive/-private/kind/collection-field.d.ts +0 -4
  350. package/declarations/reactive/-private/kind/derived-field.d.ts +0 -4
  351. package/declarations/reactive/-private/kind/generic-field.d.ts +0 -4
  352. package/declarations/reactive/-private/kind/has-many-field.d.ts +0 -4
  353. package/declarations/reactive/-private/kind/hash-field.d.ts +0 -4
  354. package/declarations/reactive/-private/kind/identity-field.d.ts +0 -4
  355. package/declarations/reactive/-private/kind/local-field.d.ts +0 -4
  356. package/declarations/reactive/-private/kind/object-field.d.ts +0 -4
  357. package/declarations/reactive/-private/kind/resource-field.d.ts +0 -4
  358. package/declarations/reactive/-private/kind/schema-array-field.d.ts +0 -4
  359. package/declarations/reactive/-private/kind/schema-object-field.d.ts +0 -4
  360. package/declarations/reactive/-private/record.d.ts +0 -83
  361. package/declarations/reactive/-private/schema.d.ts +0 -326
  362. package/declarations/reactive/-private/symbols.d.ts +0 -5
  363. package/declarations/request/-private/context.d.ts +0 -39
  364. package/declarations/request/-private/debug.d.ts +0 -4
  365. package/declarations/request/-private/fetch.d.ts +0 -33
  366. package/declarations/request/-private/future.d.ts +0 -7
  367. package/declarations/request/-private/manager.d.ts +0 -158
  368. package/declarations/request/-private/promise-cache.d.ts +0 -28
  369. package/declarations/request/-private/types.d.ts +0 -234
  370. package/declarations/request/-private/utils.d.ts +0 -57
  371. package/declarations/signals/-private.d.ts +0 -6
  372. package/declarations/signals/promise-state.d.ts +0 -264
  373. package/declarations/signals/reactivity/configure.d.ts +0 -138
  374. package/declarations/signals/reactivity/internal.d.ts +0 -154
  375. package/declarations/signals/reactivity/signal.d.ts +0 -56
  376. package/declarations/signals/request-state.d.ts +0 -372
  377. package/declarations/signals/request-subscription.d.ts +0 -177
  378. package/declarations/store/-private/cache-handler/handler.d.ts +0 -56
  379. package/declarations/store/-private/cache-handler/types.d.ts +0 -84
  380. package/declarations/store/-private/cache-handler/utils.d.ts +0 -31
  381. package/declarations/store/-private/caches/instance-cache.d.ts +0 -58
  382. package/declarations/store/-private/caches/resource-utils.d.ts +0 -9
  383. package/declarations/store/-private/debug/utils.d.ts +0 -7
  384. package/declarations/store/-private/default-cache-policy.d.ts +0 -387
  385. package/declarations/store/-private/managers/cache-capabilities-manager.d.ts +0 -34
  386. package/declarations/store/-private/managers/cache-key-manager.d.ts +0 -304
  387. package/declarations/store/-private/managers/cache-manager.d.ts +0 -367
  388. package/declarations/store/-private/managers/notification-manager.d.ts +0 -83
  389. package/declarations/store/-private/managers/record-array-manager.d.ts +0 -100
  390. package/declarations/store/-private/network/request-cache.d.ts +0 -103
  391. package/declarations/store/-private/record-arrays/-utils.d.ts +0 -80
  392. package/declarations/store/-private/record-arrays/legacy-live-array.d.ts +0 -81
  393. package/declarations/store/-private/record-arrays/legacy-many-array.d.ts +0 -133
  394. package/declarations/store/-private/record-arrays/legacy-query.d.ts +0 -81
  395. package/declarations/store/-private/record-arrays/native-proxy-type-fix.d.ts +0 -3
  396. package/declarations/store/-private/record-arrays/resource-array.d.ts +0 -67
  397. package/declarations/store/-private/store-service.d.ts +0 -874
  398. package/declarations/store/-private/utils/coerce-id.d.ts +0 -4
  399. package/declarations/store/-private/utils/construct-resource.d.ts +0 -6
  400. package/declarations/store/-private/utils/is-non-empty-string.d.ts +0 -1
  401. package/declarations/store/-private/utils/normalize-model-name.d.ts +0 -1
  402. package/declarations/store/-private/utils/uuid-polyfill.d.ts +0 -1
  403. package/declarations/store/-types/q/cache-capabilities-manager.d.ts +0 -90
  404. package/declarations/store/-types/q/identifier.d.ts +0 -23
  405. package/declarations/store/-types/q/record-instance.d.ts +0 -22
  406. package/declarations/store/-types/q/store.d.ts +0 -31
  407. package/declarations/store/deprecated/-private.d.ts +0 -223
  408. package/declarations/store/deprecated/store.d.ts +0 -784
  409. package/dist/configure-DPUFCemT.js +0 -1940
  410. package/dist/default-cache-policy-D7_u4YRH.js +0 -572
  411. package/dist/future-BKkJJkj7.js +0 -678
  412. package/dist/index-CQP2NSqg.js +0 -9982
  413. package/dist/symbols-3C1OkYtZ.js +0 -39
  414. package/dist/unpkg/dev/-leaked-Co0EI6Go.js +0 -1939
  415. package/dist/unpkg/dev/-private-sql1_mdx.js +0 -39
  416. package/dist/unpkg/dev/future-DFfOzSoe.js +0 -672
  417. package/dist/unpkg/dev/index-BzcBOnw9.js +0 -9392
  418. package/dist/unpkg/dev/runtime-E1M51_-O.js +0 -135
  419. package/dist/unpkg/dev-deprecated/-leaked-DjMeRqdU.js +0 -1939
  420. package/dist/unpkg/dev-deprecated/-private-3C1OkYtZ.js +0 -39
  421. package/dist/unpkg/dev-deprecated/future-DFfOzSoe.js +0 -672
  422. package/dist/unpkg/dev-deprecated/index-6TnTv-WG.js +0 -10007
  423. package/dist/unpkg/dev-deprecated/runtime-DAu5b0IR.js +0 -135
  424. package/dist/unpkg/prod/-leaked-DUONXQDB.js +0 -1676
  425. package/dist/unpkg/prod/-private-sql1_mdx.js +0 -39
  426. package/dist/unpkg/prod/handler-EU_8ncB2.js +0 -1619
  427. package/dist/unpkg/prod/promise-cache-DIT8Ypjq.js +0 -19
  428. package/dist/unpkg/prod/request-BrJSCG6r.js +0 -421
  429. package/dist/unpkg/prod/schema-BSkHyoWz.js +0 -5219
  430. package/dist/unpkg/prod-deprecated/-leaked-DRNv9VIX.js +0 -1676
  431. package/dist/unpkg/prod-deprecated/-private-3C1OkYtZ.js +0 -39
  432. package/dist/unpkg/prod-deprecated/handler-CCIu4sQ3.js +0 -334
  433. package/dist/unpkg/prod-deprecated/hooks-Dv4Np0MY.js +0 -26
  434. package/dist/unpkg/prod-deprecated/promise-cache-DIT8Ypjq.js +0 -19
  435. package/dist/unpkg/prod-deprecated/request-BrJSCG6r.js +0 -421
  436. package/dist/unpkg/prod-deprecated/schema-CJcjHv0E.js +0 -6939
@@ -1,2208 +1,1583 @@
1
- import { getOrSetGlobal, peekTransient, setTransient } from '../types/-private.js';
1
+ import { a as setTransient, r as peekTransient, t as getOrSetGlobal } from "../-private-BK71-uXR.js";
2
+
3
+ //#region src/graph/-private/-utils.ts
4
+ /** Retrieves the private {@link Store} instance from a capabilities manager (or any object exposing `_store`). */
2
5
  function getStore(wrapper) {
3
- return wrapper._store;
6
+ return wrapper._store;
4
7
  }
8
+ /** Reads a value from a two-level keyed cache, without creating the first-level entry if it is missing. */
5
9
  function expandingGet(cache, key1, key2) {
6
- const mainCache = cache[key1] = cache[key1] || Object.create(null);
7
- return mainCache[key2];
10
+ return (cache[key1] = cache[key1] || Object.create(null))[key2];
8
11
  }
12
+ /** Writes a value into a two-level keyed cache, lazily creating the first-level entry if it does not yet exist. */
9
13
  function expandingSet(cache, key1, key2, value) {
10
- const mainCache = cache[key1] = cache[key1] || Object.create(null);
11
- mainCache[key2] = value;
14
+ const mainCache = cache[key1] = cache[key1] || Object.create(null);
15
+ mainCache[key2] = value;
12
16
  }
17
+ /** Returns whether the given resource is new, i.e. it has no `id` or is flagged as new by the cache. */
13
18
  function checkIfNew(store, resourceKey) {
14
- if (!resourceKey.id) {
15
- return true;
16
- }
17
- return store.cache.isNew(resourceKey);
19
+ if (!resourceKey.id) return true;
20
+ return store.cache.isNew(resourceKey);
18
21
  }
22
+ /** A type-guard returning whether the given edge is a `belongsTo` {@link ResourceEdge}. */
19
23
  function isBelongsTo(relationship) {
20
- return relationship.definition.kind === 'belongsTo';
24
+ return relationship.definition.kind === "belongsTo";
21
25
  }
26
+ /** A type-guard returning whether the given edge is an {@link ImplicitEdge}. */
22
27
  function isImplicit(relationship) {
23
- return relationship.definition.isImplicit;
28
+ return relationship.definition.isImplicit;
24
29
  }
30
+ /** A type-guard returning whether the given edge is a `hasMany` {@link CollectionEdge}. */
25
31
  function isHasMany(relationship) {
26
- return relationship.definition.kind === 'hasMany';
32
+ return relationship.definition.kind === "hasMany";
27
33
  }
34
+ /** Invokes `cb` for every resource identifier currently related through `rel`, covering both local and remote membership regardless of the edge's kind. */
28
35
  function forAllRelatedIdentifiers(rel, cb) {
29
- if (isBelongsTo(rel)) {
30
- if (rel.remoteState) {
31
- cb(rel.remoteState);
32
- }
33
- if (rel.localState && rel.localState !== rel.remoteState) {
34
- cb(rel.localState);
35
- }
36
- } else if (isHasMany(rel)) {
37
- // TODO
38
- // rel.remoteMembers.forEach(cb);
39
- // might be simpler if performance is not a concern
40
- for (let i = 0; i < rel.remoteState.length; i++) {
41
- const inverseIdentifier = rel.remoteState[i];
42
- cb(inverseIdentifier);
43
- }
44
- rel.additions?.forEach(cb);
45
- } else {
46
- rel.localMembers.forEach(cb);
47
- rel.remoteMembers.forEach(inverseIdentifier => {
48
- if (!rel.localMembers.has(inverseIdentifier)) {
49
- cb(inverseIdentifier);
50
- }
51
- });
52
- }
36
+ if (isBelongsTo(rel)) {
37
+ if (rel.remoteState) cb(rel.remoteState);
38
+ if (rel.localState && rel.localState !== rel.remoteState) cb(rel.localState);
39
+ } else if (isHasMany(rel)) {
40
+ for (let i = 0; i < rel.remoteState.length; i++) {
41
+ const inverseIdentifier = rel.remoteState[i];
42
+ cb(inverseIdentifier);
43
+ }
44
+ rel.additions?.forEach(cb);
45
+ } else {
46
+ rel.localMembers.forEach(cb);
47
+ rel.remoteMembers.forEach((inverseIdentifier) => {
48
+ if (!rel.localMembers.has(inverseIdentifier)) cb(inverseIdentifier);
49
+ });
50
+ }
53
51
  }
54
-
55
- /*
56
- Removes the given identifier from BOTH remote AND local state.
57
-
58
- This method is useful when either a deletion or a rollback on a new record
59
- needs to entirely purge itself from an inverse relationship.
60
- */
52
+ /**
53
+ * Removes the given identifier from BOTH remote AND local state.
54
+ *
55
+ * This method is useful when either a deletion or a rollback on a new record
56
+ * needs to entirely purge itself from an inverse relationship.
57
+ */
61
58
  function removeIdentifierCompletelyFromRelationship(graph, relationship, value, silenceNotifications) {
62
- if (isBelongsTo(relationship)) {
63
- if (relationship.remoteState === value) {
64
- relationship.remoteState = null;
65
- }
66
- if (relationship.localState === value) {
67
- relationship.localState = null;
68
- // This allows dematerialized inverses to be rematerialized
69
- // we shouldn't be notifying here though, figure out where
70
- // a notification was missed elsewhere.
71
- {
72
- notifyChange(graph, relationship);
73
- }
74
- }
75
- } else if (isHasMany(relationship)) {
76
- relationship.remoteMembers.delete(value);
77
- relationship.additions?.delete(value);
78
- const wasInRemovals = relationship.removals?.delete(value);
79
- const canonicalIndex = relationship.remoteState.indexOf(value);
80
- if (canonicalIndex !== -1) {
81
- relationship.remoteState.splice(canonicalIndex, 1);
82
- }
83
- if (!wasInRemovals) {
84
- const currentIndex = relationship.localState?.indexOf(value);
85
- if (currentIndex !== -1 && currentIndex !== undefined) {
86
- relationship.localState.splice(currentIndex, 1);
87
- // This allows dematerialized inverses to be rematerialized
88
- // we shouldn't be notifying here though, figure out where
89
- // a notification was missed elsewhere.
90
- {
91
- notifyChange(graph, relationship);
92
- }
93
- }
94
- }
95
- } else {
96
- relationship.remoteMembers.delete(value);
97
- relationship.localMembers.delete(value);
98
- }
99
- }
59
+ if (isBelongsTo(relationship)) {
60
+ if (relationship.remoteState === value) relationship.remoteState = null;
61
+ if (relationship.localState === value) {
62
+ relationship.localState = null;
63
+ if (!silenceNotifications) notifyChange(graph, relationship);
64
+ }
65
+ } else if (isHasMany(relationship)) {
66
+ relationship.remoteMembers.delete(value);
67
+ relationship.additions?.delete(value);
68
+ const wasInRemovals = relationship.removals?.delete(value);
69
+ const canonicalIndex = relationship.remoteState.indexOf(value);
70
+ if (canonicalIndex !== -1) relationship.remoteState.splice(canonicalIndex, 1);
71
+ if (!wasInRemovals) {
72
+ const currentIndex = relationship.localState?.indexOf(value);
73
+ if (currentIndex !== -1 && currentIndex !== void 0) {
74
+ relationship.localState.splice(currentIndex, 1);
75
+ if (!silenceNotifications) notifyChange(graph, relationship);
76
+ }
77
+ }
78
+ } else {
79
+ relationship.remoteMembers.delete(value);
80
+ relationship.localMembers.delete(value);
81
+ }
82
+ }
83
+ /** Notifies the store that a relationship has changed, provided the relationship has been accessed and is not currently being removed. */
100
84
  function notifyChange(graph, relationship) {
101
- if (!relationship.accessed) {
102
- return;
103
- }
104
- const resourceKey = relationship.identifier;
105
- const key = relationship.definition.key;
106
- if (resourceKey === graph._removing) {
107
- return;
108
- }
109
- graph.store.notifyChange(resourceKey, 'relationships', key);
110
- }
111
- function isLegacyField(field) {
112
- return field.kind === 'belongsTo' || field.kind === 'hasMany';
113
- }
114
- function temporaryConvertToLegacy(field) {
115
- return {
116
- kind: field.kind === 'resource' ? 'belongsTo' : 'hasMany',
117
- name: field.name,
118
- type: field.type,
119
- options: Object.assign({}, {
120
- async: false,
121
- inverse: null,
122
- resetOnRemoteUpdate: false
123
- }, field.options)
124
- };
125
- }
126
-
127
- /**
128
- *
129
- * Given RHS (Right Hand Side)
130
- *
131
- * ```ts
132
- * class User extends Model {
133
- * @hasMany('animal', { async: false, inverse: 'owner' }) pets;
134
- * }
135
- * ```
136
- *
137
- * Given LHS (Left Hand Side)
138
- *
139
- * ```ts
140
- * class Animal extends Model {
141
- * @belongsTo('user', { async: false, inverse: 'pets' }) owner;
142
- * }
143
- * ```
144
- *
145
- * The UpgradedMeta for the RHS would be:
146
- *
147
- * ```ts
148
- * {
149
- * kind: 'hasMany',
150
- * key: 'pets',
151
- * type: 'animal',
152
- * isAsync: false,
153
- * isImplicit: false,
154
- * isCollection: true,
155
- * isPolymorphic: false,
156
- * inverseKind: 'belongsTo',
157
- * inverseKey: 'owner',
158
- * inverseType: 'user',
159
- * inverseIsAsync: false,
160
- * inverseIsImplicit: false,
161
- * inverseIsCollection: false,
162
- * inverseIsPolymorphic: false,
163
- * }
164
- * ```
165
- *
166
- * The UpgradeMeta for the LHS would be:
167
- *
168
- * ```ts
169
- * {
170
- * kind: 'belongsTo',
171
- * key: 'owner',
172
- * type: 'user',
173
- * isAsync: false,
174
- * isImplicit: false,
175
- * isCollection: false,
176
- * isPolymorphic: false,
177
- * inverseKind: 'hasMany',
178
- * inverseKey: 'pets',
179
- * inverseType: 'animal',
180
- * inverseIsAsync: false,
181
- * inverseIsImplicit: false,
182
- * inverseIsCollection: true,
183
- * inverseIsPolymorphic: false,
184
- * }
185
- * ```
186
- *
187
- * @private
188
- */
189
-
190
- const BOOL_LATER = null;
191
- const STR_LATER = '';
192
- const IMPLICIT_KEY_RAND = Date.now();
193
- function implicitKeyFor(type, key) {
194
- return `implicit-${type}:${key}${IMPLICIT_KEY_RAND}`;
195
- }
196
- function syncMeta(definition, inverseDefinition) {
197
- definition.inverseKind = inverseDefinition.kind;
198
- definition.inverseKey = inverseDefinition.key;
199
- definition.inverseName = inverseDefinition.name;
200
- definition.inverseType = inverseDefinition.type;
201
- definition.inverseIsAsync = inverseDefinition.isAsync;
202
- definition.inverseIsCollection = inverseDefinition.isCollection;
203
- definition.inverseIsPolymorphic = inverseDefinition.isPolymorphic;
204
- definition.inverseIsImplicit = inverseDefinition.isImplicit;
205
- definition.inverseIsLinksMode = inverseDefinition.isLinksMode;
206
- const resetOnRemoteUpdate = definition.resetOnRemoteUpdate === false || inverseDefinition.resetOnRemoteUpdate === false ? false : true;
207
- definition.resetOnRemoteUpdate = resetOnRemoteUpdate;
208
- inverseDefinition.resetOnRemoteUpdate = resetOnRemoteUpdate;
209
- }
210
- function upgradeMeta(meta) {
211
- if (!isLegacyField(meta)) {
212
- meta = temporaryConvertToLegacy(meta);
213
- }
214
- const niceMeta = {};
215
- const options = meta.options;
216
- niceMeta.kind = meta.kind;
217
- niceMeta.key = meta.sourceKey ?? meta.name;
218
- niceMeta.name = meta.name;
219
- niceMeta.type = meta.type;
220
- niceMeta.isAsync = options.async;
221
- niceMeta.isImplicit = false;
222
- niceMeta.isCollection = meta.kind === 'hasMany';
223
- niceMeta.isPolymorphic = options && !!options.polymorphic;
224
- niceMeta.isLinksMode = options.linksMode ?? false;
225
- niceMeta.inverseKey = options && options.inverse || STR_LATER;
226
- niceMeta.inverseName = options && options.inverse || STR_LATER;
227
- niceMeta.inverseType = STR_LATER;
228
- niceMeta.inverseIsAsync = BOOL_LATER;
229
- niceMeta.inverseIsImplicit = options && options.inverse === null || BOOL_LATER;
230
- niceMeta.inverseIsCollection = BOOL_LATER;
231
- niceMeta.inverseIsLinksMode = BOOL_LATER;
232
-
233
- // prettier-ignore
234
- niceMeta.resetOnRemoteUpdate = !isLegacyField(meta) ? false : meta.options?.linksMode ? false : meta.options?.resetOnRemoteUpdate === false ? false : true;
235
- return niceMeta;
236
- }
237
- function isLHS(info, type, key) {
238
- const isSelfReferential = info.isSelfReferential;
239
- const isRelationship = key === info.lhs_relationshipName;
240
- if (isRelationship === true) {
241
- return isSelfReferential === true ||
242
- // itself
243
- type === info.lhs_baseModelName ||
244
- // base or non-polymorphic
245
- // if the other side is polymorphic then we need to scan our modelNames
246
- info.rhs_isPolymorphic && info.lhs_modelNames.includes(type) // polymorphic
247
- ;
248
- }
249
- return false;
250
- }
251
- function upgradeDefinition(graph, key, propertyName, isImplicit = false) {
252
- const cache = graph._definitionCache;
253
- const storeWrapper = graph.store;
254
- const polymorphicLookup = graph._potentialPolymorphicTypes;
255
- const {
256
- type
257
- } = key;
258
- let cached = /*#__NOINLINE__*/expandingGet(cache, type, propertyName);
259
-
260
- // CASE: We have a cached resolution (null if no relationship exists)
261
- if (cached !== undefined) {
262
- return cached;
263
- }
264
- const relationships = storeWrapper.schema.fields(key);
265
- const relationshipsBySourceKey = storeWrapper.schema.cacheFields?.(key) ?? relationships;
266
- const meta = relationshipsBySourceKey.get(propertyName);
267
- if (!meta) {
268
- // TODO potentially we should just be permissive here since this is an implicit relationship
269
- // and not require the lookup table to be populated
270
- if (polymorphicLookup[type]) {
271
- const altTypes = Object.keys(polymorphicLookup[type]);
272
- for (let i = 0; i < altTypes.length; i++) {
273
- const _cached = expandingGet(cache, altTypes[i], propertyName);
274
- if (_cached) {
275
- /*#__NOINLINE__*/expandingSet(cache, type, propertyName, _cached);
276
- _cached.rhs_modelNames.push(type);
277
- return _cached;
278
- }
279
- }
280
- }
281
- cache[type][propertyName] = null;
282
- return null;
283
- }
284
- const definition = /*#__NOINLINE__*/upgradeMeta(meta);
285
- let inverseDefinition;
286
- let inverseKey;
287
- const inverseType = definition.type;
288
-
289
- // CASE: Inverse is explicitly null
290
- if (definition.inverseKey === null) {
291
- inverseDefinition = null;
292
- } else {
293
- inverseKey = /*#__NOINLINE__*/inverseForRelationship(getStore(storeWrapper), key, propertyName);
294
-
295
- // CASE: If we are polymorphic, and we declared an inverse that is non-null
296
- // we must assume that the lack of inverseKey means that there is no
297
- // concrete type as the baseType, so we must construct and artificial
298
- // placeholder
299
- if (!inverseKey && definition.isPolymorphic && definition.inverseKey) {
300
- inverseDefinition = {
301
- kind: 'belongsTo',
302
- // this must be updated when we find the first belongsTo or hasMany definition that matches
303
- key: definition.inverseKey,
304
- name: definition.inverseName,
305
- type: type,
306
- isAsync: false,
307
- // this must be updated when we find the first belongsTo or hasMany definition that matches
308
- isImplicit: false,
309
- isCollection: false,
310
- // this must be updated when we find the first belongsTo or hasMany definition that matches
311
- isPolymorphic: false
312
- }; // the rest of the fields are populated by syncMeta
313
-
314
- // CASE: Inverse resolves to null
315
- } else if (!inverseKey) {
316
- inverseDefinition = null;
317
- } else {
318
- // CASE: We have an explicit inverse or were able to resolve one
319
- // for the inverse we use "name" for lookup not "sourceKey"
320
- const inverseDefinitions = storeWrapper.schema.fields({
321
- type: inverseType
322
- });
323
- const metaFromInverse = inverseDefinitions.get(inverseKey);
324
- inverseDefinition = upgradeMeta(metaFromInverse);
325
- }
326
- }
327
-
328
- // CASE: We have no inverse
329
- if (!inverseDefinition) {
330
- // polish off meta
331
- inverseKey = /*#__NOINLINE__*/implicitKeyFor(type, propertyName);
332
- inverseDefinition = {
333
- kind: 'implicit',
334
- key: inverseKey,
335
- type: type,
336
- isAsync: false,
337
- isImplicit: true,
338
- isCollection: true,
339
- // with implicits any number of records could point at us
340
- isPolymorphic: false
341
- }; // the rest of the fields are populated by syncMeta
342
-
343
- syncMeta(definition, inverseDefinition);
344
- syncMeta(inverseDefinition, definition);
345
- const info = {
346
- lhs_key: `${type}:${propertyName}`,
347
- lhs_modelNames: [type],
348
- lhs_baseModelName: type,
349
- lhs_relationshipName: propertyName,
350
- lhs_definition: definition,
351
- lhs_isPolymorphic: definition.isPolymorphic,
352
- rhs_key: inverseDefinition.key,
353
- rhs_modelNames: [inverseType],
354
- rhs_baseModelName: inverseType,
355
- rhs_relationshipName: inverseDefinition.key,
356
- rhs_definition: inverseDefinition,
357
- rhs_isPolymorphic: false,
358
- hasInverse: false,
359
- isSelfReferential: type === inverseType,
360
- // this could be wrong if we are self-referential but also polymorphic
361
- isReflexive: false // we can't be reflexive if we don't define an inverse
362
- };
363
- expandingSet(cache, inverseType, inverseKey, info);
364
- expandingSet(cache, type, propertyName, info);
365
- return info;
366
- }
367
-
368
- // CASE: We do have an inverse
369
- const baseType = inverseDefinition.type;
370
- cached = expandingGet(cache, baseType, propertyName) || expandingGet(cache, inverseType, inverseKey);
371
- if (cached) {
372
- const _isLHS = cached.lhs_baseModelName === baseType;
373
- const modelNames = _isLHS ? cached.lhs_modelNames : cached.rhs_modelNames;
374
- // make this lookup easier in the future by caching the key
375
- modelNames.push(type);
376
- expandingSet(cache, type, propertyName, cached);
377
- return cached;
378
- }
379
-
380
- // this is our first time so polish off the metas
381
- syncMeta(definition, inverseDefinition);
382
- syncMeta(inverseDefinition, definition);
383
- const lhs_modelNames = [type];
384
- if (type !== baseType) {
385
- lhs_modelNames.push(baseType);
386
- }
387
- const isSelfReferential = baseType === inverseType;
388
- const info = {
389
- lhs_key: `${baseType}:${propertyName}`,
390
- lhs_modelNames,
391
- lhs_baseModelName: baseType,
392
- lhs_relationshipName: propertyName,
393
- lhs_definition: definition,
394
- lhs_isPolymorphic: definition.isPolymorphic,
395
- rhs_key: `${inverseType}:${inverseKey}`,
396
- rhs_modelNames: [inverseType],
397
- rhs_baseModelName: inverseType,
398
- rhs_relationshipName: inverseKey,
399
- rhs_definition: inverseDefinition,
400
- rhs_isPolymorphic: inverseDefinition.isPolymorphic,
401
- hasInverse: true,
402
- isSelfReferential,
403
- isReflexive: isSelfReferential && propertyName === inverseKey
404
- };
405
-
406
- // Create entries for the baseModelName as well as modelName to speed up
407
- // inverse lookups
408
- expandingSet(cache, baseType, propertyName, info);
409
- expandingSet(cache, type, propertyName, info);
410
-
411
- // Greedily populate the inverse
412
- expandingSet(cache, inverseType, inverseKey, info);
413
- return info;
414
- }
415
- function inverseForRelationship(store, resourceKey, key) {
416
- const fields = store.schema.fields(resourceKey);
417
- const definition = fields.get(key);
418
- if (!definition) {
419
- return null;
420
- }
421
- return definition.options.inverse;
85
+ if (!relationship.accessed) return;
86
+ const resourceKey = relationship.identifier;
87
+ const key = relationship.definition.key;
88
+ if (resourceKey === graph._removing) return;
89
+ graph.store.notifyChange(resourceKey, "relationships", key);
422
90
  }
423
91
 
424
- /*
425
- case many:1
426
- ========
427
- In a bi-directional graph with Many:1 edges, adding a value
428
- results in up-to 3 discrete value transitions, while removing
429
- a value is only 2 transitions.
430
-
431
- For adding C to A
432
- If: A <<-> B, C <->> D is the initial state,
433
- and: B <->> A <<-> C, D is the final state
434
-
435
- then we would undergo the following transitions.
436
-
437
- add C to A
438
- remove C from D
439
- add A to C
440
-
441
- For removing B from A
442
- If: A <<-> B, C <->> D is the initial state,
443
- and: A, B, C <->> D is the final state
444
-
445
- then we would undergo the following transitions.
446
-
447
- remove B from A
448
- remove A from B
449
-
450
- case many:many
451
- ===========
452
- In a bi-directional graph with Many:Many edges, adding or
453
- removing a value requires only 2 value transitions.
454
-
455
- For Adding
456
- If: A<<->>B, C<<->>D is the initial state (double arrows representing the many side)
457
- And: D<<->>C<<->>A<<->>B is the final state
458
-
459
- Then we would undergo two transitions.
460
-
461
- add C to A.
462
- add A to C
463
-
464
- For Removing
465
- If: A<<->>B, C<<->>D is the initial state (double arrows representing the many side)
466
- And: A, B, C<<->>D is the final state
467
-
468
- Then we would undergo two transitions.
469
-
470
- remove B from A
471
- remove A from B
472
-
473
- case many:?
474
- ========
475
- In a uni-directional graph with Many:? edges (modeled in WarpDrive with `inverse:null`) with
476
- artificial (implicit) inverses, replacing a value results in 2 discrete value transitions.
477
- This is because a Many:? relationship is effectively Many:Many.
478
- */
92
+ //#endregion
93
+ //#region src/graph/-private/operations/replace-related-records.ts
479
94
  function replaceRelatedRecords(graph, op, isRemote) {
480
- if (isRemote) {
481
- replaceRelatedRecordsRemote(graph, op, isRemote);
482
- } else {
483
- replaceRelatedRecordsLocal(graph, op, isRemote);
484
- }
95
+ if (isRemote) replaceRelatedRecordsRemote(graph, op, isRemote);
96
+ else replaceRelatedRecordsLocal(graph, op, isRemote);
485
97
  }
486
98
  function replaceRelatedRecordsLocal(graph, op, isRemote) {
487
- const resourceKeys = op.value;
488
- const relationship = graph.get(op.record, op.field);
489
- relationship.state.hasReceivedData = true;
490
- const {
491
- additions,
492
- removals
493
- } = relationship;
494
- const {
495
- inverseKey,
496
- type
497
- } = relationship.definition;
498
- const {
499
- record
500
- } = op;
501
- const wasDirty = relationship.isDirty;
502
- let localBecameDirty = false;
503
- const onAdd = resourceKey => {
504
- // Since we are diffing against the remote state, we check
505
- // if our previous local state did not contain this ResourceKey
506
- const removalsHas = removals?.has(resourceKey);
507
- if (removalsHas || !additions?.has(resourceKey)) {
508
- if (type !== resourceKey.type) {
509
- graph.registerPolymorphicType(type, resourceKey.type);
510
- }
511
-
512
- // we've added a record locally that wasn't in the local state before
513
- localBecameDirty = true;
514
- addToInverse(graph, resourceKey, inverseKey, op.record, isRemote);
515
- if (removalsHas) {
516
- removals.delete(resourceKey);
517
- }
518
- }
519
- };
520
- const onRemove = resourceKey => {
521
- // Since we are diffing against the remote state, we check
522
- // if our previous local state had contained this ResourceKey
523
- const additionsHas = additions?.has(resourceKey);
524
- if (additionsHas || !removals?.has(resourceKey)) {
525
- // we've removed a record locally that was in the local state before
526
- localBecameDirty = true;
527
- removeFromInverse(graph, resourceKey, inverseKey, record, isRemote);
528
- if (additionsHas) {
529
- additions.delete(resourceKey);
530
- }
531
- }
532
- };
533
- const diff = diffCollection(resourceKeys, relationship, onAdd, onRemove);
534
-
535
- // any additions no longer in the local state
536
- // also need to be removed from the inverse
537
- if (additions && additions.size > 0) {
538
- additions.forEach(resourceKey => {
539
- if (!diff.add.has(resourceKey)) {
540
- localBecameDirty = true;
541
- onRemove(resourceKey);
542
- }
543
- });
544
- }
545
-
546
- // any removals no longer in the local state
547
- // also need to be added back to the inverse
548
- if (removals && removals.size > 0) {
549
- removals.forEach(resourceKey => {
550
- if (!diff.del.has(resourceKey)) {
551
- localBecameDirty = true;
552
- onAdd(resourceKey);
553
- }
554
- });
555
- }
556
- const becameDirty = diff.changed || localBecameDirty;
557
- relationship.additions = diff.add;
558
- relationship.removals = diff.del;
559
- relationship.localState = diff.finalState;
560
-
561
- // we only notify if the localState changed and were not already dirty before
562
- // because if we were already dirty then we have already notified
563
- if (becameDirty && !wasDirty) {
564
- notifyChange(graph, relationship);
565
- }
99
+ const resourceKeys = op.value;
100
+ const relationship = graph.get(op.record, op.field);
101
+ relationship.state.hasReceivedData = true;
102
+ const { additions, removals } = relationship;
103
+ const { inverseKey, type } = relationship.definition;
104
+ const { record } = op;
105
+ const wasDirty = relationship.isDirty;
106
+ let localBecameDirty = false;
107
+ const onAdd = (resourceKey) => {
108
+ const removalsHas = removals?.has(resourceKey);
109
+ if (removalsHas || !additions?.has(resourceKey)) {
110
+ if (type !== resourceKey.type) graph.registerPolymorphicType(type, resourceKey.type);
111
+ localBecameDirty = true;
112
+ addToInverse(graph, resourceKey, inverseKey, op.record, isRemote);
113
+ if (removalsHas) removals.delete(resourceKey);
114
+ }
115
+ };
116
+ const onRemove = (resourceKey) => {
117
+ const additionsHas = additions?.has(resourceKey);
118
+ if (additionsHas || !removals?.has(resourceKey)) {
119
+ localBecameDirty = true;
120
+ removeFromInverse(graph, resourceKey, inverseKey, record, isRemote);
121
+ if (additionsHas) additions.delete(resourceKey);
122
+ }
123
+ };
124
+ const diff = diffCollection(resourceKeys, relationship, onAdd, onRemove);
125
+ if (additions && additions.size > 0) additions.forEach((resourceKey) => {
126
+ if (!diff.add.has(resourceKey)) {
127
+ localBecameDirty = true;
128
+ onRemove(resourceKey);
129
+ }
130
+ });
131
+ if (removals && removals.size > 0) removals.forEach((resourceKey) => {
132
+ if (!diff.del.has(resourceKey)) {
133
+ localBecameDirty = true;
134
+ onAdd(resourceKey);
135
+ }
136
+ });
137
+ const becameDirty = diff.changed || localBecameDirty;
138
+ relationship.additions = diff.add;
139
+ relationship.removals = diff.del;
140
+ relationship.localState = diff.finalState;
141
+ if (becameDirty && !wasDirty) notifyChange(graph, relationship);
566
142
  }
567
143
  function replaceRelatedRecordsRemote(graph, op, isRemote) {
568
- const resourceKeys = op.value;
569
- const relationship = graph.get(op.record, op.field);
570
- if (isRemote) {
571
- graph._addToTransaction(relationship);
572
- }
573
- const wasDirty = relationship.isDirty;
574
- // if this is our first time receiving data
575
- // we need to mark the relationship as dirty
576
- // so that non-materializing APIs like `hasManyReference.value()`
577
- // will get notified and updated.
578
- if (!relationship.state.hasReceivedData) {
579
- relationship.isDirty = true;
580
- }
581
- relationship.state.hasReceivedData = true;
582
-
583
- // cache existing state
584
- const {
585
- definition
586
- } = relationship;
587
- const {
588
- type
589
- } = relationship.definition;
590
- const diff = diffCollection(resourceKeys, relationship, resourceKey => {
591
- if (type !== resourceKey.type) {
592
- graph.registerPolymorphicType(type, resourceKey.type);
593
- }
594
- // commit additions
595
- // TODO build this into the diff?
596
- // because we are not dirty if this was a committed local addition
597
- if (relationship.additions?.has(resourceKey)) {
598
- relationship.additions.delete(resourceKey);
599
- }
600
- addToInverse(graph, resourceKey, definition.inverseKey, op.record, isRemote);
601
- }, resourceKey => {
602
- // commit removals
603
- // TODO build this into the diff?
604
- // because we are not dirty if this was a committed local addition
605
- if (relationship.removals?.has(resourceKey)) {
606
- relationship.removals.delete(resourceKey);
607
- }
608
- removeFromInverse(graph, resourceKey, definition.inverseKey, op.record, isRemote);
609
- });
610
-
611
- // replace existing state
612
- relationship.remoteMembers = diff.finalSet;
613
- relationship.remoteState = diff.finalState;
614
-
615
- // changed also indicates a change in order
616
- if (diff.changed) {
617
- relationship.isDirty = true;
618
- }
619
-
620
- // TODO unsure if we need this but it
621
- // may allow us to more efficiently patch
622
- // the associated ManyArray
623
- relationship._diff = diff;
624
- if (relationship.isDirty && !wasDirty) {
625
- flushCanonical(graph, relationship);
626
- }
144
+ const resourceKeys = op.value;
145
+ const relationship = graph.get(op.record, op.field);
146
+ if (isRemote) graph._addToTransaction(relationship);
147
+ const wasDirty = relationship.isDirty;
148
+ if (!relationship.state.hasReceivedData) relationship.isDirty = true;
149
+ relationship.state.hasReceivedData = true;
150
+ const { definition } = relationship;
151
+ const { type } = relationship.definition;
152
+ const diff = diffCollection(resourceKeys, relationship, (resourceKey) => {
153
+ if (type !== resourceKey.type) graph.registerPolymorphicType(type, resourceKey.type);
154
+ if (relationship.additions?.has(resourceKey)) relationship.additions.delete(resourceKey);
155
+ addToInverse(graph, resourceKey, definition.inverseKey, op.record, isRemote);
156
+ }, (resourceKey) => {
157
+ if (relationship.removals?.has(resourceKey)) relationship.removals.delete(resourceKey);
158
+ removeFromInverse(graph, resourceKey, definition.inverseKey, op.record, isRemote);
159
+ });
160
+ relationship.remoteMembers = diff.finalSet;
161
+ relationship.remoteState = diff.finalState;
162
+ if (diff.changed) relationship.isDirty = true;
163
+ relationship._diff = diff;
164
+ if (relationship.isDirty && !wasDirty) flushCanonical(graph, relationship);
627
165
  }
628
166
  function addToInverse(graph, resourceKey, key, value, isRemote) {
629
- const relationship = graph.get(resourceKey, key);
630
- const {
631
- type
632
- } = relationship.definition;
633
- if (type !== value.type) {
634
- graph.registerPolymorphicType(type, value.type);
635
- }
636
- if (isBelongsTo(relationship)) {
637
- relationship.state.hasReceivedData = true;
638
- relationship.state.isEmpty = false;
639
- if (isRemote) {
640
- graph._addToTransaction(relationship);
641
- if (relationship.remoteState !== null) {
642
- removeFromInverse(graph, relationship.remoteState, relationship.definition.inverseKey, resourceKey, isRemote);
643
- }
644
- relationship.remoteState = value;
645
- }
646
- if (relationship.localState !== value) {
647
- if (!isRemote && relationship.localState) {
648
- removeFromInverse(graph, relationship.localState, relationship.definition.inverseKey, resourceKey, isRemote);
649
- }
650
- relationship.localState = value;
651
- notifyChange(graph, relationship);
652
- }
653
- } else if (isHasMany(relationship)) {
654
- if (isRemote) {
655
- // TODO this needs to alert stuffs
656
- // And patch state better
657
- // This is almost definitely wrong
658
- // WARNING WARNING WARNING
659
-
660
- if (!relationship.remoteMembers.has(value)) {
661
- graph._addToTransaction(relationship);
662
- relationship.remoteState.push(value);
663
- relationship.remoteMembers.add(value);
664
- if (relationship.additions?.has(value)) {
665
- relationship.additions.delete(value);
666
- } else {
667
- relationship.isDirty = true;
668
- relationship.state.hasReceivedData = true;
669
- flushCanonical(graph, relationship);
670
- }
671
- }
672
- } else {
673
- // if we are not dirty but have a null localState then we
674
- // are mutating a relationship that has never been fetched
675
- // so we initialize localState to an empty array
676
- if (!relationship.isDirty && !relationship.localState) {
677
- relationship.localState = [];
678
- }
679
- if (_add(graph, resourceKey, relationship, value, null, isRemote)) {
680
- notifyChange(graph, relationship);
681
- }
682
- }
683
- } else {
684
- if (isRemote) {
685
- if (!relationship.remoteMembers.has(value)) {
686
- relationship.remoteMembers.add(value);
687
- relationship.localMembers.add(value);
688
- }
689
- } else {
690
- if (!relationship.localMembers.has(value)) {
691
- relationship.localMembers.add(value);
692
- }
693
- }
694
- }
167
+ const relationship = graph.get(resourceKey, key);
168
+ const { type } = relationship.definition;
169
+ if (type !== value.type) graph.registerPolymorphicType(type, value.type);
170
+ if (isBelongsTo(relationship)) {
171
+ relationship.state.hasReceivedData = true;
172
+ relationship.state.isEmpty = false;
173
+ if (isRemote) {
174
+ graph._addToTransaction(relationship);
175
+ if (relationship.remoteState !== null) removeFromInverse(graph, relationship.remoteState, relationship.definition.inverseKey, resourceKey, isRemote);
176
+ relationship.remoteState = value;
177
+ }
178
+ if (relationship.localState !== value) {
179
+ if (!isRemote && relationship.localState) removeFromInverse(graph, relationship.localState, relationship.definition.inverseKey, resourceKey, isRemote);
180
+ relationship.localState = value;
181
+ notifyChange(graph, relationship);
182
+ }
183
+ } else if (isHasMany(relationship)) {
184
+ if (isRemote) {
185
+ if (!relationship.remoteMembers.has(value)) {
186
+ graph._addToTransaction(relationship);
187
+ relationship.remoteState.push(value);
188
+ relationship.remoteMembers.add(value);
189
+ if (relationship.additions?.has(value)) relationship.additions.delete(value);
190
+ else {
191
+ relationship.isDirty = true;
192
+ relationship.state.hasReceivedData = true;
193
+ flushCanonical(graph, relationship);
194
+ }
195
+ }
196
+ } else {
197
+ if (!relationship.isDirty && !relationship.localState) relationship.localState = [];
198
+ if (_add(graph, resourceKey, relationship, value, null, isRemote)) notifyChange(graph, relationship);
199
+ }
200
+ } else if (isRemote) {
201
+ if (!relationship.remoteMembers.has(value)) {
202
+ relationship.remoteMembers.add(value);
203
+ relationship.localMembers.add(value);
204
+ }
205
+ } else if (!relationship.localMembers.has(value)) relationship.localMembers.add(value);
695
206
  }
696
207
  function notifyInverseOfPotentialMaterialization(graph, resourceKey, key, value, isRemote) {
697
- const relationship = graph.get(resourceKey, key);
698
- if (isHasMany(relationship) && isRemote && relationship.remoteMembers.has(value)) {
699
- notifyChange(graph, relationship);
700
- }
208
+ const relationship = graph.get(resourceKey, key);
209
+ if (isHasMany(relationship) && isRemote && relationship.remoteMembers.has(value)) notifyChange(graph, relationship);
701
210
  }
702
211
  function removeFromInverse(graph, resourceKey, key, value, isRemote) {
703
- const relationship = graph.get(resourceKey, key);
704
- if (isBelongsTo(relationship)) {
705
- relationship.state.isEmpty = true;
706
- if (isRemote) {
707
- graph._addToTransaction(relationship);
708
- relationship.remoteState = null;
709
- }
710
- if (relationship.localState === value) {
711
- relationship.localState = null;
712
- notifyChange(graph, relationship);
713
- }
714
- } else if (isHasMany(relationship)) {
715
- if (isRemote) {
716
- graph._addToTransaction(relationship);
717
- if (_removeRemote(relationship, value)) {
718
- notifyChange(graph, relationship);
719
- }
720
- } else {
721
- if (_removeLocal(relationship, value)) {
722
- notifyChange(graph, relationship);
723
- }
724
- }
725
- } else {
726
- if (isRemote) {
727
- relationship.remoteMembers.delete(value);
728
- relationship.localMembers.delete(value);
729
- } else {
730
- if (value && relationship.localMembers.has(value)) {
731
- relationship.localMembers.delete(value);
732
- }
733
- }
734
- }
212
+ const relationship = graph.get(resourceKey, key);
213
+ if (isBelongsTo(relationship)) {
214
+ relationship.state.isEmpty = true;
215
+ if (isRemote) {
216
+ graph._addToTransaction(relationship);
217
+ relationship.remoteState = null;
218
+ }
219
+ if (relationship.localState === value) {
220
+ relationship.localState = null;
221
+ notifyChange(graph, relationship);
222
+ }
223
+ } else if (isHasMany(relationship)) {
224
+ if (isRemote) {
225
+ graph._addToTransaction(relationship);
226
+ if (_removeRemote(relationship, value)) notifyChange(graph, relationship);
227
+ } else if (_removeLocal(relationship, value)) notifyChange(graph, relationship);
228
+ } else if (isRemote) {
229
+ relationship.remoteMembers.delete(value);
230
+ relationship.localMembers.delete(value);
231
+ } else if (value && relationship.localMembers.has(value)) relationship.localMembers.delete(value);
735
232
  }
736
233
  function flushCanonical(graph, rel) {
737
- if (rel.accessed) {
738
- graph._scheduleLocalSync(rel);
739
- }
234
+ if (rel.accessed) graph._scheduleLocalSync(rel);
740
235
  }
741
- function replaceRelatedRecord(graph, op, isRemote = false) {
742
- const relationship = graph.get(op.record, op.field);
743
- if (isRemote) {
744
- graph._addToTransaction(relationship);
745
- }
746
- const {
747
- definition,
748
- state
749
- } = relationship;
750
- const prop = isRemote ? 'remoteState' : 'localState';
751
- const existingState = relationship[prop];
752
-
753
- /*
754
- case 1:1
755
- ========
756
- In a bi-directional graph with 1:1 edges, replacing a value
757
- results in up-to 4 discrete value transitions.
758
- If: A <-> B, C <-> D is the initial state,
759
- and: A <-> C, B, D is the final state
760
- then we would undergo the following 4 transitions.
761
- remove A from B
762
- add C to A
763
- remove C from D
764
- add A to C
765
- case 1:many
766
- ===========
767
- In a bi-directional graph with 1:Many edges, replacing a value
768
- results in up-to 3 discrete value transitions.
769
- If: A<->>B<<->D, C<<->D is the initial state (double arrows representing the many side)
770
- And: A<->>C<<->D, B<<->D is the final state
771
- Then we would undergo three transitions.
772
- remove A from B
773
- add C to A.
774
- add A to C
775
- case 1:?
776
- ========
777
- In a uni-directional graph with 1:? edges (modeled in WarpDrive with `inverse:null`) with
778
- artificial (implicit) inverses, replacing a value results in up-to 3 discrete value transitions.
779
- This is because a 1:? relationship is effectively 1:many.
780
- If: A->B, C->B is the initial state
781
- And: A->C, C->B is the final state
782
- Then we would undergo three transitions.
783
- Remove A from B
784
- Add C to A
785
- Add A to C
786
- */
787
-
788
- // nothing for us to do
789
- if (op.value === existingState) {
790
- // if we were empty before but now know we are empty this needs to be true
791
- state.hasReceivedData = true;
792
- // if this is a remote update we still sync
793
- if (isRemote) {
794
- const {
795
- localState
796
- } = relationship;
797
- // don't sync if localState is a new record and our remoteState is null
798
- if (localState && checkIfNew(graph._realStore, localState) && !existingState) {
799
- return;
800
- }
801
- if (existingState && localState === existingState) {
802
- notifyInverseOfPotentialMaterialization(graph, existingState, definition.inverseKey, op.record, isRemote);
803
- }
804
- }
805
- return;
806
- }
807
-
808
- // remove this value from the inverse if required
809
- if (existingState) {
810
- removeFromInverse(graph, existingState, definition.inverseKey, op.record, isRemote);
811
- }
812
-
813
- // update value to the new value
814
- relationship[prop] = op.value;
815
- state.hasReceivedData = true;
816
- state.isEmpty = op.value === null;
817
- state.isStale = false;
818
- state.hasFailedLoadAttempt = false;
819
- if (op.value) {
820
- if (definition.type !== op.value.type) {
821
- // assert(
822
- // `The '<${definition.inverseType}>.${op.field}' relationship expects only '${definition.type}' records since it is not polymorphic. Received a Record of type '${op.value.type}'`,
823
- // definition.isPolymorphic
824
- // );
825
236
 
826
- // TODO this should now handle the deprecation warning if isPolymorphic is not set
827
- // but the record does turn out to be polymorphic
828
- // this should still assert if the user is relying on legacy inheritance/mixins to
829
- // provide polymorphic behavior and has not yet added the polymorphic flags
830
-
831
- graph.registerPolymorphicType(definition.type, op.value.type);
832
- }
833
- addToInverse(graph, op.value, definition.inverseKey, op.record, isRemote);
834
- }
835
- if (isRemote) {
836
- const {
837
- localState,
838
- remoteState
839
- } = relationship;
840
- if (localState && checkIfNew(graph._realStore, localState) && !remoteState) {
841
- return;
842
- }
843
- // when localState does not match the new remoteState and
844
- // localState === existingState then we had no local mutation
845
- // and we can safely sync the new remoteState to local
846
- if (localState !== remoteState && localState === existingState) {
847
- relationship.localState = remoteState;
848
- notifyChange(graph, relationship);
849
- // But when localState does not match the new remoteState and
850
- // and localState !== existingState then we know we have a local mutation
851
- // that has not been persisted yet.
852
- }
853
- } else {
854
- notifyChange(graph, relationship);
855
- }
856
- }
237
+ //#endregion
238
+ //#region src/graph/-private/operations/replace-related-record.ts
239
+ function replaceRelatedRecord(graph, op, isRemote = false) {
240
+ const relationship = graph.get(op.record, op.field);
241
+ if (isRemote) graph._addToTransaction(relationship);
242
+ const { definition, state } = relationship;
243
+ const prop = isRemote ? "remoteState" : "localState";
244
+ const existingState = relationship[prop];
245
+ if (op.value === existingState) {
246
+ state.hasReceivedData = true;
247
+ if (isRemote) {
248
+ state.hasReceivedRemoteData = true;
249
+ const { localState } = relationship;
250
+ if (localState && checkIfNew(graph._realStore, localState) && !existingState) return;
251
+ if (existingState && localState === existingState) notifyInverseOfPotentialMaterialization(graph, existingState, definition.inverseKey, op.record, isRemote);
252
+ }
253
+ return;
254
+ }
255
+ if (existingState) removeFromInverse(graph, existingState, definition.inverseKey, op.record, isRemote);
256
+ relationship[prop] = op.value;
257
+ state.hasReceivedData = true;
258
+ /**
259
+ * Local mutations never give us additional information about
260
+ * whether the relationship has received *remote* data. This is
261
+ * what `getRemoteRelationship` relies on to avoid reflecting
262
+ * local-only edits.
263
+ */
264
+ if (isRemote) state.hasReceivedRemoteData = true;
265
+ state.isEmpty = op.value === null;
266
+ state.isStale = false;
267
+ state.hasFailedLoadAttempt = false;
268
+ if (op.value) {
269
+ if (definition.type !== op.value.type) graph.registerPolymorphicType(definition.type, op.value.type);
270
+ addToInverse(graph, op.value, definition.inverseKey, op.record, isRemote);
271
+ }
272
+ if (isRemote) {
273
+ const { localState, remoteState } = relationship;
274
+ if (localState && checkIfNew(graph._realStore, localState) && !remoteState) return;
275
+ if (localState !== remoteState && localState === existingState) {
276
+ relationship.localState = remoteState;
277
+ notifyChange(graph, relationship);
278
+ }
279
+ } else notifyChange(graph, relationship);
280
+ }
281
+
282
+ //#endregion
283
+ //#region src/graph/-private/-diff.ts
857
284
  function _compare(priorLocalState, finalState, finalSet, prevState, prevSet, onAdd, onDel, remoteClearsLocal) {
858
- const finalLength = finalState.length;
859
- const prevLength = prevState.length;
860
- const iterationLength = Math.max(finalLength, prevLength);
861
- const equalLength = priorLocalState ? finalLength === priorLocalState.length : finalLength === prevLength;
862
- let remoteOrderChanged = finalSet.size !== prevSet.size;
863
- let changed = priorLocalState ? finalSet.size !== priorLocalState.length : remoteOrderChanged;
864
- const added = new Set();
865
- const removed = new Set();
866
- const priorLocalLength = priorLocalState?.length ?? 0;
867
- for (let i = 0; i < iterationLength; i++) {
868
- let member;
869
-
870
- // accumulate anything added
871
- if (i < finalLength) {
872
- member = finalState[i];
873
- if (!prevSet.has(member)) {
874
- // Avoid unnecessarily notifying a change that already exists locally
875
- if (i < priorLocalLength) {
876
- const priorLocalMember = priorLocalState[i];
877
- if (priorLocalMember !== member) {
878
- changed = true;
879
- }
880
- }
881
- added.add(member);
882
- onAdd(member);
883
- }
884
- }
885
-
886
- // accumulate anything removed
887
- if (i < prevLength) {
888
- const prevMember = prevState[i];
889
-
890
- // detect reordering
891
- if (equalLength && member !== prevMember) {
892
- // the new remote order does not match the current remote order
893
- // indicating a change in membership or reordering
894
- remoteOrderChanged = true;
895
- // however: if the new remote order matches the current local order
896
- // we can disregard the change notification generation so long as
897
- // we are not configured to reset on remote update (which is deprecated)
898
-
899
- if (i < priorLocalLength) {
900
- const priorLocalMember = priorLocalState[i];
901
- if (priorLocalMember !== member) {
902
- changed = true;
903
- }
904
- } else if (i < finalLength) {
905
- // if we have exceeded the length of priorLocalState and we are within the range
906
- // of the finalState then we must have changed
907
-
908
- changed = true;
909
- }
910
-
911
- // if remote order hasn't changed but local order differs
912
- // and we are configured to reset on remote update (which is deprecated)
913
- // then we still need to mark the relationship as changed
914
- }
915
- if (!finalSet.has(prevMember)) {
916
- // if we are within finalLength, we can only be "changed" if we've already exceeded
917
- // the index range of priorLocalState, as otherwise the previous member may still
918
- // be removed.
919
- //
920
- // prior local: [1, 2, 3, 4]
921
- // final state: [1, 2, 3]
922
- // prev remote state: [1, 2, 5, 3, 4]
923
- // i === 2
924
- // prevMember === 5
925
- // !finalSet.has(prevMember) === true
926
- //
927
- // because we will become changed at i===3,
928
- // we do not need to worry about becoming changed at i===2
929
- // as the arrays until now are still the same
930
- //
931
- // prior local: [1, 2, 3]
932
- // final state: [1, 2, 3, 4]
933
- // prev remote state: [1, 2, 5, 3, 4]
934
- // i === 2
935
- // prevMember === 5
936
- // !finalSet.has(prevMember) === true
937
- //
938
- // because we will become changed at i===3
939
- // we do not need to worry about becoming changed at i===2
940
- //
941
- // prior local: [1, 2, 3]
942
- // final state: [1, 2, 3]
943
- // prev remote state: [1, 2, 5, 3, 4]
944
- // i === 2
945
- // prevMember === 5
946
- // !finalSet.has(prevMember) === true
947
- //
948
- // because we have same length and same membership order
949
- // we do not need to worry about becoming changed at i===2
950
- //
951
- // if you do not have a priorLocalState you can't be changed
952
- // ergo, we never need to set changed in this branch.
953
- // this log can still be useful for debugging.
954
-
955
- //
956
- // we do still set remoteOrderChanged as it has
957
- remoteOrderChanged = true;
958
- removed.add(prevMember);
959
- onDel(prevMember);
960
- }
961
- }
962
- }
963
- return {
964
- add: added,
965
- del: removed,
966
- finalState,
967
- finalSet,
968
- changed,
969
- remoteOrderChanged
970
- };
285
+ const finalLength = finalState.length;
286
+ const prevLength = prevState.length;
287
+ const iterationLength = Math.max(finalLength, prevLength);
288
+ const equalLength = priorLocalState ? finalLength === priorLocalState.length : finalLength === prevLength;
289
+ let remoteOrderChanged = finalSet.size !== prevSet.size;
290
+ let changed = priorLocalState ? finalSet.size !== priorLocalState.length : remoteOrderChanged;
291
+ const added = /* @__PURE__ */ new Set();
292
+ const removed = /* @__PURE__ */ new Set();
293
+ const priorLocalLength = priorLocalState?.length ?? 0;
294
+ for (let i = 0; i < iterationLength; i++) {
295
+ let member;
296
+ if (i < finalLength) {
297
+ member = finalState[i];
298
+ if (!prevSet.has(member)) {
299
+ if (i < priorLocalLength) {
300
+ if (priorLocalState[i] !== member) changed = true;
301
+ }
302
+ added.add(member);
303
+ onAdd(member);
304
+ }
305
+ }
306
+ if (i < prevLength) {
307
+ const prevMember = prevState[i];
308
+ if (equalLength && member !== prevMember) {
309
+ remoteOrderChanged = true;
310
+ if (i < priorLocalLength) {
311
+ if (priorLocalState[i] !== member) changed = true;
312
+ } else if (i < finalLength) changed = true;
313
+ }
314
+ if (!finalSet.has(prevMember)) {
315
+ remoteOrderChanged = true;
316
+ removed.add(prevMember);
317
+ onDel(prevMember);
318
+ }
319
+ }
320
+ }
321
+ return {
322
+ add: added,
323
+ del: removed,
324
+ finalState,
325
+ finalSet,
326
+ changed,
327
+ remoteOrderChanged
328
+ };
971
329
  }
972
330
  function diffCollection(finalState, relationship, onAdd, onDel) {
973
- const finalSet = new Set(finalState);
974
- const {
975
- localState: priorLocalState,
976
- remoteState,
977
- remoteMembers
978
- } = relationship;
979
- return _compare(priorLocalState, finalState, finalSet, remoteState, remoteMembers, onAdd, onDel, relationship.definition.resetOnRemoteUpdate);
331
+ const finalSet = new Set(finalState);
332
+ const { localState: priorLocalState, remoteState, remoteMembers } = relationship;
333
+ return _compare(priorLocalState, finalState, finalSet, remoteState, remoteMembers, onAdd, onDel, relationship.definition.resetOnRemoteUpdate);
980
334
  }
981
335
  function computeLocalState(storage) {
982
- if (!storage.isDirty) {
983
- return storage.localState;
984
- }
985
- const state = storage.remoteState.slice();
986
- storage.removals?.forEach(v => {
987
- const index = state.indexOf(v);
988
- state.splice(index, 1);
989
- });
990
- storage.additions?.forEach(v => {
991
- state.push(v);
992
- });
993
- storage.localState = state;
994
- storage.isDirty = false;
995
- return state;
336
+ if (!storage.isDirty && storage.localState) return storage.localState;
337
+ const state = storage.remoteState.slice();
338
+ storage.removals?.forEach((v) => {
339
+ const index = state.indexOf(v);
340
+ state.splice(index, 1);
341
+ });
342
+ storage.additions?.forEach((v) => {
343
+ state.push(v);
344
+ });
345
+ storage.localState = state;
346
+ storage.isDirty = false;
347
+ return state;
996
348
  }
997
-
998
349
  /**
999
- * A function which attempts to add a value to the local state of a collection
1000
- * relationship, and returns true if the value was added, or false if it was
1001
- * already present.
1002
- *
1003
- * It will not generate a notification, will not update the relationships to dirty,
1004
- * and will not update the inverse relationships, making it suitable for use as
1005
- * an internal util to perform the just the addition to a specific side of a
1006
- * relationship.
1007
- *
1008
- * @internal
1009
- */
350
+ * A function which attempts to add a value to the local state of a collection
351
+ * relationship, and returns true if the value was added, or false if it was
352
+ * already present.
353
+ *
354
+ * It will not generate a notification, will not update the relationships to dirty,
355
+ * and will not update the inverse relationships, making it suitable for use as
356
+ * an internal util to perform the just the addition to a specific side of a
357
+ * relationship.
358
+ *
359
+ * @internal
360
+ */
1010
361
  function _add(graph, record, relationship, value, index, isRemote) {
1011
- return !isRemote ? _addLocal(graph, record, relationship, value, index) : _addRemote(graph, record, relationship, value, index);
362
+ return !isRemote ? _addLocal(graph, record, relationship, value, index) : _addRemote(graph, record, relationship, value, index);
1012
363
  }
1013
364
  function _addRemote(graph, record, relationship, value, index) {
1014
- const {
1015
- remoteMembers,
1016
- additions,
1017
- removals,
1018
- remoteState
1019
- } = relationship;
1020
- if (remoteMembers.has(value)) {
1021
- return false;
1022
- }
1023
-
1024
- // add to the remote state
1025
- remoteMembers.add(value);
1026
- const hasValidIndex = index !== null && index >= 0 && index < remoteState.length;
1027
- if (hasValidIndex) {
1028
- remoteState.splice(index, 0, value);
1029
- } else {
1030
- remoteState.push(value);
1031
- }
1032
-
1033
- // remove from additions if present
1034
- if (additions?.has(value)) {
1035
- additions.delete(value);
1036
-
1037
- // nothing more to do this was our state already
1038
- return false;
1039
- }
1040
-
1041
- // if the relationship already needs to recalc, we don't bother
1042
- // attempting to patch the localState
1043
- if (relationship.isDirty) {
1044
- return true;
1045
- }
1046
-
1047
- // if we have existing localState
1048
- // we attempt to patch it without blowing it away
1049
- // as this is more efficient than recomputing
1050
- // it allows us to preserve local ordering
1051
- // to a small extent. Local ordering should not
1052
- // be relied upon as any remote change could blow it away
1053
- if (relationship.localState) {
1054
- if (!hasValidIndex) {
1055
- relationship.localState.push(value);
1056
- } else if (index === 0) {
1057
- relationship.localState.unshift(value);
1058
- } else if (!removals?.size) {
1059
- relationship.localState.splice(index, 0, value);
1060
- } else {
1061
- relationship.isDirty = true;
1062
- }
1063
- }
1064
- return true;
365
+ const { remoteMembers, additions, removals, remoteState } = relationship;
366
+ if (remoteMembers.has(value)) return false;
367
+ remoteMembers.add(value);
368
+ const hasValidIndex = index !== null && index >= 0 && index < remoteState.length;
369
+ if (hasValidIndex) remoteState.splice(index, 0, value);
370
+ else remoteState.push(value);
371
+ if (additions?.has(value)) {
372
+ additions.delete(value);
373
+ return false;
374
+ }
375
+ if (relationship.isDirty) return true;
376
+ if (relationship.localState) {
377
+ if (!hasValidIndex) relationship.localState.push(value);
378
+ else if (index === 0) relationship.localState.unshift(value);
379
+ else if (!removals?.size) relationship.localState.splice(index, 0, value);
380
+ else relationship.isDirty = true;
381
+ }
382
+ return true;
1065
383
  }
1066
384
  function _addLocal(graph, record, relationship, value, index) {
1067
- const {
1068
- remoteMembers,
1069
- removals
1070
- } = relationship;
1071
- let additions = relationship.additions;
1072
- const hasPresence = remoteMembers.has(value) || additions?.has(value);
1073
- if (hasPresence && !removals?.has(value)) {
1074
- return false;
1075
- }
1076
- if (removals?.has(value)) {
1077
- removals.delete(value);
1078
- } else {
1079
- if (!additions) {
1080
- additions = relationship.additions = new Set();
1081
- }
1082
- relationship.state.hasReceivedData = true;
1083
- additions.add(value);
1084
- const {
1085
- type
1086
- } = relationship.definition;
1087
- if (type !== value.type) {
1088
- graph.registerPolymorphicType(value.type, type);
1089
- }
1090
- }
1091
-
1092
- // if we have existing localState
1093
- // and we have an index
1094
- // apply the change, as this is more efficient
1095
- // than recomputing localState and
1096
- // it allows us to preserve local ordering
1097
- // to a small extend. Local ordering should not
1098
- // be relied upon as any remote change will blow it away
1099
- if (relationship.localState) {
1100
- if (index !== null) {
1101
- relationship.localState.splice(index, 0, value);
1102
- } else {
1103
- relationship.localState.push(value);
1104
- }
1105
- }
1106
- return true;
385
+ const { remoteMembers, removals } = relationship;
386
+ let additions = relationship.additions;
387
+ if ((remoteMembers.has(value) || additions?.has(value)) && !removals?.has(value)) return false;
388
+ if (removals?.has(value)) removals.delete(value);
389
+ else {
390
+ if (!additions) additions = relationship.additions = /* @__PURE__ */ new Set();
391
+ relationship.state.hasReceivedData = true;
392
+ additions.add(value);
393
+ const { type } = relationship.definition;
394
+ if (type !== value.type) graph.registerPolymorphicType(value.type, type);
395
+ }
396
+ if (relationship.localState) {
397
+ if (index !== null) relationship.localState.splice(index, 0, value);
398
+ else relationship.localState.push(value);
399
+ }
400
+ return true;
1107
401
  }
1108
402
  function _remove(graph, record, relationship, value, index, isRemote) {
1109
- return !isRemote ? _removeLocal(relationship, value) : _removeRemote(relationship, value);
403
+ return !isRemote ? _removeLocal(relationship, value) : _removeRemote(relationship, value);
1110
404
  }
1111
405
  function _removeLocal(relationship, value) {
1112
- const {
1113
- remoteMembers,
1114
- additions
1115
- } = relationship;
1116
- let removals = relationship.removals;
1117
- const hasPresence = remoteMembers.has(value) || additions?.has(value);
1118
- if (!hasPresence || removals?.has(value)) {
1119
- return false;
1120
- }
1121
- if (additions?.has(value)) {
1122
- additions.delete(value);
1123
- } else {
1124
- if (!removals) {
1125
- removals = relationship.removals = new Set();
1126
- }
1127
- removals.add(value);
1128
- }
1129
-
1130
- // if we have existing localState
1131
- // apply the change, as this is more efficient
1132
- // than recomputing localState and
1133
- // it allows us to preserve local ordering
1134
- // to a small extend. Local ordering should not
1135
- // be relied upon as any remote change will blow it away
1136
- if (relationship.localState) {
1137
- const index = relationship.localState.indexOf(value);
1138
- relationship.localState.splice(index, 1);
1139
- }
1140
- return true;
406
+ const { remoteMembers, additions } = relationship;
407
+ let removals = relationship.removals;
408
+ if (!(remoteMembers.has(value) || additions?.has(value)) || removals?.has(value)) return false;
409
+ if (additions?.has(value)) additions.delete(value);
410
+ else {
411
+ if (!removals) removals = relationship.removals = /* @__PURE__ */ new Set();
412
+ removals.add(value);
413
+ }
414
+ if (relationship.localState) {
415
+ const index = relationship.localState.indexOf(value);
416
+ relationship.localState.splice(index, 1);
417
+ }
418
+ return true;
1141
419
  }
1142
420
  function _removeRemote(relationship, value) {
1143
- const {
1144
- remoteMembers,
1145
- additions,
1146
- removals,
1147
- remoteState
1148
- } = relationship;
1149
- if (!remoteMembers.has(value)) {
1150
- return false;
1151
- }
1152
-
1153
- // remove from remote state
1154
- remoteMembers.delete(value);
1155
- let index = remoteState.indexOf(value);
1156
- remoteState.splice(index, 1);
1157
-
1158
- // remove from removals if present
1159
- if (removals?.has(value)) {
1160
- removals.delete(value);
1161
-
1162
- // nothing more to do this was our state already
1163
- return false;
1164
- }
1165
-
1166
- // if we have existing localState
1167
- // and we have an index
1168
- // apply the change, as this is more efficient
1169
- // than recomputing localState and
1170
- // it allows us to preserve local ordering
1171
- // to a small extend. Local ordering should not
1172
- // be relied upon as any remote change will blow it away
1173
- if (relationship.localState) {
1174
- index = relationship.localState.indexOf(value);
1175
- relationship.localState.splice(index, 1);
1176
- }
1177
- return true;
421
+ const { remoteMembers, additions, removals, remoteState } = relationship;
422
+ if (!remoteMembers.has(value)) return false;
423
+ remoteMembers.delete(value);
424
+ let index = remoteState.indexOf(value);
425
+ remoteState.splice(index, 1);
426
+ if (removals?.has(value)) {
427
+ removals.delete(value);
428
+ return false;
429
+ }
430
+ if (relationship.localState) {
431
+ index = relationship.localState.indexOf(value);
432
+ relationship.localState.splice(index, 1);
433
+ }
434
+ return true;
1178
435
  }
1179
436
  function rollbackRelationship(graph, key, field, relationship) {
1180
- if (isBelongsTo(relationship)) {
1181
- replaceRelatedRecord(graph, {
1182
- record: key,
1183
- field,
1184
- value: relationship.remoteState
1185
- }, false);
1186
- } else {
1187
- replaceRelatedRecords(graph, {
1188
- record: key,
1189
- field,
1190
- value: relationship.remoteState.slice()
1191
- }, false);
1192
-
1193
- // when the change was a "reorder" only we wont have generated
1194
- // a notification yet.
1195
- // if we give rollback a unique operation we can use the ability of
1196
- // diff to report a separate `remoteOrderChanged` flag to trigger this
1197
- // if needed to avoid the duplicate.
1198
- notifyChange(graph, relationship);
1199
- }
437
+ if (isBelongsTo(relationship)) replaceRelatedRecord(graph, {
438
+ op: "replaceRelatedRecord",
439
+ record: key,
440
+ field,
441
+ value: relationship.remoteState
442
+ }, false);
443
+ else {
444
+ replaceRelatedRecords(graph, {
445
+ op: "replaceRelatedRecords",
446
+ record: key,
447
+ field,
448
+ value: relationship.remoteState.slice()
449
+ }, false);
450
+ notifyChange(graph, relationship);
451
+ }
452
+ }
453
+
454
+ //#endregion
455
+ //#region src/graph/-private/-edge-definition.ts
456
+ function isLegacyField(field) {
457
+ return field.kind === "belongsTo" || field.kind === "hasMany";
1200
458
  }
1201
- function createState() {
1202
- return {
1203
- hasReceivedData: false,
1204
- isEmpty: true,
1205
- isStale: false,
1206
- hasFailedLoadAttempt: false,
1207
- shouldForceReload: false,
1208
- hasDematerializedInverse: false
1209
- };
459
+ function temporaryConvertToLegacy(field) {
460
+ return {
461
+ kind: field.kind === "resource" ? "belongsTo" : "hasMany",
462
+ name: field.name,
463
+ type: field.type,
464
+ options: Object.assign({}, {
465
+ async: false,
466
+ inverse: null,
467
+ resetOnRemoteUpdate: false
468
+ }, field.options)
469
+ };
1210
470
  }
471
+ /**
472
+ *
473
+ * Given RHS (Right Hand Side)
474
+ *
475
+ * ```ts
476
+ * class User extends Model {
477
+ * @hasMany('animal', { async: false, inverse: 'owner' }) pets;
478
+ * }
479
+ * ```
480
+ *
481
+ * Given LHS (Left Hand Side)
482
+ *
483
+ * ```ts
484
+ * class Animal extends Model {
485
+ * @belongsTo('user', { async: false, inverse: 'pets' }) owner;
486
+ * }
487
+ * ```
488
+ *
489
+ * The UpgradedMeta for the RHS would be:
490
+ *
491
+ * ```ts
492
+ * {
493
+ * kind: 'hasMany',
494
+ * key: 'pets',
495
+ * type: 'animal',
496
+ * isAsync: false,
497
+ * isImplicit: false,
498
+ * isCollection: true,
499
+ * isPolymorphic: false,
500
+ * inverseKind: 'belongsTo',
501
+ * inverseKey: 'owner',
502
+ * inverseType: 'user',
503
+ * inverseIsAsync: false,
504
+ * inverseIsImplicit: false,
505
+ * inverseIsCollection: false,
506
+ * inverseIsPolymorphic: false,
507
+ * }
508
+ * ```
509
+ *
510
+ * The UpgradeMeta for the LHS would be:
511
+ *
512
+ * ```ts
513
+ * {
514
+ * kind: 'belongsTo',
515
+ * key: 'owner',
516
+ * type: 'user',
517
+ * isAsync: false,
518
+ * isImplicit: false,
519
+ * isCollection: false,
520
+ * isPolymorphic: false,
521
+ * inverseKind: 'hasMany',
522
+ * inverseKey: 'pets',
523
+ * inverseType: 'animal',
524
+ * inverseIsAsync: false,
525
+ * inverseIsImplicit: false,
526
+ * inverseIsCollection: true,
527
+ * inverseIsPolymorphic: false,
528
+ * }
529
+ * ```
530
+ *
531
+ * @private
532
+ */
533
+ const BOOL_LATER = null;
534
+ const STR_LATER = "";
535
+ const IMPLICIT_KEY_RAND = Date.now();
536
+ function implicitKeyFor(type, key) {
537
+ return `implicit-${type}:${key}${IMPLICIT_KEY_RAND}`;
538
+ }
539
+ function syncMeta(definition, inverseDefinition) {
540
+ definition.inverseKind = inverseDefinition.kind;
541
+ definition.inverseKey = inverseDefinition.key;
542
+ definition.inverseName = inverseDefinition.name;
543
+ definition.inverseType = inverseDefinition.type;
544
+ definition.inverseIsAsync = inverseDefinition.isAsync;
545
+ definition.inverseIsCollection = inverseDefinition.isCollection;
546
+ definition.inverseIsPolymorphic = inverseDefinition.isPolymorphic;
547
+ definition.inverseIsImplicit = inverseDefinition.isImplicit;
548
+ definition.inverseIsLinksMode = inverseDefinition.isLinksMode;
549
+ const resetOnRemoteUpdate = definition.resetOnRemoteUpdate === false || inverseDefinition.resetOnRemoteUpdate === false ? false : true;
550
+ definition.resetOnRemoteUpdate = resetOnRemoteUpdate;
551
+ inverseDefinition.resetOnRemoteUpdate = resetOnRemoteUpdate;
552
+ }
553
+ function upgradeMeta(meta) {
554
+ if (!isLegacyField(meta)) meta = temporaryConvertToLegacy(meta);
555
+ const niceMeta = {};
556
+ const options = meta.options;
557
+ niceMeta.kind = meta.kind;
558
+ niceMeta.key = meta.sourceKey ?? meta.name;
559
+ niceMeta.name = meta.name;
560
+ niceMeta.type = meta.type;
561
+ niceMeta.isAsync = options.async;
562
+ niceMeta.isImplicit = false;
563
+ niceMeta.isCollection = meta.kind === "hasMany";
564
+ niceMeta.isPolymorphic = options && !!options.polymorphic;
565
+ niceMeta.isLinksMode = options.linksMode ?? false;
566
+ niceMeta.inverseKey = options && options.inverse || STR_LATER;
567
+ niceMeta.inverseName = options && options.inverse || STR_LATER;
568
+ niceMeta.inverseType = STR_LATER;
569
+ niceMeta.inverseIsAsync = BOOL_LATER;
570
+ niceMeta.inverseIsImplicit = options && options.inverse === null || BOOL_LATER;
571
+ niceMeta.inverseIsCollection = BOOL_LATER;
572
+ niceMeta.inverseIsLinksMode = BOOL_LATER;
573
+ niceMeta.resetOnRemoteUpdate = !isLegacyField(meta) ? false : meta.options?.linksMode ? false : meta.options?.resetOnRemoteUpdate === false ? false : true;
574
+ return niceMeta;
575
+ }
576
+ function isLHS(info, type, key) {
577
+ const isSelfReferential = info.isSelfReferential;
578
+ if (key === info.lhs_relationshipName === true) return isSelfReferential === true || type === info.lhs_baseModelName || info.rhs_isPolymorphic && info.lhs_modelNames.includes(type);
579
+ return false;
580
+ }
581
+ function upgradeDefinition(graph, key, propertyName, isImplicit = false) {
582
+ const cache = graph._definitionCache;
583
+ const storeWrapper = graph.store;
584
+ const polymorphicLookup = graph._potentialPolymorphicTypes;
585
+ const { type } = key;
586
+ let cached = expandingGet(cache, type, propertyName);
587
+ if (cached !== void 0) return cached;
588
+ const relationships = storeWrapper.schema.fields(key);
589
+ const meta = (storeWrapper.schema.cacheFields?.(key) ?? relationships).get(propertyName);
590
+ if (!meta) {
591
+ if (polymorphicLookup[type]) {
592
+ const altTypes = Object.keys(polymorphicLookup[type]);
593
+ for (let i = 0; i < altTypes.length; i++) {
594
+ const _cached = expandingGet(cache, altTypes[i], propertyName);
595
+ if (_cached) {
596
+ expandingSet(cache, type, propertyName, _cached);
597
+ _cached.rhs_modelNames.push(type);
598
+ return _cached;
599
+ }
600
+ }
601
+ }
602
+ cache[type][propertyName] = null;
603
+ return null;
604
+ }
605
+ const definition = upgradeMeta(meta);
606
+ let inverseDefinition;
607
+ let inverseKey;
608
+ const inverseType = definition.type;
609
+ if (definition.inverseKey === null) inverseDefinition = null;
610
+ else {
611
+ inverseKey = inverseForRelationship(getStore(storeWrapper), key, propertyName);
612
+ if (!inverseKey && definition.isPolymorphic && definition.inverseKey) inverseDefinition = {
613
+ kind: "belongsTo",
614
+ key: definition.inverseKey,
615
+ name: definition.inverseName,
616
+ type,
617
+ isAsync: false,
618
+ isImplicit: false,
619
+ isCollection: false,
620
+ isPolymorphic: false
621
+ };
622
+ else if (!inverseKey) inverseDefinition = null;
623
+ else inverseDefinition = upgradeMeta(storeWrapper.schema.fields({ type: inverseType }).get(inverseKey));
624
+ }
625
+ if (!inverseDefinition) {
626
+ inverseKey = implicitKeyFor(type, propertyName);
627
+ inverseDefinition = {
628
+ kind: "implicit",
629
+ key: inverseKey,
630
+ type,
631
+ isAsync: false,
632
+ isImplicit: true,
633
+ isCollection: true,
634
+ isPolymorphic: false
635
+ };
636
+ syncMeta(definition, inverseDefinition);
637
+ syncMeta(inverseDefinition, definition);
638
+ const info = {
639
+ lhs_key: `${type}:${propertyName}`,
640
+ lhs_modelNames: [type],
641
+ lhs_baseModelName: type,
642
+ lhs_relationshipName: propertyName,
643
+ lhs_definition: definition,
644
+ lhs_isPolymorphic: definition.isPolymorphic,
645
+ rhs_key: inverseDefinition.key,
646
+ rhs_modelNames: [inverseType],
647
+ rhs_baseModelName: inverseType,
648
+ rhs_relationshipName: inverseDefinition.key,
649
+ rhs_definition: inverseDefinition,
650
+ rhs_isPolymorphic: false,
651
+ hasInverse: false,
652
+ isSelfReferential: type === inverseType,
653
+ isReflexive: false
654
+ };
655
+ expandingSet(cache, inverseType, inverseKey, info);
656
+ expandingSet(cache, type, propertyName, info);
657
+ return info;
658
+ }
659
+ const baseType = inverseDefinition.type;
660
+ cached = expandingGet(cache, baseType, propertyName) || expandingGet(cache, inverseType, inverseKey);
661
+ if (cached) {
662
+ const _isLHS = cached.lhs_baseModelName === baseType;
663
+ _isLHS ? cached.lhs_relationshipName : cached.rhs_relationshipName;
664
+ cached.lhs_baseModelName === baseType && cached.lhs_relationshipName === propertyName || cached.rhs_baseModelName === baseType && cached.rhs_relationshipName;
665
+ (_isLHS ? cached.lhs_modelNames : cached.rhs_modelNames).push(type);
666
+ expandingSet(cache, type, propertyName, cached);
667
+ return cached;
668
+ }
669
+ syncMeta(definition, inverseDefinition);
670
+ syncMeta(inverseDefinition, definition);
671
+ const lhs_modelNames = [type];
672
+ if (type !== baseType) lhs_modelNames.push(baseType);
673
+ const isSelfReferential = baseType === inverseType;
674
+ const info = {
675
+ lhs_key: `${baseType}:${propertyName}`,
676
+ lhs_modelNames,
677
+ lhs_baseModelName: baseType,
678
+ lhs_relationshipName: propertyName,
679
+ lhs_definition: definition,
680
+ lhs_isPolymorphic: definition.isPolymorphic,
681
+ rhs_key: `${inverseType}:${inverseKey}`,
682
+ rhs_modelNames: [inverseType],
683
+ rhs_baseModelName: inverseType,
684
+ rhs_relationshipName: inverseKey,
685
+ rhs_definition: inverseDefinition,
686
+ rhs_isPolymorphic: inverseDefinition.isPolymorphic,
687
+ hasInverse: true,
688
+ isSelfReferential,
689
+ isReflexive: isSelfReferential && propertyName === inverseKey
690
+ };
691
+ expandingSet(cache, baseType, propertyName, info);
692
+ expandingSet(cache, type, propertyName, info);
693
+ expandingSet(cache, inverseType, inverseKey, info);
694
+ return info;
695
+ }
696
+ function inverseForRelationship(store, resourceKey, key) {
697
+ const definition = store.schema.fields(resourceKey).get(key);
698
+ if (!definition) return null;
699
+ return definition.options.inverse;
700
+ }
701
+
702
+ //#endregion
703
+ //#region src/graph/-private/-state.ts
704
+ function createState() {
705
+ return {
706
+ hasReceivedData: false,
707
+ hasReceivedRemoteData: false,
708
+ isEmpty: true,
709
+ isStale: false,
710
+ hasFailedLoadAttempt: false,
711
+ shouldForceReload: false,
712
+ hasDematerializedInverse: false
713
+ };
714
+ }
715
+
716
+ //#endregion
717
+ //#region src/graph/-private/edges/collection.ts
718
+ /**
719
+ * Stores the graph's internal state for one side of a collection (`hasMany`) relationship.
720
+ */
721
+ /** Creates a new {@link CollectionEdge} for the given definition and identifier. */
1211
722
  function createCollectionEdge(definition, identifier) {
1212
- return {
1213
- definition,
1214
- identifier,
1215
- state: createState(),
1216
- remoteMembers: new Set(),
1217
- remoteState: [],
1218
- additions: null,
1219
- removals: null,
1220
- meta: null,
1221
- links: null,
1222
- localState: null,
1223
- isDirty: false,
1224
- transactionRef: 0,
1225
- accessed: false,
1226
- _diff: undefined
1227
- };
723
+ return {
724
+ definition,
725
+ identifier,
726
+ state: createState(),
727
+ remoteMembers: /* @__PURE__ */ new Set(),
728
+ remoteState: [],
729
+ additions: null,
730
+ removals: null,
731
+ meta: null,
732
+ links: null,
733
+ localState: null,
734
+ isDirty: false,
735
+ transactionRef: 0,
736
+ accessed: false,
737
+ _diff: void 0
738
+ };
1228
739
  }
1229
740
  const cp = structuredClone;
741
+ /**
742
+ * Builds a legacy `{ data, links, meta }` {@link CollectionRelationship} payload for this edge,
743
+ * using either the remote or the computed local state, and marks the edge as accessed.
744
+ */
1230
745
  function legacyGetCollectionRelationshipData(source, getRemoteState) {
1231
- source.accessed = true;
1232
- const payload = {};
1233
- if (source.state.hasReceivedData) {
1234
- payload.data = getRemoteState ? source.remoteState.slice() : computeLocalState(source);
1235
- }
1236
- if (source.links) {
1237
- payload.links = cp(source.links);
1238
- }
1239
- if (source.meta) {
1240
- payload.meta = cp(source.meta);
1241
- }
1242
- return payload;
746
+ source.accessed = true;
747
+ const payload = {};
748
+ if (source.state.hasReceivedData) payload.data = getRemoteState ? source.remoteState.slice() : computeLocalState(source);
749
+ if (source.links) payload.links = cp(source.links);
750
+ if (source.meta) payload.meta = cp(source.meta);
751
+ return payload;
1243
752
  }
1244
753
 
754
+ //#endregion
755
+ //#region src/graph/-private/edges/implicit.ts
756
+ /**
757
+ * The relationship definition (schema metadata) backing an {@link ImplicitEdge}, tagging
758
+ * the {@link UpgradedMeta} as belonging to an implicit inverse relationship.
759
+ */
1245
760
  /**
1246
- Implicit relationships are relationships which have not been declared but the inverse side exists on
1247
- another record somewhere
761
+ Implicit relationships are relationships which have not been declared but the inverse side exists on
762
+ another record somewhere
1248
763
 
1249
- For example consider the following two models
764
+ For example consider the following two models
1250
765
 
1251
- ::: code-group
766
+ ::: code-group
1252
767
 
1253
- ```js [./models/comment.js]
1254
- import { Model, attr } from '@warp-drive/legacy/model';
768
+ ```js [./models/comment.js]
769
+ import { Model, attr } from '@warp-drive/legacy/model';
1255
770
 
1256
- export default class Comment extends Model {
1257
- @attr text;
1258
- }
1259
- ```
771
+ export default class Comment extends Model {
772
+ @attr text;
773
+ }
774
+ ```
1260
775
 
1261
- ```js [./models/post.js]
1262
- import { Model, attr, hasMany } from '@warp-drive/legacy/model';
776
+ ```js [./models/post.js]
777
+ import { Model, attr, hasMany } from '@warp-drive/legacy/model';
1263
778
 
1264
- export default class Post extends Model {
1265
- @attr title;
1266
- @hasMany('comment', { async: true, inverse: null }) comments;
1267
- }
1268
- ```
779
+ export default class Post extends Model {
780
+ @attr title;
781
+ @hasMany('comment', { async: true, inverse: null }) comments;
782
+ }
783
+ ```
1269
784
 
1270
- :::
785
+ :::
1271
786
 
1272
- Then we would have a implicit 'post' relationship for the comment record in order
1273
- to be do things like remove the comment from the post if the comment were to be deleted.
787
+ Then we would have a implicit 'post' relationship for the comment record in order
788
+ to be do things like remove the comment from the post if the comment were to be deleted.
1274
789
  */
1275
-
790
+ /** Creates a new {@link ImplicitEdge} for the given definition and identifier. */
1276
791
  function createImplicitEdge(definition, identifier) {
1277
- return {
1278
- definition,
1279
- identifier,
1280
- localMembers: new Set(),
1281
- remoteMembers: new Set()
1282
- };
792
+ return {
793
+ definition,
794
+ identifier,
795
+ localMembers: /* @__PURE__ */ new Set(),
796
+ remoteMembers: /* @__PURE__ */ new Set()
797
+ };
1283
798
  }
1284
799
 
1285
- /**
1286
- * Stores the data for one side of a "single" resource relationship.
1287
- *
1288
- * @private
1289
- */
1290
-
800
+ //#endregion
801
+ //#region src/graph/-private/edges/resource.ts
1291
802
  function createResourceEdge(definition, identifier) {
1292
- return {
1293
- definition,
1294
- identifier,
1295
- state: createState(),
1296
- transactionRef: 0,
1297
- localState: null,
1298
- remoteState: null,
1299
- meta: null,
1300
- links: null,
1301
- accessed: false
1302
- };
803
+ return {
804
+ definition,
805
+ identifier,
806
+ state: createState(),
807
+ transactionRef: 0,
808
+ localState: null,
809
+ remoteState: null,
810
+ meta: null,
811
+ links: null,
812
+ accessed: false
813
+ };
1303
814
  }
1304
815
  function legacyGetResourceRelationshipData(source, getRemoteState) {
1305
- source.accessed = true;
1306
- let data;
1307
- const payload = {};
1308
- if (getRemoteState && source.remoteState) {
1309
- data = source.remoteState;
1310
- } else if (!getRemoteState && source.localState) {
1311
- data = source.localState;
1312
- }
1313
- if ((getRemoteState && source.remoteState === null || source.localState === null) && source.state.hasReceivedData) {
1314
- data = null;
1315
- }
1316
- if (source.links) {
1317
- payload.links = source.links;
1318
- }
1319
- if (data !== undefined) {
1320
- payload.data = data;
1321
- }
1322
- if (source.meta) {
1323
- payload.meta = source.meta;
1324
- }
1325
- return payload;
1326
- }
816
+ source.accessed = true;
817
+ let data;
818
+ const payload = {};
819
+ if (getRemoteState && (source.remoteState || source.state.hasReceivedRemoteData)) data = source.remoteState ?? null;
820
+ else if (!getRemoteState && (source.localState || source.state.hasReceivedData)) data = source.localState ?? null;
821
+ if (source.links) payload.links = source.links;
822
+ if (data !== void 0) payload.data = data;
823
+ if (source.meta) payload.meta = source.meta;
824
+ return payload;
825
+ }
826
+
827
+ //#endregion
828
+ //#region src/graph/-private/operations/add-to-related-records.ts
1327
829
  function addToRelatedRecords(graph, op, isRemote) {
1328
- const {
1329
- record,
1330
- value,
1331
- index
1332
- } = op;
1333
- const relationship = graph.get(record, op.field);
1334
- const _isBelongsTo = isBelongsTo(relationship);
1335
- if (isRemote && _isBelongsTo) {
1336
- if (value !== relationship.remoteState) {
1337
- const newOp = {
1338
- record,
1339
- field: op.field,
1340
- value: value
1341
- };
1342
- return replaceRelatedRecord(graph, newOp, isRemote);
1343
- }
1344
- return;
1345
- }
1346
-
1347
- // if we are not dirty but have a null localState then we
1348
- // are mutating a relationship that has never been fetched
1349
- // so we initialize localState to an empty array
1350
- if (!relationship.isDirty && !relationship.localState) {
1351
- relationship.localState = [];
1352
- }
1353
- if (Array.isArray(value)) {
1354
- for (let i = 0; i < value.length; i++) {
1355
- addRelatedRecord(graph, relationship, record, value[i], index !== undefined ? index + i : null, isRemote);
1356
- }
1357
- } else {
1358
- addRelatedRecord(graph, relationship, record, value, index ?? null, isRemote);
1359
- }
1360
- notifyChange(graph, relationship);
830
+ const { record, value, index } = op;
831
+ const relationship = graph.get(record, op.field);
832
+ const _isBelongsTo = isBelongsTo(relationship);
833
+ if (isRemote && _isBelongsTo) {
834
+ if (value !== relationship.remoteState) {
835
+ const newOp = {
836
+ op: "replaceRelatedRecord",
837
+ record,
838
+ field: op.field,
839
+ value
840
+ };
841
+ return replaceRelatedRecord(graph, newOp, isRemote);
842
+ }
843
+ return;
844
+ }
845
+ if (!relationship.isDirty && !relationship.localState) relationship.localState = [];
846
+ if (Array.isArray(value)) for (let i = 0; i < value.length; i++) addRelatedRecord(graph, relationship, record, value[i], index !== void 0 ? index + i : null, isRemote);
847
+ else addRelatedRecord(graph, relationship, record, value, index ?? null, isRemote);
848
+ notifyChange(graph, relationship);
1361
849
  }
1362
850
  function addRelatedRecord(graph, relationship, record, value, index, isRemote) {
1363
- if (_add(graph, record, relationship, value, index, isRemote)) {
1364
- addToInverse(graph, value, relationship.definition.inverseKey, record, isRemote);
1365
- }
851
+ if (_add(graph, record, relationship, value, index, isRemote)) addToInverse(graph, value, relationship.definition.inverseKey, record, isRemote);
1366
852
  }
853
+
854
+ //#endregion
855
+ //#region src/graph/-private/operations/merge-identifier.ts
1367
856
  function mergeIdentifier(graph, op, relationships) {
1368
- Object.keys(relationships).forEach(key => {
1369
- const rel = relationships[key];
1370
- if (!rel) {
1371
- return;
1372
- }
1373
- mergeIdentifierForRelationship(graph, op, rel);
1374
- });
857
+ Object.keys(relationships).forEach((key) => {
858
+ const rel = relationships[key];
859
+ if (!rel) return;
860
+ mergeIdentifierForRelationship(graph, op, rel);
861
+ });
1375
862
  }
1376
863
  function mergeIdentifierForRelationship(graph, op, rel) {
1377
- rel.identifier = op.value;
1378
- forAllRelatedIdentifiers(rel, identifier => {
1379
- const inverse = graph.get(identifier, rel.definition.inverseKey);
1380
- mergeInRelationship(graph, inverse, op);
1381
- });
864
+ rel.identifier = op.value;
865
+ forAllRelatedIdentifiers(rel, (identifier) => {
866
+ mergeInRelationship(graph, graph.get(identifier, rel.definition.inverseKey), op);
867
+ });
1382
868
  }
1383
869
  function mergeInRelationship(graph, rel, op) {
1384
- if (isBelongsTo(rel)) {
1385
- mergeBelongsTo(graph, rel, op);
1386
- } else if (isHasMany(rel)) {
1387
- mergeHasMany(graph, rel, op);
1388
- } else {
1389
- mergeImplicit(graph, rel, op);
1390
- }
870
+ if (isBelongsTo(rel)) mergeBelongsTo(graph, rel, op);
871
+ else if (isHasMany(rel)) mergeHasMany(graph, rel, op);
872
+ else mergeImplicit(graph, rel, op);
1391
873
  }
1392
874
  function mergeBelongsTo(graph, rel, op) {
1393
- if (rel.remoteState === op.record) {
1394
- rel.remoteState = op.value;
1395
- }
1396
- if (rel.localState === op.record) {
1397
- rel.localState = op.value;
1398
- notifyChange(graph, rel);
1399
- }
875
+ if (rel.remoteState === op.record) rel.remoteState = op.value;
876
+ if (rel.localState === op.record) {
877
+ rel.localState = op.value;
878
+ notifyChange(graph, rel);
879
+ }
1400
880
  }
1401
881
  function mergeHasMany(graph, rel, op) {
1402
- if (rel.remoteMembers.has(op.record)) {
1403
- rel.remoteMembers.delete(op.record);
1404
- rel.remoteMembers.add(op.value);
1405
- const index = rel.remoteState.indexOf(op.record);
1406
- rel.remoteState.splice(index, 1, op.value);
1407
- rel.isDirty = true;
1408
- }
1409
- if (rel.additions?.has(op.record)) {
1410
- rel.additions.delete(op.record);
1411
- rel.additions.add(op.value);
1412
- rel.isDirty = true;
1413
- }
1414
- if (rel.removals?.has(op.record)) {
1415
- rel.removals.delete(op.record);
1416
- rel.removals.add(op.value);
1417
- rel.isDirty = true;
1418
- }
1419
- if (rel.isDirty) {
1420
- notifyChange(graph, rel);
1421
- }
882
+ if (rel.remoteMembers.has(op.record)) {
883
+ rel.remoteMembers.delete(op.record);
884
+ rel.remoteMembers.add(op.value);
885
+ const index = rel.remoteState.indexOf(op.record);
886
+ rel.remoteState.splice(index, 1, op.value);
887
+ rel.isDirty = true;
888
+ }
889
+ if (rel.additions?.has(op.record)) {
890
+ rel.additions.delete(op.record);
891
+ rel.additions.add(op.value);
892
+ rel.isDirty = true;
893
+ }
894
+ if (rel.removals?.has(op.record)) {
895
+ rel.removals.delete(op.record);
896
+ rel.removals.add(op.value);
897
+ rel.isDirty = true;
898
+ }
899
+ if (rel.isDirty) notifyChange(graph, rel);
1422
900
  }
1423
901
  function mergeImplicit(graph, rel, op) {
1424
- if (rel.remoteMembers.has(op.record)) {
1425
- rel.remoteMembers.delete(op.record);
1426
- rel.remoteMembers.add(op.value);
1427
- }
1428
- if (rel.localMembers.has(op.record)) {
1429
- rel.localMembers.delete(op.record);
1430
- rel.localMembers.add(op.value);
1431
- }
1432
- }
902
+ if (rel.remoteMembers.has(op.record)) {
903
+ rel.remoteMembers.delete(op.record);
904
+ rel.remoteMembers.add(op.value);
905
+ }
906
+ if (rel.localMembers.has(op.record)) {
907
+ rel.localMembers.delete(op.record);
908
+ rel.localMembers.add(op.value);
909
+ }
910
+ }
911
+
912
+ //#endregion
913
+ //#region src/graph/-private/operations/remove-from-related-records.ts
1433
914
  function removeFromRelatedRecords(graph, op, isRemote) {
1434
- const {
1435
- record,
1436
- value
1437
- } = op;
1438
- const relationship = graph.get(record, op.field);
1439
- const _isBelongsTo = isBelongsTo(relationship);
1440
- if (isRemote && _isBelongsTo) {
1441
- if (value === relationship.remoteState) {
1442
- const newOp = {
1443
- record,
1444
- field: op.field,
1445
- value: null
1446
- };
1447
- return replaceRelatedRecord(graph, newOp, isRemote);
1448
- }
1449
- return;
1450
- }
1451
- if (Array.isArray(value)) {
1452
- for (let i = 0; i < value.length; i++) {
1453
- removeRelatedRecord(graph, record, relationship, value[i], op.index ?? null, isRemote);
1454
- }
1455
- } else {
1456
- removeRelatedRecord(graph, record, relationship, value, op.index ?? null, isRemote);
1457
- }
1458
- notifyChange(graph, relationship);
915
+ const { record, value } = op;
916
+ const relationship = graph.get(record, op.field);
917
+ const _isBelongsTo = isBelongsTo(relationship);
918
+ if (isRemote && _isBelongsTo) {
919
+ if (value === relationship.remoteState) {
920
+ const newOp = {
921
+ op: "replaceRelatedRecord",
922
+ record,
923
+ field: op.field,
924
+ value: null
925
+ };
926
+ return replaceRelatedRecord(graph, newOp, isRemote);
927
+ }
928
+ return;
929
+ }
930
+ if (Array.isArray(value)) for (let i = 0; i < value.length; i++) removeRelatedRecord(graph, record, relationship, value[i], op.index ?? null, isRemote);
931
+ else removeRelatedRecord(graph, record, relationship, value, op.index ?? null, isRemote);
932
+ notifyChange(graph, relationship);
1459
933
  }
1460
934
  function removeRelatedRecord(graph, record, relationship, value, index, isRemote) {
1461
- if (_remove(graph, record, relationship, value, index, isRemote)) {
1462
- removeFromInverse(graph, value, relationship.definition.inverseKey, record, isRemote);
1463
- }
935
+ if (_remove(graph, record, relationship, value, index, isRemote)) removeFromInverse(graph, value, relationship.definition.inverseKey, record, isRemote);
1464
936
  }
1465
937
 
1466
- /*
1467
- This method normalizes a link to an "links object". If the passed link is
1468
- already an object it's returned without any modifications.
1469
-
1470
- See http://jsonapi.org/format/#document-links for more information.
1471
- */
938
+ //#endregion
939
+ //#region src/graph/-private/normalize-link.ts
1472
940
  function _normalizeLink(link) {
1473
- switch (typeof link) {
1474
- case 'object':
1475
- return link;
1476
- case 'string':
1477
- return {
1478
- href: link
1479
- };
1480
- }
941
+ switch (typeof link) {
942
+ case "object": return link;
943
+ case "string": return { href: link };
944
+ }
1481
945
  }
1482
946
 
1483
- /*
1484
- Updates the "canonical" or "remote" state of a relationship, replacing any existing
1485
- state and blowing away any local changes (excepting new records).
1486
- */
947
+ //#endregion
948
+ //#region src/graph/-private/operations/update-relationship.ts
1487
949
  function updateRelationshipOperation(graph, op) {
1488
- const relationship = graph.get(op.record, op.field);
1489
- const {
1490
- definition,
1491
- state,
1492
- identifier
1493
- } = relationship;
1494
- const {
1495
- isCollection
1496
- } = definition;
1497
- const payload = op.value;
1498
- let hasRelationshipDataProperty = false;
1499
- let hasUpdatedLink = false;
1500
- if (payload.meta) {
1501
- relationship.meta = payload.meta;
1502
- }
1503
- if (payload.data !== undefined) {
1504
- hasRelationshipDataProperty = true;
1505
- if (isCollection) {
1506
- // TODO deprecate this case. We
1507
- // have tests saying we support it.
1508
- if (payload.data === null) {
1509
- payload.data = [];
1510
- }
1511
- const cache = graph.store.cacheKeyManager;
1512
- graph.update({
1513
- op: 'replaceRelatedRecords',
1514
- record: identifier,
1515
- field: op.field,
1516
- value: upgradeIdentifiers(payload.data, cache)
1517
- }, true);
1518
- } else {
1519
- graph.update({
1520
- op: 'replaceRelatedRecord',
1521
- record: identifier,
1522
- field: op.field,
1523
- value: payload.data ? graph.store.cacheKeyManager.upgradeIdentifier(payload.data) : null
1524
- }, true);
1525
- }
1526
- } else if (definition.isAsync === false && !state.hasReceivedData) {
1527
- hasRelationshipDataProperty = true;
1528
- if (isCollection) {
1529
- graph.update({
1530
- op: 'replaceRelatedRecords',
1531
- record: identifier,
1532
- field: op.field,
1533
- value: []
1534
- }, true);
1535
- } else {
1536
- graph.update({
1537
- op: 'replaceRelatedRecord',
1538
- record: identifier,
1539
- field: op.field,
1540
- value: null
1541
- }, true);
1542
- }
1543
- }
1544
- if (payload.links) {
1545
- const originalLinks = relationship.links;
1546
- relationship.links = payload.links;
1547
- if (payload.links.related) {
1548
- const relatedLink = _normalizeLink(payload.links.related);
1549
- const currentLink = originalLinks && originalLinks.related ? _normalizeLink(originalLinks.related) : null;
1550
- const currentLinkHref = currentLink ? currentLink.href : null;
1551
- if (relatedLink && relatedLink.href && relatedLink.href !== currentLinkHref) {
1552
- hasUpdatedLink = true;
1553
- }
1554
- }
1555
- }
1556
-
1557
- /*
1558
- Data being pushed into the relationship might contain only data or links,
1559
- or a combination of both.
1560
- IF contains only data
1561
- IF contains both links and data
1562
- state.isEmpty -> true if is empty array (has-many) or is null (belongs-to)
1563
- state.hasReceivedData -> true
1564
- hasDematerializedInverse -> false
1565
- state.isStale -> false
1566
- allInverseRecordsAreLoaded -> run-check-to-determine
1567
- IF contains only links
1568
- state.isStale -> true
1569
- */
1570
- relationship.state.hasFailedLoadAttempt = false;
1571
- if (hasRelationshipDataProperty) {
1572
- const relationshipIsEmpty = payload.data === null || Array.isArray(payload.data) && payload.data.length === 0;
1573
-
1574
- // we don't need to notify here as the update op we pushed in above will notify once
1575
- // membership is in the correct state.
1576
- relationship.state.hasReceivedData = true;
1577
- relationship.state.isStale = false;
1578
- relationship.state.hasDematerializedInverse = false;
1579
- relationship.state.isEmpty = relationshipIsEmpty;
1580
- } else if (hasUpdatedLink) {
1581
- // only notify stale if we have not previously received membership data.
1582
- // within this same transaction
1583
- // this prevents refetching when only one side of the relationship in the
1584
- // payload contains the info while the other side contains just a link
1585
- // this only works when the side with just a link is a belongsTo, as we
1586
- // don't know if a hasMany has full information or not.
1587
- // see #7049 for context.
1588
- if (isCollection || !relationship.state.hasReceivedData || isStaleTransaction(relationship.transactionRef, graph._transaction)) {
1589
- relationship.state.isStale = true;
1590
- notifyChange(graph, relationship);
1591
- } else {
1592
- relationship.state.isStale = false;
1593
- }
1594
- }
950
+ const relationship = graph.get(op.record, op.field);
951
+ const { definition, identifier } = relationship;
952
+ const { isCollection } = definition;
953
+ const payload = op.value;
954
+ let hasRelationshipDataProperty = false;
955
+ let hasUpdatedLink = false;
956
+ if (payload.meta) relationship.meta = payload.meta;
957
+ if (payload.data !== void 0) {
958
+ hasRelationshipDataProperty = true;
959
+ if (isCollection) {
960
+ if (payload.data === null) payload.data = [];
961
+ const cache = graph.store.cacheKeyManager;
962
+ graph.update({
963
+ op: "replaceRelatedRecords",
964
+ record: identifier,
965
+ field: op.field,
966
+ value: upgradeIdentifiers(payload.data, cache)
967
+ }, true);
968
+ } else graph.update({
969
+ op: "replaceRelatedRecord",
970
+ record: identifier,
971
+ field: op.field,
972
+ value: payload.data ? graph.store.cacheKeyManager.upgradeIdentifier(payload.data) : null
973
+ }, true);
974
+ }
975
+ if (payload.links) {
976
+ const originalLinks = relationship.links;
977
+ relationship.links = payload.links;
978
+ if (payload.links.related) {
979
+ const relatedLink = _normalizeLink(payload.links.related);
980
+ const currentLink = originalLinks && originalLinks.related ? _normalizeLink(originalLinks.related) : null;
981
+ const currentLinkHref = currentLink ? currentLink.href : null;
982
+ if (relatedLink && relatedLink.href && relatedLink.href !== currentLinkHref) hasUpdatedLink = true;
983
+ }
984
+ }
985
+ relationship.state.hasFailedLoadAttempt = false;
986
+ if (hasRelationshipDataProperty) {
987
+ const relationshipIsEmpty = payload.data === null || Array.isArray(payload.data) && payload.data.length === 0;
988
+ relationship.state.hasReceivedData = true;
989
+ relationship.state.hasReceivedRemoteData = true;
990
+ relationship.state.isStale = false;
991
+ relationship.state.hasDematerializedInverse = false;
992
+ relationship.state.isEmpty = relationshipIsEmpty;
993
+ } else if (hasUpdatedLink) {
994
+ if (isCollection || !relationship.state.hasReceivedData || isStaleTransaction(relationship.transactionRef, graph._transaction)) {
995
+ relationship.state.isStale = true;
996
+ notifyChange(graph, relationship);
997
+ } else relationship.state.isStale = false;
998
+ }
1595
999
  }
1596
1000
  function isStaleTransaction(relationshipTransactionId, graphTransactionId) {
1597
- return relationshipTransactionId === 0 ||
1598
- // relationship has never notified
1599
- graphTransactionId === null ||
1600
- // we are not in a transaction
1601
- relationshipTransactionId < graphTransactionId // we are not part of the current transaction
1602
- ;
1001
+ return relationshipTransactionId === 0 || graphTransactionId === null || relationshipTransactionId < graphTransactionId;
1603
1002
  }
1604
1003
  function upgradeIdentifiers(arr, cache) {
1605
- for (let i = 0; i < arr.length; i++) {
1606
- arr[i] = cache.upgradeIdentifier(arr[i]);
1607
- }
1608
- return arr;
1609
- }
1610
- const Graphs = getOrSetGlobal('Graphs', new Map());
1611
- /*
1612
- * Graph acts as the cache for relationship data. It allows for
1613
- * us to ask about and update relationships for a given Identifier
1614
- * without requiring other objects for that Identifier to be
1615
- * instantiated (such as `RecordData` or a `Record`)
1616
- *
1617
- * This also allows for us to make more substantive changes to relationships
1618
- * with increasingly minor alterations to other portions of the internals
1619
- * over time.
1620
- *
1621
- * The graph is made up of nodes and edges. Each unique identifier gets
1622
- * its own node, which is a dictionary with a list of that node's edges
1623
- * (or connections) to other nodes. In `Model` terms, a node represents a
1624
- * record instance, with each key (an edge) in the dictionary correlating
1625
- * to either a `hasMany` or `belongsTo` field on that record instance.
1626
- *
1627
- * The value for each key, or `edge` is the identifier(s) the node relates
1628
- * to in the graph from that key.
1629
- */
1630
- class Graph {
1631
- constructor(store) {
1632
- this._definitionCache = Object.create(null);
1633
- this._metaCache = Object.create(null);
1634
- this._potentialPolymorphicTypes = Object.create(null);
1635
- this.identifiers = new Map();
1636
- this.store = store;
1637
- this._realStore = store._store;
1638
- this.isDestroyed = false;
1639
- this._willSyncRemote = false;
1640
- this._willSyncLocal = false;
1641
- this._pushedUpdates = {
1642
- belongsTo: undefined,
1643
- hasMany: undefined,
1644
- deletions: []
1645
- };
1646
- this._updatedRelationships = new Set();
1647
- this._transaction = null;
1648
- this._removing = null;
1649
- this.silenceNotifications = false;
1650
- }
1651
- has(resourceKey, propertyName) {
1652
- const relationships = this.identifiers.get(resourceKey);
1653
- if (!relationships) {
1654
- return false;
1655
- }
1656
- return relationships[propertyName] !== undefined;
1657
- }
1658
- getDefinition(resourceKey, propertyName) {
1659
- let defs = this._metaCache[resourceKey.type];
1660
- let meta = defs?.[propertyName];
1661
- if (!meta) {
1662
- const info = /*#__NOINLINE__*/upgradeDefinition(this, resourceKey, propertyName);
1663
-
1664
- // if (info.rhs_definition?.kind === 'implicit') {
1665
- // we should possibly also do this
1666
- // but it would result in being extremely permissive for other relationships by accident
1667
- // this.registerPolymorphicType(info.rhs_baseModelName, identifier.type);
1668
- // }
1669
-
1670
- meta = /*#__NOINLINE__*/isLHS(info, resourceKey.type, propertyName) ? info.lhs_definition : info.rhs_definition;
1671
- defs = this._metaCache[resourceKey.type] = defs || {};
1672
- defs[propertyName] = meta;
1673
- }
1674
- return meta;
1675
- }
1676
- get(resourceKey, propertyName) {
1677
- let relationships = this.identifiers.get(resourceKey);
1678
- if (!relationships) {
1679
- relationships = Object.create(null);
1680
- this.identifiers.set(resourceKey, relationships);
1681
- }
1682
- let relationship = relationships[propertyName];
1683
- if (!relationship) {
1684
- const meta = this.getDefinition(resourceKey, propertyName);
1685
- if (meta.kind === 'belongsTo') {
1686
- relationship = relationships[propertyName] = createResourceEdge(meta, resourceKey);
1687
- } else if (meta.kind === 'hasMany') {
1688
- relationship = relationships[propertyName] = createCollectionEdge(meta, resourceKey);
1689
- } else {
1690
- relationship = relationships[propertyName] = createImplicitEdge(meta, resourceKey);
1691
- }
1692
- }
1693
- return relationship;
1694
- }
1695
- getData(resourceKey, propertyName) {
1696
- const relationship = this.get(resourceKey, propertyName);
1697
- if (isBelongsTo(relationship)) {
1698
- return legacyGetResourceRelationshipData(relationship, false);
1699
- }
1700
- return legacyGetCollectionRelationshipData(relationship, false);
1701
- }
1702
- getRemoteData(resourceKey, propertyName) {
1703
- const relationship = this.get(resourceKey, propertyName);
1704
- if (isBelongsTo(relationship)) {
1705
- return legacyGetResourceRelationshipData(relationship, true);
1706
- }
1707
- return legacyGetCollectionRelationshipData(relationship, true);
1708
- }
1709
-
1710
- /*
1711
- * Allows for the graph to dynamically discover polymorphic connections
1712
- * without needing to walk prototype chains.
1713
- *
1714
- * Used by edges when an added `type` does not match the expected `type`
1715
- * for that edge.
1716
- *
1717
- * Currently we assert before calling this. For a public API we will want
1718
- * to call out to the schema manager to ask if we should consider these
1719
- * types as equivalent for a given relationship.
1720
- */
1721
- registerPolymorphicType(type1, type2) {
1722
- const typeCache = this._potentialPolymorphicTypes;
1723
- let t1 = typeCache[type1];
1724
- if (!t1) {
1725
- t1 = typeCache[type1] = Object.create(null);
1726
- }
1727
- t1[type2] = true;
1728
- let t2 = typeCache[type2];
1729
- if (!t2) {
1730
- t2 = typeCache[type2] = Object.create(null);
1731
- }
1732
- t2[type1] = true;
1733
- }
1734
- isReleasable(resourceKey) {
1735
- const relationships = this.identifiers.get(resourceKey);
1736
- if (!relationships) {
1737
- return true;
1738
- }
1739
- const keys = Object.keys(relationships);
1740
- for (let i = 0; i < keys.length; i++) {
1741
- const relationship = relationships[keys[i]];
1742
- // account for previously unloaded relationships
1743
- // typically from a prior deletion of a record that pointed to this one implicitly
1744
- if (relationship === undefined) {
1745
- continue;
1746
- }
1747
- if (relationship.definition.inverseIsAsync && !checkIfNew(this._realStore, resourceKey)) {
1748
- return false;
1749
- }
1750
- }
1751
- return true;
1752
- }
1753
- unload(resourceKey, silenceNotifications) {
1754
- const relationships = this.identifiers.get(resourceKey);
1755
- if (relationships) {
1756
- // cleans up the graph but retains some nodes
1757
- // to allow for rematerialization
1758
- Object.keys(relationships).forEach(key => {
1759
- const rel = relationships[key];
1760
- if (!rel) {
1761
- return;
1762
- }
1763
- /*#__NOINLINE__*/
1764
- destroyRelationship(this, rel, silenceNotifications);
1765
- if (/*#__NOINLINE__*/isImplicit(rel)) {
1766
- // @ts-expect-error
1767
- relationships[key] = undefined;
1768
- }
1769
- });
1770
- }
1771
- }
1772
- _isDirty(resourceKey, field) {
1773
- const relationships = this.identifiers.get(resourceKey);
1774
- if (!relationships) {
1775
- return false;
1776
- }
1777
- const relationship = relationships[field];
1778
- if (!relationship) {
1779
- return false;
1780
- }
1781
- if (isBelongsTo(relationship)) {
1782
- return relationship.localState !== relationship.remoteState;
1783
- } else if (isHasMany(relationship)) {
1784
- const hasAdditions = relationship.additions !== null && relationship.additions.size > 0;
1785
- const hasRemovals = relationship.removals !== null && relationship.removals.size > 0;
1786
- return hasAdditions || hasRemovals || isReordered(relationship);
1787
- }
1788
- return false;
1789
- }
1790
- getChanged(resourceKey) {
1791
- const relationships = this.identifiers.get(resourceKey);
1792
- const changed = new Map();
1793
- if (!relationships) {
1794
- return changed;
1795
- }
1796
- const keys = Object.keys(relationships);
1797
- for (let i = 0; i < keys.length; i++) {
1798
- const field = keys[i];
1799
- const relationship = relationships[field];
1800
- if (!relationship) {
1801
- continue;
1802
- }
1803
- if (isBelongsTo(relationship)) {
1804
- if (relationship.localState !== relationship.remoteState) {
1805
- changed.set(field, {
1806
- kind: 'resource',
1807
- remoteState: relationship.remoteState,
1808
- localState: relationship.localState
1809
- });
1810
- }
1811
- } else if (isHasMany(relationship)) {
1812
- const hasAdditions = relationship.additions !== null && relationship.additions.size > 0;
1813
- const hasRemovals = relationship.removals !== null && relationship.removals.size > 0;
1814
- const reordered = isReordered(relationship);
1815
- if (hasAdditions || hasRemovals || reordered) {
1816
- changed.set(field, {
1817
- kind: 'collection',
1818
- additions: new Set(relationship.additions),
1819
- removals: new Set(relationship.removals),
1820
- remoteState: relationship.remoteState,
1821
- localState: legacyGetCollectionRelationshipData(relationship, false).data || [],
1822
- reordered
1823
- });
1824
- }
1825
- }
1826
- }
1827
- return changed;
1828
- }
1829
- hasChanged(resourceKey) {
1830
- const relationships = this.identifiers.get(resourceKey);
1831
- if (!relationships) {
1832
- return false;
1833
- }
1834
- const keys = Object.keys(relationships);
1835
- for (let i = 0; i < keys.length; i++) {
1836
- if (this._isDirty(resourceKey, keys[i])) {
1837
- return true;
1838
- }
1839
- }
1840
- return false;
1841
- }
1842
- rollback(resourceKey) {
1843
- const relationships = this.identifiers.get(resourceKey);
1844
- const changed = [];
1845
- if (!relationships) {
1846
- return changed;
1847
- }
1848
- const keys = Object.keys(relationships);
1849
- for (let i = 0; i < keys.length; i++) {
1850
- const field = keys[i];
1851
- const relationship = relationships[field];
1852
- if (!relationship) {
1853
- continue;
1854
- }
1855
- if (this._isDirty(resourceKey, field)) {
1856
- rollbackRelationship(this, resourceKey, field, relationship);
1857
- changed.push(field);
1858
- }
1859
- }
1860
- return changed;
1861
- }
1862
- remove(resourceKey) {
1863
- this._removing = resourceKey;
1864
- this.unload(resourceKey);
1865
- this.identifiers.delete(resourceKey);
1866
- this._removing = null;
1867
- }
1868
-
1869
- /*
1870
- * Remote state changes
1871
- */
1872
- push(op) {
1873
- if (op.op === 'deleteRecord') {
1874
- this._pushedUpdates.deletions.push(op);
1875
- } else {
1876
- const definition = this.getDefinition(op.record, op.field);
1877
- addPending(this._pushedUpdates, definition, op);
1878
- }
1879
- if (!this._willSyncRemote) {
1880
- this._willSyncRemote = true;
1881
- const store = getStore(this.store);
1882
- if (!store._cbs) {
1883
- store._run(() => this._flushRemoteQueue());
1884
- } else {
1885
- store._schedule('coalesce', () => this._flushRemoteQueue());
1886
- }
1887
- }
1888
- }
1889
-
1890
- /*
1891
- * Local state changes
1892
- */
1893
-
1894
- update(op, isRemote = false) {
1895
- switch (op.op) {
1896
- case 'mergeIdentifiers':
1897
- {
1898
- const relationships = this.identifiers.get(op.record);
1899
- if (relationships) {
1900
- /*#__NOINLINE__*/mergeIdentifier(this, op, relationships);
1901
- }
1902
- break;
1903
- }
1904
- case 'update':
1905
- case 'updateRelationship':
1906
- /*#__NOINLINE__*/updateRelationshipOperation(this, op);
1907
- break;
1908
- case 'deleteRecord':
1909
- {
1910
- const identifier = op.record;
1911
- const relationships = this.identifiers.get(identifier);
1912
- if (relationships) {
1913
- Object.keys(relationships).forEach(key => {
1914
- const rel = relationships[key];
1915
- if (!rel) {
1916
- return;
1917
- }
1918
- // works together with the has check
1919
- // @ts-expect-error
1920
- relationships[key] = undefined;
1921
- /*#__NOINLINE__*/
1922
- removeCompletelyFromInverse(this, rel);
1923
- });
1924
- this.identifiers.delete(identifier);
1925
- }
1926
- break;
1927
- }
1928
- case 'replaceRelatedRecord':
1929
- /*#__NOINLINE__*/replaceRelatedRecord(this, op, isRemote);
1930
- break;
1931
- case 'add':
1932
- /*#__NOINLINE__*/addToRelatedRecords(this, op, isRemote);
1933
- break;
1934
- case 'remove':
1935
- /*#__NOINLINE__*/removeFromRelatedRecords(this, op, isRemote);
1936
- break;
1937
- case 'replaceRelatedRecords':
1938
- /*#__NOINLINE__*/replaceRelatedRecords(this, op, isRemote);
1939
- break;
1940
- }
1941
- }
1942
- _scheduleLocalSync(relationship) {
1943
- this._updatedRelationships.add(relationship);
1944
- if (!this._willSyncLocal) {
1945
- this._willSyncLocal = true;
1946
- getStore(this.store)._schedule('sync', () => this._flushLocalQueue());
1947
- }
1948
- }
1949
- _flushRemoteQueue() {
1950
- if (!this._willSyncRemote) {
1951
- return;
1952
- }
1953
- let transactionRef = peekTransient('transactionRef') ?? 0;
1954
- this._transaction = ++transactionRef;
1955
- setTransient('transactionRef', transactionRef);
1956
- this._willSyncRemote = false;
1957
- const updates = this._pushedUpdates;
1958
- const {
1959
- deletions,
1960
- hasMany,
1961
- belongsTo
1962
- } = updates;
1963
- updates.deletions = [];
1964
- updates.hasMany = undefined;
1965
- updates.belongsTo = undefined;
1966
- for (let i = 0; i < deletions.length; i++) {
1967
- this.update(deletions[i], true);
1968
- }
1969
- if (hasMany) {
1970
- flushPending(this, hasMany);
1971
- }
1972
- if (belongsTo) {
1973
- flushPending(this, belongsTo);
1974
- }
1975
- this._transaction = null;
1976
- }
1977
- _addToTransaction(relationship) {
1978
- relationship.transactionRef = this._transaction;
1979
- }
1980
- _flushLocalQueue() {
1981
- if (!this._willSyncLocal) {
1982
- return;
1983
- }
1984
- if (this.silenceNotifications) {
1985
- this.silenceNotifications = false;
1986
- this._updatedRelationships = new Set();
1987
- return;
1988
- }
1989
- this._willSyncLocal = false;
1990
- const updated = this._updatedRelationships;
1991
- this._updatedRelationships = new Set();
1992
- updated.forEach(rel => notifyChange(this, rel));
1993
- }
1994
- destroy() {
1995
- Graphs.delete(this.store);
1996
- this.identifiers.clear();
1997
- this.store = null;
1998
- this.isDestroyed = true;
1999
- }
1004
+ for (let i = 0; i < arr.length; i++) arr[i] = cache.upgradeIdentifier(arr[i]);
1005
+ return arr;
2000
1006
  }
1007
+
1008
+ //#endregion
1009
+ //#region src/graph/-private/graph.ts
1010
+ /**
1011
+ * A single edge (relationship field) tracked by the {@link Graph} for a resource. Which kind of edge
1012
+ * a field is depends on its relationship definition:
1013
+ *
1014
+ * - {@link ResourceEdge}: the "one" side of a `belongsTo`/resource relationship
1015
+ * - {@link CollectionEdge}: the "many" side of a `hasMany`/collection relationship
1016
+ * - {@link ImplicitEdge}: a synthesized inverse used when a relationship has no field defined for its inverse
1017
+ */
1018
+ const Graphs = getOrSetGlobal("Graphs", /* @__PURE__ */ new Map());
1019
+ /**
1020
+ * Graph is WarpDrive's internal, private cache of all resource relationships.
1021
+ *
1022
+ * It acts as the cache for relationship data, allowing us to ask about and update
1023
+ * relationships for a given Identifier without requiring other objects for that
1024
+ * Identifier to be instantiated (such as `RecordData` or a `Record`).
1025
+ *
1026
+ * This also allows for us to make more substantive changes to relationships
1027
+ * with increasingly minor alterations to other portions of the internals
1028
+ * over time.
1029
+ *
1030
+ * The graph is made up of nodes and edges. Each unique identifier gets
1031
+ * its own node, which is a dictionary with a list of that node's edges
1032
+ * (or connections) to other nodes. In `Model` terms, a node represents a
1033
+ * record instance, with each key (an edge) in the dictionary correlating
1034
+ * to either a `hasMany` or `belongsTo` field on that record instance.
1035
+ *
1036
+ * The value for each key, or `edge` is the identifier(s) the node relates
1037
+ * to in the graph from that key.
1038
+ */
1039
+ var Graph = class {
1040
+ /**
1041
+ * Cache of resolved {@link EdgeDefinition}s (the paired LHS/RHS relationship definitions
1042
+ * for a `type` + `key`), keyed by resource `type` and then field name. Populated lazily by
1043
+ * `upgradeDefinition` and consulted by {@link Graph.getDefinition}.
1044
+ */
1045
+ /**
1046
+ * Cache of the resolved {@link UpgradedMeta} for a given resource `type` and field name, as
1047
+ * returned by {@link Graph.getDefinition}. Derived from `_definitionCache` the first time a
1048
+ * field's definition is looked up.
1049
+ */
1050
+ /**
1051
+ * Tracks pairs of resource types that have been discovered to be polymorphically related, so
1052
+ * that the graph can recognize this connection without needing to walk prototype/schema
1053
+ * inheritance chains. Populated by {@link Graph.registerPolymorphicType}.
1054
+ */
1055
+ /**
1056
+ * The nodes of the graph. Each {@link ResourceKey} maps to a dictionary of its relationship
1057
+ * fields (edges), keyed by field name.
1058
+ */
1059
+ /**
1060
+ * The `CacheCapabilitiesManager` (store wrapper) this graph was created for. Used to schedule
1061
+ * work and to notify of relationship changes.
1062
+ */
1063
+ /**
1064
+ * The real underlying `Store` instance backing {@link Graph.store}, used for capabilities
1065
+ * (such as checking whether a resource is a new/unsaved client-side record) that are not
1066
+ * exposed on `CacheCapabilitiesManager`.
1067
+ */
1068
+ /**
1069
+ * `true` once {@link Graph.destroy} has been called. After this point the graph should no
1070
+ * longer be used.
1071
+ */
1072
+ /**
1073
+ * `true` when a flush of the remote-update queue has been scheduled but has not yet run.
1074
+ * Set by {@link Graph.push} and cleared by {@link Graph._flushRemoteQueue}.
1075
+ */
1076
+ /**
1077
+ * `true` when a flush of the local-update notification queue has been scheduled but has not
1078
+ * yet run. Set by {@link Graph._scheduleLocalSync} and cleared by
1079
+ * {@link Graph._flushLocalQueue}.
1080
+ */
1081
+ /**
1082
+ * When `true`, the next scheduled local sync will be dropped without notifying subscribers of
1083
+ * the relationships that changed. Consumed and reset by {@link Graph._flushLocalQueue}.
1084
+ */
1085
+ /**
1086
+ * Remote relationship operations and record deletions that have been pushed via
1087
+ * {@link Graph.push} but not yet flushed into the graph by {@link Graph._flushRemoteQueue}.
1088
+ */
1089
+ /**
1090
+ * The set of {@link CollectionEdge}s that have local changes pending notification, scheduled
1091
+ * via {@link Graph._scheduleLocalSync} and drained by {@link Graph._flushLocalQueue}.
1092
+ */
1093
+ /**
1094
+ * The identifier of the current remote-update transaction, or `null` when no transaction is
1095
+ * in progress. Set by {@link Graph._flushRemoteQueue} and stamped onto edges via
1096
+ * {@link Graph._addToTransaction}.
1097
+ */
1098
+ /**
1099
+ * The {@link ResourceKey} currently being removed via {@link Graph.remove}, or `null`. Used to
1100
+ * guard against re-entrant removal of the same resource.
1101
+ */
1102
+ constructor(store) {
1103
+ this._definitionCache = Object.create(null);
1104
+ this._metaCache = Object.create(null);
1105
+ this._potentialPolymorphicTypes = Object.create(null);
1106
+ this.identifiers = /* @__PURE__ */ new Map();
1107
+ this.store = store;
1108
+ this._realStore = store._store;
1109
+ this.isDestroyed = false;
1110
+ this._willSyncRemote = false;
1111
+ this._willSyncLocal = false;
1112
+ this._pushedUpdates = {
1113
+ belongsTo: void 0,
1114
+ hasMany: void 0,
1115
+ deletions: []
1116
+ };
1117
+ this._updatedRelationships = /* @__PURE__ */ new Set();
1118
+ this._transaction = null;
1119
+ this._removing = null;
1120
+ this.silenceNotifications = false;
1121
+ }
1122
+ /**
1123
+ * Returns `true` if an edge has already been created in the graph for the given resource and
1124
+ * field name, without creating one if it does not yet exist.
1125
+ */
1126
+ has(resourceKey, propertyName) {
1127
+ const relationships = this.identifiers.get(resourceKey);
1128
+ if (!relationships) return false;
1129
+ return relationships[propertyName] !== void 0;
1130
+ }
1131
+ /**
1132
+ * Returns the resolved {@link UpgradedMeta} (relationship definition) for the given resource
1133
+ * `type` and field name, computing and caching it via `upgradeDefinition` on first access.
1134
+ */
1135
+ getDefinition(resourceKey, propertyName) {
1136
+ let defs = this._metaCache[resourceKey.type];
1137
+ let meta = defs?.[propertyName];
1138
+ if (!meta) {
1139
+ const info = upgradeDefinition(this, resourceKey, propertyName);
1140
+ meta = isLHS(info, resourceKey.type, propertyName) ? info.lhs_definition : info.rhs_definition;
1141
+ defs = this._metaCache[resourceKey.type] = defs || {};
1142
+ defs[propertyName] = meta;
1143
+ }
1144
+ return meta;
1145
+ }
1146
+ /**
1147
+ * Returns the {@link GraphEdge} for the given resource and field name, creating it (as a
1148
+ * {@link ResourceEdge}, {@link CollectionEdge}, or {@link ImplicitEdge} depending on the
1149
+ * field's kind) if it does not already exist.
1150
+ */
1151
+ get(resourceKey, propertyName) {
1152
+ let relationships = this.identifiers.get(resourceKey);
1153
+ if (!relationships) {
1154
+ relationships = Object.create(null);
1155
+ this.identifiers.set(resourceKey, relationships);
1156
+ }
1157
+ let relationship = relationships[propertyName];
1158
+ if (!relationship) {
1159
+ const meta = this.getDefinition(resourceKey, propertyName);
1160
+ if (meta.kind === "belongsTo") relationship = relationships[propertyName] = createResourceEdge(meta, resourceKey);
1161
+ else if (meta.kind === "hasMany") relationship = relationships[propertyName] = createCollectionEdge(meta, resourceKey);
1162
+ else relationship = relationships[propertyName] = createImplicitEdge(meta, resourceKey);
1163
+ }
1164
+ return relationship;
1165
+ }
1166
+ /**
1167
+ * Returns the local (current, potentially dirty) JSON:API relationship data for a `belongsTo`
1168
+ * or `hasMany` field. Asserts if the field is an implicit relationship.
1169
+ */
1170
+ getData(resourceKey, propertyName) {
1171
+ const relationship = this.get(resourceKey, propertyName);
1172
+ if (isBelongsTo(relationship)) return legacyGetResourceRelationshipData(relationship, false);
1173
+ return legacyGetCollectionRelationshipData(relationship, false);
1174
+ }
1175
+ /**
1176
+ * Returns the remote (last known server-confirmed) JSON:API relationship data for a
1177
+ * `belongsTo` or `hasMany` field. Asserts if the field is an implicit relationship.
1178
+ */
1179
+ getRemoteData(resourceKey, propertyName) {
1180
+ const relationship = this.get(resourceKey, propertyName);
1181
+ if (isBelongsTo(relationship)) return legacyGetResourceRelationshipData(relationship, true);
1182
+ return legacyGetCollectionRelationshipData(relationship, true);
1183
+ }
1184
+ /**
1185
+ * Allows for the graph to dynamically discover polymorphic connections
1186
+ * without needing to walk prototype chains.
1187
+ *
1188
+ * Used by edges when an added `type` does not match the expected `type`
1189
+ * for that edge.
1190
+ *
1191
+ * Currently we assert before calling this. For a public API we will want
1192
+ * to call out to the schema manager to ask if we should consider these
1193
+ * types as equivalent for a given relationship.
1194
+ */
1195
+ registerPolymorphicType(type1, type2) {
1196
+ const typeCache = this._potentialPolymorphicTypes;
1197
+ let t1 = typeCache[type1];
1198
+ if (!t1) t1 = typeCache[type1] = Object.create(null);
1199
+ t1[type2] = true;
1200
+ let t2 = typeCache[type2];
1201
+ if (!t2) t2 = typeCache[type2] = Object.create(null);
1202
+ t2[type1] = true;
1203
+ }
1204
+ /**
1205
+ * Determines whether a resource is safe to fully release from memory. A resource is not
1206
+ * releasable if it has a relationship whose inverse is async and the resource itself is not a
1207
+ * new (unsaved, client-only) record, since the inverse may still need to reference it.
1208
+ */
1209
+ isReleasable(resourceKey) {
1210
+ const relationships = this.identifiers.get(resourceKey);
1211
+ if (!relationships) return true;
1212
+ const keys = Object.keys(relationships);
1213
+ for (let i = 0; i < keys.length; i++) {
1214
+ const relationship = relationships[keys[i]];
1215
+ if (relationship === void 0) continue;
1216
+ if (relationship.definition.inverseIsAsync && !checkIfNew(this._realStore, resourceKey)) return false;
1217
+ }
1218
+ return true;
1219
+ }
1220
+ /**
1221
+ * Cleans up all relationships for a resource that is being dematerialized, notifying inverses
1222
+ * so they can update their own state. This retains the graph's node structure (so it can be
1223
+ * rematerialized later) except for implicit edges, which are discarded outright.
1224
+ */
1225
+ unload(resourceKey, silenceNotifications) {
1226
+ const relationships = this.identifiers.get(resourceKey);
1227
+ if (relationships) Object.keys(relationships).forEach((key) => {
1228
+ const rel = relationships[key];
1229
+ if (!rel) return;
1230
+ destroyRelationship(this, rel, silenceNotifications);
1231
+ if (isImplicit(rel)) relationships[key] = void 0;
1232
+ });
1233
+ }
1234
+ /**
1235
+ * Returns `true` if the given field on a resource has uncommitted local changes: for a
1236
+ * `belongsTo` this means local and remote state differ; for a `hasMany` this means there are
1237
+ * pending additions, removals, or a reordering relative to remote state.
1238
+ */
1239
+ _isDirty(resourceKey, field) {
1240
+ const relationships = this.identifiers.get(resourceKey);
1241
+ if (!relationships) return false;
1242
+ const relationship = relationships[field];
1243
+ if (!relationship) return false;
1244
+ if (isBelongsTo(relationship)) return relationship.localState !== relationship.remoteState;
1245
+ else if (isHasMany(relationship)) {
1246
+ const hasAdditions = relationship.additions !== null && relationship.additions.size > 0;
1247
+ const hasRemovals = relationship.removals !== null && relationship.removals.size > 0;
1248
+ return hasAdditions || hasRemovals || isReordered(relationship);
1249
+ }
1250
+ return false;
1251
+ }
1252
+ /**
1253
+ * Builds a map, keyed by field name, of every relationship on a resource that has uncommitted
1254
+ * local changes, describing each change as a {@link RelationshipDiff}.
1255
+ */
1256
+ getChanged(resourceKey) {
1257
+ const relationships = this.identifiers.get(resourceKey);
1258
+ const changed = /* @__PURE__ */ new Map();
1259
+ if (!relationships) return changed;
1260
+ const keys = Object.keys(relationships);
1261
+ for (let i = 0; i < keys.length; i++) {
1262
+ const field = keys[i];
1263
+ const relationship = relationships[field];
1264
+ if (!relationship) continue;
1265
+ if (isBelongsTo(relationship)) {
1266
+ if (relationship.localState !== relationship.remoteState) changed.set(field, {
1267
+ kind: "resource",
1268
+ remoteState: relationship.remoteState,
1269
+ localState: relationship.localState
1270
+ });
1271
+ } else if (isHasMany(relationship)) {
1272
+ const hasAdditions = relationship.additions !== null && relationship.additions.size > 0;
1273
+ const hasRemovals = relationship.removals !== null && relationship.removals.size > 0;
1274
+ const reordered = isReordered(relationship);
1275
+ if (hasAdditions || hasRemovals || reordered) changed.set(field, {
1276
+ kind: "collection",
1277
+ additions: new Set(relationship.additions),
1278
+ removals: new Set(relationship.removals),
1279
+ remoteState: relationship.remoteState,
1280
+ localState: legacyGetCollectionRelationshipData(relationship, false).data || [],
1281
+ reordered
1282
+ });
1283
+ }
1284
+ }
1285
+ return changed;
1286
+ }
1287
+ /**
1288
+ * Returns `true` if any relationship field on the resource has uncommitted local changes.
1289
+ */
1290
+ hasChanged(resourceKey) {
1291
+ const relationships = this.identifiers.get(resourceKey);
1292
+ if (!relationships) return false;
1293
+ const keys = Object.keys(relationships);
1294
+ for (let i = 0; i < keys.length; i++) if (this._isDirty(resourceKey, keys[i])) return true;
1295
+ return false;
1296
+ }
1297
+ /**
1298
+ * Reverts all locally dirty relationships on a resource back to their remote state, returning
1299
+ * the list of field names that were rolled back.
1300
+ */
1301
+ rollback(resourceKey) {
1302
+ const relationships = this.identifiers.get(resourceKey);
1303
+ const changed = [];
1304
+ if (!relationships) return changed;
1305
+ const keys = Object.keys(relationships);
1306
+ for (let i = 0; i < keys.length; i++) {
1307
+ const field = keys[i];
1308
+ const relationship = relationships[field];
1309
+ if (!relationship) continue;
1310
+ if (this._isDirty(resourceKey, field)) {
1311
+ rollbackRelationship(this, resourceKey, field, relationship);
1312
+ changed.push(field);
1313
+ }
1314
+ }
1315
+ return changed;
1316
+ }
1317
+ /**
1318
+ * Fully removes a resource and all of its relationships from the graph, unloading its edges
1319
+ * first so inverses are properly notified/cleaned up.
1320
+ */
1321
+ remove(resourceKey) {
1322
+ this._removing = resourceKey;
1323
+ this.unload(resourceKey);
1324
+ this.identifiers.delete(resourceKey);
1325
+ this._removing = null;
1326
+ }
1327
+ /**
1328
+ * Queues a remote relationship operation (or record deletion) to be applied to the graph.
1329
+ * Pushed operations are batched and flushed asynchronously (see {@link Graph._flushRemoteQueue})
1330
+ * rather than applied immediately.
1331
+ */
1332
+ push(op) {
1333
+ if (op.op === "deleteRecord") this._pushedUpdates.deletions.push(op);
1334
+ else {
1335
+ const definition = this.getDefinition(op.record, op.field);
1336
+ addPending(this._pushedUpdates, definition, op);
1337
+ }
1338
+ if (!this._willSyncRemote) {
1339
+ this._willSyncRemote = true;
1340
+ const store = getStore(this.store);
1341
+ if (!store._cbs) store._run(() => this._flushRemoteQueue());
1342
+ else store._schedule("coalesce", () => this._flushRemoteQueue());
1343
+ }
1344
+ }
1345
+ /**
1346
+ * Applies a remote relationship operation or identifier merge directly to the graph. Pass
1347
+ * `isRemote: true` to indicate the operation represents confirmed server state rather than a
1348
+ * local (client-side) change.
1349
+ */
1350
+ /**
1351
+ * Applies a local (client-side) relationship operation directly to the graph, such as adding,
1352
+ * removing, or replacing related records before it has been confirmed by the server.
1353
+ */
1354
+ /**
1355
+ * Implementation for both the remote and local {@link Graph.update} overloads: dispatches the
1356
+ * given operation (`mergeIdentifiers`, `updateRelationship`, `deleteRecord`, `add`, `remove`,
1357
+ * `replaceRelatedRecord`, or `replaceRelatedRecords`) to the appropriate handler.
1358
+ */
1359
+ update(op, isRemote = false) {
1360
+ switch (op.op) {
1361
+ case "mergeIdentifiers": {
1362
+ const relationships = this.identifiers.get(op.record);
1363
+ if (relationships) mergeIdentifier(this, op, relationships);
1364
+ break;
1365
+ }
1366
+ case "update":
1367
+ case "updateRelationship":
1368
+ updateRelationshipOperation(this, op);
1369
+ break;
1370
+ case "deleteRecord": {
1371
+ const identifier = op.record;
1372
+ const relationships = this.identifiers.get(identifier);
1373
+ if (relationships) {
1374
+ Object.keys(relationships).forEach((key) => {
1375
+ const rel = relationships[key];
1376
+ if (!rel) return;
1377
+ relationships[key] = void 0;
1378
+ removeCompletelyFromInverse(this, rel);
1379
+ });
1380
+ this.identifiers.delete(identifier);
1381
+ }
1382
+ break;
1383
+ }
1384
+ case "replaceRelatedRecord":
1385
+ replaceRelatedRecord(this, op, isRemote);
1386
+ break;
1387
+ case "add":
1388
+ addToRelatedRecords(this, op, isRemote);
1389
+ break;
1390
+ case "remove":
1391
+ removeFromRelatedRecords(this, op, isRemote);
1392
+ break;
1393
+ case "replaceRelatedRecords": replaceRelatedRecords(this, op, isRemote);
1394
+ }
1395
+ }
1396
+ /**
1397
+ * Marks a {@link CollectionEdge} as having local changes that need to be notified, and
1398
+ * schedules a flush of the local sync queue (see {@link Graph._flushLocalQueue}) if one is not
1399
+ * already pending.
1400
+ */
1401
+ _scheduleLocalSync(relationship) {
1402
+ this._updatedRelationships.add(relationship);
1403
+ if (!this._willSyncLocal) {
1404
+ this._willSyncLocal = true;
1405
+ getStore(this.store)._schedule("sync", () => this._flushLocalQueue());
1406
+ }
1407
+ }
1408
+ /**
1409
+ * Flushes all remote operations queued via {@link Graph.push}, applying them within a single
1410
+ * transaction (see {@link Graph._transaction} and {@link Graph._addToTransaction}) so that
1411
+ * dependent edges updated during the flush can be distinguished from those that were not.
1412
+ */
1413
+ _flushRemoteQueue() {
1414
+ if (!this._willSyncRemote) return;
1415
+ let transactionRef = peekTransient("transactionRef") ?? 0;
1416
+ this._transaction = ++transactionRef;
1417
+ setTransient("transactionRef", transactionRef);
1418
+ this._willSyncRemote = false;
1419
+ const updates = this._pushedUpdates;
1420
+ const { deletions, hasMany, belongsTo } = updates;
1421
+ updates.deletions = [];
1422
+ updates.hasMany = void 0;
1423
+ updates.belongsTo = void 0;
1424
+ for (let i = 0; i < deletions.length; i++) this.update(deletions[i], true);
1425
+ if (hasMany) flushPending(this, hasMany);
1426
+ if (belongsTo) flushPending(this, belongsTo);
1427
+ this._transaction = null;
1428
+ }
1429
+ /**
1430
+ * Stamps the current remote-update transaction id onto an edge, marking it as having been
1431
+ * touched during the in-progress {@link Graph._flushRemoteQueue} run. Must only be called while
1432
+ * a transaction is active.
1433
+ */
1434
+ _addToTransaction(relationship) {
1435
+ relationship.transactionRef = this._transaction;
1436
+ }
1437
+ /**
1438
+ * Flushes the queue of relationships scheduled via {@link Graph._scheduleLocalSync}, notifying
1439
+ * subscribers of each changed relationship — unless {@link Graph.silenceNotifications} is set,
1440
+ * in which case the queue is discarded without notifying.
1441
+ */
1442
+ _flushLocalQueue() {
1443
+ if (!this._willSyncLocal) return;
1444
+ if (this.silenceNotifications) {
1445
+ this.silenceNotifications = false;
1446
+ this._updatedRelationships = /* @__PURE__ */ new Set();
1447
+ return;
1448
+ }
1449
+ this._willSyncLocal = false;
1450
+ const updated = this._updatedRelationships;
1451
+ this._updatedRelationships = /* @__PURE__ */ new Set();
1452
+ updated.forEach((rel) => notifyChange(this, rel));
1453
+ }
1454
+ /**
1455
+ * Tears down the graph: clears all tracked identifiers/edges, removes it from the global
1456
+ * {@link Graphs} registry, and marks it as destroyed. In debug builds this also asserts that no
1457
+ * other live, non-destroying graphs remain registered, to help catch memory leaks from
1458
+ * improperly torn down store instances.
1459
+ */
1460
+ destroy() {
1461
+ Graphs.delete(this.store);
1462
+ this.identifiers.clear();
1463
+ this.store = null;
1464
+ this.isDestroyed = true;
1465
+ }
1466
+ };
2001
1467
  function flushPending(graph, ops) {
2002
- for (const type of ops.values()) {
2003
- for (const opList of type.values()) {
2004
- flushPendingList(graph, opList);
2005
- }
2006
- }
1468
+ for (const type of ops.values()) for (const opList of type.values()) flushPendingList(graph, opList);
2007
1469
  }
2008
1470
  function flushPendingList(graph, opList) {
2009
- for (let i = 0; i < opList.length; i++) {
2010
- graph.update(opList[i], true);
2011
- }
1471
+ for (let i = 0; i < opList.length; i++) graph.update(opList[i], true);
2012
1472
  }
2013
-
2014
- // Handle dematerialization for relationship `rel`. In all cases, notify the
2015
- // relationship of the dematerialization: this is done so the relationship can
2016
- // notify its inverse which needs to update state
2017
- //
2018
- // If the inverse is sync, unloading this record is treated as a client-side
2019
- // delete, so we remove the inverse records from this relationship to
2020
- // disconnect the graph. Because it's not async, we don't need to keep around
2021
- // the identifier as an id-wrapper for references
2022
1473
  function destroyRelationship(graph, rel, silenceNotifications) {
2023
- if (isImplicit(rel)) {
2024
- if (graph.isReleasable(rel.identifier)) {
2025
- /*#__NOINLINE__*/removeCompletelyFromInverse(graph, rel);
2026
- }
2027
- return;
2028
- }
2029
- const {
2030
- identifier
2031
- } = rel;
2032
- const {
2033
- inverseKey
2034
- } = rel.definition;
2035
- if (!rel.definition.inverseIsImplicit) {
2036
- /*#__NOINLINE__*/forAllRelatedIdentifiers(rel, inverseIdentifer => /*#__NOINLINE__*/notifyInverseOfDematerialization(graph, inverseIdentifer, inverseKey, identifier, silenceNotifications));
2037
- }
2038
- if (!rel.definition.inverseIsImplicit && !rel.definition.inverseIsAsync) {
2039
- rel.state.isStale = true;
2040
- /*#__NOINLINE__*/
2041
- clearRelationship(rel);
2042
-
2043
- // necessary to clear relationships in the ui from dematerialized records
2044
- // hasMany is managed by Model which calls `retreiveLatest` after
2045
- // dematerializing the resource-cache instance.
2046
- // but sync belongsTo requires this since they don't have a proxy to update.
2047
- // so we have to notify so it will "update" to null.
2048
- // we should discuss whether we still care about this, probably fine to just
2049
- // leave the ui relationship populated since the record is destroyed and
2050
- // internally we've fully cleaned up.
2051
- if (!rel.definition.isAsync && !silenceNotifications) {
2052
- /*#__NOINLINE__*/notifyChange(graph, rel);
2053
- }
2054
- }
1474
+ if (isImplicit(rel)) {
1475
+ if (graph.isReleasable(rel.identifier)) removeCompletelyFromInverse(graph, rel);
1476
+ return;
1477
+ }
1478
+ const { identifier } = rel;
1479
+ const { inverseKey } = rel.definition;
1480
+ if (!rel.definition.inverseIsImplicit) forAllRelatedIdentifiers(rel, (inverseIdentifer) => notifyInverseOfDematerialization(graph, inverseIdentifer, inverseKey, identifier, silenceNotifications));
1481
+ if (!rel.definition.inverseIsImplicit && !rel.definition.inverseIsAsync) {
1482
+ rel.state.isStale = true;
1483
+ clearRelationship(rel);
1484
+ if (!rel.definition.isAsync && !silenceNotifications) notifyChange(graph, rel);
1485
+ }
2055
1486
  }
2056
1487
  function notifyInverseOfDematerialization(graph, inverseIdentifier, inverseKey, resourceKey, silenceNotifications) {
2057
- if (!graph.has(inverseIdentifier, inverseKey)) {
2058
- return;
2059
- }
2060
- const relationship = graph.get(inverseIdentifier, inverseKey);
2061
-
2062
- // For remote members, it is possible that inverseRecordData has already been associated to
2063
- // to another record. For such cases, do not dematerialize the inverseRecordData
2064
- if (!isBelongsTo(relationship) || !relationship.localState || resourceKey === relationship.localState) {
2065
- /*#__NOINLINE__*/removeDematerializedInverse(graph, relationship, resourceKey, silenceNotifications);
2066
- }
1488
+ if (!graph.has(inverseIdentifier, inverseKey)) return;
1489
+ const relationship = graph.get(inverseIdentifier, inverseKey);
1490
+ if (!isBelongsTo(relationship) || !relationship.localState || resourceKey === relationship.localState) removeDematerializedInverse(graph, relationship, resourceKey, silenceNotifications);
2067
1491
  }
2068
1492
  function clearRelationship(relationship) {
2069
- if (isBelongsTo(relationship)) {
2070
- relationship.localState = null;
2071
- relationship.remoteState = null;
2072
- relationship.state.hasReceivedData = false;
2073
- relationship.state.isEmpty = true;
2074
- } else {
2075
- relationship.remoteMembers.clear();
2076
- relationship.remoteState = [];
2077
- relationship.additions = null;
2078
- relationship.removals = null;
2079
- relationship.localState = null;
2080
- }
1493
+ if (isBelongsTo(relationship)) {
1494
+ relationship.localState = null;
1495
+ relationship.remoteState = null;
1496
+ relationship.state.hasReceivedData = false;
1497
+ relationship.state.isEmpty = true;
1498
+ } else {
1499
+ relationship.remoteMembers.clear();
1500
+ relationship.remoteState = [];
1501
+ relationship.additions = null;
1502
+ relationship.removals = null;
1503
+ relationship.localState = null;
1504
+ }
2081
1505
  }
2082
1506
  function removeDematerializedInverse(graph, relationship, inverseIdentifier, silenceNotifications) {
2083
- if (isBelongsTo(relationship)) {
2084
- const localInverse = relationship.localState;
2085
- if (!relationship.definition.isAsync || localInverse && checkIfNew(graph._realStore, localInverse)) {
2086
- // unloading inverse of a sync relationship is treated as a client-side
2087
- // delete, so actually remove the models don't merely invalidate the cp
2088
- // cache.
2089
- // if the record being unloaded only exists on the client, we similarly
2090
- // treat it as a client side delete
2091
- if (relationship.localState === localInverse && localInverse !== null) {
2092
- relationship.localState = null;
2093
- }
2094
- if (relationship.remoteState === localInverse && localInverse !== null) {
2095
- relationship.remoteState = null;
2096
- relationship.state.hasReceivedData = true;
2097
- relationship.state.isEmpty = true;
2098
- if (relationship.localState && !checkIfNew(graph._realStore, relationship.localState)) {
2099
- relationship.localState = null;
2100
- }
2101
- }
2102
- } else {
2103
- relationship.state.hasDematerializedInverse = true;
2104
- }
2105
- if (!silenceNotifications) {
2106
- notifyChange(graph, relationship);
2107
- }
2108
- } else {
2109
- if (!relationship.definition.isAsync || inverseIdentifier && checkIfNew(graph._realStore, inverseIdentifier)) {
2110
- // unloading inverse of a sync relationship is treated as a client-side
2111
- // delete, so actually remove the models don't merely invalidate the cp
2112
- // cache.
2113
- // if the record being unloaded only exists on the client, we similarly
2114
- // treat it as a client side delete
2115
- /*#__NOINLINE__*/
2116
- removeIdentifierCompletelyFromRelationship(graph, relationship, inverseIdentifier);
2117
- } else {
2118
- relationship.state.hasDematerializedInverse = true;
2119
- }
2120
- if (!silenceNotifications) {
2121
- notifyChange(graph, relationship);
2122
- }
2123
- }
1507
+ if (isBelongsTo(relationship)) {
1508
+ const localInverse = relationship.localState;
1509
+ if (!relationship.definition.isAsync || localInverse && checkIfNew(graph._realStore, localInverse)) {
1510
+ if (relationship.localState === localInverse && localInverse !== null) relationship.localState = null;
1511
+ if (relationship.remoteState === localInverse && localInverse !== null) {
1512
+ relationship.remoteState = null;
1513
+ relationship.state.hasReceivedData = true;
1514
+ relationship.state.isEmpty = true;
1515
+ if (relationship.localState && !checkIfNew(graph._realStore, relationship.localState)) relationship.localState = null;
1516
+ }
1517
+ } else relationship.state.hasDematerializedInverse = true;
1518
+ if (!silenceNotifications) notifyChange(graph, relationship);
1519
+ } else {
1520
+ if (!relationship.definition.isAsync || inverseIdentifier && checkIfNew(graph._realStore, inverseIdentifier)) removeIdentifierCompletelyFromRelationship(graph, relationship, inverseIdentifier);
1521
+ else relationship.state.hasDematerializedInverse = true;
1522
+ if (!silenceNotifications) notifyChange(graph, relationship);
1523
+ }
2124
1524
  }
2125
1525
  function removeCompletelyFromInverse(graph, relationship) {
2126
- const {
2127
- identifier
2128
- } = relationship;
2129
- const {
2130
- inverseKey
2131
- } = relationship.definition;
2132
- forAllRelatedIdentifiers(relationship, inverseIdentifier => {
2133
- if (graph.has(inverseIdentifier, inverseKey)) {
2134
- removeIdentifierCompletelyFromRelationship(graph, graph.get(inverseIdentifier, inverseKey), identifier);
2135
- }
2136
- });
2137
- if (isBelongsTo(relationship)) {
2138
- if (!relationship.definition.isAsync) {
2139
- clearRelationship(relationship);
2140
- }
2141
- relationship.localState = null;
2142
- } else if (isHasMany(relationship)) {
2143
- if (!relationship.definition.isAsync) {
2144
- clearRelationship(relationship);
2145
- notifyChange(graph, relationship);
2146
- }
2147
- } else {
2148
- relationship.remoteMembers.clear();
2149
- relationship.localMembers.clear();
2150
- }
1526
+ const { identifier } = relationship;
1527
+ const { inverseKey } = relationship.definition;
1528
+ forAllRelatedIdentifiers(relationship, (inverseIdentifier) => {
1529
+ if (graph.has(inverseIdentifier, inverseKey)) removeIdentifierCompletelyFromRelationship(graph, graph.get(inverseIdentifier, inverseKey), identifier);
1530
+ });
1531
+ if (isBelongsTo(relationship)) {
1532
+ if (!relationship.definition.isAsync) clearRelationship(relationship);
1533
+ relationship.localState = null;
1534
+ } else if (isHasMany(relationship)) {
1535
+ if (!relationship.definition.isAsync) {
1536
+ clearRelationship(relationship);
1537
+ notifyChange(graph, relationship);
1538
+ }
1539
+ } else {
1540
+ relationship.remoteMembers.clear();
1541
+ relationship.localMembers.clear();
1542
+ }
2151
1543
  }
2152
1544
  function addPending(cache, definition, op) {
2153
- const cacheForKind = cache[definition.kind] = cache[definition.kind] || new Map();
2154
- let cacheForType = cacheForKind.get(definition.inverseType);
2155
- if (!cacheForType) {
2156
- cacheForType = new Map();
2157
- cacheForKind.set(definition.inverseType, cacheForType);
2158
- }
2159
- let cacheForField = cacheForType.get(op.field);
2160
- if (!cacheForField) {
2161
- cacheForField = [];
2162
- cacheForType.set(op.field, cacheForField);
2163
- }
2164
- cacheForField.push(op);
1545
+ const cacheForKind = cache[definition.kind] = cache[definition.kind] || /* @__PURE__ */ new Map();
1546
+ let cacheForType = cacheForKind.get(definition.inverseType);
1547
+ if (!cacheForType) {
1548
+ cacheForType = /* @__PURE__ */ new Map();
1549
+ cacheForKind.set(definition.inverseType, cacheForType);
1550
+ }
1551
+ let cacheForField = cacheForType.get(op.field);
1552
+ if (!cacheForField) {
1553
+ cacheForField = [];
1554
+ cacheForType.set(op.field, cacheForField);
1555
+ }
1556
+ cacheForField.push(op);
2165
1557
  }
2166
1558
  function isReordered(relationship) {
2167
- // if we are dirty we are never re-ordered because accessing
2168
- // the state would flush away any reordering.
2169
- if (relationship.isDirty) {
2170
- return false;
2171
- }
2172
- const {
2173
- remoteState,
2174
- localState,
2175
- additions,
2176
- removals
2177
- } = relationship;
2178
- if (localState === null) {
2179
- // the relationship has never been accessed, so it hasn't been reordered either
2180
- return false;
2181
- }
2182
- for (let i = 0, j = 0; i < remoteState.length; i++) {
2183
- const member = remoteState[i];
2184
- const localMember = localState[j];
2185
- if (member !== localMember) {
2186
- if (removals && removals.has(member)) {
2187
- // dont increment j because we want to skip this
2188
- continue;
2189
- }
2190
- if (additions && additions.has(localMember)) {
2191
- // increment j to skip this localMember
2192
- // decrement i to repeat this remoteMember
2193
- j++;
2194
- i--;
2195
- continue;
2196
- }
2197
- return true;
2198
- }
2199
-
2200
- // if we made it here, increment j
2201
- j++;
2202
- }
2203
- return false;
2204
- }
2205
-
1559
+ if (relationship.isDirty) return false;
1560
+ const { remoteState, localState, additions, removals } = relationship;
1561
+ if (localState === null) return false;
1562
+ for (let i = 0, j = 0; i < remoteState.length; i++) {
1563
+ const member = remoteState[i];
1564
+ const localMember = localState[j];
1565
+ if (member !== localMember) {
1566
+ if (removals && removals.has(member)) continue;
1567
+ if (additions && additions.has(localMember)) {
1568
+ j++;
1569
+ i--;
1570
+ continue;
1571
+ }
1572
+ return true;
1573
+ }
1574
+ j++;
1575
+ }
1576
+ return false;
1577
+ }
1578
+
1579
+ //#endregion
1580
+ //#region src/graph/-private.ts
2206
1581
  /**
2207
1582
  Provides a performance tuned normalized graph for intelligently managing relationships between resources based on identity
2208
1583
 
@@ -2210,26 +1585,31 @@ While this Graph is abstract, it currently is a private implementation required
2210
1585
 
2211
1586
  We intend to make this Graph public API after some additional iteration during the 5.x timeframe, until then all APIs should be considered experimental and unstable, not fit for direct application or 3rd party library usage.
2212
1587
 
2213
- @module
1588
+ @module
2214
1589
  */
2215
-
2216
1590
  function isStore(maybeStore) {
2217
- return maybeStore._instanceCache !== undefined;
1591
+ return maybeStore._instanceCache !== void 0;
2218
1592
  }
2219
1593
  function getWrapper(store) {
2220
- return isStore(store) ? store._instanceCache._storeWrapper : store;
1594
+ return isStore(store) ? store._instanceCache._storeWrapper : store;
2221
1595
  }
1596
+ /** Returns the {@link Graph} associated with the given store or capabilities manager, if one has been created. */
2222
1597
  function peekGraph(store) {
2223
- return Graphs.get(getWrapper(store));
1598
+ return Graphs.get(getWrapper(store));
2224
1599
  }
1600
+ /** The type of the {@link peekGraph} function. */
1601
+ /** Returns the {@link Graph} for the given store, creating (and caching) one first if it does not yet exist. */
2225
1602
  function graphFor(store) {
2226
- const wrapper = getWrapper(store);
2227
- let graph = Graphs.get(wrapper);
2228
- if (!graph) {
2229
- graph = new Graph(wrapper);
2230
- Graphs.set(wrapper, graph);
2231
- getStore(wrapper)._graph = graph;
2232
- }
2233
- return graph;
2234
- }
2235
- export { graphFor, isBelongsTo, peekGraph };
1603
+ const wrapper = getWrapper(store);
1604
+ let graph = Graphs.get(wrapper);
1605
+ if (!graph) {
1606
+ graph = new Graph(wrapper);
1607
+ Graphs.set(wrapper, graph);
1608
+ getStore(wrapper)._graph = graph;
1609
+ }
1610
+ return graph;
1611
+ }
1612
+
1613
+ //#endregion
1614
+ export { graphFor, isBelongsTo, peekGraph };
1615
+ //# sourceMappingURL=-private.js.map