@shadow-js/core 0.1.0 → 1.0.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 (387) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +2 -2
  3. package/README.md +134 -331
  4. package/dist/dev/action.d.ts +34 -0
  5. package/dist/dev/action.js +89 -0
  6. package/dist/dev/action.js.map +1 -0
  7. package/dist/dev/cache.d.ts +33 -0
  8. package/dist/dev/cache.js +61 -0
  9. package/dist/dev/cache.js.map +1 -0
  10. package/dist/dev/context.d.ts +104 -0
  11. package/dist/dev/context.js +110 -0
  12. package/dist/dev/context.js.map +1 -0
  13. package/dist/dev/effect.d.ts +120 -0
  14. package/dist/dev/effect.js +110 -0
  15. package/dist/dev/effect.js.map +1 -0
  16. package/dist/dev/env.d.ts +27 -0
  17. package/dist/dev/env.js +30 -0
  18. package/dist/dev/env.js.map +1 -0
  19. package/dist/dev/events.d.ts +26 -0
  20. package/dist/dev/events.js +32 -0
  21. package/dist/dev/events.js.map +1 -0
  22. package/dist/dev/extend/index.d.ts +329 -0
  23. package/dist/dev/extend/index.js +316 -0
  24. package/dist/dev/extend/index.js.map +1 -0
  25. package/dist/dev/external-store.d.ts +58 -0
  26. package/dist/dev/external-store.js +59 -0
  27. package/dist/dev/external-store.js.map +1 -0
  28. package/dist/dev/index.d.ts +29 -0
  29. package/dist/dev/index.js +26 -0
  30. package/dist/dev/internal/concurrency.d.ts +211 -0
  31. package/dist/dev/internal/concurrency.js +631 -0
  32. package/dist/dev/internal/concurrency.js.map +1 -0
  33. package/dist/dev/internal/devtools.d.ts +69 -0
  34. package/dist/dev/internal/devtools.js +76 -0
  35. package/dist/dev/internal/devtools.js.map +1 -0
  36. package/dist/dev/internal/diagnostics.d.ts +88 -0
  37. package/dist/dev/internal/diagnostics.js +101 -0
  38. package/dist/dev/internal/diagnostics.js.map +1 -0
  39. package/dist/dev/internal/dispose.d.ts +26 -0
  40. package/dist/dev/internal/dispose.js +48 -0
  41. package/dist/dev/internal/dispose.js.map +1 -0
  42. package/dist/dev/internal/flags.js +10 -0
  43. package/dist/dev/internal/flags.js.map +1 -0
  44. package/dist/dev/internal/graph.d.ts +231 -0
  45. package/dist/dev/internal/graph.js +577 -0
  46. package/dist/dev/internal/graph.js.map +1 -0
  47. package/dist/dev/internal/hydration-flag.d.ts +31 -0
  48. package/dist/dev/internal/hydration-flag.js +45 -0
  49. package/dist/dev/internal/hydration-flag.js.map +1 -0
  50. package/dist/dev/internal/messages.d.ts +54 -0
  51. package/dist/dev/internal/messages.js +53 -0
  52. package/dist/dev/internal/messages.js.map +1 -0
  53. package/dist/dev/internal/owner.d.ts +140 -0
  54. package/dist/dev/internal/owner.js +289 -0
  55. package/dist/dev/internal/owner.js.map +1 -0
  56. package/dist/dev/internal/queue.js +45 -0
  57. package/dist/dev/internal/queue.js.map +1 -0
  58. package/dist/dev/internal/scheduler.d.ts +111 -0
  59. package/dist/dev/internal/scheduler.js +286 -0
  60. package/dist/dev/internal/scheduler.js.map +1 -0
  61. package/dist/dev/internal/state.d.ts +139 -0
  62. package/dist/dev/internal/state.js +27 -0
  63. package/dist/dev/internal/state.js.map +1 -0
  64. package/dist/dev/internal/suspension.d.ts +41 -0
  65. package/dist/dev/internal/suspension.js +44 -0
  66. package/dist/dev/internal/suspension.js.map +1 -0
  67. package/dist/dev/internal/types.d.ts +44 -0
  68. package/dist/dev/list.d.ts +91 -0
  69. package/dist/dev/list.js +281 -0
  70. package/dist/dev/list.js.map +1 -0
  71. package/dist/dev/memo.d.ts +58 -0
  72. package/dist/dev/memo.js +40 -0
  73. package/dist/dev/memo.js.map +1 -0
  74. package/dist/dev/optimistic.d.ts +24 -0
  75. package/dist/dev/optimistic.js +44 -0
  76. package/dist/dev/optimistic.js.map +1 -0
  77. package/dist/dev/ownership.d.ts +202 -0
  78. package/dist/dev/ownership.js +260 -0
  79. package/dist/dev/ownership.js.map +1 -0
  80. package/dist/dev/props.d.ts +132 -0
  81. package/dist/dev/props.js +281 -0
  82. package/dist/dev/props.js.map +1 -0
  83. package/dist/dev/reaction.d.ts +27 -0
  84. package/dist/dev/reaction.js +60 -0
  85. package/dist/dev/reaction.js.map +1 -0
  86. package/dist/dev/reducer.d.ts +39 -0
  87. package/dist/dev/reducer.js +38 -0
  88. package/dist/dev/reducer.js.map +1 -0
  89. package/dist/dev/refs.d.ts +50 -0
  90. package/dist/dev/refs.js +57 -0
  91. package/dist/dev/refs.js.map +1 -0
  92. package/dist/dev/resource.d.ts +133 -0
  93. package/dist/dev/resource.js +176 -0
  94. package/dist/dev/resource.js.map +1 -0
  95. package/dist/dev/selector.d.ts +32 -0
  96. package/dist/dev/selector.js +117 -0
  97. package/dist/dev/selector.js.map +1 -0
  98. package/dist/dev/signal.d.ts +40 -0
  99. package/dist/dev/signal.js +38 -0
  100. package/dist/dev/signal.js.map +1 -0
  101. package/dist/dev/store/index.d.ts +5 -0
  102. package/dist/dev/store/index.js +4 -0
  103. package/dist/dev/store/nodes.js +191 -0
  104. package/dist/dev/store/nodes.js.map +1 -0
  105. package/dist/dev/store/proxies.js +159 -0
  106. package/dist/dev/store/proxies.js.map +1 -0
  107. package/dist/dev/store/read.d.ts +37 -0
  108. package/dist/dev/store/read.js +71 -0
  109. package/dist/dev/store/read.js.map +1 -0
  110. package/dist/dev/store/reconcile.d.ts +71 -0
  111. package/dist/dev/store/reconcile.js +185 -0
  112. package/dist/dev/store/reconcile.js.map +1 -0
  113. package/dist/dev/store/types.d.ts +104 -0
  114. package/dist/dev/store/use-store.d.ts +58 -0
  115. package/dist/dev/store/use-store.js +167 -0
  116. package/dist/dev/store/use-store.js.map +1 -0
  117. package/dist/dev/suspense.d.ts +99 -0
  118. package/dist/dev/suspense.js +44 -0
  119. package/dist/dev/suspense.js.map +1 -0
  120. package/dist/dev/transitions.d.ts +75 -0
  121. package/dist/dev/transitions.js +100 -0
  122. package/dist/dev/transitions.js.map +1 -0
  123. package/dist/prod/action.js +2 -0
  124. package/dist/prod/action.js.map +1 -0
  125. package/dist/prod/cache.js +2 -0
  126. package/dist/prod/cache.js.map +1 -0
  127. package/dist/prod/context.js +2 -0
  128. package/dist/prod/context.js.map +1 -0
  129. package/dist/prod/effect.js +2 -0
  130. package/dist/prod/effect.js.map +1 -0
  131. package/dist/prod/env.js +2 -0
  132. package/dist/prod/env.js.map +1 -0
  133. package/dist/prod/events.js +2 -0
  134. package/dist/prod/events.js.map +1 -0
  135. package/dist/prod/extend/index.js +2 -0
  136. package/dist/prod/extend/index.js.map +1 -0
  137. package/dist/prod/external-store.js +2 -0
  138. package/dist/prod/external-store.js.map +1 -0
  139. package/dist/prod/index.js +1 -0
  140. package/dist/prod/internal/concurrency.js +2 -0
  141. package/dist/prod/internal/concurrency.js.map +1 -0
  142. package/dist/prod/internal/devtools.js +2 -0
  143. package/dist/prod/internal/devtools.js.map +1 -0
  144. package/dist/prod/internal/diagnostics.js +2 -0
  145. package/dist/prod/internal/diagnostics.js.map +1 -0
  146. package/dist/prod/internal/dispose.js +2 -0
  147. package/dist/prod/internal/dispose.js.map +1 -0
  148. package/dist/prod/internal/flags.js +2 -0
  149. package/dist/prod/internal/flags.js.map +1 -0
  150. package/dist/prod/internal/graph.js +2 -0
  151. package/dist/prod/internal/graph.js.map +1 -0
  152. package/dist/prod/internal/hydration-flag.js +2 -0
  153. package/dist/prod/internal/hydration-flag.js.map +1 -0
  154. package/dist/prod/internal/messages.js +2 -0
  155. package/dist/prod/internal/messages.js.map +1 -0
  156. package/dist/prod/internal/owner.js +2 -0
  157. package/dist/prod/internal/owner.js.map +1 -0
  158. package/dist/prod/internal/queue.js +2 -0
  159. package/dist/prod/internal/queue.js.map +1 -0
  160. package/dist/prod/internal/scheduler.js +2 -0
  161. package/dist/prod/internal/scheduler.js.map +1 -0
  162. package/dist/prod/internal/state.js +2 -0
  163. package/dist/prod/internal/state.js.map +1 -0
  164. package/dist/prod/internal/suspension.js +2 -0
  165. package/dist/prod/internal/suspension.js.map +1 -0
  166. package/dist/prod/list.js +2 -0
  167. package/dist/prod/list.js.map +1 -0
  168. package/dist/prod/memo.js +2 -0
  169. package/dist/prod/memo.js.map +1 -0
  170. package/dist/prod/optimistic.js +2 -0
  171. package/dist/prod/optimistic.js.map +1 -0
  172. package/dist/prod/ownership.js +2 -0
  173. package/dist/prod/ownership.js.map +1 -0
  174. package/dist/prod/props.js +2 -0
  175. package/dist/prod/props.js.map +1 -0
  176. package/dist/prod/reaction.js +2 -0
  177. package/dist/prod/reaction.js.map +1 -0
  178. package/dist/prod/reducer.js +2 -0
  179. package/dist/prod/reducer.js.map +1 -0
  180. package/dist/prod/refs.js +2 -0
  181. package/dist/prod/refs.js.map +1 -0
  182. package/dist/prod/resource.js +2 -0
  183. package/dist/prod/resource.js.map +1 -0
  184. package/dist/prod/selector.js +2 -0
  185. package/dist/prod/selector.js.map +1 -0
  186. package/dist/prod/signal.js +2 -0
  187. package/dist/prod/signal.js.map +1 -0
  188. package/dist/prod/store/index.js +1 -0
  189. package/dist/prod/store/nodes.js +2 -0
  190. package/dist/prod/store/nodes.js.map +1 -0
  191. package/dist/prod/store/proxies.js +2 -0
  192. package/dist/prod/store/proxies.js.map +1 -0
  193. package/dist/prod/store/read.js +2 -0
  194. package/dist/prod/store/read.js.map +1 -0
  195. package/dist/prod/store/reconcile.js +2 -0
  196. package/dist/prod/store/reconcile.js.map +1 -0
  197. package/dist/prod/store/use-store.js +2 -0
  198. package/dist/prod/store/use-store.js.map +1 -0
  199. package/dist/prod/suspense.js +2 -0
  200. package/dist/prod/suspense.js.map +1 -0
  201. package/dist/prod/transitions.js +2 -0
  202. package/dist/prod/transitions.js.map +1 -0
  203. package/dist/server/action.js +2 -0
  204. package/dist/server/action.js.map +1 -0
  205. package/dist/server/cache.js +2 -0
  206. package/dist/server/cache.js.map +1 -0
  207. package/dist/server/context.js +2 -0
  208. package/dist/server/context.js.map +1 -0
  209. package/dist/server/effect.js +2 -0
  210. package/dist/server/effect.js.map +1 -0
  211. package/dist/server/env.js +2 -0
  212. package/dist/server/env.js.map +1 -0
  213. package/dist/server/events.js +2 -0
  214. package/dist/server/events.js.map +1 -0
  215. package/dist/server/extend/index.js +2 -0
  216. package/dist/server/extend/index.js.map +1 -0
  217. package/dist/server/external-store.js +2 -0
  218. package/dist/server/external-store.js.map +1 -0
  219. package/dist/server/index.js +1 -0
  220. package/dist/server/internal/concurrency.js +2 -0
  221. package/dist/server/internal/concurrency.js.map +1 -0
  222. package/dist/server/internal/devtools.js +2 -0
  223. package/dist/server/internal/devtools.js.map +1 -0
  224. package/dist/server/internal/diagnostics.js +2 -0
  225. package/dist/server/internal/diagnostics.js.map +1 -0
  226. package/dist/server/internal/dispose.js +2 -0
  227. package/dist/server/internal/dispose.js.map +1 -0
  228. package/dist/server/internal/flags.js +2 -0
  229. package/dist/server/internal/flags.js.map +1 -0
  230. package/dist/server/internal/graph.js +2 -0
  231. package/dist/server/internal/graph.js.map +1 -0
  232. package/dist/server/internal/hydration-flag.js +2 -0
  233. package/dist/server/internal/hydration-flag.js.map +1 -0
  234. package/dist/server/internal/messages.js +2 -0
  235. package/dist/server/internal/messages.js.map +1 -0
  236. package/dist/server/internal/owner.js +2 -0
  237. package/dist/server/internal/owner.js.map +1 -0
  238. package/dist/server/internal/queue.js +2 -0
  239. package/dist/server/internal/queue.js.map +1 -0
  240. package/dist/server/internal/scheduler.js +2 -0
  241. package/dist/server/internal/scheduler.js.map +1 -0
  242. package/dist/server/internal/state.js +2 -0
  243. package/dist/server/internal/state.js.map +1 -0
  244. package/dist/server/internal/suspension.js +2 -0
  245. package/dist/server/internal/suspension.js.map +1 -0
  246. package/dist/server/list.js +2 -0
  247. package/dist/server/list.js.map +1 -0
  248. package/dist/server/memo.js +2 -0
  249. package/dist/server/memo.js.map +1 -0
  250. package/dist/server/optimistic.js +2 -0
  251. package/dist/server/optimistic.js.map +1 -0
  252. package/dist/server/ownership.js +2 -0
  253. package/dist/server/ownership.js.map +1 -0
  254. package/dist/server/props.js +2 -0
  255. package/dist/server/props.js.map +1 -0
  256. package/dist/server/reaction.js +2 -0
  257. package/dist/server/reaction.js.map +1 -0
  258. package/dist/server/reducer.js +2 -0
  259. package/dist/server/reducer.js.map +1 -0
  260. package/dist/server/refs.js +2 -0
  261. package/dist/server/refs.js.map +1 -0
  262. package/dist/server/resource.js +2 -0
  263. package/dist/server/resource.js.map +1 -0
  264. package/dist/server/selector.js +2 -0
  265. package/dist/server/selector.js.map +1 -0
  266. package/dist/server/signal.js +2 -0
  267. package/dist/server/signal.js.map +1 -0
  268. package/dist/server/store/index.js +1 -0
  269. package/dist/server/store/nodes.js +2 -0
  270. package/dist/server/store/nodes.js.map +1 -0
  271. package/dist/server/store/proxies.js +2 -0
  272. package/dist/server/store/proxies.js.map +1 -0
  273. package/dist/server/store/read.js +2 -0
  274. package/dist/server/store/read.js.map +1 -0
  275. package/dist/server/store/reconcile.js +2 -0
  276. package/dist/server/store/reconcile.js.map +1 -0
  277. package/dist/server/store/use-store.js +2 -0
  278. package/dist/server/store/use-store.js.map +1 -0
  279. package/dist/server/suspense.js +2 -0
  280. package/dist/server/suspense.js.map +1 -0
  281. package/dist/server/transitions.js +2 -0
  282. package/dist/server/transitions.js.map +1 -0
  283. package/dist/server-dev/action.js +89 -0
  284. package/dist/server-dev/action.js.map +1 -0
  285. package/dist/server-dev/cache.js +61 -0
  286. package/dist/server-dev/cache.js.map +1 -0
  287. package/dist/server-dev/context.js +107 -0
  288. package/dist/server-dev/context.js.map +1 -0
  289. package/dist/server-dev/effect.js +55 -0
  290. package/dist/server-dev/effect.js.map +1 -0
  291. package/dist/server-dev/env.js +30 -0
  292. package/dist/server-dev/env.js.map +1 -0
  293. package/dist/server-dev/events.js +32 -0
  294. package/dist/server-dev/events.js.map +1 -0
  295. package/dist/server-dev/extend/index.js +316 -0
  296. package/dist/server-dev/extend/index.js.map +1 -0
  297. package/dist/server-dev/external-store.js +45 -0
  298. package/dist/server-dev/external-store.js.map +1 -0
  299. package/dist/server-dev/index.js +26 -0
  300. package/dist/server-dev/internal/concurrency.js +632 -0
  301. package/dist/server-dev/internal/concurrency.js.map +1 -0
  302. package/dist/server-dev/internal/devtools.js +76 -0
  303. package/dist/server-dev/internal/devtools.js.map +1 -0
  304. package/dist/server-dev/internal/diagnostics.js +101 -0
  305. package/dist/server-dev/internal/diagnostics.js.map +1 -0
  306. package/dist/server-dev/internal/dispose.js +48 -0
  307. package/dist/server-dev/internal/dispose.js.map +1 -0
  308. package/dist/server-dev/internal/flags.js +10 -0
  309. package/dist/server-dev/internal/flags.js.map +1 -0
  310. package/dist/server-dev/internal/graph.js +577 -0
  311. package/dist/server-dev/internal/graph.js.map +1 -0
  312. package/dist/server-dev/internal/hydration-flag.js +45 -0
  313. package/dist/server-dev/internal/hydration-flag.js.map +1 -0
  314. package/dist/server-dev/internal/messages.js +53 -0
  315. package/dist/server-dev/internal/messages.js.map +1 -0
  316. package/dist/server-dev/internal/owner.js +289 -0
  317. package/dist/server-dev/internal/owner.js.map +1 -0
  318. package/dist/server-dev/internal/queue.js +45 -0
  319. package/dist/server-dev/internal/queue.js.map +1 -0
  320. package/dist/server-dev/internal/scheduler.js +286 -0
  321. package/dist/server-dev/internal/scheduler.js.map +1 -0
  322. package/dist/server-dev/internal/state.js +27 -0
  323. package/dist/server-dev/internal/state.js.map +1 -0
  324. package/dist/server-dev/internal/suspension.js +44 -0
  325. package/dist/server-dev/internal/suspension.js.map +1 -0
  326. package/dist/server-dev/list.js +281 -0
  327. package/dist/server-dev/list.js.map +1 -0
  328. package/dist/server-dev/memo.js +40 -0
  329. package/dist/server-dev/memo.js.map +1 -0
  330. package/dist/server-dev/optimistic.js +44 -0
  331. package/dist/server-dev/optimistic.js.map +1 -0
  332. package/dist/server-dev/ownership.js +260 -0
  333. package/dist/server-dev/ownership.js.map +1 -0
  334. package/dist/server-dev/props.js +281 -0
  335. package/dist/server-dev/props.js.map +1 -0
  336. package/dist/server-dev/reaction.js +60 -0
  337. package/dist/server-dev/reaction.js.map +1 -0
  338. package/dist/server-dev/reducer.js +38 -0
  339. package/dist/server-dev/reducer.js.map +1 -0
  340. package/dist/server-dev/refs.js +57 -0
  341. package/dist/server-dev/refs.js.map +1 -0
  342. package/dist/server-dev/resource.js +175 -0
  343. package/dist/server-dev/resource.js.map +1 -0
  344. package/dist/server-dev/selector.js +81 -0
  345. package/dist/server-dev/selector.js.map +1 -0
  346. package/dist/server-dev/signal.js +38 -0
  347. package/dist/server-dev/signal.js.map +1 -0
  348. package/dist/server-dev/store/index.js +4 -0
  349. package/dist/server-dev/store/nodes.js +191 -0
  350. package/dist/server-dev/store/nodes.js.map +1 -0
  351. package/dist/server-dev/store/proxies.js +159 -0
  352. package/dist/server-dev/store/proxies.js.map +1 -0
  353. package/dist/server-dev/store/read.js +71 -0
  354. package/dist/server-dev/store/read.js.map +1 -0
  355. package/dist/server-dev/store/reconcile.js +185 -0
  356. package/dist/server-dev/store/reconcile.js.map +1 -0
  357. package/dist/server-dev/store/use-store.js +167 -0
  358. package/dist/server-dev/store/use-store.js.map +1 -0
  359. package/dist/server-dev/suspense.js +44 -0
  360. package/dist/server-dev/suspense.js.map +1 -0
  361. package/dist/server-dev/transitions.js +85 -0
  362. package/dist/server-dev/transitions.js.map +1 -0
  363. package/package.json +111 -50
  364. package/dist/chunk-6Y3CCWOY.js +0 -887
  365. package/dist/chunk-6Y3CCWOY.js.map +0 -1
  366. package/dist/chunk-B5FTYDUG.js +0 -3
  367. package/dist/chunk-B5FTYDUG.js.map +0 -1
  368. package/dist/index.cjs +0 -2000
  369. package/dist/index.cjs.map +0 -1
  370. package/dist/index.d.cts +0 -609
  371. package/dist/index.d.ts +0 -609
  372. package/dist/index.js +0 -1094
  373. package/dist/index.js.map +0 -1
  374. package/dist/jsx-Bugil6CJ.d.cts +0 -151
  375. package/dist/jsx-Bugil6CJ.d.ts +0 -151
  376. package/dist/jsx-dev-runtime.cjs +0 -742
  377. package/dist/jsx-dev-runtime.cjs.map +0 -1
  378. package/dist/jsx-dev-runtime.d.cts +0 -2
  379. package/dist/jsx-dev-runtime.d.ts +0 -2
  380. package/dist/jsx-dev-runtime.js +0 -4
  381. package/dist/jsx-dev-runtime.js.map +0 -1
  382. package/dist/jsx-runtime.cjs +0 -742
  383. package/dist/jsx-runtime.cjs.map +0 -1
  384. package/dist/jsx-runtime.d.cts +0 -36
  385. package/dist/jsx-runtime.d.ts +0 -36
  386. package/dist/jsx-runtime.js +0 -4
  387. package/dist/jsx-runtime.js.map +0 -1
