effect-start 0.23.0 → 0.25.0

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 (356) hide show
  1. package/dist/ChildProcess.js +32 -20
  2. package/dist/Commander.js +377 -293
  3. package/dist/ContentNegotiation.js +424 -318
  4. package/dist/Cookies.js +340 -271
  5. package/dist/Development.js +85 -49
  6. package/dist/Effectify.js +22 -14
  7. package/dist/Entity.js +260 -195
  8. package/dist/Fetch.js +192 -0
  9. package/dist/FilePathPattern.js +88 -77
  10. package/dist/FileRouter.js +192 -136
  11. package/dist/FileRouterCodegen.js +262 -191
  12. package/dist/FileSystem.js +126 -64
  13. package/dist/Http.js +96 -77
  14. package/dist/PathPattern.js +311 -273
  15. package/dist/PlatformError.js +36 -21
  16. package/dist/PlatformRuntime.js +65 -40
  17. package/dist/Route.js +122 -79
  18. package/dist/RouteBody.js +83 -58
  19. package/dist/RouteError.js +46 -25
  20. package/dist/RouteHook.js +58 -34
  21. package/dist/RouteHttp.js +346 -237
  22. package/dist/RouteHttpTracer.js +86 -58
  23. package/dist/RouteMount.js +81 -58
  24. package/dist/RouteSchema.js +253 -170
  25. package/dist/RouteSse.js +87 -72
  26. package/dist/RouteTree.js +99 -73
  27. package/dist/RouteTrie.js +160 -133
  28. package/dist/SchemaExtra.js +87 -62
  29. package/dist/Socket.js +32 -21
  30. package/dist/SqlIntrospect.js +317 -268
  31. package/dist/Start.js +55 -25
  32. package/dist/StartApp.js +3 -21
  33. package/dist/StreamExtra.js +109 -74
  34. package/dist/System.js +37 -21
  35. package/dist/TuplePathPattern.js +64 -58
  36. package/dist/Unique.js +159 -120
  37. package/dist/Values.js +48 -32
  38. package/dist/bun/BunBundle.js +158 -109
  39. package/dist/bun/BunChildProcessSpawner.js +121 -82
  40. package/dist/bun/BunImportTrackerPlugin.js +85 -63
  41. package/dist/bun/BunRoute.js +135 -99
  42. package/dist/bun/BunRuntime.js +39 -29
  43. package/dist/bun/BunServer.js +268 -140
  44. package/dist/bun/BunVirtualFilesPlugin.js +47 -33
  45. package/dist/bun/_BunEnhancedResolve.js +107 -82
  46. package/dist/bun/index.js +5 -5
  47. package/dist/bundler/Bundle.js +82 -38
  48. package/dist/bundler/BundleFiles.js +140 -82
  49. package/dist/bundler/BundleRoute.js +49 -38
  50. package/dist/client/Overlay.js +29 -28
  51. package/dist/client/ScrollState.js +94 -82
  52. package/dist/client/index.js +79 -61
  53. package/dist/console/Console.js +40 -24
  54. package/dist/console/ConsoleErrors.js +189 -178
  55. package/dist/console/ConsoleLogger.js +52 -43
  56. package/dist/console/ConsoleMetrics.js +69 -58
  57. package/dist/console/ConsoleProcess.js +57 -47
  58. package/dist/console/ConsoleStore.js +56 -45
  59. package/dist/console/ConsoleTracer.js +101 -88
  60. package/dist/console/Simulation.js +714 -563
  61. package/dist/console/index.js +3 -3
  62. package/dist/console/routes/tree.js +29 -28
  63. package/dist/datastar/actions/fetch.js +514 -381
  64. package/dist/datastar/actions/peek.js +12 -12
  65. package/dist/datastar/actions/setAll.js +18 -11
  66. package/dist/datastar/actions/toggleAll.js +18 -11
  67. package/dist/datastar/attributes/attr.js +48 -47
  68. package/dist/datastar/attributes/bind.js +186 -167
  69. package/dist/datastar/attributes/class.js +51 -44
  70. package/dist/datastar/attributes/computed.js +23 -24
  71. package/dist/datastar/attributes/effect.js +9 -8
  72. package/dist/datastar/attributes/indicator.js +32 -29
  73. package/dist/datastar/attributes/init.js +26 -25
  74. package/dist/datastar/attributes/jsonSignals.js +32 -29
  75. package/dist/datastar/attributes/on.js +76 -73
  76. package/dist/datastar/attributes/onIntersect.js +51 -51
  77. package/dist/datastar/attributes/onInterval.js +30 -29
  78. package/dist/datastar/attributes/onSignalPatch.js +49 -40
  79. package/dist/datastar/attributes/ref.js +10 -9
  80. package/dist/datastar/attributes/show.js +31 -30
  81. package/dist/datastar/attributes/signals.js +17 -16
  82. package/dist/datastar/attributes/style.js +56 -49
  83. package/dist/datastar/attributes/text.js +28 -25
  84. package/dist/datastar/engine.js +1079 -933
  85. package/dist/datastar/index.js +25 -24
  86. package/dist/datastar/utils.js +203 -155
  87. package/dist/datastar/watchers/patchElements.js +459 -372
  88. package/dist/datastar/watchers/patchSignals.js +13 -13
  89. package/dist/experimental/EncryptedCookies.js +305 -189
  90. package/dist/experimental/index.js +1 -1
  91. package/dist/hyper/Hyper.js +22 -17
  92. package/dist/hyper/HyperHtml.js +138 -123
  93. package/dist/hyper/HyperNode.js +11 -9
  94. package/dist/hyper/HyperRoute.js +41 -28
  95. package/dist/hyper/html.js +27 -27
  96. package/dist/hyper/index.js +5 -5
  97. package/dist/hyper/jsx-runtime.js +11 -5
  98. package/dist/index.js +8 -8
  99. package/dist/node/NodeFileSystem.js +606 -341
  100. package/dist/node/NodeUtils.js +21 -18
  101. package/dist/sql/Sql.js +8 -0
  102. package/dist/sql/bun/index.js +134 -67
  103. package/dist/sql/index.js +1 -0
  104. package/dist/sql/libsql/index.js +156 -0
  105. package/dist/sql/mssql/docker.js +103 -60
  106. package/dist/sql/mssql/index.js +182 -101
  107. package/dist/testing/TestLogger.js +39 -29
  108. package/dist/testing/index.js +2 -2
  109. package/dist/testing/utils.js +45 -23
  110. package/dist/x/cloudflare/CloudflareTunnel.js +61 -28
  111. package/dist/x/cloudflare/index.js +1 -1
  112. package/dist/x/tailscale/TailscaleTunnel.js +86 -60
  113. package/dist/x/tailscale/index.js +1 -1
  114. package/dist/x/tailwind/TailwindPlugin.js +280 -205
  115. package/dist/x/tailwind/compile.js +185 -129
  116. package/dist/x/tailwind/plugin.js +13 -11
  117. package/package.json +1 -1
  118. package/src/Development.ts +3 -1
  119. package/src/Entity.ts +17 -0
  120. package/src/Fetch.ts +271 -0
  121. package/src/SqlIntrospect.ts +64 -70
  122. package/src/Start.ts +24 -22
  123. package/src/StartApp.ts +11 -0
  124. package/src/bun/BunServer.ts +89 -16
  125. package/src/hyper/HyperHtml.ts +0 -1
  126. package/src/sql/bun/index.ts +1 -1
  127. package/src/sql/index.ts +1 -0
  128. package/src/sql/libsql/index.ts +173 -0
  129. package/src/sql/libsql/libsql.d.ts +39 -0
  130. package/src/sql/mssql/index.ts +1 -1
  131. package/src/x/tailscale/TailscaleTunnel.ts +7 -5
  132. package/dist/BlobStore.d.ts +0 -80
  133. package/dist/BlobStore.js +0 -19
  134. package/dist/ChildProcess.d.ts +0 -60
  135. package/dist/Commander.d.ts +0 -100
  136. package/dist/ContentNegotiation.d.ts +0 -12
  137. package/dist/Cookies.d.ts +0 -47
  138. package/dist/Development.d.ts +0 -39
  139. package/dist/Effectify.d.ts +0 -209
  140. package/dist/Entity.d.ts +0 -47
  141. package/dist/FilePathPattern.d.ts +0 -29
  142. package/dist/FileRouter.d.ts +0 -56
  143. package/dist/FileRouterCodegen.d.ts +0 -18
  144. package/dist/FileRouterPattern.d.ts +0 -9
  145. package/dist/FileRouterPattern.js +0 -35
  146. package/dist/FileSystem.d.ts +0 -158
  147. package/dist/Http.d.ts +0 -37
  148. package/dist/HttpAppExtra.d.ts +0 -7
  149. package/dist/HttpAppExtra.js +0 -320
  150. package/dist/HttpUtils.d.ts +0 -3
  151. package/dist/HttpUtils.js +0 -11
  152. package/dist/PathPattern.d.ts +0 -134
  153. package/dist/PlatformError.d.ts +0 -38
  154. package/dist/PlatformRuntime.d.ts +0 -27
  155. package/dist/Route.d.ts +0 -97
  156. package/dist/RouteBody.d.ts +0 -47
  157. package/dist/RouteError.d.ts +0 -98
  158. package/dist/RouteHook.d.ts +0 -12
  159. package/dist/RouteHttp.d.ts +0 -21
  160. package/dist/RouteHttpTracer.d.ts +0 -10
  161. package/dist/RouteMount.d.ts +0 -86
  162. package/dist/RouteSchema.d.ts +0 -86
  163. package/dist/RouteSse.d.ts +0 -21
  164. package/dist/RouteTree.d.ts +0 -57
  165. package/dist/RouteTrie.d.ts +0 -20
  166. package/dist/RouterPattern.d.ts +0 -118
  167. package/dist/RouterPattern.js +0 -269
  168. package/dist/SchemaExtra.d.ts +0 -7
  169. package/dist/Socket.d.ts +0 -27
  170. package/dist/Sql.d.ts +0 -34
  171. package/dist/Sql.js +0 -5
  172. package/dist/SqlIntrospect.d.ts +0 -91
  173. package/dist/Start.d.ts +0 -44
  174. package/dist/StartApp.d.ts +0 -19
  175. package/dist/StreamExtra.d.ts +0 -28
  176. package/dist/System.d.ts +0 -7
  177. package/dist/TuplePathPattern.d.ts +0 -9
  178. package/dist/Unique.d.ts +0 -50
  179. package/dist/Values.d.ts +0 -27
  180. package/dist/bun/BunBlobStoreDisk.d.ts +0 -6
  181. package/dist/bun/BunBlobStoreDisk.js +0 -116
  182. package/dist/bun/BunBlobStoreS3.d.ts +0 -11
  183. package/dist/bun/BunBlobStoreS3.js +0 -89
  184. package/dist/bun/BunBlobWatcherDisk.d.ts +0 -6
  185. package/dist/bun/BunBlobWatcherDisk.js +0 -60
  186. package/dist/bun/BunBlobWatcherQueue.d.ts +0 -6
  187. package/dist/bun/BunBlobWatcherQueue.js +0 -17
  188. package/dist/bun/BunBundle.d.ts +0 -11
  189. package/dist/bun/BunChildProcessSpawner.d.ts +0 -3
  190. package/dist/bun/BunHttpServer.d.ts +0 -44
  191. package/dist/bun/BunHttpServer.js +0 -186
  192. package/dist/bun/BunHttpServer_web.d.ts +0 -60
  193. package/dist/bun/BunHttpServer_web.js +0 -252
  194. package/dist/bun/BunImportTrackerPlugin.d.ts +0 -13
  195. package/dist/bun/BunPlatformHttpServer.d.ts +0 -10
  196. package/dist/bun/BunPlatformHttpServer.js +0 -53
  197. package/dist/bun/BunRoute.d.ts +0 -48
  198. package/dist/bun/BunRuntime.d.ts +0 -2
  199. package/dist/bun/BunServer.d.ts +0 -40
  200. package/dist/bun/BunServerRequest.d.ts +0 -60
  201. package/dist/bun/BunServerRequest.js +0 -252
  202. package/dist/bun/BunSql.d.ts +0 -4
  203. package/dist/bun/BunSql.js +0 -81
  204. package/dist/bun/BunVirtualFilesPlugin.d.ts +0 -4
  205. package/dist/bun/_BunEnhancedResolve.d.ts +0 -45
  206. package/dist/bun/index.d.ts +0 -5
  207. package/dist/bundler/Bundle.d.ts +0 -61
  208. package/dist/bundler/BundleFiles.d.ts +0 -13
  209. package/dist/bundler/BundleHttp.d.ts +0 -45
  210. package/dist/bundler/BundleHttp.js +0 -176
  211. package/dist/bundler/BundleRoute.d.ts +0 -27
  212. package/dist/client/Overlay.d.ts +0 -2
  213. package/dist/client/ScrollState.d.ts +0 -6
  214. package/dist/client/index.d.ts +0 -6
  215. package/dist/console/Console.d.ts +0 -6
  216. package/dist/console/ConsoleErrors.d.ts +0 -3
  217. package/dist/console/ConsoleLogger.d.ts +0 -3
  218. package/dist/console/ConsoleMetrics.d.ts +0 -3
  219. package/dist/console/ConsoleProcess.d.ts +0 -3
  220. package/dist/console/ConsoleStore.d.ts +0 -144
  221. package/dist/console/ConsoleTracer.d.ts +0 -3
  222. package/dist/console/Simulation.d.ts +0 -2
  223. package/dist/console/index.d.ts +0 -3
  224. package/dist/console/routes/errors/route.d.ts +0 -10
  225. package/dist/console/routes/errors/route.js +0 -47
  226. package/dist/console/routes/fiberDetail.d.ts +0 -16
  227. package/dist/console/routes/fiberDetail.js +0 -38
  228. package/dist/console/routes/fibers/route.d.ts +0 -10
  229. package/dist/console/routes/fibers/route.js +0 -19
  230. package/dist/console/routes/git/route.d.ts +0 -11
  231. package/dist/console/routes/git/route.js +0 -33
  232. package/dist/console/routes/layout.d.ts +0 -9
  233. package/dist/console/routes/layout.js +0 -3
  234. package/dist/console/routes/logs/route.d.ts +0 -10
  235. package/dist/console/routes/logs/route.js +0 -32
  236. package/dist/console/routes/metrics/route.d.ts +0 -10
  237. package/dist/console/routes/metrics/route.js +0 -17
  238. package/dist/console/routes/route.d.ts +0 -6
  239. package/dist/console/routes/route.js +0 -5
  240. package/dist/console/routes/routes/route.d.ts +0 -6
  241. package/dist/console/routes/routes/route.js +0 -20
  242. package/dist/console/routes/services/route.d.ts +0 -6
  243. package/dist/console/routes/services/route.js +0 -12
  244. package/dist/console/routes/system/route.d.ts +0 -10
  245. package/dist/console/routes/system/route.js +0 -18
  246. package/dist/console/routes/traceDetail.d.ts +0 -16
  247. package/dist/console/routes/traceDetail.js +0 -14
  248. package/dist/console/routes/traces/route.d.ts +0 -10
  249. package/dist/console/routes/traces/route.js +0 -39
  250. package/dist/console/routes/tree.d.ts +0 -153
  251. package/dist/console/ui/Errors.d.ts +0 -4
  252. package/dist/console/ui/Errors.js +0 -15
  253. package/dist/console/ui/Fibers.d.ts +0 -24
  254. package/dist/console/ui/Fibers.js +0 -121
  255. package/dist/console/ui/Git.d.ts +0 -20
  256. package/dist/console/ui/Git.js +0 -95
  257. package/dist/console/ui/Logs.d.ts +0 -4
  258. package/dist/console/ui/Logs.js +0 -25
  259. package/dist/console/ui/Metrics.d.ts +0 -4
  260. package/dist/console/ui/Metrics.js +0 -26
  261. package/dist/console/ui/Routes.d.ts +0 -8
  262. package/dist/console/ui/Routes.js +0 -70
  263. package/dist/console/ui/Services.d.ts +0 -10
  264. package/dist/console/ui/Services.js +0 -246
  265. package/dist/console/ui/Shell.d.ts +0 -10
  266. package/dist/console/ui/Shell.js +0 -7
  267. package/dist/console/ui/System.d.ts +0 -4
  268. package/dist/console/ui/System.js +0 -35
  269. package/dist/console/ui/Traces.d.ts +0 -12
  270. package/dist/console/ui/Traces.js +0 -179
  271. package/dist/datastar/actions/fetch.d.ts +0 -30
  272. package/dist/datastar/actions/peek.d.ts +0 -1
  273. package/dist/datastar/actions/setAll.d.ts +0 -1
  274. package/dist/datastar/actions/toggleAll.d.ts +0 -1
  275. package/dist/datastar/attributes/attr.d.ts +0 -1
  276. package/dist/datastar/attributes/bind.d.ts +0 -1
  277. package/dist/datastar/attributes/class.d.ts +0 -1
  278. package/dist/datastar/attributes/computed.d.ts +0 -1
  279. package/dist/datastar/attributes/effect.d.ts +0 -1
  280. package/dist/datastar/attributes/indicator.d.ts +0 -1
  281. package/dist/datastar/attributes/init.d.ts +0 -1
  282. package/dist/datastar/attributes/jsonSignals.d.ts +0 -1
  283. package/dist/datastar/attributes/on.d.ts +0 -1
  284. package/dist/datastar/attributes/onIntersect.d.ts +0 -1
  285. package/dist/datastar/attributes/onInterval.d.ts +0 -1
  286. package/dist/datastar/attributes/onSignalPatch.d.ts +0 -1
  287. package/dist/datastar/attributes/ref.d.ts +0 -1
  288. package/dist/datastar/attributes/show.d.ts +0 -1
  289. package/dist/datastar/attributes/signals.d.ts +0 -1
  290. package/dist/datastar/attributes/style.d.ts +0 -1
  291. package/dist/datastar/attributes/text.d.ts +0 -1
  292. package/dist/datastar/engine.d.ts +0 -162
  293. package/dist/datastar/happydom.d.ts +0 -1
  294. package/dist/datastar/happydom.js +0 -8
  295. package/dist/datastar/index.d.ts +0 -24
  296. package/dist/datastar/load.d.ts +0 -24
  297. package/dist/datastar/load.js +0 -24
  298. package/dist/datastar/utils.d.ts +0 -51
  299. package/dist/datastar/watchers/patchElements.d.ts +0 -1
  300. package/dist/datastar/watchers/patchSignals.d.ts +0 -1
  301. package/dist/experimental/EncryptedCookies.d.ts +0 -48
  302. package/dist/experimental/SseHttpResponse.d.ts +0 -7
  303. package/dist/experimental/SseHttpResponse.js +0 -28
  304. package/dist/experimental/index.d.ts +0 -1
  305. package/dist/hyper/Hyper.d.ts +0 -25
  306. package/dist/hyper/HyperHtml.d.ts +0 -23
  307. package/dist/hyper/HyperHtml.test.d.ts +0 -1
  308. package/dist/hyper/HyperHtml.test.js +0 -197
  309. package/dist/hyper/HyperNode.d.ts +0 -14
  310. package/dist/hyper/HyperRoute.d.ts +0 -8
  311. package/dist/hyper/HyperRoute.test.d.ts +0 -1
  312. package/dist/hyper/HyperRoute.test.js +0 -83
  313. package/dist/hyper/html.d.ts +0 -11
  314. package/dist/hyper/index.d.ts +0 -6
  315. package/dist/hyper/jsx-runtime.d.ts +0 -7
  316. package/dist/index.d.ts +0 -8
  317. package/dist/inference_check.d.ts +0 -1
  318. package/dist/inference_check.js +0 -15
  319. package/dist/lint/plugin.d.ts +0 -86
  320. package/dist/lint/plugin.js +0 -341
  321. package/dist/middlewares/BasicAuthMiddleware.d.ts +0 -8
  322. package/dist/middlewares/BasicAuthMiddleware.js +0 -22
  323. package/dist/middlewares/index.d.ts +0 -1
  324. package/dist/middlewares/index.js +0 -1
  325. package/dist/node/Effectify.d.ts +0 -209
  326. package/dist/node/Effectify.js +0 -19
  327. package/dist/node/FileSystem.d.ts +0 -7
  328. package/dist/node/FileSystem.js +0 -420
  329. package/dist/node/NodeFileSystem.d.ts +0 -7
  330. package/dist/node/NodeUtils.d.ts +0 -2
  331. package/dist/node/PlatformError.d.ts +0 -46
  332. package/dist/node/PlatformError.js +0 -43
  333. package/dist/node/Utils.d.ts +0 -1
  334. package/dist/node/Utils.js +0 -19
  335. package/dist/repro_fail.d.ts +0 -1
  336. package/dist/repro_fail.js +0 -14
  337. package/dist/sql/bun/index.d.ts +0 -3
  338. package/dist/sql/mssql/docker.d.ts +0 -2
  339. package/dist/sql/mssql/index.d.ts +0 -21
  340. package/dist/testing/TestHttpClient.d.ts +0 -13
  341. package/dist/testing/TestHttpClient.js +0 -68
  342. package/dist/testing/TestLogger.d.ts +0 -13
  343. package/dist/testing/index.d.ts +0 -2
  344. package/dist/testing/utils.d.ts +0 -9
  345. package/dist/x/cloudflare/CloudflareTunnel.d.ts +0 -10
  346. package/dist/x/cloudflare/index.d.ts +0 -1
  347. package/dist/x/datastar/Datastar.d.ts +0 -6
  348. package/dist/x/datastar/Datastar.js +0 -47
  349. package/dist/x/datastar/index.d.ts +0 -1
  350. package/dist/x/datastar/index.js +0 -1
  351. package/dist/x/tailscale/TailscaleTunnel.d.ts +0 -15
  352. package/dist/x/tailscale/index.d.ts +0 -1
  353. package/dist/x/tailwind/TailwindPlugin.d.ts +0 -23
  354. package/dist/x/tailwind/compile.d.ts +0 -19
  355. package/dist/x/tailwind/plugin.d.ts +0 -2
  356. /package/src/{Sql.ts → sql/Sql.ts} +0 -0
