@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,286 @@
1
+ import { DEVTOOLS_EVENT, report } from "./devtools.js";
2
+ import { ShadowError, warn } from "./diagnostics.js";
3
+ import { FLAG_PARKED, FLAG_TRANSITION } from "./flags.js";
4
+ import { rt } from "./state.js";
5
+ import { handleError } from "./owner.js";
6
+ import { clearQueues, hasPending, renderQueue, userQueue } from "./queue.js";
7
+ import { recompute, updateIfNecessary } from "./graph.js";
8
+ // src/internal/scheduler.ts
9
+ /**
10
+ * Flush passes allowed before the loop guard aborts (R-CODE-12).
11
+ * @internal
12
+ */
13
+ const MAX_FLUSH_PASSES = 1e3;
14
+ let microtaskScheduled = false;
15
+ /**
16
+ * Sort order of queued effects: parents before children (by depth), siblings by creation (G-16).
17
+ * @internal
18
+ */
19
+ function byCreation(a, b) {
20
+ return a.depth - b.depth || a.seq - b.seq;
21
+ }
22
+ /**
23
+ * Effects mostly arrive in creation order already; checking is O(n), sorting is not.
24
+ * @internal
25
+ */
26
+ function isOrdered(list) {
27
+ for (let i = 1; i < list.length; i += 1) {
28
+ const previous = list[i - 1];
29
+ const current = list[i];
30
+ if (previous !== void 0 && current !== void 0 && byCreation(previous, current) > 0) return false;
31
+ }
32
+ return true;
33
+ }
34
+ /** Runs every queued effect of one phase, parents before children, siblings by creation. */
35
+ function drain(queue) {
36
+ const batch = queue.splice(0, queue.length);
37
+ if (!isOrdered(batch)) batch.sort(byCreation);
38
+ for (const effect of batch) {
39
+ effect.flags &= -3;
40
+ if ((effect.flags & 4) !== 0) continue;
41
+ if (effect.pauses > 0) {
42
+ effect.flags = effect.flags & ~FLAG_TRANSITION | FLAG_PARKED;
43
+ continue;
44
+ }
45
+ try {
46
+ if (effect.state === 2) recompute(effect);
47
+ else updateIfNecessary(effect);
48
+ } catch (error) {
49
+ handleError(error, effect);
50
+ }
51
+ }
52
+ return batch.length;
53
+ }
54
+ function raise(errors) {
55
+ const [first] = errors;
56
+ if (errors.length === 1) throw first;
57
+ throw new AggregateError(errors, "Several errors were thrown while flushing effects.");
58
+ }
59
+ /**
60
+ * Runs every pending effect now: what the scheduled microtask does. Drains the queues until nothing
61
+ * is pending; render effects always run before user effects, parents before children, siblings by
62
+ * creation, and effects woken by a write in the flush run in a following pass. Errors nobody handled
63
+ * are collected and thrown once the queues are empty, so one failing effect never wedges the rest.
64
+ * Does nothing when a flush is already running. Transition-lane effects are not run here: they wait
65
+ * for the lane. Prefer {@link flushSync} in application code.
66
+ *
67
+ * @throws The error of an effect that no error handler caught; several are thrown together as an
68
+ * `AggregateError`.
69
+ * @throws ShadowError `INFINITE_LOOP` when effects keep waking each other for more than 1000 passes:
70
+ * the queued effects are dropped and the flush stops.
71
+ * @example
72
+ * import { flush, writeSignal, type SignalNode } from "@shadow-js/core/extend";
73
+ *
74
+ * export function setNow(signal: SignalNode<number>, value: number): void {
75
+ * writeSignal(signal, value);
76
+ * flush(); // the effects that read the signal have run
77
+ * }
78
+ * @see flushSync
79
+ * @since 1.0.0
80
+ */
81
+ function flush() {
82
+ if (rt.flushing) return;
83
+ rt.flushing = true;
84
+ let passes = 0;
85
+ let runs = 0;
86
+ report(DEVTOOLS_EVENT.FLUSH_START, renderQueue.length, userQueue.length);
87
+ try {
88
+ while (hasPending()) {
89
+ let aborted = false;
90
+ while (renderQueue.length > 0) {
91
+ passes += 1;
92
+ if (passes > 1e3) {
93
+ aborted = true;
94
+ break;
95
+ }
96
+ runs += drain(renderQueue);
97
+ }
98
+ if (!aborted && userQueue.length > 0) {
99
+ passes += 1;
100
+ if (passes > 1e3) aborted = true;
101
+ else runs += drain(userQueue);
102
+ }
103
+ if (aborted) {
104
+ clearQueues();
105
+ rt.uncaught.push(new ShadowError("INFINITE_LOOP"));
106
+ break;
107
+ }
108
+ }
109
+ } finally {
110
+ rt.flushing = false;
111
+ report(DEVTOOLS_EVENT.FLUSH_END, runs);
112
+ }
113
+ const errors = rt.uncaught;
114
+ if (errors.length > 0) {
115
+ rt.uncaught = [];
116
+ raise(errors);
117
+ }
118
+ }
119
+ /**
120
+ * Surfaces an error from the microtask flush where nothing can catch it.
121
+ * @internal
122
+ */
123
+ function reportUncaught(error) {
124
+ if (typeof reportError === "function") reportError(error);
125
+ else queueMicrotask(() => Promise.reject(error));
126
+ }
127
+ function runScheduled() {
128
+ microtaskScheduled = false;
129
+ if (!hasPending()) return;
130
+ try {
131
+ flush();
132
+ } catch (error) {
133
+ reportUncaught(error);
134
+ }
135
+ }
136
+ /**
137
+ * Requests a flush in a microtask: call it after marking observers stale by hand, as
138
+ * {@link markObservers} does not. Does nothing when a microtask flush is already due, inside a
139
+ * batch or group (which flushes or schedules on exit), during a flush (which picks the work up), or
140
+ * during a transition (its effects wait for the lane). An error thrown by that flush has no caller
141
+ * to reach: it goes to the global `reportError`, or becomes an unhandled promise rejection.
142
+ *
143
+ * @example
144
+ * import { markObservers, schedule, type SourceNode } from "@shadow-js/core/extend";
145
+ *
146
+ * export function touch(source: SourceNode): void {
147
+ * markObservers(source);
148
+ * schedule(); // the effects that read `source` run in a microtask
149
+ * }
150
+ * @see notify
151
+ * @since 1.0.0
152
+ */
153
+ function schedule() {
154
+ if (rt.inTransition || rt.batchDepth > 0 || rt.flushing || microtaskScheduled) return;
155
+ microtaskScheduled = true;
156
+ queueMicrotask(runScheduled);
157
+ }
158
+ /**
159
+ * Groups writes like {@link batch} but never flushes synchronously: when the outermost group ends,
160
+ * the effects wait for the microtask (or an enclosing batch / `flushSync`), exactly as after a
161
+ * single signal write. Store setters use it so that one `setStore(...)` behaves like one `set()`;
162
+ * use it for a primitive whose one call makes several writes.
163
+ *
164
+ * @param fn The writes to group. Its throw is rethrown after the group ends.
165
+ * @returns What `fn` returns.
166
+ * @example
167
+ * import { useSignal } from "@shadow-js/core";
168
+ * import { group } from "@shadow-js/core/extend";
169
+ *
170
+ * const [first, setFirst] = useSignal("Ada");
171
+ * const [last, setLast] = useSignal("Lovelace");
172
+ * group(() => {
173
+ * setFirst("Grace");
174
+ * setLast("Hopper");
175
+ * }); // effects reading both run once, in the next microtask
176
+ * @see batch
177
+ * @since 1.0.0
178
+ */
179
+ function group(fn) {
180
+ rt.batchDepth += 1;
181
+ try {
182
+ return fn();
183
+ } finally {
184
+ rt.batchDepth -= 1;
185
+ if (rt.batchDepth === 0 && hasPending()) schedule();
186
+ }
187
+ }
188
+ function combine(primary, secondary) {
189
+ return new AggregateError([primary, secondary], "Both the callback and the flush threw.");
190
+ }
191
+ /**
192
+ * Runs `fn` with effects deferred until the outermost batch ends, then runs them synchronously, so
193
+ * several writes wake each effect once. Reads inside the batch always see fresh values (G-6). Inside
194
+ * a running flush the effects are left to that flush.
195
+ *
196
+ * @param fn The writes to batch.
197
+ * @returns What `fn` returns.
198
+ * @throws What `fn` throws, after the flush. An error of the flush is thrown as {@link flush} throws
199
+ * it (including ShadowError `INFINITE_LOOP`); when `fn` threw as well, both are thrown together as an
200
+ * `AggregateError`.
201
+ * @example
202
+ * import { batch, useSignal } from "@shadow-js/core";
203
+ *
204
+ * const [first, setFirst] = useSignal("Ada");
205
+ * const [last, setLast] = useSignal("Lovelace");
206
+ * batch(() => {
207
+ * setFirst("Grace");
208
+ * setLast("Hopper");
209
+ * }); // effects reading both have run once, here
210
+ * @see flushSync
211
+ * @since 1.0.0
212
+ */
213
+ function batch(fn) {
214
+ rt.batchDepth += 1;
215
+ let result;
216
+ let failure;
217
+ let failed = false;
218
+ try {
219
+ result = fn();
220
+ } catch (error) {
221
+ failed = true;
222
+ failure = error;
223
+ } finally {
224
+ rt.batchDepth -= 1;
225
+ }
226
+ if (rt.batchDepth === 0 && !rt.flushing && hasPending()) try {
227
+ flush();
228
+ } catch (error) {
229
+ if (failed) throw combine(failure, error);
230
+ throw error;
231
+ }
232
+ if (failed) throw failure;
233
+ return result;
234
+ }
235
+ /**
236
+ * Runs `fn` (optional) in a batch and runs every pending effect before returning, so the DOM and
237
+ * other effects are up to date on the next line. Use it in tests and before reading layout. Called
238
+ * while a flush is running (from inside an effect), it only runs `fn` and leaves the effects to that
239
+ * flush; development builds warn `FLUSH_IN_EFFECT`.
240
+ *
241
+ * @param fn The writes to make first. Undefined only flushes.
242
+ * @returns What `fn` returns, or undefined without `fn`.
243
+ * @throws What `fn` throws, after the flush. An error of the flush is thrown as {@link flush} throws
244
+ * it (including ShadowError `INFINITE_LOOP`); when `fn` threw as well, both are thrown together as an
245
+ * `AggregateError`.
246
+ * @example
247
+ * import { createRoot, flushSync, useEffect, useSignal } from "@shadow-js/core";
248
+ *
249
+ * createRoot(() => {
250
+ * const [count, setCount] = useSignal(0);
251
+ * useEffect(() => console.log(count()));
252
+ * flushSync(); // logs 0
253
+ * flushSync(() => setCount(1)); // logs 1 before this line ends
254
+ * });
255
+ * @see batch
256
+ * @since 1.0.0
257
+ */
258
+ function flushSync(fn) {
259
+ if (rt.flushing) {
260
+ warn("FLUSH_IN_EFFECT");
261
+ return fn === void 0 ? void 0 : fn();
262
+ }
263
+ rt.batchDepth += 1;
264
+ let result;
265
+ let failure;
266
+ let failed = false;
267
+ try {
268
+ result = fn === void 0 ? void 0 : fn();
269
+ } catch (error) {
270
+ failed = true;
271
+ failure = error;
272
+ } finally {
273
+ rt.batchDepth -= 1;
274
+ }
275
+ try {
276
+ flush();
277
+ } catch (error) {
278
+ if (failed) throw combine(failure, error);
279
+ throw error;
280
+ }
281
+ if (failed) throw failure;
282
+ return result;
283
+ }
284
+ export { MAX_FLUSH_PASSES, batch, byCreation, flush, flushSync, group, isOrdered, reportUncaught, schedule };
285
+
286
+ //# sourceMappingURL=scheduler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.js","names":[],"sources":["../../../src/internal/scheduler.ts"],"sourcesContent":["import { DEVTOOLS_EVENT, report } from \"./devtools\";\nimport { ShadowError, warn } from \"./diagnostics\";\nimport { DIRTY, FLAG_DISPOSED, FLAG_PARKED, FLAG_QUEUED, FLAG_TRANSITION } from \"./flags\";\nimport type { Computation } from \"./graph\";\nimport { recompute, updateIfNecessary } from \"./graph\";\nimport { handleError } from \"./owner\";\nimport { clearQueues, hasPending, renderQueue, userQueue } from \"./queue\";\nimport { rt } from \"./state\";\n\n/**\n * Flush passes allowed before the loop guard aborts (R-CODE-12).\n * @internal\n */\nexport const MAX_FLUSH_PASSES = 1000;\n\nlet microtaskScheduled = false;\n\n/**\n * Sort order of queued effects: parents before children (by depth), siblings by creation (G-16).\n * @internal\n */\nexport function byCreation(a: Computation, b: Computation): number {\n return a.depth - b.depth || a.seq - b.seq;\n}\n\n/**\n * Effects mostly arrive in creation order already; checking is O(n), sorting is not.\n * @internal\n */\nexport function isOrdered(list: readonly Computation[]): boolean {\n for (let i = 1; i < list.length; i += 1) {\n const previous = list[i - 1];\n const current = list[i];\n if (previous !== undefined && current !== undefined && byCreation(previous, current) > 0) {\n return false;\n }\n }\n return true;\n}\n\n/** Runs every queued effect of one phase, parents before children, siblings by creation. */\nfunction drain(queue: Computation[]): number {\n const batch = queue.splice(0, queue.length);\n if (!isOrdered(batch)) batch.sort(byCreation);\n for (const effect of batch) {\n effect.flags &= ~FLAG_QUEUED;\n if ((effect.flags & FLAG_DISPOSED) !== 0) continue;\n if (effect.pauses > 0) {\n // Paused after it was queued: wait (unqueued) until the owner resumes.\n effect.flags = (effect.flags & ~FLAG_TRANSITION) | FLAG_PARKED;\n continue;\n }\n try {\n // A directly dirtied effect needs no source verification: skip the frame machinery.\n if (effect.state === DIRTY) recompute(effect);\n else updateIfNecessary(effect);\n } catch (error) {\n handleError(error, effect);\n }\n }\n return batch.length;\n}\n\nfunction raise(errors: unknown[]): void {\n const [first] = errors;\n if (errors.length === 1) throw first;\n throw new AggregateError(\n errors,\n __DEV__ ? \"Several errors were thrown while flushing effects.\" : \"SHADOW_FLUSH_ERRORS\",\n );\n}\n\n/**\n * Runs every pending effect now: what the scheduled microtask does. Drains the queues until nothing\n * is pending; render effects always run before user effects, parents before children, siblings by\n * creation, and effects woken by a write in the flush run in a following pass. Errors nobody handled\n * are collected and thrown once the queues are empty, so one failing effect never wedges the rest.\n * Does nothing when a flush is already running. Transition-lane effects are not run here: they wait\n * for the lane. Prefer {@link flushSync} in application code.\n *\n * @throws The error of an effect that no error handler caught; several are thrown together as an\n * `AggregateError`.\n * @throws ShadowError `INFINITE_LOOP` when effects keep waking each other for more than 1000 passes:\n * the queued effects are dropped and the flush stops.\n * @example\n * import { flush, writeSignal, type SignalNode } from \"@shadow-js/core/extend\";\n *\n * export function setNow(signal: SignalNode<number>, value: number): void {\n * writeSignal(signal, value);\n * flush(); // the effects that read the signal have run\n * }\n * @see flushSync\n * @since 1.0.0\n */\nexport function flush(): void {\n if (rt.flushing) return;\n rt.flushing = true;\n let passes = 0;\n let runs = 0;\n if (__DEV__) report(DEVTOOLS_EVENT.FLUSH_START, renderQueue.length, userQueue.length);\n try {\n while (hasPending()) {\n let aborted = false;\n // Render passes count too: a render effect that keeps writing its own source must abort.\n while (renderQueue.length > 0) {\n passes += 1;\n if (passes > MAX_FLUSH_PASSES) {\n aborted = true;\n break;\n }\n runs += drain(renderQueue);\n }\n if (!aborted && userQueue.length > 0) {\n passes += 1;\n if (passes > MAX_FLUSH_PASSES) aborted = true;\n else runs += drain(userQueue);\n }\n if (aborted) {\n clearQueues();\n rt.uncaught.push(new ShadowError(\"INFINITE_LOOP\"));\n break;\n }\n }\n } finally {\n rt.flushing = false;\n if (__DEV__) report(DEVTOOLS_EVENT.FLUSH_END, runs);\n }\n const errors = rt.uncaught;\n if (errors.length > 0) {\n rt.uncaught = [];\n raise(errors);\n }\n}\n\n/**\n * Surfaces an error from the microtask flush where nothing can catch it.\n * @internal\n */\nexport function reportUncaught(error: unknown): void {\n if (typeof reportError === \"function\") reportError(error);\n else queueMicrotask(() => Promise.reject(error));\n}\n\nfunction runScheduled(): void {\n microtaskScheduled = false;\n if (!hasPending()) return;\n try {\n flush();\n } catch (error) {\n reportUncaught(error);\n }\n}\n\n/**\n * Requests a flush in a microtask: call it after marking observers stale by hand, as\n * {@link markObservers} does not. Does nothing when a microtask flush is already due, inside a\n * batch or group (which flushes or schedules on exit), during a flush (which picks the work up), or\n * during a transition (its effects wait for the lane). An error thrown by that flush has no caller\n * to reach: it goes to the global `reportError`, or becomes an unhandled promise rejection.\n *\n * @example\n * import { markObservers, schedule, type SourceNode } from \"@shadow-js/core/extend\";\n *\n * export function touch(source: SourceNode): void {\n * markObservers(source);\n * schedule(); // the effects that read `source` run in a microtask\n * }\n * @see notify\n * @since 1.0.0\n */\nexport function schedule(): void {\n // A transition write waits for the lane (scheduled when the effect is queued), never for a microtask.\n if (rt.inTransition || rt.batchDepth > 0 || rt.flushing || microtaskScheduled) return;\n microtaskScheduled = true;\n queueMicrotask(runScheduled);\n}\n\n/**\n * Groups writes like {@link batch} but never flushes synchronously: when the outermost group ends,\n * the effects wait for the microtask (or an enclosing batch / `flushSync`), exactly as after a\n * single signal write. Store setters use it so that one `setStore(...)` behaves like one `set()`;\n * use it for a primitive whose one call makes several writes.\n *\n * @param fn The writes to group. Its throw is rethrown after the group ends.\n * @returns What `fn` returns.\n * @example\n * import { useSignal } from \"@shadow-js/core\";\n * import { group } from \"@shadow-js/core/extend\";\n *\n * const [first, setFirst] = useSignal(\"Ada\");\n * const [last, setLast] = useSignal(\"Lovelace\");\n * group(() => {\n * setFirst(\"Grace\");\n * setLast(\"Hopper\");\n * }); // effects reading both run once, in the next microtask\n * @see batch\n * @since 1.0.0\n */\nexport function group<T>(fn: () => T): T {\n rt.batchDepth += 1;\n try {\n return fn();\n } finally {\n rt.batchDepth -= 1;\n if (rt.batchDepth === 0 && hasPending()) schedule();\n }\n}\n\nfunction combine(primary: unknown, secondary: unknown): unknown {\n return new AggregateError(\n [primary, secondary],\n __DEV__ ? \"Both the callback and the flush threw.\" : \"SHADOW_BATCH_ERRORS\",\n );\n}\n\n/**\n * Runs `fn` with effects deferred until the outermost batch ends, then runs them synchronously, so\n * several writes wake each effect once. Reads inside the batch always see fresh values (G-6). Inside\n * a running flush the effects are left to that flush.\n *\n * @param fn The writes to batch.\n * @returns What `fn` returns.\n * @throws What `fn` throws, after the flush. An error of the flush is thrown as {@link flush} throws\n * it (including ShadowError `INFINITE_LOOP`); when `fn` threw as well, both are thrown together as an\n * `AggregateError`.\n * @example\n * import { batch, useSignal } from \"@shadow-js/core\";\n *\n * const [first, setFirst] = useSignal(\"Ada\");\n * const [last, setLast] = useSignal(\"Lovelace\");\n * batch(() => {\n * setFirst(\"Grace\");\n * setLast(\"Hopper\");\n * }); // effects reading both have run once, here\n * @see flushSync\n * @since 1.0.0\n */\nexport function batch<T>(fn: () => T): T {\n rt.batchDepth += 1;\n let result: T | undefined;\n let failure: unknown;\n let failed = false;\n try {\n result = fn();\n } catch (error) {\n failed = true;\n failure = error;\n } finally {\n rt.batchDepth -= 1;\n }\n if (rt.batchDepth === 0 && !rt.flushing && hasPending()) {\n try {\n flush();\n } catch (error) {\n if (failed) throw combine(failure, error);\n throw error;\n }\n }\n if (failed) throw failure;\n return result as T;\n}\n\n/**\n * Runs `fn` (optional) in a batch and runs every pending effect before returning, so the DOM and\n * other effects are up to date on the next line. Use it in tests and before reading layout. Called\n * while a flush is running (from inside an effect), it only runs `fn` and leaves the effects to that\n * flush; development builds warn `FLUSH_IN_EFFECT`.\n *\n * @param fn The writes to make first. Undefined only flushes.\n * @returns What `fn` returns, or undefined without `fn`.\n * @throws What `fn` throws, after the flush. An error of the flush is thrown as {@link flush} throws\n * it (including ShadowError `INFINITE_LOOP`); when `fn` threw as well, both are thrown together as an\n * `AggregateError`.\n * @example\n * import { createRoot, flushSync, useEffect, useSignal } from \"@shadow-js/core\";\n *\n * createRoot(() => {\n * const [count, setCount] = useSignal(0);\n * useEffect(() => console.log(count()));\n * flushSync(); // logs 0\n * flushSync(() => setCount(1)); // logs 1 before this line ends\n * });\n * @see batch\n * @since 1.0.0\n */\nexport function flushSync<T = void>(fn?: () => T): T {\n if (rt.flushing) {\n warn(\"FLUSH_IN_EFFECT\");\n return (fn === undefined ? undefined : fn()) as T;\n }\n rt.batchDepth += 1;\n let result: T | undefined;\n let failure: unknown;\n let failed = false;\n try {\n result = fn === undefined ? undefined : fn();\n } catch (error) {\n failed = true;\n failure = error;\n } finally {\n rt.batchDepth -= 1;\n }\n try {\n flush();\n } catch (error) {\n if (failed) throw combine(failure, error);\n throw error;\n }\n if (failed) throw failure;\n return result as T;\n}\n"],"mappings":"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"}
@@ -0,0 +1,27 @@
1
+ // src/internal/state.ts
2
+ /**
3
+ * The runtime registers (R-CODE-4): the only module-level mutable state of core besides
4
+ * monotonic counters. Every write is paired with a `finally` restore (R-CODE-5).
5
+ */
6
+ const rt = {
7
+ owner: null,
8
+ listener: null,
9
+ running: null,
10
+ batchDepth: 0,
11
+ flushing: false,
12
+ recomputeDepth: 0,
13
+ disposing: 0,
14
+ uncaught: [],
15
+ inTransition: false,
16
+ hydrating: false,
17
+ hooks: null
18
+ };
19
+ let counter = 0;
20
+ /** Monotonic creation sequence; orders sibling effects (G-16). */
21
+ function nextSequence() {
22
+ counter += 1;
23
+ return counter;
24
+ }
25
+ export { nextSequence, rt };
26
+
27
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","names":[],"sources":["../../../src/internal/state.ts"],"sourcesContent":["import type { Computation } from \"./graph\";\nimport type { Owner } from \"./owner\";\n\n/**\n * A hold for a value a transition builds while its reads wait for data (a `Show` branch built during a\n * transition): the reads that wait count against this hold, not against the Suspense boundary around\n * the value, so the shown value stays until the hold settles. Created by `createTransitionHold`.\n * @since 1.0.0\n */\nexport interface TransitionHold {\n /** Runs `fn` with this hold as the Suspense boundary of the reads inside it. */\n run<T>(fn: () => T): T;\n /** Reads still waiting for data. */\n count(): number;\n /** True once a read in the hold was held (the value is then pending, not ready). */\n engaged(): boolean;\n /** Ends the hold without waking anything (the value is being dropped). */\n drop(): void;\n}\n\n/**\n * The choice a branch slot (Show, Switch) shows: its index, and what decides a rebuild. Renderers\n * extend it with what they need to build the branch.\n * @since 1.0.0\n */\nexport interface SlotChoice {\n /** Which branch is chosen: its position among the slot's branches, or the renderer's fallback index. */\n readonly index: number;\n /** True when the branch is rebuilt whenever `when` changes identity; false when only `index` counts. */\n readonly keyed: boolean;\n /** The condition value that selected the branch. */\n readonly when: unknown;\n}\n\n/**\n * The record of a branch's owner that its accessors read: the value and whether it is being torn down.\n * @since 1.0.0\n */\nexport interface SlotOwned {\n /** The index of the branch this record belongs to (see {@link SlotChoice.index}). */\n index: number;\n /** The latest condition value while the branch is shown; what its accessor returns during teardown. */\n value: unknown;\n /** True while the branch is torn down: its accessor then returns `value` instead of throwing. */\n disposing: boolean;\n}\n\n/**\n * A branch a slot shows, or builds during a transition. A branch without `dispose` is torn down by its memo.\n * @since 1.0.0\n */\nexport interface SlotBranch<C extends SlotChoice> {\n /** The choice the branch was built for. */\n readonly choice: C;\n /** The record its accessors read. */\n readonly owned: SlotOwned;\n /** What the branch produced: the value the slot's output returns while the branch is shown. */\n readonly result: unknown;\n /**\n * Tears the branch down. Set for the branches a transition built: they live under the holder, not\n * under the memo.\n * @default undefined (the slot's memo tears the branch down)\n */\n readonly dispose?: () => void;\n /**\n * The hold a transition built the branch under; null when none of its reads was held.\n * @default undefined (a memo-owned branch has no hold)\n */\n readonly hold?: TransitionHold | null;\n}\n\n/**\n * What a slot's output memo keeps: the branch on screen (its result is the value the output returns).\n * @since 1.0.0\n */\nexport interface SlotState<C extends SlotChoice> {\n /** The branch on screen, or undefined before the first build and while the memo rebuilds it. */\n live: SlotBranch<C> | undefined;\n}\n\n/**\n * The transition layer of one slot, from `transitionHooks()?.slotSwap(...)`: it keeps the shown\n * branch while a transition builds the next one (see `slotSwap` in concurrency.ts).\n * @since 1.0.0\n */\nexport interface SlotSwap<C extends SlotChoice> {\n /**\n * Runs one pass of the slot's memo for `choice`. True when a transition keeps the shown value. False\n * when the usual path follows: the branch on screen has been retired and `state.live` is cleared.\n */\n step(choice: C): boolean;\n}\n\n/**\n * The hold of one Suspense boundary: engaged by a transition read, released when its data arrived.\n * From `transitionHooks()?.boundaryHold()`.\n * @since 1.0.0\n */\nexport interface BoundaryHold {\n /** Engages the hold (a second call changes nothing) so the boundary keeps its content; returns true. */\n engage(): boolean;\n /** When engaged: wakes the readers that waited, then ends the hold. Otherwise does nothing. */\n release(): void;\n /** True while the hold is engaged. */\n holding(): boolean;\n}\n\n/**\n * The transition machinery the engine calls through `rt.hooks`: transition lanes, pauses (Activity)\n * and suspensions (Suspense holds). It is created by the first call of an API that needs it (see\n * concurrency.ts), so an application that never calls one of those APIs carries none of their code.\n * Renderers reach it through `transitionHooks()`.\n * @since 1.0.0\n */\nexport interface Hooks {\n /** The transition layer of one branch slot (see `slotSwap` in concurrency.ts). */\n slotSwap<C extends SlotChoice>(\n state: SlotState<C>,\n build: (choice: C, owned: SlotOwned, eager: boolean) => unknown,\n same: (a: C, b: C) => boolean,\n parent: Owner | null,\n ): SlotSwap<C>;\n /** A Suspense boundary's hold (see `boundaryHold` in concurrency.ts). */\n boundaryHold(): BoundaryHold;\n /** Queues an effect that is due in the transition lane, or parks it while its owner is paused. */\n place(effect: Computation, inLane: boolean): void;\n /** An urgent write reached an effect that waits in the lane or is parked: it moves to the urgent queue. */\n promote(effect: Computation): void;\n /** Forgets the lane queues (the flush loop guard). */\n clear(): void;\n /** Keeps `node` for a transition when `error` is a suspension; returns false for any other error. */\n suspend(node: Computation, error: unknown): boolean;\n /** The engine side of `holdReader`: throws the suspension sentinel when `tracker.hold()` is true. */\n holdReader(tracker: { hold?(): boolean }): void;\n /** The engine side of `startTransition` in the browser. */\n startTransition(fn: () => void): Promise<void>;\n /** The engine side of `beginHold`: counts one more Suspense hold. */\n beginHold(): void;\n /** The engine side of `endHold`: counts one hold less and settles the lane. */\n endHold(): void;\n /** The engine side of `wakeSuspended`. */\n wakeSuspended(): void;\n /** The engine side of `pauseOwner`. */\n pause(owner: Owner): void;\n /** The engine side of `resumeOwner`. */\n resume(owner: Owner): void;\n /** The engine side of `createTransitionHold`. */\n createHold(onSettled: () => void): TransitionHold;\n}\n\n/**\n * The runtime registers (R-CODE-4): the only module-level mutable state of core besides\n * monotonic counters. Every write is paired with a `finally` restore (R-CODE-5).\n */\nexport const rt: {\n /** Owner that adopts newly created nodes. */\n owner: Owner | null;\n /** Computation that records the signals it reads; null while untracked. */\n listener: Computation | null;\n /** Computation currently executing, tracked or not (used by the write-in-memo guard). */\n running: Computation | null;\n /** Nesting depth of batch()/flushSync() calls. */\n batchDepth: number;\n /** True while the flush loop is draining the effect queues. */\n flushing: boolean;\n /** Nesting depth of memo recomputation, used to switch to iterative pulls. */\n recomputeDepth: number;\n /** Nesting depth of disposals in progress; errors raised meanwhile are collected, not thrown. */\n disposing: number;\n /** Errors nobody handled during the current flush or disposal; rethrown once it completes. */\n uncaught: unknown[];\n /** True while a transition callback or a transition-lane run is active: writes made now join the lane. */\n inTransition: boolean;\n /** True while `hydrate()` claims server markup (useExternalStore reads its server snapshot). */\n hydrating: boolean;\n /** The transition machinery, or null until an API that needs it is first called. */\n hooks: Hooks | null;\n} = {\n owner: null,\n listener: null,\n running: null,\n batchDepth: 0,\n flushing: false,\n recomputeDepth: 0,\n disposing: 0,\n uncaught: [],\n inTransition: false,\n hydrating: false,\n hooks: null,\n};\n\nlet counter = 0;\n/** Monotonic creation sequence; orders sibling effects (G-16). */\nexport function nextSequence(): number {\n counter += 1;\n return counter;\n}\n"],"mappings":";;;;;AA0JA,MAAa,KAuBT;CACF,OAAO;CACP,UAAU;CACV,SAAS;CACT,YAAY;CACZ,UAAU;CACV,gBAAgB;CAChB,WAAW;CACX,UAAU,CAAC;CACX,cAAc;CACd,WAAW;CACX,OAAO;AACT;AAEA,IAAI,UAAU;;AAEd,SAAgB,eAAuB;CACrC,WAAW;CACX,OAAO;AACT"}
@@ -0,0 +1,44 @@
1
+ // src/internal/suspension.ts
2
+ /**
3
+ * The value a reader throws when a transition reads data that is still loading inside a Suspense
4
+ * boundary that is showing content. The reader keeps its previous value (a memo keeps its value, an
5
+ * effect leaves the DOM as it is) until the boundary releases it. A single frozen object: identity is
6
+ * the test, so user code cannot forge it. {@link holdReader} throws it for you; a custom primitive
7
+ * that catches errors must let it through, which {@link isSuspension} checks.
8
+ *
9
+ * @example
10
+ * import { SUSPENSION } from "@shadow-js/core/extend";
11
+ *
12
+ * function rethrowIfHeld(error: unknown): void {
13
+ * if (error === SUSPENSION) throw error; // not a failure: the boundary resumes the reader later
14
+ * }
15
+ * @see isSuspension
16
+ * @since 1.0.0
17
+ */
18
+ const SUSPENSION = /* @__PURE__ */ Object.freeze({ suspended: true });
19
+ /**
20
+ * Tells whether a caught value is the suspension sentinel rather than an error. Use it in a custom
21
+ * primitive's `catch` so a transition's held read is passed on instead of being reported as a
22
+ * failure.
23
+ *
24
+ * @param value The caught value.
25
+ * @returns True when `value` is {@link SUSPENSION}.
26
+ * @example
27
+ * import { isSuspension } from "@shadow-js/core/extend";
28
+ *
29
+ * function attempt(run: () => void, onError: (error: unknown) => void): void {
30
+ * try {
31
+ * run();
32
+ * } catch (error) {
33
+ * if (isSuspension(error)) throw error;
34
+ * onError(error);
35
+ * }
36
+ * }
37
+ * @since 1.0.0
38
+ */
39
+ function isSuspension(value) {
40
+ return value === SUSPENSION;
41
+ }
42
+ export { SUSPENSION, isSuspension };
43
+
44
+ //# sourceMappingURL=suspension.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"suspension.js","names":[],"sources":["../../../src/internal/suspension.ts"],"sourcesContent":["/**\n * The value a reader throws when a transition reads data that is still loading inside a Suspense\n * boundary that is showing content. The reader keeps its previous value (a memo keeps its value, an\n * effect leaves the DOM as it is) until the boundary releases it. A single frozen object: identity is\n * the test, so user code cannot forge it. {@link holdReader} throws it for you; a custom primitive\n * that catches errors must let it through, which {@link isSuspension} checks.\n *\n * @example\n * import { SUSPENSION } from \"@shadow-js/core/extend\";\n *\n * function rethrowIfHeld(error: unknown): void {\n * if (error === SUSPENSION) throw error; // not a failure: the boundary resumes the reader later\n * }\n * @see isSuspension\n * @since 1.0.0\n */\nexport const SUSPENSION: { readonly suspended: true } = /* @__PURE__ */ Object.freeze({\n suspended: true as const,\n});\n\n/**\n * Tells whether a caught value is the suspension sentinel rather than an error. Use it in a custom\n * primitive's `catch` so a transition's held read is passed on instead of being reported as a\n * failure.\n *\n * @param value The caught value.\n * @returns True when `value` is {@link SUSPENSION}.\n * @example\n * import { isSuspension } from \"@shadow-js/core/extend\";\n *\n * function attempt(run: () => void, onError: (error: unknown) => void): void {\n * try {\n * run();\n * } catch (error) {\n * if (isSuspension(error)) throw error;\n * onError(error);\n * }\n * }\n * @since 1.0.0\n */\nexport function isSuspension(value: unknown): boolean {\n return value === SUSPENSION;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAgBA,MAAa,aAA2D,uBAAO,OAAO,EACpF,WAAW,KACb,CAAC;;;;;;;;;;;;;;;;;;;;;AAsBD,SAAgB,aAAa,OAAyB;CACpD,OAAO,UAAU;AACnB"}