@@ -0,0 +1,191 @@
1
+ import { track } from "../internal/graph.js";
2
+ import { createSignalNode, isTracking, notify as notify$1 } from "../extend/index.js";
3
+ // src/store/nodes.ts
4
+ /** Keys that could reach `Object.prototype` if written; they are never stored as own properties. */
5
+ const RESERVED_KEYS = /* @__PURE__ */ new Set([
6
+ "__proto__",
7
+ "constructor",
8
+ "prototype"
9
+ ]);
10
+ /** Raw object -> its tracking state. Keys are the raw data, so nothing is stamped on user objects. */
11
+ const NODES = /* @__PURE__ */ new WeakMap();
12
+ /** Any store proxy (read-only or draft) -> the raw object it wraps. */
13
+ const RAW = /* @__PURE__ */ new WeakMap();
14
+ /** True for the keys that must never be written as own properties. */
15
+ function isReservedKey(key) {
16
+ return RESERVED_KEYS.has(key);
17
+ }
18
+ /** True for the values the store wraps: plain objects (null or Object.prototype) and arrays. */
19
+ function isWrappable(value) {
20
+ if (typeof value !== "object" || value === null) return false;
21
+ if (Array.isArray(value)) return true;
22
+ if (value === Object.prototype) return false;
23
+ const prototype = Object.getPrototypeOf(value);
24
+ return prototype === Object.prototype || prototype === null;
25
+ }
26
+ /** Replaces a store proxy by the raw object it wraps; any other value is returned as is. */
27
+ function unwrapValue(value) {
28
+ if (typeof value !== "object" || value === null) return value;
29
+ return RAW.get(value) ?? value;
30
+ }
31
+ /** True when a store proxy (draft or read-only view) sits anywhere inside the plain data `root`. */
32
+ function containsProxy(root) {
33
+ const seen = /* @__PURE__ */ new Set([root]);
34
+ const stack = [root];
35
+ while (stack.length > 0) {
36
+ const container = stack.pop();
37
+ for (const key of Object.keys(container)) {
38
+ const member = Reflect.get(container, key);
39
+ if (typeof member !== "object" || member === null) continue;
40
+ if (RAW.has(member)) return true;
41
+ if (isWrappable(member) && !seen.has(member)) {
42
+ seen.add(member);
43
+ stack.push(member);
44
+ }
45
+ }
46
+ }
47
+ return false;
48
+ }
49
+ /**
50
+ * Like {@link unwrapValue}, and also for proxies hidden inside plain data: `draft.todos =
51
+ * draft.todos.filter(...)` assigns an array whose members are drafts, and a draft stored in the
52
+ * data would throw on its next use. Data without proxies is returned as is; otherwise a copy with
53
+ * every proxy replaced by its raw object. Iterative, so nesting depth is not limited by the stack.
54
+ */
55
+ function unwrapDeep(value) {
56
+ const first = unwrapValue(value);
57
+ if (first !== value || !isWrappable(first) || !containsProxy(first)) return first;
58
+ const copies = /* @__PURE__ */ new Map();
59
+ const copyOf = (source) => {
60
+ const existing = copies.get(source);
61
+ if (existing !== void 0) return existing;
62
+ const made = Array.isArray(source) ? [] : Object.create(Object.getPrototypeOf(source));
63
+ copies.set(source, made);
64
+ return made;
65
+ };
66
+ const rootCopy = copyOf(first);
67
+ const queue = [first];
68
+ for (let head = 0; head < queue.length; head += 1) {
69
+ const source = queue[head];
70
+ const target = copies.get(source);
71
+ for (const key of Object.keys(source)) {
72
+ let member = Reflect.get(source, key);
73
+ if (typeof member === "object" && member !== null) {
74
+ const raw = RAW.get(member);
75
+ if (raw !== void 0) member = raw;
76
+ else if (isWrappable(member)) {
77
+ const known = copies.has(member);
78
+ member = copyOf(member);
79
+ if (!known) queue.push(Reflect.get(source, key));
80
+ }
81
+ }
82
+ Object.defineProperty(target, key, {
83
+ value: member,
84
+ enumerable: true,
85
+ writable: true,
86
+ configurable: true
87
+ });
88
+ }
89
+ }
90
+ return rootCopy;
91
+ }
92
+ /** Reads an own or inherited property without tracking and without invoking proxies. */
93
+ function readRaw(target, key) {
94
+ return Reflect.get(target, key);
95
+ }
96
+ /** Returns the tracking state of `raw`, creating an empty one on first use. */
97
+ function nodeOf(raw) {
98
+ let node = NODES.get(raw);
99
+ if (node === void 0) {
100
+ node = {
101
+ signals: null,
102
+ keys: null,
103
+ proxy: null
104
+ };
105
+ NODES.set(raw, node);
106
+ }
107
+ return node;
108
+ }
109
+ function signalFor(node, key) {
110
+ let signals = node.signals;
111
+ if (signals === null) {
112
+ signals = /* @__PURE__ */ new Map();
113
+ node.signals = signals;
114
+ }
115
+ let signal = signals.get(key);
116
+ if (signal === void 0) {
117
+ signal = createSignalNode(0, false);
118
+ signals.set(key, signal);
119
+ }
120
+ return signal;
121
+ }
122
+ /** Subscribes the running computation (if any) to one property of the object. */
123
+ function trackProperty(node, key) {
124
+ if (!isTracking()) return;
125
+ track(signalFor(node, key));
126
+ }
127
+ /** Subscribes the running computation (if any) to the key set of the object. */
128
+ function trackKeys(node) {
129
+ if (!isTracking()) return;
130
+ if (node.keys === null) node.keys = createSignalNode(0, false);
131
+ track(node.keys);
132
+ }
133
+ /**
134
+ * Notifies the observers of one signal. The same engine steps as a signal write: mark the
135
+ * observers dirty, then request a flush (which the surrounding batch defers).
136
+ */
137
+ function notify(signal) {
138
+ if (signal === void 0 || signal === null) return;
139
+ notify$1(signal);
140
+ }
141
+ function isIndexKey(key) {
142
+ const index = Number(key);
143
+ return Number.isInteger(index) && index >= 0 && String(index) === key;
144
+ }
145
+ /** Notifies the index signals of an array at or beyond `from` (used when the array shrinks). */
146
+ function notifyIndicesFrom(node, from) {
147
+ const signals = node.signals;
148
+ if (signals === null) return;
149
+ for (const [key, signal] of signals) if (isIndexKey(key) && Number(key) >= from) notify(signal);
150
+ }
151
+ /**
152
+ * Writes one property of a raw object and notifies exactly what changed: the property's signal,
153
+ * the key set when a property appears, and the length when an array's length moves. An equal
154
+ * value (Object.is) writes nothing. Returns whether anything changed.
155
+ */
156
+ function writeRaw(target, key, value) {
157
+ if (isReservedKey(key)) throw new TypeError(`ShadowStore: "${key}" cannot be written; it is a reserved key`);
158
+ value = unwrapDeep(value);
159
+ const array = Array.isArray(target);
160
+ const had = Object.hasOwn(target, key);
161
+ if (had && Object.is(readRaw(target, key), value)) return false;
162
+ const before = array ? target.length : 0;
163
+ if (!Reflect.set(target, key, value)) throw new TypeError(`ShadowStore: property "${key}" is read-only and cannot be written`);
164
+ const node = NODES.get(target);
165
+ if (node === void 0) return true;
166
+ notify(node.signals?.get(key));
167
+ if (!had) notify(node.keys);
168
+ if (array) {
169
+ const after = target.length;
170
+ if (after !== before) {
171
+ notify(node.signals?.get("length"));
172
+ notify(node.keys);
173
+ if (after < before) notifyIndicesFrom(node, after);
174
+ }
175
+ }
176
+ return true;
177
+ }
178
+ /** Deletes one own property of a raw object and notifies its signal and the key set. */
179
+ function deleteRaw(target, key) {
180
+ if (isReservedKey(key)) throw new TypeError(`ShadowStore: "${key}" cannot be deleted; it is a reserved key`);
181
+ if (!Object.hasOwn(target, key)) return true;
182
+ if (!Reflect.deleteProperty(target, key)) throw new TypeError(`ShadowStore: property "${key}" cannot be deleted`);
183
+ const node = NODES.get(target);
184
+ if (node === void 0) return true;
185
+ notify(node.signals?.get(key));
186
+ notify(node.keys);
187
+ return true;
188
+ }
189
+ export { NODES, RAW, deleteRaw, isReservedKey, isWrappable, nodeOf, readRaw, trackKeys, trackProperty, unwrapDeep, unwrapValue, writeRaw };
190
+
191
+ //# sourceMappingURL=nodes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nodes.js","names":[],"sources":["../../../src/store/nodes.ts"],"sourcesContent":["import {\n createSignalNode,\n isTracking,\n notify as notifySource,\n type SignalNode,\n track,\n} from \"../extend\";\n\n/** Keys that could reach `Object.prototype` if written; they are never stored as own properties. */\nconst RESERVED_KEYS: ReadonlySet<string> = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/**\n * Tracking state of one plain object or array. Signals are created lazily, on the first read made\n * inside a tracked computation, so untracked data costs one small record per object.\n */\nexport interface NodeState {\n /** One signal per property read in a tracked context, keyed by property name. */\n signals: Map<string, SignalNode<number>> | null;\n /** Notified when a property is added or removed, or an array changes length. */\n keys: SignalNode<number> | null;\n /** The read-only proxy for this object, created on first read. */\n proxy: object | null;\n}\n\n/** Raw object -> its tracking state. Keys are the raw data, so nothing is stamped on user objects. */\nexport const NODES: WeakMap<object, NodeState> = new WeakMap();\n\n/** Any store proxy (read-only or draft) -> the raw object it wraps. */\nexport const RAW: WeakMap<object, object> = new WeakMap();\n\n/** True for the keys that must never be written as own properties. */\nexport function isReservedKey(key: string): boolean {\n return RESERVED_KEYS.has(key);\n}\n\n/** True for the values the store wraps: plain objects (null or Object.prototype) and arrays. */\nexport function isWrappable(value: unknown): value is object {\n if (typeof value !== \"object\" || value === null) return false;\n if (Array.isArray(value)) return true;\n if (value === Object.prototype) return false;\n const prototype: unknown = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n\n/** Replaces a store proxy by the raw object it wraps; any other value is returned as is. */\nexport function unwrapValue(value: unknown): unknown {\n if (typeof value !== \"object\" || value === null) return value;\n return RAW.get(value) ?? value;\n}\n\n/** True when a store proxy (draft or read-only view) sits anywhere inside the plain data `root`. */\nfunction containsProxy(root: object): boolean {\n const seen = new Set<object>([root]);\n const stack: object[] = [root];\n while (stack.length > 0) {\n const container = stack.pop() as object;\n for (const key of Object.keys(container)) {\n const member: unknown = Reflect.get(container, key);\n if (typeof member !== \"object\" || member === null) continue;\n if (RAW.has(member)) return true;\n if (isWrappable(member) && !seen.has(member)) {\n seen.add(member);\n stack.push(member);\n }\n }\n }\n return false;\n}\n\n/**\n * Like {@link unwrapValue}, and also for proxies hidden inside plain data: `draft.todos =\n * draft.todos.filter(...)` assigns an array whose members are drafts, and a draft stored in the\n * data would throw on its next use. Data without proxies is returned as is; otherwise a copy with\n * every proxy replaced by its raw object. Iterative, so nesting depth is not limited by the stack.\n */\nexport function unwrapDeep(value: unknown): unknown {\n const first = unwrapValue(value);\n if (first !== value || !isWrappable(first) || !containsProxy(first)) return first;\n const copies = new Map<object, object>();\n const copyOf = (source: object): object => {\n const existing = copies.get(source);\n if (existing !== undefined) return existing;\n const made: object = Array.isArray(source) ? [] : Object.create(Object.getPrototypeOf(source));\n copies.set(source, made);\n return made;\n };\n const rootCopy = copyOf(first);\n const queue: object[] = [first];\n for (let head = 0; head < queue.length; head += 1) {\n const source = queue[head] as object;\n const target = copies.get(source) as object;\n for (const key of Object.keys(source)) {\n let member: unknown = Reflect.get(source, key);\n if (typeof member === \"object\" && member !== null) {\n const raw = RAW.get(member);\n if (raw !== undefined) member = raw;\n else if (isWrappable(member)) {\n const known = copies.has(member);\n member = copyOf(member);\n if (!known) queue.push(Reflect.get(source, key) as object);\n }\n }\n Object.defineProperty(target, key, {\n value: member,\n enumerable: true,\n writable: true,\n configurable: true,\n });\n }\n }\n return rootCopy;\n}\n\n/** Reads an own or inherited property without tracking and without invoking proxies. */\nexport function readRaw(target: object, key: string): unknown {\n return Reflect.get(target, key);\n}\n\n/** Returns the tracking state of `raw`, creating an empty one on first use. */\nexport function nodeOf(raw: object): NodeState {\n let node = NODES.get(raw);\n if (node === undefined) {\n node = { signals: null, keys: null, proxy: null };\n NODES.set(raw, node);\n }\n return node;\n}\n\nfunction signalFor(node: NodeState, key: string): SignalNode<number> {\n let signals = node.signals;\n if (signals === null) {\n signals = new Map();\n node.signals = signals;\n }\n let signal = signals.get(key);\n if (signal === undefined) {\n signal = createSignalNode(0, false);\n signals.set(key, signal);\n }\n return signal;\n}\n\n/** Subscribes the running computation (if any) to one property of the object. */\nexport function trackProperty(node: NodeState, key: string): void {\n if (!isTracking()) return;\n track(signalFor(node, key));\n}\n\n/** Subscribes the running computation (if any) to the key set of the object. */\nexport function trackKeys(node: NodeState): void {\n if (!isTracking()) return;\n if (node.keys === null) node.keys = createSignalNode(0, false);\n track(node.keys);\n}\n\n/**\n * Notifies the observers of one signal. The same engine steps as a signal write: mark the\n * observers dirty, then request a flush (which the surrounding batch defers).\n */\nfunction notify(signal: SignalNode<number> | null | undefined): void {\n if (signal === undefined || signal === null) return;\n notifySource(signal);\n}\n\nfunction isIndexKey(key: string): boolean {\n const index = Number(key);\n return Number.isInteger(index) && index >= 0 && String(index) === key;\n}\n\n/** Notifies the index signals of an array at or beyond `from` (used when the array shrinks). */\nfunction notifyIndicesFrom(node: NodeState, from: number): void {\n const signals = node.signals;\n if (signals === null) return;\n for (const [key, signal] of signals) {\n if (isIndexKey(key) && Number(key) >= from) notify(signal);\n }\n}\n\n/**\n * Writes one property of a raw object and notifies exactly what changed: the property's signal,\n * the key set when a property appears, and the length when an array's length moves. An equal\n * value (Object.is) writes nothing. Returns whether anything changed.\n */\nexport function writeRaw(target: object, key: string, value: unknown): boolean {\n if (isReservedKey(key)) {\n throw new TypeError(\n __DEV__\n ? `ShadowStore: \"${key}\" cannot be written; it is a reserved key`\n : \"SHADOW_STORE_RESERVED_KEY\",\n );\n }\n value = unwrapDeep(value);\n const array = Array.isArray(target);\n const had = Object.hasOwn(target, key);\n if (had && Object.is(readRaw(target, key), value)) return false;\n const before = array ? (target as unknown[]).length : 0;\n if (!Reflect.set(target, key, value)) {\n throw new TypeError(\n __DEV__\n ? `ShadowStore: property \"${key}\" is read-only and cannot be written`\n : \"SHADOW_STORE_PROP_READ_ONLY\",\n );\n }\n const node = NODES.get(target);\n if (node === undefined) return true;\n notify(node.signals?.get(key));\n if (!had) notify(node.keys);\n if (array) {\n const after = (target as unknown[]).length;\n if (after !== before) {\n notify(node.signals?.get(\"length\"));\n notify(node.keys);\n if (after < before) notifyIndicesFrom(node, after);\n }\n }\n return true;\n}\n\n/** Deletes one own property of a raw object and notifies its signal and the key set. */\nexport function deleteRaw(target: object, key: string): boolean {\n if (isReservedKey(key)) {\n throw new TypeError(\n __DEV__\n ? `ShadowStore: \"${key}\" cannot be deleted; it is a reserved key`\n : \"SHADOW_STORE_RESERVED_DELETE\",\n );\n }\n if (!Object.hasOwn(target, key)) return true;\n if (!Reflect.deleteProperty(target, key)) {\n throw new TypeError(\n __DEV__ ? `ShadowStore: property \"${key}\" cannot be deleted` : \"SHADOW_STORE_DELETE\",\n );\n }\n const node = NODES.get(target);\n if (node === undefined) return true;\n notify(node.signals?.get(key));\n notify(node.keys);\n return true;\n}\n"],"mappings":";;;;AASA,MAAM,gCAAqC,IAAI,IAAI;CAAC;CAAa;CAAe;AAAW,CAAC;;AAgB5F,MAAa,wBAAoC,IAAI,QAAQ;;AAG7D,MAAa,sBAA+B,IAAI,QAAQ;;AAGxD,SAAgB,cAAc,KAAsB;CAClD,OAAO,cAAc,IAAI,GAAG;AAC9B;;AAGA,SAAgB,YAAY,OAAiC;CAC3D,IAAI,OAAO,UAAU,YAAY,UAAU,MAAM,OAAO;CACxD,IAAI,MAAM,QAAQ,KAAK,GAAG,OAAO;CACjC,IAAI,UAAU,OAAO,WAAW,OAAO;CACvC,MAAM,YAAqB,OAAO,eAAe,KAAK;CACtD,OAAO,cAAc,OAAO,aAAa,cAAc;AACzD;;AAGA,SAAgB,YAAY,OAAyB;CACnD,IAAI,OAAO,UAAU,YAAY,UAAU,MAAM,OAAO;CACxD,OAAO,IAAI,IAAI,KAAK,KAAK;AAC3B;;AAGA,SAAS,cAAc,MAAuB;CAC5C,MAAM,uBAAO,IAAI,IAAY,CAAC,IAAI,CAAC;CACnC,MAAM,QAAkB,CAAC,IAAI;CAC7B,OAAO,MAAM,SAAS,GAAG;EACvB,MAAM,YAAY,MAAM,IAAI;EAC5B,KAAK,MAAM,OAAO,OAAO,KAAK,SAAS,GAAG;GACxC,MAAM,SAAkB,QAAQ,IAAI,WAAW,GAAG;GAClD,IAAI,OAAO,WAAW,YAAY,WAAW,MAAM;GACnD,IAAI,IAAI,IAAI,MAAM,GAAG,OAAO;GAC5B,IAAI,YAAY,MAAM,KAAK,CAAC,KAAK,IAAI,MAAM,GAAG;IAC5C,KAAK,IAAI,MAAM;IACf,MAAM,KAAK,MAAM;GACnB;EACF;CACF;CACA,OAAO;AACT;;;;;;;AAQA,SAAgB,WAAW,OAAyB;CAClD,MAAM,QAAQ,YAAY,KAAK;CAC/B,IAAI,UAAU,SAAS,CAAC,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AO,QAAQ,GAAG;CACrC,IAAI,OAAO,OAAO,GAAG,QAAQ,QAAQ,GAAG,GAAG,KAAK,GAAG,OAAO;CAC1D,MAAM,SAAS,QAAS,OAAqB,SAAS;CACtD,IAAI,CAAC,QAAQ,IAAI,QAAQ,KAAK,KAAK,GACjC,MAAM,IAAI,UAEJ,0BAA0B,IAAI,qCAEpC;CAEF,MAAM,OAAO,MAAM,IAAI,MAAM;CAC7B,IAAI,SAAS,KAAA,GAAW,OAAO;CAC/B,OAAO,KAAK,SAAS,IAAI,GAAG,CAAC;CAC7B,IAAI,CAAC,KAAK,OAAO,KAAK,IAAI;CAC1B,IAAI,OAAO;EACT,MAAM,QAAS,OAAqB;EACpC,IAAI,UAAU,QAAQ;GACpB,OAAO,KAAK,SAAS,IAAI,QAAQ,CAAC;GAClC,OAAO,KAAK,IAAI;GAChB,IAAI,QAAQ,QAAQ,kBAAkB,MAAM,KAAK;EACnD;CACF;CACA,OAAO;AACT;;AAGA,SAAgB,UAAU,QAAgB,KAAsB;CAC9D,IAAI,cAAc,GAAG,GACnB,MAAM,IAAI,UAEJ,iBAAiB,IAAI,0CAE3B;CAEF,IAAI,CAAC,OAAO,OAAO,QAAQ,GAAG,GAAG,OAAO;CACxC,IAAI,CAAC,QAAQ,eAAe,QAAQ,GAAG,GACrC,MAAM,IAAI,UACE,0BAA0B,IAAI,oBAC1C;CAEF,MAAM,OAAO,MAAM,IAAI,MAAM;CAC7B,IAAI,SAAS,KAAA,GAAW,OAAO;CAC/B,OAAO,KAAK,SAAS,IAAI,GAAG,CAAC;CAC7B,OAAO,KAAK,IAAI;CAChB,OAAO;AACT"}
@@ -0,0 +1,159 @@
1
+ import { RAW, deleteRaw, isReservedKey, isWrappable, nodeOf, readRaw, trackKeys, trackProperty, unwrapValue, writeRaw } from "./nodes.js";
2
+ // src/store/proxies.ts
3
+ /**
4
+ * One `setStore` call. Drafts made for it are cached here, so a draft object keeps its identity
5
+ * inside the update, and they stop accepting writes when the call returns.
6
+ */
7
+ var Transaction = class {
8
+ label;
9
+ open;
10
+ drafts;
11
+ handler;
12
+ constructor(label) {
13
+ this.label = label;
14
+ this.open = true;
15
+ this.drafts = /* @__PURE__ */ new Map();
16
+ this.handler = draftHandler(this);
17
+ }
18
+ };
19
+ function readOnly(key) {
20
+ const name = typeof key === "symbol" ? key.toString() : `"${key}"`;
21
+ return /* @__PURE__ */ new TypeError(`ShadowStore: the store is read-only; ${name} can only be changed inside setStore((draft) => ...)`);
22
+ }
23
+ /**
24
+ * Returns the value to hand out for `key`: a wrapped object when the value is an own, writable
25
+ * wrappable slot, otherwise the raw value. Frozen slots must return the raw value because a proxy
26
+ * may not report a different value for a non-writable, non-configurable property.
27
+ */
28
+ function childOf(target, key, value, wrap) {
29
+ if (!isWrappable(value) || !Object.hasOwn(target, key)) return value;
30
+ const descriptor = Reflect.getOwnPropertyDescriptor(target, key);
31
+ if (descriptor !== void 0 && descriptor.configurable === false && descriptor.writable === false) return value;
32
+ return wrap(value);
33
+ }
34
+ /** Descriptor passthrough that wraps the value the same way `get` does. */
35
+ function describeChild(target, key, wrap) {
36
+ const descriptor = Reflect.getOwnPropertyDescriptor(target, key);
37
+ if (descriptor === void 0 || typeof key === "symbol") return descriptor;
38
+ if (descriptor.configurable === false) return descriptor;
39
+ const value = descriptor.value;
40
+ if (!isWrappable(value)) return descriptor;
41
+ return {
42
+ ...descriptor,
43
+ value: wrap(value)
44
+ };
45
+ }
46
+ /** `__proto__` is only reachable through the prototype chain, so the store never exposes it. */
47
+ function hidesPrototype(target, key) {
48
+ return key === "__proto__" && !Object.hasOwn(target, key);
49
+ }
50
+ /** True when `key` resolves to an accessor property (own or inherited). */
51
+ function hasGetter(target, key) {
52
+ for (let object = target; object !== null; object = Object.getPrototypeOf(object)) {
53
+ const descriptor = Reflect.getOwnPropertyDescriptor(object, key);
54
+ if (descriptor !== void 0) return typeof descriptor.get === "function";
55
+ }
56
+ return false;
57
+ }
58
+ /** Read-only handler: every read subscribes the running computation to the property it touches. */
59
+ const READ_HANDLER = {
60
+ get(target, key, receiver) {
61
+ if (typeof key === "symbol") return Reflect.get(target, key);
62
+ if (hidesPrototype(target, key)) return void 0;
63
+ trackProperty(nodeOf(target), key);
64
+ if (hasGetter(target, key)) return Reflect.get(target, key, receiver);
65
+ return childOf(target, key, readRaw(target, key), readProxyOf);
66
+ },
67
+ has(target, key) {
68
+ if (typeof key !== "symbol") trackProperty(nodeOf(target), key);
69
+ return Reflect.has(target, key);
70
+ },
71
+ ownKeys(target) {
72
+ trackKeys(nodeOf(target));
73
+ return Reflect.ownKeys(target);
74
+ },
75
+ getOwnPropertyDescriptor(target, key) {
76
+ return describeChild(target, key, readProxyOf);
77
+ },
78
+ set(_target, key) {
79
+ throw readOnly(key);
80
+ },
81
+ deleteProperty(_target, key) {
82
+ throw readOnly(key);
83
+ },
84
+ defineProperty(_target, key) {
85
+ throw readOnly(key);
86
+ },
87
+ setPrototypeOf() {
88
+ throw readOnly("[[Prototype]]");
89
+ },
90
+ preventExtensions() {
91
+ throw readOnly("[[Extensible]]");
92
+ }
93
+ };
94
+ /** Draft handler for one transaction: writes go through the notifying helpers. */
95
+ function draftHandler(tx) {
96
+ const wrap = (raw) => draftOf(raw, tx);
97
+ /** Throws unless the draft is still inside its update and `key` is a string key. */
98
+ const writable = (key) => {
99
+ if (!tx.open) throw new TypeError(`ShadowStore (${tx.label}): a draft was used after its setStore() call returned; write only inside the update`);
100
+ if (typeof key === "symbol") throw new TypeError(`ShadowStore (${tx.label}): symbol keys cannot be written`);
101
+ return key;
102
+ };
103
+ return {
104
+ get(target, key) {
105
+ if (typeof key === "symbol") return Reflect.get(target, key);
106
+ if (hidesPrototype(target, key)) return void 0;
107
+ return childOf(target, key, readRaw(target, key), wrap);
108
+ },
109
+ has(target, key) {
110
+ return Reflect.has(target, key);
111
+ },
112
+ ownKeys(target) {
113
+ return Reflect.ownKeys(target);
114
+ },
115
+ getOwnPropertyDescriptor(target, key) {
116
+ return describeChild(target, key, wrap);
117
+ },
118
+ set(target, key, value) {
119
+ const name = writable(key);
120
+ if (isReservedKey(name)) throw new TypeError(`ShadowStore (${tx.label}): "${name}" cannot be written; it is a reserved key`);
121
+ writeRaw(target, name, unwrapValue(value));
122
+ return true;
123
+ },
124
+ deleteProperty(target, key) {
125
+ return deleteRaw(target, writable(key));
126
+ },
127
+ defineProperty(_target, key) {
128
+ throw new TypeError(`ShadowStore (${tx.label}): defineProperty is not supported on drafts; assign the property instead (key "${String(key)}")`);
129
+ },
130
+ setPrototypeOf() {
131
+ throw new TypeError(`ShadowStore (${tx.label}): drafts cannot change their prototype`);
132
+ },
133
+ preventExtensions() {
134
+ throw new TypeError(`ShadowStore (${tx.label}): drafts cannot be made non-extensible`);
135
+ }
136
+ };
137
+ }
138
+ /** Returns the read-only proxy of a raw object. The same raw object always yields the same proxy. */
139
+ function readProxyOf(raw) {
140
+ const node = nodeOf(raw);
141
+ if (node.proxy === null) {
142
+ const proxy = new Proxy(raw, READ_HANDLER);
143
+ RAW.set(proxy, raw);
144
+ node.proxy = proxy;
145
+ }
146
+ return node.proxy;
147
+ }
148
+ /** Returns the draft proxy of a raw object for one transaction. */
149
+ function draftOf(raw, tx) {
150
+ const existing = tx.drafts.get(raw);
151
+ if (existing !== void 0) return existing;
152
+ const proxy = new Proxy(raw, tx.handler);
153
+ RAW.set(proxy, raw);
154
+ tx.drafts.set(raw, proxy);
155
+ return proxy;
156
+ }
157
+ export { Transaction, draftOf, readProxyOf };
158
+
159
+ //# sourceMappingURL=proxies.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxies.js","names":[],"sources":["../../../src/store/proxies.ts"],"sourcesContent":["import {\n deleteRaw,\n isReservedKey,\n isWrappable,\n nodeOf,\n RAW,\n readRaw,\n trackKeys,\n trackProperty,\n unwrapValue,\n writeRaw,\n} from \"./nodes\";\n\n/**\n * One `setStore` call. Drafts made for it are cached here, so a draft object keeps its identity\n * inside the update, and they stop accepting writes when the call returns.\n */\nexport class Transaction {\n readonly label: string;\n open: boolean;\n readonly drafts: Map<object, object>;\n readonly handler: ProxyHandler<object>;\n\n constructor(label: string) {\n this.label = label;\n this.open = true;\n this.drafts = new Map();\n this.handler = draftHandler(this);\n }\n}\n\nfunction readOnly(key: string | symbol): TypeError {\n const name = typeof key === \"symbol\" ? key.toString() : `\"${key}\"`;\n return new TypeError(\n __DEV__\n ? `ShadowStore: the store is read-only; ${name} can only be changed inside setStore((draft) => ...)`\n : \"SHADOW_STORE_READ_ONLY\",\n );\n}\n\n/**\n * Returns the value to hand out for `key`: a wrapped object when the value is an own, writable\n * wrappable slot, otherwise the raw value. Frozen slots must return the raw value because a proxy\n * may not report a different value for a non-writable, non-configurable property.\n */\nfunction childOf(\n target: object,\n key: string,\n value: unknown,\n wrap: (raw: object) => object,\n): unknown {\n if (!isWrappable(value) || !Object.hasOwn(target, key)) return value;\n const descriptor = Reflect.getOwnPropertyDescriptor(target, key);\n if (\n descriptor !== undefined &&\n descriptor.configurable === false &&\n descriptor.writable === false\n ) {\n return value;\n }\n return wrap(value);\n}\n\n/** Descriptor passthrough that wraps the value the same way `get` does. */\nfunction describeChild(\n target: object,\n key: string | symbol,\n wrap: (raw: object) => object,\n): PropertyDescriptor | undefined {\n const descriptor = Reflect.getOwnPropertyDescriptor(target, key);\n if (descriptor === undefined || typeof key === \"symbol\") return descriptor;\n if (descriptor.configurable === false) return descriptor;\n const value: unknown = descriptor.value;\n if (!isWrappable(value)) return descriptor;\n return { ...descriptor, value: wrap(value) };\n}\n\n/** `__proto__` is only reachable through the prototype chain, so the store never exposes it. */\nfunction hidesPrototype(target: object, key: string): boolean {\n return key === \"__proto__\" && !Object.hasOwn(target, key);\n}\n\n/** True when `key` resolves to an accessor property (own or inherited). */\nfunction hasGetter(target: object, key: string): boolean {\n for (\n let object: object | null = target;\n object !== null;\n object = Object.getPrototypeOf(object)\n ) {\n const descriptor = Reflect.getOwnPropertyDescriptor(object, key);\n if (descriptor !== undefined) return typeof descriptor.get === \"function\";\n }\n return false;\n}\n\n/** Read-only handler: every read subscribes the running computation to the property it touches. */\nconst READ_HANDLER: ProxyHandler<object> = {\n get(target, key, receiver) {\n if (typeof key === \"symbol\") return Reflect.get(target, key);\n if (hidesPrototype(target, key)) return undefined;\n trackProperty(nodeOf(target), key);\n // A getter runs with the store view as `this`, so what it reads is tracked too.\n if (hasGetter(target, key)) return Reflect.get(target, key, receiver);\n return childOf(target, key, readRaw(target, key), readProxyOf);\n },\n has(target, key) {\n if (typeof key !== \"symbol\") trackProperty(nodeOf(target), key);\n return Reflect.has(target, key);\n },\n ownKeys(target) {\n trackKeys(nodeOf(target));\n return Reflect.ownKeys(target);\n },\n getOwnPropertyDescriptor(target, key) {\n return describeChild(target, key, readProxyOf);\n },\n set(_target, key) {\n throw readOnly(key);\n },\n deleteProperty(_target, key) {\n throw readOnly(key);\n },\n defineProperty(_target, key) {\n throw readOnly(key);\n },\n setPrototypeOf() {\n throw readOnly(\"[[Prototype]]\");\n },\n preventExtensions() {\n throw readOnly(\"[[Extensible]]\");\n },\n};\n\n/** Draft handler for one transaction: writes go through the notifying helpers. */\nfunction draftHandler(tx: Transaction): ProxyHandler<object> {\n const wrap = (raw: object): object => draftOf(raw, tx);\n /** Throws unless the draft is still inside its update and `key` is a string key. */\n const writable = (key: string | symbol): string => {\n if (!tx.open) {\n throw new TypeError(\n __DEV__\n ? `ShadowStore (${tx.label}): a draft was used after its setStore() call returned; write only inside the update`\n : \"SHADOW_STORE_DRAFT_STALE\",\n );\n }\n if (typeof key === \"symbol\") {\n throw new TypeError(\n __DEV__\n ? `ShadowStore (${tx.label}): symbol keys cannot be written`\n : \"SHADOW_STORE_SYMBOL_KEY\",\n );\n }\n return key;\n };\n return {\n get(target, key) {\n if (typeof key === \"symbol\") return Reflect.get(target, key);\n if (hidesPrototype(target, key)) return undefined;\n return childOf(target, key, readRaw(target, key), wrap);\n },\n has(target, key) {\n return Reflect.has(target, key);\n },\n ownKeys(target) {\n return Reflect.ownKeys(target);\n },\n getOwnPropertyDescriptor(target, key) {\n return describeChild(target, key, wrap);\n },\n set(target, key, value) {\n const name = writable(key);\n if (isReservedKey(name)) {\n throw new TypeError(\n __DEV__\n ? `ShadowStore (${tx.label}): \"${name}\" cannot be written; it is a reserved key`\n : \"SHADOW_STORE_RESERVED_KEY\",\n );\n }\n writeRaw(target, name, unwrapValue(value));\n return true;\n },\n deleteProperty(target, key) {\n return deleteRaw(target, writable(key));\n },\n defineProperty(_target, key) {\n throw new TypeError(\n __DEV__\n ? `ShadowStore (${tx.label}): defineProperty is not supported on drafts; assign the property instead (key \"${String(key)}\")`\n : \"SHADOW_STORE_DEFINE_PROPERTY\",\n );\n },\n setPrototypeOf() {\n throw new TypeError(\n __DEV__\n ? `ShadowStore (${tx.label}): drafts cannot change their prototype`\n : \"SHADOW_STORE_PROTOTYPE\",\n );\n },\n preventExtensions() {\n throw new TypeError(\n __DEV__\n ? `ShadowStore (${tx.label}): drafts cannot be made non-extensible`\n : \"SHADOW_STORE_NON_EXTENSIBLE\",\n );\n },\n };\n}\n\n/** Returns the read-only proxy of a raw object. The same raw object always yields the same proxy. */\nexport function readProxyOf(raw: object): object {\n const node = nodeOf(raw);\n if (node.proxy === null) {\n const proxy = new Proxy(raw, READ_HANDLER);\n RAW.set(proxy, raw);\n node.proxy = proxy;\n }\n return node.proxy;\n}\n\n/** Returns the draft proxy of a raw object for one transaction. */\nexport function draftOf(raw: object, tx: Transaction): object {\n const existing = tx.drafts.get(raw);\n if (existing !== undefined) return existing;\n const proxy = new Proxy(raw, tx.handler);\n RAW.set(proxy, raw);\n tx.drafts.set(raw, proxy);\n return proxy;\n}\n"],"mappings":";;;;;;AAiBA,IAAa,cAAb,MAAyB;CACvB;CACA;CACA;CACA;CAEA,YAAY,OAAe;EACzB,KAAK,QAAQ;EACb,KAAK,OAAO;EACZ,KAAK,yBAAS,IAAI,IAAI;EACtB,KAAK,UAAU,aAAa,IAAI;CAClC;AACF;AAEA,SAAS,SAAS,KAAiC;CACjD,MAAM,OAAO,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ns the raw data behind a store. Reads and writes on the result bypass tracking and
5
+ * notification, so writing to it is a bug; use it for identity checks and passing data on.
6
+ *
7
+ * @param store A store view. A value that is not a store view is returned as is.
8
+ * @returns The underlying object, without proxies. It is the live data, not a copy.
9
+ * @example
10
+ * import { unwrap, useStore } from "@shadow-js/core";
11
+ *
12
+ * const initial = { items: [1, 2, 3] };
13
+ * const [list] = useStore(initial);
14
+ * console.log(unwrap(list) === initial); // true: the store wraps its data in place
15
+ * @see snapshot
16
+ * @since 1.0.0
17
+ */
18
+ function unwrap(store) {
19
+ return unwrapValue(store);
20
+ }
21
+ /** A new, empty container of the same kind as `value`, with the same length for arrays. */
22
+ function shellOf(value) {
23
+ return Array.isArray(value) ? new Array(value.length) : {};
24
+ }
25
+ /**
26
+ * Returns a deep plain copy of a store's current data. It reads raw data directly, so it does
27
+ * not subscribe the running computation, and the copy is independent of later writes. Cycles and
28
+ * shared objects are preserved. The walk is iterative, so deep data cannot overflow the stack. Use
29
+ * it to serialize a store, send it to a server or keep an undo history.
30
+ *
31
+ * @param store A store view. A value that is not plain data is returned as is.
32
+ * @returns A plain, untracked deep copy.
33
+ * @example
34
+ * import { snapshot, useStore } from "@shadow-js/core";
35
+ *
36
+ * const [form, setForm] = useStore({ name: "Ada", tags: ["admin"] });
37
+ * const saved = snapshot(form);
38
+ * setForm({ name: "Grace" });
39
+ * console.log(saved.name, JSON.stringify(saved)); // Ada {"name":"Ada","tags":["admin"]}
40
+ * @see unwrap
41
+ * @since 1.0.0
42
+ */
43
+ function snapshot(store) {
44
+ const root = unwrapValue(store);
45
+ if (!isWrappable(root)) return root;
46
+ const copies = /* @__PURE__ */ new Map();
47
+ const out = shellOf(root);
48
+ copies.set(root, out);
49
+ const pending = [root];
50
+ for (let source = pending.pop(); source !== void 0; source = pending.pop()) {
51
+ const copy = copies.get(source);
52
+ if (copy === void 0) continue;
53
+ for (const key of Object.keys(source)) {
54
+ if (isReservedKey(key)) continue;
55
+ const value = unwrapValue(readRaw(source, key));
56
+ if (isWrappable(value)) {
57
+ let child = copies.get(value);
58
+ if (child === void 0) {
59
+ child = shellOf(value);
60
+ copies.set(value, child);
61
+ pending.push(value);
62
+ }
63
+ Reflect.set(copy, key, child);
64
+ } else Reflect.set(copy, key, value);
65
+ }
66
+ }
67
+ return out;
68
+ }
69
+ export { snapshot, unwrap };
70
+
71
+ //# sourceMappingURL=read.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read.js","names":[],"sources":["../../../src/store/read.ts"],"sourcesContent":["import { isReservedKey, isWrappable, readRaw, unwrapValue } from \"./nodes\";\nimport type { Mutable, Store } from \"./types\";\n\n/**\n * Returns the raw data behind a store. Reads and writes on the result bypass tracking and\n * notification, so writing to it is a bug; use it for identity checks and passing data on.\n *\n * @param store A store view. A value that is not a store view is returned as is.\n * @returns The underlying object, without proxies. It is the live data, not a copy.\n * @example\n * import { unwrap, useStore } from \"@shadow-js/core\";\n *\n * const initial = { items: [1, 2, 3] };\n * const [list] = useStore(initial);\n * console.log(unwrap(list) === initial); // true: the store wraps its data in place\n * @see snapshot\n * @since 1.0.0\n */\nexport function unwrap<T>(store: Store<T>): Mutable<T> {\n return unwrapValue(store) as Mutable<T>;\n}\n\n/** A new, empty container of the same kind as `value`, with the same length for arrays. */\nfunction shellOf(value: object): object {\n return Array.isArray(value) ? new Array<unknown>((value as unknown[]).length) : {};\n}\n\n/**\n * Returns a deep plain copy of a store's current data. It reads raw data directly, so it does\n * not subscribe the running computation, and the copy is independent of later writes. Cycles and\n * shared objects are preserved. The walk is iterative, so deep data cannot overflow the stack. Use\n * it to serialize a store, send it to a server or keep an undo history.\n *\n * @param store A store view. A value that is not plain data is returned as is.\n * @returns A plain, untracked deep copy.\n * @example\n * import { snapshot, useStore } from \"@shadow-js/core\";\n *\n * const [form, setForm] = useStore({ name: \"Ada\", tags: [\"admin\"] });\n * const saved = snapshot(form);\n * setForm({ name: \"Grace\" });\n * console.log(saved.name, JSON.stringify(saved)); // Ada {\"name\":\"Ada\",\"tags\":[\"admin\"]}\n * @see unwrap\n * @since 1.0.0\n */\nexport function snapshot<T>(store: Store<T>): Mutable<T> {\n const root = unwrapValue(store);\n if (!isWrappable(root)) return root as Mutable<T>;\n const copies = new Map<object, object>();\n const out: object = shellOf(root);\n copies.set(root, out);\n const pending: object[] = [root];\n for (let source = pending.pop(); source !== undefined; source = pending.pop()) {\n const copy = copies.get(source);\n if (copy === undefined) continue;\n for (const key of Object.keys(source)) {\n if (isReservedKey(key)) continue;\n const value = unwrapValue(readRaw(source, key));\n if (isWrappable(value)) {\n let child = copies.get(value);\n if (child === undefined) {\n child = shellOf(value);\n copies.set(value, child);\n pending.push(value);\n }\n Reflect.set(copy, key, child);\n } else {\n Reflect.set(copy, key, value);\n }\n }\n }\n return out as Mutable<T>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAkBA,SAAgB,OAAU,OAA6B;CACrD,OAAO,YAAY,KAAK;AAC1B;;AAGA,SAAS,QAAQ,OAAuB;CACtC,OAAO,MAAM,QAAQ,KAAK,IAAI,IAAI,MAAgB,MAAoB,MAAM,IAAI,CAAC;AACnF;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,SAAY,OAA6B;CACvD,MAAM,OAAO,YAAY,KAAK;CAC9B,IAAI,CAAC,YAAY,IAAI,GAAG,OAAO;CAC/B,MAAM,yBAAS,IAAI,IAAoB;CACvC,MAAM,MAAc,QAAQ,IAAI;CAChC,OAAO,IAAI,MAAM,GAAG;CACpB,MAAM,UAAoB,CAAC,IAAI;CAC/B,KAAK,IAAI,SAAS,QAAQ,IAAI,GAAG,WAAW,KAAA,GAAW,SAAS,QAAQ,IAAI,GAAG;EAC7E,MAAM,OAAO,OAAO,IAAI,MAAM;EAC9B,IAAI,SAAS,KAAA,GAAW;EACxB,KAAK,MAAM,OAAO,OAAO,KAAK,MAAM,GAAG;GACrC,IAAI,cAAc,GAAG,GAAG;GACxB,MAAM,QAAQ,YAAY,QAAQ,QAAQ,GAAG,CAAC;GAC9C,IAAI,YAAY,KAAK,GAAG;IACtB,IAAI,QAAQ,OAAO,IAAI,KAAK;IAC5B,IAAI,UAAU,KAAA,GAAW;KACvB,QAAQ,QAAQ,KAAK;KACrB,OAAO,IAAI,OAAO,KAAK;KACvB,QAAQ,KAAK,KAAK;IACpB;IACA,QAAQ,IAAI,MAAM,KAAK,KAAK;GAC9B,OACE,QAAQ,IAAI,MAAM,KAAK,KAAK;EAEhC;CACF;CACA,OAAO;AACT"}
@@ -0,0 +1,185 @@
1
+ import { deleteRaw, isReservedKey, isWrappable, unwrapValue, writeRaw } from "./nodes.js";
2
+ // src/store/reconcile.ts
3
+ /** Each reconciler function maps to its spec. Keys are the reconciler functions themselves. */
4
+ const SPECS = /* @__PURE__ */ new WeakMap();
5
+ /**
6
+ * Looks up the spec of a reconciler created by {@link reconcile}. Not re-exported by any entry: the
7
+ * store setter uses it to tell a reconciler from a draft updater.
8
+ *
9
+ * @param update Any function or object passed to `setStore`.
10
+ * @returns The spec, or `undefined` when `update` is not a `reconcile()` result.
11
+ * @internal
12
+ */
13
+ function specOf(update) {
14
+ return SPECS.get(update);
15
+ }
16
+ /**
17
+ * Describes a new value for a store. Passed to `setStore`, it changes only what differs from the
18
+ * current state: objects are merged key by key, arrays are matched by `key` so retained items keep
19
+ * their identity, and unchanged leaves are not written. Nothing is applied until `setStore` runs.
20
+ * Use it to apply fresh server data to a store without re-rendering what did not change.
21
+ *
22
+ * @param next The desired data. Store proxies are accepted and unwrapped.
23
+ * @param options Item identity (`key`) and whether to keep absent keys (`merge`). Undefined uses
24
+ * the defaults (`key: "id"`, `merge: false`).
25
+ * @returns A reconciler to pass to `setStore`, alone or as the last argument of a path.
26
+ * @throws TypeError (production message `SHADOW_STORE_RECONCILE`) when the returned reconciler is
27
+ * called directly instead of being passed to `setStore`. `setStore` throws TypeError
28
+ * (`SHADOW_STORE_RECONCILE_SHAPE`) when `next` is not the same kind of container (object or
29
+ * array) as the value it reconciles.
30
+ * @example
31
+ * import { reconcile, useStore } from "@shadow-js/core";
32
+ *
33
+ * interface Todo { id: number; title: string; done: boolean }
34
+ * const [todos, setTodos] = useStore({ items: [] as Todo[] });
35
+ *
36
+ * async function refresh(): Promise<void> {
37
+ * const response = await fetch("/api/todos");
38
+ * setTodos("items", reconcile((await response.json()) as Todo[], { key: "id" }));
39
+ * }
40
+ * @see useStore
41
+ * @since 1.0.0
42
+ */
43
+ function reconcile(next, options) {
44
+ const spec = {
45
+ next,
46
+ key: options?.key ?? "id",
47
+ merge: options?.merge === true
48
+ };
49
+ const reconciler = () => {
50
+ throw new TypeError("ShadowStore: a reconcile() result must be passed to setStore()");
51
+ };
52
+ SPECS.set(reconciler, spec);
53
+ return reconciler;
54
+ }
55
+ /** Field name or function giving an item's identity; `undefined` means the item has none. */
56
+ function identityOf(item, key) {
57
+ if (!isWrappable(item)) return void 0;
58
+ if (typeof key === "function") return key(item);
59
+ return Object.hasOwn(item, key) ? Reflect.get(item, key) : void 0;
60
+ }
61
+ /** Same container kind: both arrays or both objects. */
62
+ function sameKind(a, b) {
63
+ return Array.isArray(a) === Array.isArray(b);
64
+ }
65
+ /**
66
+ * Applies a reconciler to the store root. The diff is iterative: pending (target, source) pairs
67
+ * live on an explicit stack, so data nested thousands of levels deep cannot overflow the call
68
+ * stack. Each pair is visited once, which also terminates on cyclic input. Not re-exported by any
69
+ * entry: only the store setter calls it, inside its transaction.
70
+ *
71
+ * @param root The raw (unwrapped) object or array to update in place.
72
+ * @param spec The reconciler's spec, from {@link specOf}.
73
+ * @param label The store label for messages (` (name)`, or an empty string).
74
+ * @throws TypeError (production message `SHADOW_STORE_RECONCILE_SHAPE`) when `spec.next` is not the
75
+ * same kind of container as `root`.
76
+ * @internal
77
+ */
78
+ function applyReconcile(root, spec, label) {
79
+ const next = unwrapValue(spec.next);
80
+ if (!isWrappable(next) || !sameKind(root, next)) throw new TypeError(`ShadowStore${label}: reconcile() needs data of the same shape as the store root (an object or an array)`);
81
+ const stack = [];
82
+ const seen = /* @__PURE__ */ new Map();
83
+ const enqueue = (target, source) => {
84
+ let targets = seen.get(source);
85
+ if (targets === void 0) {
86
+ targets = /* @__PURE__ */ new Set();
87
+ seen.set(source, targets);
88
+ }
89
+ if (targets.has(target)) return;
90
+ targets.add(target);
91
+ stack.push({
92
+ target,
93
+ source
94
+ });
95
+ };
96
+ enqueue(root, next);
97
+ for (let frame = stack.pop(); frame !== void 0; frame = stack.pop()) if (Array.isArray(frame.source)) mergeArray(frame.target, frame.source, spec, enqueue);
98
+ else mergeObject(frame.target, frame.source, spec, enqueue);
99
+ }
100
+ function mergeObject(target, source, spec, enqueue) {
101
+ for (const key of Object.keys(source)) {
102
+ if (isReservedKey(key)) continue;
103
+ const value = unwrapValue(Reflect.get(source, key));
104
+ const had = Object.hasOwn(target, key);
105
+ const current = had ? Reflect.get(target, key) : void 0;
106
+ if (had && Object.is(current, value)) continue;
107
+ if (had && isWrappable(current) && isWrappable(value) && sameKind(current, value)) {
108
+ enqueue(current, value);
109
+ continue;
110
+ }
111
+ writeRaw(target, key, value);
112
+ }
113
+ if (spec.merge) return;
114
+ for (const key of Object.keys(target)) if (!Object.hasOwn(source, key)) deleteRaw(target, key);
115
+ }
116
+ function mergeArray(target, source, spec, enqueue) {
117
+ const list = target;
118
+ const incoming = source;
119
+ const original = list.slice();
120
+ const identities = original.map((item) => identityOf(item, spec.key));
121
+ const byIdentity = /* @__PURE__ */ new Map();
122
+ for (let index = 0; index < original.length; index += 1) {
123
+ const identity = identities[index];
124
+ if (identity !== void 0 && !byIdentity.has(identity)) byIdentity.set(identity, index);
125
+ }
126
+ const used = new Array(original.length).fill(false);
127
+ const matches = [];
128
+ for (let position = 0; position < incoming.length; position += 1) {
129
+ const identity = identityOf(unwrapValue(incoming[position]), spec.key);
130
+ let match = -1;
131
+ if (identity !== void 0) {
132
+ const found = byIdentity.get(identity);
133
+ if (found !== void 0 && used[found] !== true) match = found;
134
+ } else if (position < original.length && identities[position] === void 0) {
135
+ if (used[position] !== true) match = position;
136
+ }
137
+ if (match !== -1) used[match] = true;
138
+ matches.push(match);
139
+ }
140
+ const values = [];
141
+ for (let position = 0; position < incoming.length; position += 1) {
142
+ const item = unwrapValue(incoming[position]);
143
+ const match = matches[position] ?? -1;
144
+ if (match === -1) {
145
+ values.push(item);
146
+ continue;
147
+ }
148
+ const previous = original[match];
149
+ if (isWrappable(previous) && isWrappable(item) && sameKind(previous, item)) {
150
+ if (previous !== item) enqueue(previous, item);
151
+ values.push(previous);
152
+ } else values.push(item);
153
+ }
154
+ if (spec.merge) {
155
+ for (let index = 0; index < original.length; index += 1) if (used[index] !== true) values.push(original[index]);
156
+ }
157
+ for (let position = 0; position < values.length; position += 1) writeRaw(target, String(position), values[position]);
158
+ if (list.length > values.length) writeRaw(target, "length", values.length);
159
+ }
160
+ /**
161
+ * Marks a draft updater. It is the function itself (`setStore(produce((d) => { d.n += 1 }))` is
162
+ * `setStore((d) => { d.n += 1 })`); it exists so code written for other libraries migrates as is.
163
+ *
164
+ * @param update The draft updater: it mutates the draft it receives.
165
+ * @returns `update`, unchanged.
166
+ * @example
167
+ * import { produce, useStore } from "@shadow-js/core";
168
+ *
169
+ * const [cart, setCart] = useStore({ items: [] as string[], count: 0 });
170
+ * setCart(
171
+ * produce((draft) => {
172
+ * draft.items.push("apple");
173
+ * draft.count += 1;
174
+ * }),
175
+ * );
176
+ * console.log(cart.count); // 1
177
+ * @see useStore
178
+ * @since 1.0.0
179
+ */
180
+ function produce(update) {
181
+ return update;
182
+ }
183
+ export { applyReconcile, produce, reconcile, specOf };
184
+
185
+ //# sourceMappingURL=reconcile.js.map