@@ -1,999 +1,1145 @@
1
- import { aliasify, hasOwn, isHTMLOrSVG, isPojo, pathToObj, snake } from "./utils.js";
1
+ import { aliasify, hasOwn, isHTMLOrSVG, isPojo, pathToObj, snake } from "./utils.js"
2
+
2
3
  /*********
3
4
  * consts.ts
4
5
  *********/
5
- const lol = /🖕JS_DS🚀/.source;
6
- export const DSP = lol.slice(0, 5);
7
- export const DSS = lol.slice(4);
8
- export const DATASTAR_FETCH_EVENT = "datastar-fetch";
9
- export const DATASTAR_SIGNAL_PATCH_EVENT = "datastar-signal-patch";
10
- const ReactiveFlags = {
11
- None: 0,
12
- Mutable: 1 << 0,
13
- Watching: 1 << 1,
14
- RecursedCheck: 1 << 2,
15
- Recursed: 1 << 3,
16
- Dirty: 1 << 4,
17
- Pending: 1 << 5,
18
- };
19
- const EffectFlags = {
20
- Queued: 1 << 6,
21
- };
22
- const currentPatch = [];
23
- const queuedEffects = [];
24
- let batchDepth = 0;
25
- let notifyIndex = 0;
26
- let queuedEffectsLength = 0;
27
- let prevSub;
28
- let activeSub;
29
- let version = 0;
6
+ const lol = /🖕JS_DS🚀/.source
7
+ export const DSP = lol.slice(0, 5)
8
+ export const DSS = lol.slice(4)
9
+ export const DATASTAR_FETCH_EVENT = "datastar-fetch"
10
+ export const DATASTAR_SIGNAL_PATCH_EVENT = "datastar-signal-patch"
11
+
12
+ /*********
13
+ * types.ts
14
+ *********/
15
+
16
+ /*********
17
+ * signals.ts
18
+ *********/
19
+
20
+ const ReactiveFlags = /** @type {const} */ {
21
+ None: 0,
22
+ Mutable: 1 << 0,
23
+ Watching: 1 << 1,
24
+ RecursedCheck: 1 << 2,
25
+ Recursed: 1 << 3,
26
+ Dirty: 1 << 4,
27
+ Pending: 1 << 5,
28
+ }
29
+
30
+ const EffectFlags = /** @type {const} */ {
31
+ Queued: 1 << 6,
32
+ }
33
+
34
+ const currentPatch = []
35
+ const queuedEffects = []
36
+ let batchDepth = 0
37
+ let notifyIndex = 0
38
+ let queuedEffectsLength = 0
39
+ let prevSub
40
+ let activeSub
41
+ let version = 0
42
+
30
43
  export const beginBatch = () => {
31
- batchDepth++;
32
- };
44
+ batchDepth++
45
+ }
46
+
33
47
  export const endBatch = () => {
34
- if (!--batchDepth) {
35
- flush();
36
- dispatch();
37
- }
38
- };
48
+ if (!--batchDepth) {
49
+ flush()
50
+ dispatch()
51
+ }
52
+ }
53
+
39
54
  export const startPeeking = (sub) => {
40
- prevSub = activeSub;
41
- activeSub = sub;
42
- };
55
+ prevSub = activeSub
56
+ activeSub = sub
57
+ }
58
+
43
59
  export const stopPeeking = () => {
44
- activeSub = prevSub;
45
- prevSub = undefined;
46
- };
60
+ activeSub = prevSub
61
+ prevSub = undefined
62
+ }
63
+
47
64
  export const signal = (initialValue) => {
48
- return signalOper.bind(0, {
49
- previousValue: initialValue,
50
- value_: initialValue,
51
- flags_: 1,
52
- });
53
- };
54
- const computedSymbol = Symbol("computed");
65
+ return signalOper.bind(0, {
66
+ previousValue: initialValue,
67
+ value_: initialValue,
68
+ flags_: 1,
69
+ })
70
+ }
71
+
72
+ const computedSymbol = Symbol("computed")
55
73
  export const computed = (getter) => {
56
- const c = computedOper.bind(0, {
57
- flags_: 17,
58
- getter,
59
- });
60
- // @ts-ignore
61
- c[computedSymbol] = 1;
62
- return c;
63
- };
74
+ const c = computedOper.bind(0, {
75
+ flags_: 17,
76
+ getter,
77
+ })
78
+ // @ts-ignore
79
+ c[computedSymbol] = 1
80
+ return c
81
+ }
82
+
64
83
  export const effect = (fn) => {
65
- const e = {
66
- fn_: fn,
67
- flags_: 2,
68
- };
69
- if (activeSub) {
70
- link(e, activeSub);
71
- }
72
- startPeeking(e);
73
- beginBatch();
74
- try {
75
- e.fn_();
76
- }
77
- finally {
78
- endBatch();
79
- stopPeeking();
80
- }
81
- return effectOper.bind(0, e);
82
- };
84
+ const e = {
85
+ fn_: fn,
86
+ flags_: 2,
87
+ }
88
+ if (activeSub) {
89
+ link(e, activeSub)
90
+ }
91
+ startPeeking(e)
92
+ beginBatch()
93
+ try {
94
+ e.fn_()
95
+ } finally {
96
+ endBatch()
97
+ stopPeeking()
98
+ }
99
+ return effectOper.bind(0, e)
100
+ }
101
+
83
102
  const flush = () => {
84
- while (notifyIndex < queuedEffectsLength) {
85
- const effect = queuedEffects[notifyIndex];
86
- queuedEffects[notifyIndex++] = undefined;
87
- run(effect, (effect.flags_ &= ~EffectFlags.Queued));
88
- }
89
- notifyIndex = 0;
90
- queuedEffectsLength = 0;
91
- };
103
+ while (notifyIndex < queuedEffectsLength) {
104
+ const effect = queuedEffects[notifyIndex]
105
+ queuedEffects[notifyIndex++] = undefined
106
+ run(effect, (effect.flags_ &= ~EffectFlags.Queued))
107
+ }
108
+ notifyIndex = 0
109
+ queuedEffectsLength = 0
110
+ }
111
+
92
112
  const update = (signal) => {
93
- if ("getter" in signal) {
94
- return updateComputed(signal);
95
- }
96
- return updateSignal(signal, signal.value_);
97
- };
113
+ if ("getter" in signal) {
114
+ return updateComputed(signal)
115
+ }
116
+ return updateSignal(signal, signal.value_)
117
+ }
118
+
98
119
  const updateComputed = (c) => {
99
- startPeeking(c);
100
- startTracking(c);
101
- try {
102
- const oldValue = c.value_;
103
- return oldValue !== (c.value_ = c.getter(oldValue));
104
- }
105
- finally {
106
- stopPeeking();
107
- endTracking(c);
108
- }
109
- };
120
+ startPeeking(c)
121
+ startTracking(c)
122
+ try {
123
+ const oldValue = c.value_
124
+ return oldValue !== (c.value_ = c.getter(oldValue))
125
+ } finally {
126
+ stopPeeking()
127
+ endTracking(c)
128
+ }
129
+ }
130
+
110
131
  const updateSignal = (s, value) => {
111
- s.flags_ = 1;
112
- return s.previousValue !== (s.previousValue = value);
113
- };
132
+ s.flags_ = 1
133
+ return s.previousValue !== (s.previousValue = value)
134
+ }
135
+
114
136
  const notify = (e) => {
115
- const flags = e.flags_;
116
- if (!(flags & EffectFlags.Queued)) {
117
- e.flags_ = flags | EffectFlags.Queued;
118
- const subs = e.subs_;
119
- if (subs) {
120
- notify(subs.sub_);
121
- }
122
- else {
123
- queuedEffects[queuedEffectsLength++] = e;
124
- }
125
- }
126
- };
137
+ const flags = e.flags_
138
+ if (!(flags & EffectFlags.Queued)) {
139
+ e.flags_ = flags | EffectFlags.Queued
140
+ const subs = e.subs_
141
+ if (subs) {
142
+ notify(subs.sub_)
143
+ } else {
144
+ queuedEffects[queuedEffectsLength++] = e
145
+ }
146
+ }
147
+ }
148
+
127
149
  const run = (e, flags) => {
128
- if (flags & 16 ||
129
- (flags & 32 && checkDirty(e.deps_, e))) {
130
- startPeeking(e);
131
- startTracking(e);
132
- beginBatch();
133
- try {
134
- e.fn_();
135
- }
136
- finally {
137
- endBatch();
138
- stopPeeking();
139
- endTracking(e);
140
- }
141
- return;
142
- }
143
- if (flags & 32) {
144
- e.flags_ = flags & ~32;
145
- }
146
- let link = e.deps_;
147
- while (link) {
148
- const dep = link.dep_;
149
- const depFlags = dep.flags_;
150
- if (depFlags & EffectFlags.Queued) {
151
- run(dep, (dep.flags_ = depFlags & ~EffectFlags.Queued));
152
- }
153
- link = link.nextDep_;
154
- }
155
- };
150
+ if (
151
+ flags & (16) ||
152
+ (flags & (32) && checkDirty(e.deps_, e))
153
+ ) {
154
+ startPeeking(e)
155
+ startTracking(e)
156
+ beginBatch()
157
+ try {
158
+ e.fn_()
159
+ } finally {
160
+ endBatch()
161
+ stopPeeking()
162
+ endTracking(e)
163
+ }
164
+ return
165
+ }
166
+ if (flags & (32)) {
167
+ e.flags_ = flags & ~(32)
168
+ }
169
+ let link = e.deps_
170
+ while (link) {
171
+ const dep = link.dep_
172
+ const depFlags = dep.flags_
173
+ if (depFlags & EffectFlags.Queued) {
174
+ run(dep, (dep.flags_ = depFlags & ~EffectFlags.Queued))
175
+ }
176
+ link = link.nextDep_
177
+ }
178
+ }
179
+
156
180
  const signalOper = (s, ...value) => {
157
- if (value.length) {
158
- if (s.value_ !== (s.value_ = value[0])) {
159
- s.flags_ = 17;
160
- const subs = s.subs_;
161
- if (subs) {
162
- propagate(subs);
163
- if (!batchDepth) {
164
- flush();
165
- }
166
- }
167
- return true;
181
+ if (value.length) {
182
+ if (s.value_ !== (s.value_ = value[0])) {
183
+ s.flags_ = 17
184
+ const subs = s.subs_
185
+ if (subs) {
186
+ propagate(subs)
187
+ if (!batchDepth) {
188
+ flush()
168
189
  }
169
- return false;
170
- }
171
- const currentValue = s.value_;
172
- if (s.flags_ & 16) {
173
- if (updateSignal(s, currentValue)) {
174
- const subs_ = s.subs_;
175
- if (subs_) {
176
- shallowPropagate(subs_);
177
- }
178
- }
179
- }
180
- if (activeSub) {
181
- link(s, activeSub);
182
- }
183
- return currentValue;
184
- };
190
+ }
191
+ return true
192
+ }
193
+ return false
194
+ }
195
+ const currentValue = s.value_
196
+ if (s.flags_ & (16)) {
197
+ if (updateSignal(s, currentValue)) {
198
+ const subs_ = s.subs_
199
+ if (subs_) {
200
+ shallowPropagate(subs_)
201
+ }
202
+ }
203
+ }
204
+ if (activeSub) {
205
+ link(s, activeSub)
206
+ }
207
+ return currentValue
208
+ }
209
+
185
210
  const computedOper = (c) => {
186
- const flags = c.flags_;
187
- if (flags & 16 ||
188
- (flags & 32 && checkDirty(c.deps_, c))) {
189
- if (updateComputed(c)) {
190
- const subs = c.subs_;
191
- if (subs) {
192
- shallowPropagate(subs);
193
- }
194
- }
195
- }
196
- else if (flags & 32) {
197
- c.flags_ = flags & ~32;
198
- }
199
- if (activeSub) {
200
- link(c, activeSub);
201
- }
202
- return c.value_;
203
- };
211
+ const flags = c.flags_
212
+ if (
213
+ flags & (16) ||
214
+ (flags & (32) && checkDirty(c.deps_, c))
215
+ ) {
216
+ if (updateComputed(c)) {
217
+ const subs = c.subs_
218
+ if (subs) {
219
+ shallowPropagate(subs)
220
+ }
221
+ }
222
+ } else if (flags & (32)) {
223
+ c.flags_ = flags & ~(32)
224
+ }
225
+ if (activeSub) {
226
+ link(c, activeSub)
227
+ }
228
+ return c.value_
229
+ }
230
+
204
231
  const effectOper = (e) => {
205
- let dep = e.deps_;
206
- while (dep) {
207
- dep = unlink(dep, e);
208
- }
209
- const sub = e.subs_;
210
- if (sub) {
211
- unlink(sub);
212
- }
213
- e.flags_ = 0;
214
- };
232
+ let dep = e.deps_
233
+ while (dep) {
234
+ dep = unlink(dep, e)
235
+ }
236
+ const sub = e.subs_
237
+ if (sub) {
238
+ unlink(sub)
239
+ }
240
+ e.flags_ = 0
241
+ }
242
+
215
243
  const link = (dep, sub) => {
216
- const prevDep = sub.depsTail_;
217
- if (prevDep && prevDep.dep_ === dep) {
218
- return;
219
- }
220
- const nextDep = prevDep ? prevDep.nextDep_ : sub.deps_;
221
- if (nextDep && nextDep.dep_ === dep) {
222
- nextDep.version_ = version;
223
- sub.depsTail_ = nextDep;
224
- return;
225
- }
226
- const prevSub = dep.subsTail_;
227
- if (prevSub && prevSub.version_ === version && prevSub.sub_ === sub) {
228
- return;
229
- }
230
- const newLink = (sub.depsTail_ =
231
- dep.subsTail_ =
232
- {
233
- version_: version,
234
- dep_: dep,
235
- sub_: sub,
236
- prevDep_: prevDep,
237
- nextDep_: nextDep,
238
- prevSub_: prevSub,
239
- });
240
- if (nextDep) {
241
- nextDep.prevDep_ = newLink;
242
- }
243
- if (prevDep) {
244
- prevDep.nextDep_ = newLink;
245
- }
246
- else {
247
- sub.deps_ = newLink;
248
- }
249
- if (prevSub) {
250
- prevSub.nextSub_ = newLink;
251
- }
252
- else {
253
- dep.subs_ = newLink;
254
- }
255
- };
244
+ const prevDep = sub.depsTail_
245
+ if (prevDep && prevDep.dep_ === dep) {
246
+ return
247
+ }
248
+ const nextDep = prevDep ? prevDep.nextDep_ : sub.deps_
249
+ if (nextDep && nextDep.dep_ === dep) {
250
+ nextDep.version_ = version
251
+ sub.depsTail_ = nextDep
252
+ return
253
+ }
254
+ const prevSub = dep.subsTail_
255
+ if (prevSub && prevSub.version_ === version && prevSub.sub_ === sub) {
256
+ return
257
+ }
258
+ const newLink =
259
+ (sub.depsTail_ =
260
+ dep.subsTail_ =
261
+ {
262
+ version_: version,
263
+ dep_: dep,
264
+ sub_: sub,
265
+ prevDep_: prevDep,
266
+ nextDep_: nextDep,
267
+ prevSub_: prevSub,
268
+ })
269
+ if (nextDep) {
270
+ nextDep.prevDep_ = newLink
271
+ }
272
+ if (prevDep) {
273
+ prevDep.nextDep_ = newLink
274
+ } else {
275
+ sub.deps_ = newLink
276
+ }
277
+ if (prevSub) {
278
+ prevSub.nextSub_ = newLink
279
+ } else {
280
+ dep.subs_ = newLink
281
+ }
282
+ }
283
+
256
284
  const unlink = (link, sub = link.sub_) => {
257
- const dep_ = link.dep_;
258
- const prevDep_ = link.prevDep_;
259
- const nextDep_ = link.nextDep_;
260
- const nextSub_ = link.nextSub_;
261
- const prevSub_ = link.prevSub_;
262
- if (nextDep_) {
263
- nextDep_.prevDep_ = prevDep_;
264
- }
265
- else {
266
- sub.depsTail_ = prevDep_;
267
- }
268
- if (prevDep_) {
269
- prevDep_.nextDep_ = nextDep_;
270
- }
271
- else {
272
- sub.deps_ = nextDep_;
273
- }
274
- if (nextSub_) {
275
- nextSub_.prevSub_ = prevSub_;
276
- }
277
- else {
278
- dep_.subsTail_ = prevSub_;
279
- }
280
- if (prevSub_) {
281
- prevSub_.nextSub_ = nextSub_;
282
- }
283
- else if (!(dep_.subs_ = nextSub_)) {
284
- if ("getter" in dep_) {
285
- let toRemove = dep_.deps_;
286
- if (toRemove) {
287
- dep_.flags_ = 17;
288
- do {
289
- toRemove = unlink(toRemove, dep_);
290
- } while (toRemove);
291
- }
292
- }
293
- else if (!("previousValue" in dep_)) {
294
- effectOper(dep_);
295
- }
296
- }
297
- return nextDep_;
298
- };
285
+ const dep_ = link.dep_
286
+ const prevDep_ = link.prevDep_
287
+ const nextDep_ = link.nextDep_
288
+ const nextSub_ = link.nextSub_
289
+ const prevSub_ = link.prevSub_
290
+ if (nextDep_) {
291
+ nextDep_.prevDep_ = prevDep_
292
+ } else {
293
+ sub.depsTail_ = prevDep_
294
+ }
295
+ if (prevDep_) {
296
+ prevDep_.nextDep_ = nextDep_
297
+ } else {
298
+ sub.deps_ = nextDep_
299
+ }
300
+ if (nextSub_) {
301
+ nextSub_.prevSub_ = prevSub_
302
+ } else {
303
+ dep_.subsTail_ = prevSub_
304
+ }
305
+ if (prevSub_) {
306
+ prevSub_.nextSub_ = nextSub_
307
+ } else if (!(dep_.subs_ = nextSub_)) {
308
+ if ("getter" in dep_) {
309
+ let toRemove = dep_.deps_
310
+ if (toRemove) {
311
+ dep_.flags_ = 17
312
+ do {
313
+ toRemove = unlink(toRemove, dep_)
314
+ } while (toRemove)
315
+ }
316
+ } else if (!("previousValue" in dep_)) {
317
+ effectOper(dep_)
318
+ }
319
+ }
320
+ return nextDep_
321
+ }
322
+
299
323
  const propagate = (link) => {
300
- let next = link.nextSub_;
301
- let stack;
302
- top: while (true) {
303
- const sub = link.sub_;
304
- let flags = sub.flags_;
305
- if (!(flags &
306
- 60)) {
307
- sub.flags_ = flags | 32;
308
- }
309
- else if (!(flags & 12)) {
310
- flags = 0;
311
- }
312
- else if (!(flags & 4)) {
313
- sub.flags_ =
314
- (flags & ~8) | 32;
315
- }
316
- else if (!(flags & 48) &&
317
- isValidLink(link, sub)) {
318
- sub.flags_ = flags | 40;
319
- flags &= 1;
324
+ let next = link.nextSub_
325
+ let stack
326
+
327
+ top: while (true) {
328
+ const sub = link.sub_
329
+
330
+ let flags = sub.flags_
331
+
332
+ if (
333
+ !(
334
+ flags &
335
+ (60)
336
+ )
337
+ ) {
338
+ sub.flags_ = flags | (32)
339
+ } else if (!(flags & (12))) {
340
+ flags = 0
341
+ } else if (!(flags & (4))) {
342
+ sub.flags_ =
343
+ (flags & ~(8)) | (32)
344
+ } else if (
345
+ !(flags & (48)) &&
346
+ isValidLink(link, sub)
347
+ ) {
348
+ sub.flags_ = flags | (40)
349
+ flags &= 1
350
+ } else {
351
+ flags = 0
352
+ }
353
+
354
+ if (flags & (2)) {
355
+ notify(sub)
356
+ }
357
+
358
+ if (flags & (1)) {
359
+ const subSubs = sub.subs_
360
+ if (subSubs) {
361
+ const nextSub = (link = subSubs).nextSub_
362
+ if (nextSub) {
363
+ stack = { value_: next, prev_: stack }
364
+ next = nextSub
320
365
  }
321
- else {
322
- flags = 0;
323
- }
324
- if (flags & 2) {
325
- notify(sub);
326
- }
327
- if (flags & 1) {
328
- const subSubs = sub.subs_;
329
- if (subSubs) {
330
- const nextSub = (link = subSubs).nextSub_;
331
- if (nextSub) {
332
- stack = { value_: next, prev_: stack };
333
- next = nextSub;
334
- }
335
- continue;
336
- }
337
- }
338
- if ((link = next)) {
339
- next = link.nextSub_;
340
- continue;
341
- }
342
- while (stack) {
343
- link = stack.value_;
344
- stack = stack.prev_;
345
- if (link) {
346
- next = link.nextSub_;
347
- continue top;
348
- }
349
- }
350
- break;
351
- }
352
- };
366
+ continue
367
+ }
368
+ }
369
+
370
+ if ((link = next)) {
371
+ next = link.nextSub_
372
+ continue
373
+ }
374
+
375
+ while (stack) {
376
+ link = stack.value_
377
+ stack = stack.prev_
378
+ if (link) {
379
+ next = link.nextSub_
380
+ continue top
381
+ }
382
+ }
383
+
384
+ break
385
+ }
386
+ }
387
+
353
388
  const startTracking = (sub) => {
354
- version++;
355
- sub.depsTail_ = undefined;
356
- sub.flags_ =
357
- (sub.flags_ & ~56) |
358
- 4;
359
- };
389
+ version++
390
+ sub.depsTail_ = undefined
391
+ sub.flags_ =
392
+ (sub.flags_ & ~(56)) |
393
+ (4)
394
+ }
395
+
360
396
  const endTracking = (sub) => {
361
- const depsTail_ = sub.depsTail_;
362
- let toRemove = depsTail_ ? depsTail_.nextDep_ : sub.deps_;
363
- while (toRemove) {
364
- toRemove = unlink(toRemove, sub);
365
- }
366
- sub.flags_ &= ~4;
367
- };
397
+ const depsTail_ = sub.depsTail_
398
+ let toRemove = depsTail_ ? depsTail_.nextDep_ : sub.deps_
399
+ while (toRemove) {
400
+ toRemove = unlink(toRemove, sub)
401
+ }
402
+ sub.flags_ &= ~(4)
403
+ }
404
+
368
405
  const checkDirty = (link, sub) => {
369
- let stack;
370
- let checkDepth = 0;
371
- let dirty = false;
372
- top: while (true) {
373
- const dep = link.dep_;
374
- const flags = dep.flags_;
375
- if (sub.flags_ & 16) {
376
- dirty = true;
377
- }
378
- else if ((flags & 17) ===
379
- 17) {
380
- if (update(dep)) {
381
- const subs = dep.subs_;
382
- if (subs.nextSub_) {
383
- shallowPropagate(subs);
384
- }
385
- dirty = true;
386
- }
387
- }
388
- else if ((flags & 33) ===
389
- 33) {
390
- if (link.nextSub_ || link.prevSub_) {
391
- stack = { value_: link, prev_: stack };
392
- }
393
- link = dep.deps_;
394
- sub = dep;
395
- ++checkDepth;
396
- continue;
406
+ let stack
407
+ let checkDepth = 0
408
+ let dirty = false
409
+
410
+ top: while (true) {
411
+ const dep = link.dep_
412
+ const flags = dep.flags_
413
+
414
+ if (sub.flags_ & (16)) {
415
+ dirty = true
416
+ } else if (
417
+ (flags & (17)) ===
418
+ (17)
419
+ ) {
420
+ if (update(dep)) {
421
+ const subs = dep.subs_
422
+ if (subs.nextSub_) {
423
+ shallowPropagate(subs)
397
424
  }
398
- if (!dirty) {
399
- const nextDep = link.nextDep_;
400
- if (nextDep) {
401
- link = nextDep;
402
- continue;
403
- }
425
+ dirty = true
426
+ }
427
+ } else if (
428
+ (flags & (33)) ===
429
+ (33)
430
+ ) {
431
+ if (link.nextSub_ || link.prevSub_) {
432
+ stack = { value_: link, prev_: stack }
433
+ }
434
+ link = dep.deps_
435
+ sub = dep
436
+ ++checkDepth
437
+ continue
438
+ }
439
+
440
+ if (!dirty) {
441
+ const nextDep = link.nextDep_
442
+ if (nextDep) {
443
+ link = nextDep
444
+ continue
445
+ }
446
+ }
447
+
448
+ while (checkDepth--) {
449
+ const firstSub = sub.subs_
450
+ const hasMultipleSubs = firstSub.nextSub_
451
+ if (hasMultipleSubs) {
452
+ link = stack.value_
453
+ stack = stack.prev_
454
+ } else {
455
+ link = firstSub
456
+ }
457
+ if (dirty) {
458
+ if (update(sub)) {
459
+ if (hasMultipleSubs) {
460
+ shallowPropagate(firstSub)
461
+ }
462
+ sub = link.sub_
463
+ continue
404
464
  }
405
- while (checkDepth--) {
406
- const firstSub = sub.subs_;
407
- const hasMultipleSubs = firstSub.nextSub_;
408
- if (hasMultipleSubs) {
409
- link = stack.value_;
410
- stack = stack.prev_;
411
- }
412
- else {
413
- link = firstSub;
414
- }
415
- if (dirty) {
416
- if (update(sub)) {
417
- if (hasMultipleSubs) {
418
- shallowPropagate(firstSub);
419
- }
420
- sub = link.sub_;
421
- continue;
422
- }
423
- dirty = false;
424
- }
425
- else {
426
- sub.flags_ &= ~32;
427
- }
428
- sub = link.sub_;
429
- if (link.nextDep_) {
430
- link = link.nextDep_;
431
- continue top;
432
- }
433
- }
434
- return dirty;
435
- }
436
- };
465
+ dirty = false
466
+ } else {
467
+ sub.flags_ &= ~(32)
468
+ }
469
+ sub = link.sub_
470
+ if (link.nextDep_) {
471
+ link = link.nextDep_
472
+ continue top
473
+ }
474
+ }
475
+
476
+ return dirty
477
+ }
478
+ }
479
+
437
480
  const shallowPropagate = (link) => {
438
- do {
439
- const sub = link.sub_;
440
- const flags = sub.flags_;
441
- if ((flags & 48) ===
442
- 32) {
443
- sub.flags_ = flags | 16;
444
- if (flags & 2) {
445
- notify(sub);
446
- }
447
- }
448
- } while ((link = link.nextSub_));
449
- };
481
+ do {
482
+ const sub = link.sub_
483
+ const flags = sub.flags_
484
+ if (
485
+ (flags & (48)) ===
486
+ (32)
487
+ ) {
488
+ sub.flags_ = flags | (16)
489
+ if (flags & (2)) {
490
+ notify(sub)
491
+ }
492
+ }
493
+ } while ((link = link.nextSub_))
494
+ }
495
+
450
496
  const isValidLink = (checkLink, sub) => {
451
- let link = sub.depsTail_;
452
- while (link) {
453
- if (link === checkLink) {
454
- return true;
455
- }
456
- link = link.prevDep_;
457
- }
458
- return false;
459
- };
497
+ let link = sub.depsTail_
498
+ while (link) {
499
+ if (link === checkLink) {
500
+ return true
501
+ }
502
+ link = link.prevDep_
503
+ }
504
+ return false
505
+ }
506
+
460
507
  export const getPath = (path) => {
461
- let result = root;
462
- const split = path.split(".");
463
- for (const path of split) {
464
- if (result == null || !hasOwn(result, path)) {
465
- return;
466
- }
467
- result = result[path];
468
- }
469
- return result;
470
- };
508
+ let result = root
509
+ const split = path.split(".")
510
+ for (const path of split) {
511
+ if (result == null || !hasOwn(result, path)) {
512
+ return
513
+ }
514
+ result = result[path]
515
+ }
516
+ return result
517
+ }
518
+
471
519
  const deep = (value, prefix = "") => {
472
- const isArr = Array.isArray(value);
473
- if (isArr || isPojo(value)) {
474
- const deepObj = (isArr ? [] : {});
475
- for (const key in value) {
476
- deepObj[key] = signal(deep(value[key], `${prefix + key}.`));
520
+ const isArr = Array.isArray(value)
521
+ if (isArr || isPojo(value)) {
522
+ const deepObj = (isArr ? [] : {})
523
+ for (const key in value) {
524
+ deepObj[key] = signal(deep((value)[key], `${prefix + key}.`))
525
+ }
526
+ const keys = signal(0)
527
+ return new Proxy(deepObj, {
528
+ get(_, prop) {
529
+ if (!(prop === "toJSON" && !hasOwn(deepObj, prop))) {
530
+ if (isArr && prop in Array.prototype) {
531
+ keys()
532
+ return deepObj[prop]
533
+ }
534
+ if (typeof prop === "symbol") {
535
+ return deepObj[prop]
536
+ }
537
+ if (!hasOwn(deepObj, prop) || deepObj[prop]() == null) {
538
+ deepObj[prop] = signal("")
539
+ dispatch(prefix + prop, "")
540
+ keys(keys() + 1)
541
+ }
542
+ return deepObj[prop]()
477
543
  }
478
- const keys = signal(0);
479
- return new Proxy(deepObj, {
480
- get(_, prop) {
481
- if (!(prop === "toJSON" && !hasOwn(deepObj, prop))) {
482
- if (isArr && prop in Array.prototype) {
483
- keys();
484
- return deepObj[prop];
485
- }
486
- if (typeof prop === "symbol") {
487
- return deepObj[prop];
488
- }
489
- if (!hasOwn(deepObj, prop) || deepObj[prop]() == null) {
490
- deepObj[prop] = signal("");
491
- dispatch(prefix + prop, "");
492
- keys(keys() + 1);
493
- }
494
- return deepObj[prop]();
495
- }
496
- },
497
- set(_, prop, newValue) {
498
- const path = prefix + prop;
499
- if (isArr && prop === "length") {
500
- const diff = deepObj[prop] - newValue;
501
- deepObj[prop] = newValue;
502
- if (diff > 0) {
503
- const patch = {};
504
- for (let i = newValue; i < deepObj[prop]; i++) {
505
- patch[i] = null;
506
- }
507
- dispatch(prefix.slice(0, -1), patch);
508
- keys(keys() + 1);
509
- }
510
- }
511
- else if (hasOwn(deepObj, prop)) {
512
- if (newValue == null) {
513
- delete deepObj[prop];
514
- }
515
- else if (hasOwn(newValue, computedSymbol)) {
516
- deepObj[prop] = newValue;
517
- dispatch(path, "");
518
- }
519
- else {
520
- const currentValue = deepObj[prop]();
521
- const pathStr = `${path}.`;
522
- if (isPojo(currentValue) && isPojo(newValue)) {
523
- for (const key in currentValue) {
524
- if (!hasOwn(newValue, key)) {
525
- delete currentValue[key];
526
- dispatch(pathStr + key, null);
527
- }
528
- }
529
- for (const key in newValue) {
530
- const nextVal = newValue[key];
531
- if (currentValue[key] !== nextVal) {
532
- currentValue[key] = nextVal;
533
- }
534
- }
535
- }
536
- else if (deepObj[prop](deep(newValue, pathStr))) {
537
- dispatch(path, newValue);
538
- }
539
- }
544
+ },
545
+ set(_, prop, newValue) {
546
+ const path = prefix + prop
547
+ if (isArr && prop === "length") {
548
+ const diff = (deepObj[prop]) - newValue
549
+ deepObj[prop] = newValue
550
+ if (diff > 0) {
551
+ const patch = {}
552
+ for (let i = newValue; i < deepObj[prop]; i++) {
553
+ patch[i] = null
554
+ }
555
+ dispatch(prefix.slice(0, -1), patch)
556
+ keys(keys() + 1)
557
+ }
558
+ } else if (hasOwn(deepObj, prop)) {
559
+ if (newValue == null) {
560
+ delete deepObj[prop]
561
+ } else if (hasOwn(newValue, computedSymbol)) {
562
+ deepObj[prop] = newValue
563
+ dispatch(path, "")
564
+ } else {
565
+ const currentValue = deepObj[prop]()
566
+ const pathStr = `${path}.`
567
+ if (isPojo(currentValue) && isPojo(newValue)) {
568
+ for (const key in currentValue) {
569
+ if (!hasOwn(newValue, key)) {
570
+ delete currentValue[key]
571
+ dispatch(pathStr + key, null)
540
572
  }
541
- else if (newValue != null) {
542
- if (hasOwn(newValue, computedSymbol)) {
543
- deepObj[prop] = newValue;
544
- dispatch(path, "");
545
- }
546
- else {
547
- deepObj[prop] = signal(deep(newValue, `${path}.`));
548
- dispatch(path, newValue);
549
- }
550
- keys(keys() + 1);
573
+ }
574
+ for (const key in newValue) {
575
+ const nextVal = newValue[key]
576
+ if (currentValue[key] !== nextVal) {
577
+ currentValue[key] = nextVal
551
578
  }
552
- return true;
553
- },
554
- deleteProperty(_, prop) {
555
- delete deepObj[prop];
556
- keys(keys() + 1);
557
- return true;
558
- },
559
- ownKeys() {
560
- keys();
561
- return Reflect.ownKeys(deepObj);
562
- },
563
- has(_, prop) {
564
- keys();
565
- return prop in deepObj;
566
- },
567
- });
568
- }
569
- return value;
570
- };
579
+ }
580
+ } else if (deepObj[prop](deep(newValue, pathStr))) {
581
+ dispatch(path, newValue)
582
+ }
583
+ }
584
+ } else if (newValue != null) {
585
+ if (hasOwn(newValue, computedSymbol)) {
586
+ deepObj[prop] = newValue
587
+ dispatch(path, "")
588
+ } else {
589
+ deepObj[prop] = signal(deep(newValue, `${path}.`))
590
+ dispatch(path, newValue)
591
+ }
592
+ keys(keys() + 1)
593
+ }
594
+
595
+ return true
596
+ },
597
+ deleteProperty(_, prop) {
598
+ delete deepObj[prop]
599
+ keys(keys() + 1)
600
+ return true
601
+ },
602
+ ownKeys() {
603
+ keys()
604
+ return Reflect.ownKeys(deepObj)
605
+ },
606
+ has(_, prop) {
607
+ keys()
608
+ return prop in deepObj
609
+ },
610
+ })
611
+ }
612
+ return value
613
+ }
614
+
571
615
  const dispatch = (path, value) => {
572
- if (path !== undefined && value !== undefined) {
573
- currentPatch.push([path, value]);
574
- }
575
- if (!batchDepth && currentPatch.length) {
576
- const detail = pathToObj(currentPatch);
577
- currentPatch.length = 0;
578
- document.dispatchEvent(new CustomEvent(DATASTAR_SIGNAL_PATCH_EVENT, {
579
- detail,
580
- }));
581
- }
582
- };
616
+ if (path !== undefined && value !== undefined) {
617
+ currentPatch.push([path, value])
618
+ }
619
+ if (!batchDepth && currentPatch.length) {
620
+ const detail = pathToObj(currentPatch)
621
+ currentPatch.length = 0
622
+ document.dispatchEvent(
623
+ new CustomEvent(DATASTAR_SIGNAL_PATCH_EVENT, {
624
+ detail,
625
+ }),
626
+ )
627
+ }
628
+ }
629
+
583
630
  export const mergePatch = (patch, { ifMissing } = {}) => {
584
- beginBatch();
631
+ beginBatch()
632
+ for (const key in patch) {
633
+ if (patch[key] == null) {
634
+ if (!ifMissing) {
635
+ delete root[key]
636
+ }
637
+ } else {
638
+ mergeInner(patch[key], key, root, "", ifMissing)
639
+ }
640
+ }
641
+ endBatch()
642
+ }
643
+
644
+ export const mergePaths = (paths, options) =>
645
+ mergePatch(pathToObj(paths), options)
646
+
647
+ const mergeInner = (
648
+ patch,
649
+ target,
650
+ targetParent,
651
+ prefix,
652
+ ifMissing,
653
+ ) => {
654
+ if (isPojo(patch)) {
655
+ if (
656
+ !(
657
+ hasOwn(targetParent, target) &&
658
+ (isPojo(targetParent[target]) || Array.isArray(targetParent[target]))
659
+ )
660
+ ) {
661
+ targetParent[target] = {}
662
+ }
663
+
585
664
  for (const key in patch) {
586
- if (patch[key] == null) {
587
- if (!ifMissing) {
588
- delete root[key];
589
- }
590
- }
591
- else {
592
- mergeInner(patch[key], key, root, "", ifMissing);
593
- }
594
- }
595
- endBatch();
596
- };
597
- export const mergePaths = (paths, options) => mergePatch(pathToObj(paths), options);
598
- const mergeInner = (patch, target, targetParent, prefix, ifMissing) => {
599
- if (isPojo(patch)) {
600
- if (!(hasOwn(targetParent, target) &&
601
- (isPojo(targetParent[target]) || Array.isArray(targetParent[target])))) {
602
- targetParent[target] = {};
665
+ if (patch[key] == null) {
666
+ if (!ifMissing) {
667
+ delete targetParent[target][key]
603
668
  }
604
- for (const key in patch) {
605
- if (patch[key] == null) {
606
- if (!ifMissing) {
607
- delete targetParent[target][key];
608
- }
609
- }
610
- else {
611
- mergeInner(patch[key], key, targetParent[target], `${prefix + target}.`, ifMissing);
612
- }
613
- }
614
- }
615
- else if (!(ifMissing && hasOwn(targetParent, target))) {
616
- targetParent[target] = patch;
617
- }
618
- };
619
- const toRegExp = (val) => typeof val === "string" ? RegExp(val.replace(/^\/|\/$/g, "")) : val;
620
- export const filtered = ({ include = /.*/, exclude = /(?!)/ } = {}, obj = root) => {
621
- const includeRe = toRegExp(include);
622
- const excludeRe = toRegExp(exclude);
623
- const paths = [];
624
- const stack = [[obj, ""]];
625
- while (stack.length) {
626
- const [node, prefix] = stack.pop();
627
- for (const key in node) {
628
- const path = prefix + key;
629
- if (isPojo(node[key])) {
630
- stack.push([node[key], `${path}.`]);
631
- }
632
- else if (includeRe.test(path) && !excludeRe.test(path)) {
633
- paths.push([path, getPath(path)]);
634
- }
635
- }
636
- }
637
- return pathToObj(paths);
638
- };
639
- export const root = deep({});
669
+ } else {
670
+ mergeInner(patch[key], key, targetParent[target], `${prefix + target}.`, ifMissing)
671
+ }
672
+ }
673
+ } else if (!(ifMissing && hasOwn(targetParent, target))) {
674
+ targetParent[target] = patch
675
+ }
676
+ }
677
+
678
+ const toRegExp = (val) =>
679
+ typeof val === "string" ? RegExp(val.replace(/^\/|\/$/g, "")) : val
680
+
681
+ export const filtered = (
682
+ { include = /.*/, exclude = /(?!)/ } = {},
683
+ obj = root,
684
+ ) => {
685
+ const includeRe = toRegExp(include)
686
+ const excludeRe = toRegExp(exclude)
687
+ const paths = []
688
+ const stack = [[obj, ""]]
689
+
690
+ while (stack.length) {
691
+ const [node, prefix] = stack.pop()
692
+
693
+ for (const key in node) {
694
+ const path = prefix + key
695
+ if (isPojo(node[key])) {
696
+ stack.push([node[key], `${path}.`])
697
+ } else if (includeRe.test(path) && !excludeRe.test(path)) {
698
+ paths.push([path, getPath(path)])
699
+ }
700
+ }
701
+ }
702
+
703
+ return pathToObj(paths)
704
+ }
705
+
706
+ export const root = deep({})
707
+
640
708
  /*********
641
709
  * engine.ts (plugin system)
642
710
  *********/
643
- const url = "https://data-star.dev/errors";
711
+ const url = "https://data-star.dev/errors"
712
+
644
713
  const error = (ctx, reason, metadata = {}) => {
645
- Object.assign(metadata, ctx);
646
- const e = new Error();
647
- const r = snake(reason);
648
- const q = new URLSearchParams({
649
- metadata: JSON.stringify(metadata),
650
- }).toString();
651
- const c = JSON.stringify(metadata, null, 2);
652
- e.message = `${reason}\nMore info: ${url}/${r}?${q}\nContext: ${c}`;
653
- return e;
654
- };
655
- const actionPlugins = new Map();
656
- const attributePlugins = new Map();
657
- const watcherPlugins = new Map();
658
- export const actions = new Proxy({}, {
714
+ Object.assign(metadata, ctx)
715
+ const e = new Error()
716
+ const r = snake(reason)
717
+ const q = new URLSearchParams({
718
+ metadata: JSON.stringify(metadata),
719
+ }).toString()
720
+ const c = JSON.stringify(metadata, null, 2)
721
+ e.message = `${reason}\nMore info: ${url}/${r}?${q}\nContext: ${c}`
722
+ return e
723
+ }
724
+
725
+ const actionPlugins = new Map()
726
+ const attributePlugins = new Map()
727
+ const watcherPlugins = new Map()
728
+
729
+ export const actions = new Proxy(
730
+ {},
731
+ {
659
732
  get: (_, prop) => actionPlugins.get(prop)?.apply,
660
733
  has: (_, prop) => actionPlugins.has(prop),
661
734
  ownKeys: () => Reflect.ownKeys(actionPlugins),
662
735
  set: () => false,
663
736
  deleteProperty: () => false,
664
- });
665
- const removals = new Map();
666
- const queuedAttributes = [];
667
- const queuedAttributeNames = new Set();
668
- const observedRoots = new WeakSet();
669
- export const attribute = (plugin) => {
670
- queuedAttributes.push(plugin);
671
- if (queuedAttributes.length === 1) {
672
- setTimeout(() => {
673
- for (const attribute of queuedAttributes) {
674
- queuedAttributeNames.add(attribute.name);
675
- attributePlugins.set(attribute.name, attribute);
676
- }
677
- queuedAttributes.length = 0;
678
- apply();
679
- queuedAttributeNames.clear();
680
- });
681
- }
682
- };
737
+ },
738
+ )
739
+
740
+ const removals = new Map()
741
+
742
+ const queuedAttributes = []
743
+ const queuedAttributeNames = new Set()
744
+ const observedRoots = new WeakSet()
745
+ export const attribute = (
746
+ plugin,
747
+ ) => {
748
+ queuedAttributes.push(plugin)
749
+
750
+ if (queuedAttributes.length === 1) {
751
+ setTimeout(() => {
752
+ for (const attribute of queuedAttributes) {
753
+ queuedAttributeNames.add(attribute.name)
754
+ attributePlugins.set(attribute.name, attribute)
755
+ }
756
+ queuedAttributes.length = 0
757
+ apply()
758
+ queuedAttributeNames.clear()
759
+ })
760
+ }
761
+ }
762
+
683
763
  export const action = (plugin) => {
684
- actionPlugins.set(plugin.name, plugin);
685
- };
764
+ actionPlugins.set(plugin.name, plugin)
765
+ }
766
+
686
767
  document.addEventListener(DATASTAR_FETCH_EVENT, ((evt) => {
687
- const plugin = watcherPlugins.get(evt.detail.type);
688
- if (plugin) {
689
- plugin.apply({
690
- error: error.bind(0, {
691
- plugin: { type: "watcher", name: plugin.name },
692
- element: {
693
- id: evt.target.id,
694
- tag: evt.target.tagName,
695
- },
696
- }),
697
- }, evt.detail.argsRaw);
698
- }
699
- }));
768
+ const plugin = watcherPlugins.get(evt.detail.type)
769
+ if (plugin) {
770
+ plugin.apply(
771
+ {
772
+ error: error.bind(0, {
773
+ plugin: { type: "watcher", name: plugin.name },
774
+ element: {
775
+ id: (evt.target).id,
776
+ tag: (evt.target).tagName,
777
+ },
778
+ }),
779
+ },
780
+ evt.detail.argsRaw,
781
+ )
782
+ }
783
+ }))
784
+
700
785
  export const watcher = (plugin) => {
701
- watcherPlugins.set(plugin.name, plugin);
702
- };
786
+ watcherPlugins.set(plugin.name, plugin)
787
+ }
788
+
703
789
  const cleanupEls = (els) => {
704
- for (const el of els) {
705
- const elCleanups = removals.get(el);
706
- if (elCleanups && removals.delete(el)) {
707
- for (const attrCleanups of elCleanups.values()) {
708
- for (const cleanup of attrCleanups.values()) {
709
- cleanup();
710
- }
711
- }
790
+ for (const el of els) {
791
+ const elCleanups = removals.get(el)
792
+ if (elCleanups && removals.delete(el)) {
793
+ for (const attrCleanups of elCleanups.values()) {
794
+ for (const cleanup of attrCleanups.values()) {
795
+ cleanup()
712
796
  }
713
- }
714
- };
715
- const aliasedIgnore = aliasify("ignore");
716
- const aliasedIgnoreAttr = `[${aliasedIgnore}]`;
717
- const shouldIgnore = (el) => el.hasAttribute(`${aliasedIgnore}__self`) || !!el.closest(aliasedIgnoreAttr);
797
+ }
798
+ }
799
+ }
800
+ }
801
+
802
+ const aliasedIgnore = aliasify("ignore")
803
+ const aliasedIgnoreAttr = `[${aliasedIgnore}]`
804
+ const shouldIgnore = (el) =>
805
+ el.hasAttribute(`${aliasedIgnore}__self`) || !!el.closest(aliasedIgnoreAttr)
806
+
718
807
  const applyEls = (els, onlyNew) => {
719
- for (const el of els) {
720
- if (!shouldIgnore(el)) {
721
- for (const key in el.dataset) {
722
- applyAttributePlugin(el, key.replace(/[A-Z]/g, "-$&").toLowerCase(), el.dataset[key], onlyNew);
723
- }
724
- }
725
- }
726
- };
808
+ for (const el of els) {
809
+ if (!shouldIgnore(el)) {
810
+ for (const key in el.dataset) {
811
+ applyAttributePlugin(
812
+ el,
813
+ key.replace(/[A-Z]/g, "-$&").toLowerCase(),
814
+ el.dataset[key],
815
+ onlyNew,
816
+ )
817
+ }
818
+ }
819
+ }
820
+ }
821
+
727
822
  const observe = (mutations) => {
728
- for (const { target, type, attributeName, addedNodes, removedNodes } of mutations) {
729
- if (type === "childList") {
730
- for (const node of removedNodes) {
731
- if (isHTMLOrSVG(node)) {
732
- cleanupEls([node]);
733
- cleanupEls(node.querySelectorAll("*"));
734
- }
735
- }
736
- for (const node of addedNodes) {
737
- if (isHTMLOrSVG(node)) {
738
- applyEls([node]);
739
- applyEls(node.querySelectorAll("*"));
740
- }
741
- }
742
- }
743
- else if (type === "attributes" &&
744
- attributeName.startsWith("data-") &&
745
- isHTMLOrSVG(target) &&
746
- !shouldIgnore(target)) {
747
- const key = attributeName.slice(5);
748
- const value = target.getAttribute(attributeName);
749
- if (value === null) {
750
- const elCleanups = removals.get(target);
751
- if (elCleanups) {
752
- const attrCleanups = elCleanups.get(key);
753
- if (attrCleanups) {
754
- for (const cleanup of attrCleanups.values()) {
755
- cleanup();
756
- }
757
- elCleanups.delete(key);
758
- }
759
- }
760
- }
761
- else {
762
- applyAttributePlugin(target, key, value);
763
- }
823
+ for (const { target, type, attributeName, addedNodes, removedNodes } of mutations) {
824
+ if (type === "childList") {
825
+ for (const node of removedNodes) {
826
+ if (isHTMLOrSVG(node)) {
827
+ cleanupEls([node])
828
+ cleanupEls(node.querySelectorAll("*"))
764
829
  }
765
- }
766
- };
767
- const mutationObserver = new MutationObserver(observe);
768
- export const parseAttributeKey = (rawKey) => {
769
- const [namePart, ...rawModifiers] = rawKey.split("__");
770
- const [pluginName, key] = namePart.split(/:(.+)/);
771
- const mods = new Map();
772
- for (const rawMod of rawModifiers) {
773
- const [label, ...mod] = rawMod.split(".");
774
- mods.set(label, new Set(mod));
775
- }
776
- return { pluginName, key, mods };
777
- };
778
- export const isDocumentObserverActive = () => observedRoots.has(document.documentElement);
779
- export const apply = (root = document.documentElement, observeRoot = true) => {
780
- if (isHTMLOrSVG(root)) {
781
- applyEls([root], true);
782
- }
783
- applyEls(root.querySelectorAll("*"), true);
784
- if (observeRoot) {
785
- mutationObserver.observe(root, {
786
- subtree: true,
787
- childList: true,
788
- attributes: true,
789
- });
790
- observedRoots.add(root);
791
- }
792
- };
793
- const applyAttributePlugin = (el, attrKey, value, onlyNew) => {
794
- const rawKey = attrKey;
795
- const { pluginName, key, mods } = parseAttributeKey(rawKey);
796
- const plugin = attributePlugins.get(pluginName);
797
- if ((!onlyNew || queuedAttributeNames.has(pluginName)) && plugin) {
798
- const ctx = {
799
- el,
800
- rawKey,
801
- mods,
802
- error: error.bind(0, {
803
- plugin: { type: "attribute", name: plugin.name },
804
- element: { id: el.id, tag: el.tagName },
805
- expression: { rawKey, key, value },
806
- }),
807
- key,
808
- value,
809
- loadedPluginNames: {
810
- actions: new Set(actionPlugins.keys()),
811
- attributes: new Set(attributePlugins.keys()),
812
- },
813
- rx: undefined,
814
- };
815
- const keyReq = (plugin.requirement &&
816
- (typeof plugin.requirement === "string" ? plugin.requirement : plugin.requirement.key)) ||
817
- "allowed";
818
- const valueReq = (plugin.requirement &&
819
- (typeof plugin.requirement === "string" ? plugin.requirement : plugin.requirement.value)) ||
820
- "allowed";
821
- const keyProvided = key !== undefined && key !== null && key !== "";
822
- const valueProvided = value !== undefined && value !== null && value !== "";
823
- if (keyProvided) {
824
- if (keyReq === "denied") {
825
- throw ctx.error("KeyNotAllowed");
826
- }
827
- }
828
- else if (keyReq === "must") {
829
- throw ctx.error("KeyRequired");
830
- }
831
- if (valueProvided) {
832
- if (valueReq === "denied") {
833
- throw ctx.error("ValueNotAllowed");
834
- }
835
- }
836
- else if (valueReq === "must") {
837
- throw ctx.error("ValueRequired");
838
- }
839
- if (keyReq === "exclusive" || valueReq === "exclusive") {
840
- if (keyProvided && valueProvided) {
841
- throw ctx.error("KeyAndValueProvided");
842
- }
843
- if (!keyProvided && !valueProvided) {
844
- throw ctx.error("KeyOrValueRequired");
845
- }
846
- }
847
- const cleanups = new Map();
848
- if (valueProvided) {
849
- let cachedRx;
850
- ctx.rx = (...args) => {
851
- if (!cachedRx) {
852
- cachedRx = genRx(value, {
853
- returnsValue: plugin.returnsValue,
854
- argNames: plugin.argNames,
855
- cleanups,
856
- });
857
- }
858
- return cachedRx(el, ...args);
859
- };
830
+ }
831
+
832
+ for (const node of addedNodes) {
833
+ if (isHTMLOrSVG(node)) {
834
+ applyEls([node])
835
+ applyEls(node.querySelectorAll("*"))
860
836
  }
861
- const cleanup = plugin.apply(ctx);
862
- if (cleanup) {
863
- cleanups.set("attribute", cleanup);
864
- }
865
- let elCleanups = removals.get(el);
837
+ }
838
+ } else if (
839
+ type === "attributes" &&
840
+ attributeName.startsWith("data-") &&
841
+ isHTMLOrSVG(target) &&
842
+ !shouldIgnore(target)
843
+ ) {
844
+ const key = attributeName.slice(5)
845
+ const value = target.getAttribute(attributeName)
846
+ if (value === null) {
847
+ const elCleanups = removals.get(target)
866
848
  if (elCleanups) {
867
- const attrCleanups = elCleanups.get(rawKey);
868
- if (attrCleanups) {
869
- for (const oldCleanup of attrCleanups.values()) {
870
- oldCleanup();
871
- }
849
+ const attrCleanups = elCleanups.get(key)
850
+ if (attrCleanups) {
851
+ for (const cleanup of attrCleanups.values()) {
852
+ cleanup()
872
853
  }
854
+ elCleanups.delete(key)
855
+ }
873
856
  }
874
- else {
875
- elCleanups = new Map();
876
- removals.set(el, elCleanups);
857
+ } else {
858
+ applyAttributePlugin(target, key, value)
859
+ }
860
+ }
861
+ }
862
+ }
863
+
864
+ const mutationObserver = new MutationObserver(observe)
865
+
866
+ export const parseAttributeKey = (
867
+ rawKey,
868
+ ) => {
869
+ const [namePart, ...rawModifiers] = rawKey.split("__")
870
+ const [pluginName, key] = namePart.split(/:(.+)/)
871
+ const mods = new Map()
872
+
873
+ for (const rawMod of rawModifiers) {
874
+ const [label, ...mod] = rawMod.split(".")
875
+ mods.set(label, new Set(mod))
876
+ }
877
+
878
+ return { pluginName, key, mods }
879
+ }
880
+
881
+ export const isDocumentObserverActive = () => observedRoots.has(document.documentElement)
882
+
883
+ export const apply = (
884
+ root = document.documentElement,
885
+ observeRoot = true,
886
+ ) => {
887
+ if (isHTMLOrSVG(root)) {
888
+ applyEls([root], true)
889
+ }
890
+ applyEls(root.querySelectorAll("*"), true)
891
+
892
+ if (observeRoot) {
893
+ mutationObserver.observe(root, {
894
+ subtree: true,
895
+ childList: true,
896
+ attributes: true,
897
+ })
898
+ observedRoots.add(root)
899
+ }
900
+ }
901
+
902
+ const applyAttributePlugin = (
903
+ el,
904
+ attrKey,
905
+ value,
906
+ onlyNew,
907
+ ) => {
908
+ const rawKey = attrKey
909
+ const { pluginName, key, mods } = parseAttributeKey(rawKey)
910
+ const plugin = attributePlugins.get(pluginName)
911
+ if ((!onlyNew || queuedAttributeNames.has(pluginName)) && plugin) {
912
+ const ctx = {
913
+ el,
914
+ rawKey,
915
+ mods,
916
+ error: error.bind(0, {
917
+ plugin: { type: "attribute", name: plugin.name },
918
+ element: { id: el.id, tag: el.tagName },
919
+ expression: { rawKey, key, value },
920
+ }),
921
+ key,
922
+ value,
923
+ loadedPluginNames: {
924
+ actions: new Set(actionPlugins.keys()),
925
+ attributes: new Set(attributePlugins.keys()),
926
+ },
927
+ rx: undefined,
928
+ }
929
+
930
+ const keyReq =
931
+ (plugin.requirement &&
932
+ (typeof plugin.requirement === "string" ? plugin.requirement : plugin.requirement.key)) ||
933
+ "allowed"
934
+ const valueReq =
935
+ (plugin.requirement &&
936
+ (typeof plugin.requirement === "string" ? plugin.requirement : plugin.requirement.value)) ||
937
+ "allowed"
938
+
939
+ const keyProvided = key !== undefined && key !== null && key !== ""
940
+ const valueProvided = value !== undefined && value !== null && value !== ""
941
+
942
+ if (keyProvided) {
943
+ if (keyReq === "denied") {
944
+ throw ctx.error("KeyNotAllowed")
945
+ }
946
+ } else if (keyReq === "must") {
947
+ throw ctx.error("KeyRequired")
948
+ }
949
+
950
+ if (valueProvided) {
951
+ if (valueReq === "denied") {
952
+ throw ctx.error("ValueNotAllowed")
953
+ }
954
+ } else if (valueReq === "must") {
955
+ throw ctx.error("ValueRequired")
956
+ }
957
+
958
+ if (keyReq === "exclusive" || valueReq === "exclusive") {
959
+ if (keyProvided && valueProvided) {
960
+ throw ctx.error("KeyAndValueProvided")
961
+ }
962
+ if (!keyProvided && !valueProvided) {
963
+ throw ctx.error("KeyOrValueRequired")
964
+ }
965
+ }
966
+
967
+ const cleanups = new Map()
968
+ if (valueProvided) {
969
+ let cachedRx
970
+ ctx.rx = (...args) => {
971
+ if (!cachedRx) {
972
+ cachedRx = genRx(value, {
973
+ returnsValue: plugin.returnsValue,
974
+ argNames: plugin.argNames,
975
+ cleanups,
976
+ })
877
977
  }
878
- elCleanups.set(rawKey, cleanups);
879
- }
880
- };
881
- const genRx = (value, { returnsValue = false, argNames = [], cleanups = new Map() } = {}) => {
882
- if (/^\s*(?:async\s+)?(?:\(.*?\)\s*=>|[\w$]+\s*=>|function\s*[\w$]*\s*\()/.test(value)) {
883
- const userFn = Function(`return (${value.trim()})`)();
884
- return (el, ...args) => {
885
- const actionsProxy = new Proxy({}, {
886
- get: (_, name) => (...actionArgs) => {
887
- const err = error.bind(0, {
888
- plugin: { type: "action", name },
889
- element: { id: el.id, tag: el.tagName },
890
- expression: { fnContent: value, value },
891
- });
892
- const fn = actions[name];
893
- if (fn)
894
- return fn({ el, evt: undefined, error: err, cleanups }, ...actionArgs);
895
- throw err("UndefinedAction");
896
- },
897
- });
898
- const dataEvt = args[0] instanceof Event ? args[0] : new Event("datastar:expression");
899
- Object.defineProperties(dataEvt, {
900
- target: { value: el },
901
- signals: { value: root },
902
- actions: { value: actionsProxy },
903
- window: { value: window },
904
- });
905
- try {
906
- return userFn(dataEvt);
907
- }
908
- catch (e) {
909
- console.error(e);
910
- throw error({
911
- element: { id: el.id, tag: el.tagName },
912
- expression: { fnContent: value, value },
913
- error: e.message,
914
- }, "ExecuteExpression");
915
- }
916
- };
917
- }
918
- let expr = "";
919
- if (returnsValue) {
920
- const statementRe = /(\/(\\\/|[^/])*\/|"(\\"|[^"])*"|'(\\'|[^'])*'|`(\\`|[^`])*`|\(\s*((function)\s*\(\s*\)|(\(\s*\))\s*=>)\s*(?:\{[\s\S]*?\}|[^;){]*)\s*\)\s*\(\s*\)|[^;])+/gm;
921
- const statements = value.trim().match(statementRe);
922
- if (statements) {
923
- const lastIdx = statements.length - 1;
924
- const last = statements[lastIdx].trim();
925
- if (!last.startsWith("return")) {
926
- statements[lastIdx] = `return (${last});`;
927
- }
928
- expr = statements.join(";\n");
978
+ return cachedRx(el, ...args)
979
+ }
980
+ }
981
+
982
+ const cleanup = plugin.apply(ctx)
983
+ if (cleanup) {
984
+ cleanups.set("attribute", cleanup)
985
+ }
986
+
987
+ let elCleanups = removals.get(el)
988
+ if (elCleanups) {
989
+ const attrCleanups = elCleanups.get(rawKey)
990
+ if (attrCleanups) {
991
+ for (const oldCleanup of attrCleanups.values()) {
992
+ oldCleanup()
929
993
  }
930
- }
931
- else {
932
- expr = value.trim();
933
- }
934
- const escaped = new Map();
935
- const escapeRe = RegExp(`(?:${DSP})(.*?)(?:${DSS})`, "gm");
936
- let counter = 0;
937
- for (const match of expr.matchAll(escapeRe)) {
938
- const k = match[1];
939
- const v = `__escaped${counter++}`;
940
- escaped.set(v, k);
941
- expr = expr.replace(DSP + k + DSS, v);
942
- }
943
- expr = expr
944
- .replace(/\$\['([a-zA-Z_$\d][\w$]*)'\]/g, "$$$1")
945
- .replace(/\$([a-zA-Z_\d]\w*(?:[.-]\w+)*)/g, (_, signalName) => signalName.split(".").reduce((acc, part) => `${acc}['${part}']`, "$"));
946
- expr = expr.replaceAll(/@([A-Za-z_$][\w$]*)\(/g, '__action("$1",evt,');
947
- for (const [k, v] of escaped) {
948
- expr = expr.replace(k, v);
949
- }
950
- try {
951
- const fn = Function("el", "$", "__action", "evt", ...argNames, expr);
952
- return (el, ...args) => {
953
- const action = (name, evt, ...args) => {
954
- const err = error.bind(0, {
955
- plugin: { type: "action", name },
956
- element: { id: el.id, tag: el.tagName },
957
- expression: {
958
- fnContent: expr,
959
- value,
960
- },
961
- });
962
- const fn = actions[name];
963
- if (fn) {
964
- return fn({
965
- el,
966
- evt,
967
- error: err,
968
- cleanups,
969
- }, ...args);
970
- }
971
- throw err("UndefinedAction");
972
- };
973
- try {
974
- return fn(el, root, action, undefined, ...args);
975
- }
976
- catch (e) {
977
- console.error(e);
978
- throw error({
979
- element: { id: el.id, tag: el.tagName },
980
- expression: {
981
- fnContent: expr,
982
- value,
983
- },
984
- error: e.message,
985
- }, "ExecuteExpression");
986
- }
987
- };
988
- }
989
- catch (e) {
990
- console.error(e);
991
- throw error({
994
+ }
995
+ } else {
996
+ elCleanups = new Map()
997
+ removals.set(el, elCleanups)
998
+ }
999
+ elCleanups.set(rawKey, cleanups)
1000
+ }
1001
+ }
1002
+
1003
+ const genRx = (
1004
+ value,
1005
+ { returnsValue = false, argNames = [], cleanups = new Map() } = {},
1006
+ ) => {
1007
+ if (/^\s*(?:async\s+)?(?:\(.*?\)\s*=>|[\w$]+\s*=>|function\s*[\w$]*\s*\()/.test(value)) {
1008
+ const userFn = Function(`return (${value.trim()})`)()
1009
+
1010
+ return (el, ...args) => {
1011
+ const actionsProxy = new Proxy({}, {
1012
+ get:
1013
+ (_, name) =>
1014
+ (...actionArgs) => {
1015
+ const err = error.bind(0, {
1016
+ plugin: { type: "action", name },
1017
+ element: { id: el.id, tag: el.tagName },
1018
+ expression: { fnContent: value, value },
1019
+ })
1020
+ const fn = actions[name]
1021
+ if (fn) return fn({ el, evt: undefined, error: err, cleanups }, ...actionArgs)
1022
+ throw err("UndefinedAction")
1023
+ },
1024
+ })
1025
+
1026
+ const dataEvt = args[0] instanceof Event ? args[0] : new Event("datastar:expression")
1027
+ Object.defineProperties(dataEvt, {
1028
+ target: { value: el },
1029
+ signals: { value: root },
1030
+ actions: { value: actionsProxy },
1031
+ window: { value: window },
1032
+ })
1033
+
1034
+ try {
1035
+ return userFn(dataEvt)
1036
+ } catch (e) {
1037
+ console.error(e)
1038
+ throw error(
1039
+ {
1040
+ element: { id: el.id, tag: el.tagName },
1041
+ expression: { fnContent: value, value },
1042
+ error: e.message,
1043
+ },
1044
+ "ExecuteExpression",
1045
+ )
1046
+ }
1047
+ }
1048
+ }
1049
+
1050
+ let expr = ""
1051
+ if (returnsValue) {
1052
+ const statementRe =
1053
+ /(\/(\\\/|[^/])*\/|"(\\"|[^"])*"|'(\\'|[^'])*'|`(\\`|[^`])*`|\(\s*((function)\s*\(\s*\)|(\(\s*\))\s*=>)\s*(?:\{[\s\S]*?\}|[^;){]*)\s*\)\s*\(\s*\)|[^;])+/gm
1054
+ const statements = value.trim().match(statementRe)
1055
+ if (statements) {
1056
+ const lastIdx = statements.length - 1
1057
+ const last = statements[lastIdx].trim()
1058
+ if (!last.startsWith("return")) {
1059
+ statements[lastIdx] = `return (${last});`
1060
+ }
1061
+ expr = statements.join(";\n")
1062
+ }
1063
+ } else {
1064
+ expr = value.trim()
1065
+ }
1066
+
1067
+ const escaped = new Map()
1068
+ const escapeRe = RegExp(`(?:${DSP})(.*?)(?:${DSS})`, "gm")
1069
+ let counter = 0
1070
+ for (const match of expr.matchAll(escapeRe)) {
1071
+ const k = match[1]
1072
+ const v = `__escaped${counter++}`
1073
+ escaped.set(v, k)
1074
+ expr = expr.replace(DSP + k + DSS, v)
1075
+ }
1076
+
1077
+ expr = expr
1078
+ .replace(/\$\['([a-zA-Z_$\d][\w$]*)'\]/g, "$$$1")
1079
+ .replace(/\$([a-zA-Z_\d]\w*(?:[.-]\w+)*)/g, (_, signalName) =>
1080
+ signalName.split(".").reduce((acc, part) => `${acc}['${part}']`, "$"),
1081
+ )
1082
+
1083
+ expr = expr.replaceAll(/@([A-Za-z_$][\w$]*)\(/g, '__action("$1",evt,')
1084
+
1085
+ for (const [k, v] of escaped) {
1086
+ expr = expr.replace(k, v)
1087
+ }
1088
+
1089
+ try {
1090
+ const fn = Function("el", "$", "__action", "evt", ...argNames, expr)
1091
+ return (el, ...args) => {
1092
+ const action = (name, evt, ...args) => {
1093
+ const err = error.bind(0, {
1094
+ plugin: { type: "action", name },
1095
+ element: { id: el.id, tag: el.tagName },
1096
+ expression: {
1097
+ fnContent: expr,
1098
+ value,
1099
+ },
1100
+ })
1101
+ const fn = actions[name]
1102
+ if (fn) {
1103
+ return fn(
1104
+ {
1105
+ el,
1106
+ evt,
1107
+ error: err,
1108
+ cleanups,
1109
+ },
1110
+ ...args,
1111
+ )
1112
+ }
1113
+ throw err("UndefinedAction")
1114
+ }
1115
+ try {
1116
+ return fn(el, root, action, undefined, ...args)
1117
+ } catch (e) {
1118
+ console.error(e)
1119
+ throw error(
1120
+ {
1121
+ element: { id: el.id, tag: el.tagName },
992
1122
  expression: {
993
- fnContent: expr,
994
- value,
1123
+ fnContent: expr,
1124
+ value,
995
1125
  },
996
1126
  error: e.message,
997
- }, "GenerateExpression");
998
- }
999
- };
1127
+ },
1128
+ "ExecuteExpression",
1129
+ )
1130
+ }
1131
+ }
1132
+ } catch (e) {
1133
+ console.error(e)
1134
+ throw error(
1135
+ {
1136
+ expression: {
1137
+ fnContent: expr,
1138
+ value,
1139
+ },
1140
+ error: e.message,
1141
+ },
1142
+ "GenerateExpression",
1143
+ )
1144
+ }
1145
+ }