@bquery/bquery 1.6.0 → 1.8.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.
- package/README.md +192 -18
- package/dist/a11y/announce.d.ts +43 -0
- package/dist/a11y/announce.d.ts.map +1 -0
- package/dist/a11y/audit.d.ts +42 -0
- package/dist/a11y/audit.d.ts.map +1 -0
- package/dist/a11y/index.d.ts +53 -0
- package/dist/a11y/index.d.ts.map +1 -0
- package/dist/a11y/media-preferences.d.ts +77 -0
- package/dist/a11y/media-preferences.d.ts.map +1 -0
- package/dist/a11y/roving-tab-index.d.ts +38 -0
- package/dist/a11y/roving-tab-index.d.ts.map +1 -0
- package/dist/a11y/skip-link.d.ts +37 -0
- package/dist/a11y/skip-link.d.ts.map +1 -0
- package/dist/a11y/trap-focus.d.ts +49 -0
- package/dist/a11y/trap-focus.d.ts.map +1 -0
- package/dist/a11y/types.d.ts +152 -0
- package/dist/a11y/types.d.ts.map +1 -0
- package/dist/a11y-DVBCy09c.js +421 -0
- package/dist/a11y-DVBCy09c.js.map +1 -0
- package/dist/a11y.es.mjs +14 -0
- package/dist/component/component.d.ts.map +1 -1
- package/dist/component/html.d.ts.map +1 -1
- package/dist/component/index.d.ts +2 -1
- package/dist/component/index.d.ts.map +1 -1
- package/dist/component/library.d.ts.map +1 -1
- package/dist/component/scope.d.ts +138 -0
- package/dist/component/scope.d.ts.map +1 -0
- package/dist/component/types.d.ts +53 -1
- package/dist/component/types.d.ts.map +1 -1
- package/dist/component-L3-JfOFz.js +684 -0
- package/dist/component-L3-JfOFz.js.map +1 -0
- package/dist/component.es.mjs +9 -6
- package/dist/{config-DRmZZno3.js → config-DhT9auRm.js} +4 -4
- package/dist/{config-DRmZZno3.js.map → config-DhT9auRm.js.map} +1 -1
- package/dist/constraints-D5RHQLmP.js +100 -0
- package/dist/constraints-D5RHQLmP.js.map +1 -0
- package/dist/core/collection.d.ts +134 -0
- package/dist/core/collection.d.ts.map +1 -1
- package/dist/core/element.d.ts +120 -0
- package/dist/core/element.d.ts.map +1 -1
- package/dist/core/env.d.ts +18 -0
- package/dist/core/env.d.ts.map +1 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/shared.d.ts +14 -0
- package/dist/core/shared.d.ts.map +1 -1
- package/dist/core/utils/index.d.ts +52 -41
- package/dist/core/utils/index.d.ts.map +1 -1
- package/dist/core-DdtZHzsS.js +168 -0
- package/dist/core-DdtZHzsS.js.map +1 -0
- package/dist/{core-CCEabVHl.js → core-EMYSLzaT.js} +293 -194
- package/dist/core-EMYSLzaT.js.map +1 -0
- package/dist/core.es.mjs +48 -46
- package/dist/custom-directives-Dr4C5lVV.js +9 -0
- package/dist/custom-directives-Dr4C5lVV.js.map +1 -0
- package/dist/devtools/devtools.d.ts +212 -0
- package/dist/devtools/devtools.d.ts.map +1 -0
- package/dist/devtools/index.d.ts +20 -0
- package/dist/devtools/index.d.ts.map +1 -0
- package/dist/devtools/types.d.ts +69 -0
- package/dist/devtools/types.d.ts.map +1 -0
- package/dist/devtools-BhB2iDPT.js +122 -0
- package/dist/devtools-BhB2iDPT.js.map +1 -0
- package/dist/devtools.es.mjs +19 -0
- package/dist/dnd/draggable.d.ts +51 -0
- package/dist/dnd/draggable.d.ts.map +1 -0
- package/dist/dnd/droppable.d.ts +38 -0
- package/dist/dnd/droppable.d.ts.map +1 -0
- package/dist/dnd/index.d.ts +47 -0
- package/dist/dnd/index.d.ts.map +1 -0
- package/dist/dnd/sortable.d.ts +43 -0
- package/dist/dnd/sortable.d.ts.map +1 -0
- package/dist/dnd/types.d.ts +250 -0
- package/dist/dnd/types.d.ts.map +1 -0
- package/dist/dnd-NwZBYh4l.js +244 -0
- package/dist/dnd-NwZBYh4l.js.map +1 -0
- package/dist/dnd.es.mjs +6 -0
- package/dist/env-CTdvLaH2.js +19 -0
- package/dist/env-CTdvLaH2.js.map +1 -0
- package/dist/forms/create-form.d.ts +49 -0
- package/dist/forms/create-form.d.ts.map +1 -0
- package/dist/forms/index.d.ts +40 -0
- package/dist/forms/index.d.ts.map +1 -0
- package/dist/forms/types.d.ts +185 -0
- package/dist/forms/types.d.ts.map +1 -0
- package/dist/forms/use-field.d.ts +34 -0
- package/dist/forms/use-field.d.ts.map +1 -0
- package/dist/forms/validators.d.ts +204 -0
- package/dist/forms/validators.d.ts.map +1 -0
- package/dist/forms-UcRHsYxC.js +227 -0
- package/dist/forms-UcRHsYxC.js.map +1 -0
- package/dist/forms.es.mjs +16 -0
- package/dist/full.d.ts +30 -11
- package/dist/full.d.ts.map +1 -1
- package/dist/full.es.mjs +209 -93
- package/dist/full.iife.js +47 -31
- package/dist/full.iife.js.map +1 -1
- package/dist/full.umd.js +47 -31
- package/dist/full.umd.js.map +1 -1
- package/dist/function-Cybd57JV.js +33 -0
- package/dist/function-Cybd57JV.js.map +1 -0
- package/dist/i18n/formatting.d.ts +40 -0
- package/dist/i18n/formatting.d.ts.map +1 -0
- package/dist/i18n/i18n.d.ts +48 -0
- package/dist/i18n/i18n.d.ts.map +1 -0
- package/dist/i18n/index.d.ts +57 -0
- package/dist/i18n/index.d.ts.map +1 -0
- package/dist/i18n/translate.d.ts +83 -0
- package/dist/i18n/translate.d.ts.map +1 -0
- package/dist/i18n/types.d.ts +156 -0
- package/dist/i18n/types.d.ts.map +1 -0
- package/dist/i18n-kuF6Ekj6.js +89 -0
- package/dist/i18n-kuF6Ekj6.js.map +1 -0
- package/dist/i18n.es.mjs +6 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.mjs +257 -143
- package/dist/media/battery.d.ts +35 -0
- package/dist/media/battery.d.ts.map +1 -0
- package/dist/media/breakpoints.d.ts +51 -0
- package/dist/media/breakpoints.d.ts.map +1 -0
- package/dist/media/clipboard.d.ts +30 -0
- package/dist/media/clipboard.d.ts.map +1 -0
- package/dist/media/device-sensors.d.ts +54 -0
- package/dist/media/device-sensors.d.ts.map +1 -0
- package/dist/media/geolocation.d.ts +38 -0
- package/dist/media/geolocation.d.ts.map +1 -0
- package/dist/media/index.d.ts +42 -0
- package/dist/media/index.d.ts.map +1 -0
- package/dist/media/media-query.d.ts +36 -0
- package/dist/media/media-query.d.ts.map +1 -0
- package/dist/media/network.d.ts +35 -0
- package/dist/media/network.d.ts.map +1 -0
- package/dist/media/types.d.ts +173 -0
- package/dist/media/types.d.ts.map +1 -0
- package/dist/media/viewport.d.ts +32 -0
- package/dist/media/viewport.d.ts.map +1 -0
- package/dist/media-i-fB5WxI.js +340 -0
- package/dist/media-i-fB5WxI.js.map +1 -0
- package/dist/media.es.mjs +12 -0
- package/dist/motion/index.d.ts +7 -3
- package/dist/motion/index.d.ts.map +1 -1
- package/dist/motion/morph.d.ts +27 -0
- package/dist/motion/morph.d.ts.map +1 -0
- package/dist/motion/parallax.d.ts +30 -0
- package/dist/motion/parallax.d.ts.map +1 -0
- package/dist/motion/reduced-motion.d.ts +36 -3
- package/dist/motion/reduced-motion.d.ts.map +1 -1
- package/dist/motion/types.d.ts +58 -0
- package/dist/motion/types.d.ts.map +1 -1
- package/dist/motion/typewriter.d.ts +31 -0
- package/dist/motion/typewriter.d.ts.map +1 -0
- package/dist/motion-BJsAuULb.js +530 -0
- package/dist/motion-BJsAuULb.js.map +1 -0
- package/dist/motion.es.mjs +27 -23
- package/dist/{view-C70lA3vf.js → mount-B4Y8bk8Z.js} +166 -160
- package/dist/mount-B4Y8bk8Z.js.map +1 -0
- package/dist/{object-qGpWr6-J.js → object-BCk-1c8T.js} +5 -4
- package/dist/{object-qGpWr6-J.js.map → object-BCk-1c8T.js.map} +1 -1
- package/dist/{platform-Dr9b6fsq.js → platform-Dw2gE3zI.js} +21 -22
- package/dist/{platform-Dr9b6fsq.js.map → platform-Dw2gE3zI.js.map} +1 -1
- package/dist/platform.es.mjs +2 -2
- package/dist/plugin/index.d.ts +22 -0
- package/dist/plugin/index.d.ts.map +1 -0
- package/dist/plugin/registry.d.ts +108 -0
- package/dist/plugin/registry.d.ts.map +1 -0
- package/dist/plugin/types.d.ts +110 -0
- package/dist/plugin/types.d.ts.map +1 -0
- package/dist/plugin-C2WuC8SF.js +66 -0
- package/dist/plugin-C2WuC8SF.js.map +1 -0
- package/dist/plugin.es.mjs +9 -0
- package/dist/reactive/async-data.d.ts +28 -3
- package/dist/reactive/async-data.d.ts.map +1 -1
- package/dist/reactive/computed.d.ts +10 -0
- package/dist/reactive/computed.d.ts.map +1 -1
- package/dist/reactive/effect.d.ts +3 -0
- package/dist/reactive/effect.d.ts.map +1 -1
- package/dist/reactive/http.d.ts +194 -0
- package/dist/reactive/http.d.ts.map +1 -0
- package/dist/reactive/index.d.ts +2 -2
- package/dist/reactive/index.d.ts.map +1 -1
- package/dist/reactive/pagination.d.ts +126 -0
- package/dist/reactive/pagination.d.ts.map +1 -0
- package/dist/reactive/polling.d.ts +55 -0
- package/dist/reactive/polling.d.ts.map +1 -0
- package/dist/reactive/readonly.d.ts +20 -1
- package/dist/reactive/readonly.d.ts.map +1 -1
- package/dist/reactive/rest.d.ts +293 -0
- package/dist/reactive/rest.d.ts.map +1 -0
- package/dist/reactive/scope.d.ts +140 -0
- package/dist/reactive/scope.d.ts.map +1 -0
- package/dist/reactive/signal.d.ts +16 -2
- package/dist/reactive/signal.d.ts.map +1 -1
- package/dist/reactive/to-value.d.ts +57 -0
- package/dist/reactive/to-value.d.ts.map +1 -0
- package/dist/reactive/websocket.d.ts +285 -0
- package/dist/reactive/websocket.d.ts.map +1 -0
- package/dist/reactive-DwkhUJfP.js +1148 -0
- package/dist/reactive-DwkhUJfP.js.map +1 -0
- package/dist/reactive.es.mjs +38 -20
- package/dist/registry-B08iilIh.js +26 -0
- package/dist/registry-B08iilIh.js.map +1 -0
- package/dist/router/bq-link.d.ts +112 -0
- package/dist/router/bq-link.d.ts.map +1 -0
- package/dist/router/constraints.d.ts +9 -0
- package/dist/router/constraints.d.ts.map +1 -0
- package/dist/router/index.d.ts +15 -7
- package/dist/router/index.d.ts.map +1 -1
- package/dist/router/match.d.ts +0 -1
- package/dist/router/match.d.ts.map +1 -1
- package/dist/router/path-pattern.d.ts +14 -0
- package/dist/router/path-pattern.d.ts.map +1 -0
- package/dist/router/query.d.ts.map +1 -1
- package/dist/router/router.d.ts +3 -1
- package/dist/router/router.d.ts.map +1 -1
- package/dist/router/state.d.ts +25 -2
- package/dist/router/state.d.ts.map +1 -1
- package/dist/router/types.d.ts +48 -4
- package/dist/router/types.d.ts.map +1 -1
- package/dist/router/use-route.d.ts +50 -0
- package/dist/router/use-route.d.ts.map +1 -0
- package/dist/router/utils.d.ts +3 -0
- package/dist/router/utils.d.ts.map +1 -1
- package/dist/router-CQikC9Ed.js +492 -0
- package/dist/router-CQikC9Ed.js.map +1 -0
- package/dist/router.es.mjs +14 -10
- package/dist/{sanitize-Bs2dkMby.js → sanitize-B1V4JswB.js} +2 -1
- package/dist/{sanitize-Bs2dkMby.js.map → sanitize-B1V4JswB.js.map} +1 -1
- package/dist/security/index.d.ts +2 -2
- package/dist/security/index.d.ts.map +1 -1
- package/dist/security.es.mjs +1 -1
- package/dist/ssr/hydrate.d.ts +65 -0
- package/dist/ssr/hydrate.d.ts.map +1 -0
- package/dist/ssr/index.d.ts +59 -0
- package/dist/ssr/index.d.ts.map +1 -0
- package/dist/ssr/render.d.ts +62 -0
- package/dist/ssr/render.d.ts.map +1 -0
- package/dist/ssr/serialize.d.ts +118 -0
- package/dist/ssr/serialize.d.ts.map +1 -0
- package/dist/ssr/types.d.ts +70 -0
- package/dist/ssr/types.d.ts.map +1 -0
- package/dist/ssr-_dAcGdzu.js +248 -0
- package/dist/ssr-_dAcGdzu.js.map +1 -0
- package/dist/ssr.es.mjs +9 -0
- package/dist/store/create-store.d.ts.map +1 -1
- package/dist/store/index.d.ts +1 -1
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/persisted.d.ts +38 -4
- package/dist/store/persisted.d.ts.map +1 -1
- package/dist/store/types.d.ts +138 -1
- package/dist/store/types.d.ts.map +1 -1
- package/dist/store/utils.d.ts +2 -2
- package/dist/store/utils.d.ts.map +1 -1
- package/dist/store-Cb3gPRve.js +338 -0
- package/dist/store-Cb3gPRve.js.map +1 -0
- package/dist/store.es.mjs +11 -10
- package/dist/storybook/index.d.ts.map +1 -1
- package/dist/storybook.es.mjs +1 -1
- package/dist/storybook.es.mjs.map +1 -1
- package/dist/testing/index.d.ts +23 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/testing.d.ts +156 -0
- package/dist/testing/testing.d.ts.map +1 -0
- package/dist/testing/types.d.ts +134 -0
- package/dist/testing/types.d.ts.map +1 -0
- package/dist/testing-C5Sjfsna.js +224 -0
- package/dist/testing-C5Sjfsna.js.map +1 -0
- package/dist/testing.es.mjs +9 -0
- package/dist/type-guards-BMX2c0LP.js +44 -0
- package/dist/type-guards-BMX2c0LP.js.map +1 -0
- package/dist/untrack-D0fnO5k2.js +36 -0
- package/dist/untrack-D0fnO5k2.js.map +1 -0
- package/dist/view/custom-directives.d.ts +20 -0
- package/dist/view/custom-directives.d.ts.map +1 -0
- package/dist/view/evaluate.d.ts.map +1 -1
- package/dist/view/process.d.ts.map +1 -1
- package/dist/view.es.mjs +9 -9
- package/package.json +47 -11
- package/src/a11y/announce.ts +131 -0
- package/src/a11y/audit.ts +314 -0
- package/src/a11y/index.ts +68 -0
- package/src/a11y/media-preferences.ts +255 -0
- package/src/a11y/roving-tab-index.ts +164 -0
- package/src/a11y/skip-link.ts +255 -0
- package/src/a11y/trap-focus.ts +184 -0
- package/src/a11y/types.ts +183 -0
- package/src/component/component.ts +599 -524
- package/src/component/html.ts +153 -153
- package/src/component/index.ts +52 -50
- package/src/component/library.ts +540 -518
- package/src/component/scope.ts +212 -0
- package/src/component/types.ts +310 -256
- package/src/core/collection.ts +249 -1
- package/src/core/element.ts +252 -11
- package/src/core/env.ts +60 -0
- package/src/core/index.ts +1 -0
- package/src/core/shared.ts +64 -0
- package/src/core/utils/index.ts +66 -1
- package/src/devtools/devtools.ts +410 -0
- package/src/devtools/index.ts +48 -0
- package/src/devtools/types.ts +104 -0
- package/src/dnd/draggable.ts +296 -0
- package/src/dnd/droppable.ts +228 -0
- package/src/dnd/index.ts +62 -0
- package/src/dnd/sortable.ts +307 -0
- package/src/dnd/types.ts +293 -0
- package/src/forms/create-form.ts +320 -0
- package/src/forms/index.ts +70 -0
- package/src/forms/types.ts +203 -0
- package/src/forms/use-field.ts +231 -0
- package/src/forms/validators.ts +294 -0
- package/src/full.ts +554 -229
- package/src/i18n/formatting.ts +67 -0
- package/src/i18n/i18n.ts +200 -0
- package/src/i18n/index.ts +67 -0
- package/src/i18n/translate.ts +182 -0
- package/src/i18n/types.ts +171 -0
- package/src/index.ts +72 -0
- package/src/media/battery.ts +116 -0
- package/src/media/breakpoints.ts +129 -0
- package/src/media/clipboard.ts +80 -0
- package/src/media/device-sensors.ts +158 -0
- package/src/media/geolocation.ts +119 -0
- package/src/media/index.ts +76 -0
- package/src/media/media-query.ts +92 -0
- package/src/media/network.ts +115 -0
- package/src/media/types.ts +177 -0
- package/src/media/viewport.ts +84 -0
- package/src/motion/index.ts +11 -2
- package/src/motion/morph.ts +151 -0
- package/src/motion/parallax.ts +120 -0
- package/src/motion/reduced-motion.ts +52 -3
- package/src/motion/types.ts +63 -0
- package/src/motion/typewriter.ts +164 -0
- package/src/plugin/index.ts +37 -0
- package/src/plugin/registry.ts +284 -0
- package/src/plugin/types.ts +137 -0
- package/src/reactive/async-data.ts +250 -29
- package/src/reactive/computed.ts +53 -1
- package/src/reactive/effect.ts +29 -6
- package/src/reactive/http.ts +790 -0
- package/src/reactive/index.ts +60 -0
- package/src/reactive/pagination.ts +317 -0
- package/src/reactive/polling.ts +179 -0
- package/src/reactive/readonly.ts +52 -8
- package/src/reactive/rest.ts +859 -0
- package/src/reactive/scope.ts +276 -0
- package/src/reactive/signal.ts +61 -1
- package/src/reactive/to-value.ts +71 -0
- package/src/reactive/websocket.ts +849 -0
- package/src/router/bq-link.ts +279 -0
- package/src/router/constraints.ts +204 -0
- package/src/router/index.ts +15 -7
- package/src/router/match.ts +255 -49
- package/src/router/path-pattern.ts +52 -0
- package/src/router/query.ts +3 -0
- package/src/router/router.ts +258 -48
- package/src/router/state.ts +51 -3
- package/src/router/types.ts +50 -4
- package/src/router/use-route.ts +68 -0
- package/src/router/utils.ts +44 -3
- package/src/security/index.ts +12 -17
- package/src/security/sanitize.ts +70 -70
- package/src/security/trusted-html.ts +71 -71
- package/src/ssr/hydrate.ts +84 -0
- package/src/ssr/index.ts +70 -0
- package/src/ssr/render.ts +508 -0
- package/src/ssr/serialize.ts +296 -0
- package/src/ssr/types.ts +81 -0
- package/src/store/create-store.ts +146 -8
- package/src/store/define-store.ts +49 -49
- package/src/store/index.ts +5 -0
- package/src/store/mapping.ts +74 -74
- package/src/store/persisted.ts +245 -62
- package/src/store/types.ts +247 -92
- package/src/store/utils.ts +4 -10
- package/src/store/watch.ts +53 -53
- package/src/storybook/index.ts +480 -479
- package/src/testing/index.ts +42 -0
- package/src/testing/testing.ts +593 -0
- package/src/testing/types.ts +170 -0
- package/src/view/custom-directives.ts +28 -0
- package/src/view/evaluate.ts +2 -0
- package/src/view/process.ts +19 -3
- package/dist/component-BEQgt5hl.js +0 -600
- package/dist/component-BEQgt5hl.js.map +0 -1
- package/dist/core-BGQJVw0-.js +0 -35
- package/dist/core-BGQJVw0-.js.map +0 -1
- package/dist/core-CCEabVHl.js.map +0 -1
- package/dist/effect-AFRW_Plg.js +0 -84
- package/dist/effect-AFRW_Plg.js.map +0 -1
- package/dist/motion-D9TcHxOF.js +0 -415
- package/dist/motion-D9TcHxOF.js.map +0 -1
- package/dist/reactive-DSkct0dO.js +0 -254
- package/dist/reactive-DSkct0dO.js.map +0 -1
- package/dist/router-CbDhl8rS.js +0 -188
- package/dist/router-CbDhl8rS.js.map +0 -1
- package/dist/store-BwDvI45q.js +0 -263
- package/dist/store-BwDvI45q.js.map +0 -1
- package/dist/untrack-B0rVscTc.js +0 -7
- package/dist/untrack-B0rVscTc.js.map +0 -1
- package/dist/view-C70lA3vf.js.map +0 -1
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Store state serialization for SSR.
|
|
3
|
+
*
|
|
4
|
+
* Provides utilities to serialize store state into a `<script>` tag
|
|
5
|
+
* for client-side hydration, and to deserialize state on the client.
|
|
6
|
+
*
|
|
7
|
+
* @module bquery/ssr
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { getStore, listStores } from '../store/index';
|
|
11
|
+
import { isPrototypePollutionKey } from '../core/utils/object';
|
|
12
|
+
import type { DeserializedStoreState, SerializeOptions } from './types';
|
|
13
|
+
|
|
14
|
+
const isStoreStateObject = (value: unknown): value is Record<string, unknown> =>
|
|
15
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
16
|
+
|
|
17
|
+
const sanitizeHydrationState = (value: Record<string, unknown>): Record<string, unknown> => {
|
|
18
|
+
const sanitized: Record<string, unknown> = {};
|
|
19
|
+
for (const [key, entryValue] of Object.entries(value)) {
|
|
20
|
+
if (isPrototypePollutionKey(key)) continue;
|
|
21
|
+
sanitized[key] = entryValue;
|
|
22
|
+
}
|
|
23
|
+
return sanitized;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Result of store state serialization.
|
|
28
|
+
*/
|
|
29
|
+
export type SerializeResult = {
|
|
30
|
+
/** JSON string of the state map */
|
|
31
|
+
stateJson: string;
|
|
32
|
+
/** Complete `<script>` tag ready to embed in HTML */
|
|
33
|
+
scriptTag: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Escapes a string for safe embedding in a `<script>` tag.
|
|
38
|
+
* Prevents XSS via `</script>` injection and HTML entities.
|
|
39
|
+
*
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
const escapeForScript = (str: string): string => {
|
|
43
|
+
return str
|
|
44
|
+
.replace(/</g, '\\u003c')
|
|
45
|
+
.replace(/>/g, '\\u003e')
|
|
46
|
+
.replace(/\//g, '\\u002f')
|
|
47
|
+
.replace(/\u2028/g, '\\u2028')
|
|
48
|
+
.replace(/\u2029/g, '\\u2029');
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Escapes a string for safe embedding in an HTML attribute value.
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
const escapeForHtmlAttribute = (str: string): string => {
|
|
56
|
+
return str
|
|
57
|
+
.replace(/&/g, '&')
|
|
58
|
+
.replace(/"/g, '"')
|
|
59
|
+
.replace(/</g, '<')
|
|
60
|
+
.replace(/>/g, '>');
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Serializes the state of registered stores into a JSON string and
|
|
65
|
+
* a `<script>` tag suitable for embedding in server-rendered HTML.
|
|
66
|
+
*
|
|
67
|
+
* The serialized state can be picked up on the client using
|
|
68
|
+
* `deserializeStoreState()` to restore stores to their server-side values.
|
|
69
|
+
*
|
|
70
|
+
* @param options - Serialization options
|
|
71
|
+
* @returns Object with JSON string and ready-to-use script tag
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { serializeStoreState } from '@bquery/bquery/ssr';
|
|
76
|
+
* import { createStore } from '@bquery/bquery/store';
|
|
77
|
+
*
|
|
78
|
+
* const store = createStore({
|
|
79
|
+
* id: 'counter',
|
|
80
|
+
* state: () => ({ count: 42 }),
|
|
81
|
+
* });
|
|
82
|
+
*
|
|
83
|
+
* const { scriptTag } = serializeStoreState();
|
|
84
|
+
* // '<script id="__BQUERY_STORE_STATE__">window.__BQUERY_INITIAL_STATE__={"counter":{"count":42}}</script>'
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* // Serialize only specific stores
|
|
90
|
+
* const { scriptTag } = serializeStoreState({ storeIds: ['counter'] });
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export const serializeStoreState = (options: SerializeOptions = {}): SerializeResult => {
|
|
94
|
+
const {
|
|
95
|
+
scriptId = '__BQUERY_STORE_STATE__',
|
|
96
|
+
globalKey = '__BQUERY_INITIAL_STATE__',
|
|
97
|
+
storeIds,
|
|
98
|
+
serialize = JSON.stringify,
|
|
99
|
+
} = options;
|
|
100
|
+
|
|
101
|
+
if (isPrototypePollutionKey(globalKey)) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`serializeStoreState: invalid globalKey "${globalKey}" - prototype-pollution keys are not allowed.`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (isPrototypePollutionKey(scriptId)) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
`serializeStoreState: invalid scriptId "${scriptId}" - prototype-pollution keys are not allowed.`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const ids = storeIds ?? listStores();
|
|
114
|
+
const stateMap = Object.create(null) as Record<string, Record<string, unknown>>;
|
|
115
|
+
|
|
116
|
+
for (const id of ids) {
|
|
117
|
+
if (isPrototypePollutionKey(id)) {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const store = getStore<{ $state: Record<string, unknown> }>(id);
|
|
122
|
+
if (store) {
|
|
123
|
+
stateMap[id] = sanitizeHydrationState(store.$state);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const stateJson = serialize(stateMap);
|
|
128
|
+
if (typeof stateJson !== 'string') {
|
|
129
|
+
throw new Error('serializeStoreState: custom serialize function must return a string.');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (serialize !== JSON.stringify) {
|
|
133
|
+
let parsedStateJson: unknown;
|
|
134
|
+
try {
|
|
135
|
+
parsedStateJson = JSON.parse(stateJson);
|
|
136
|
+
} catch {
|
|
137
|
+
throw new Error('serializeStoreState: custom serialize function returned invalid JSON.');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (!isStoreStateObject(parsedStateJson)) {
|
|
141
|
+
throw new Error(
|
|
142
|
+
'serializeStoreState: custom serialize function must return a JSON object string.'
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const escapedJson = escapeForScript(stateJson);
|
|
148
|
+
const escapedGlobalKey = escapeForScript(JSON.stringify(globalKey));
|
|
149
|
+
const escapedScriptId = escapeForHtmlAttribute(scriptId);
|
|
150
|
+
const scriptTag = `<script id="${escapedScriptId}">window[${escapedGlobalKey}]=${escapedJson}</script>`;
|
|
151
|
+
|
|
152
|
+
return { stateJson, scriptTag };
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Deserializes store state from the global variable set by the SSR script tag.
|
|
157
|
+
*
|
|
158
|
+
* Call this on the client before creating stores to pre-populate them with
|
|
159
|
+
* server-rendered state. After deserialization, the script tag and global
|
|
160
|
+
* variable are cleaned up automatically.
|
|
161
|
+
*
|
|
162
|
+
* @param globalKey - The global variable name where state was serialized
|
|
163
|
+
* @param scriptId - The ID of the SSR script tag to remove after hydration
|
|
164
|
+
* @returns The deserialized state map, or an empty object if not found
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* import { deserializeStoreState } from '@bquery/bquery/ssr';
|
|
169
|
+
*
|
|
170
|
+
* // Call before creating stores
|
|
171
|
+
* const state = deserializeStoreState();
|
|
172
|
+
* // state = { counter: { count: 42 } }
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
export const deserializeStoreState = (
|
|
176
|
+
globalKey = '__BQUERY_INITIAL_STATE__',
|
|
177
|
+
scriptId = '__BQUERY_STORE_STATE__'
|
|
178
|
+
): DeserializedStoreState => {
|
|
179
|
+
if (isPrototypePollutionKey(globalKey)) {
|
|
180
|
+
throw new Error(
|
|
181
|
+
`deserializeStoreState: invalid globalKey "${globalKey}" - prototype-pollution keys are not allowed.`
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (isPrototypePollutionKey(scriptId)) {
|
|
186
|
+
throw new Error(
|
|
187
|
+
`deserializeStoreState: invalid scriptId "${scriptId}" - prototype-pollution keys are not allowed.`
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (typeof window === 'undefined') {
|
|
192
|
+
return {};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const state = (window as unknown as Record<string, unknown>)[globalKey];
|
|
196
|
+
if (!state) {
|
|
197
|
+
return {};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Clean up global variable
|
|
201
|
+
try {
|
|
202
|
+
delete (window as unknown as Record<string, unknown>)[globalKey];
|
|
203
|
+
} catch {
|
|
204
|
+
// In strict mode on some environments, delete may fail
|
|
205
|
+
(window as unknown as Record<string, unknown>)[globalKey] = undefined;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Clean up script tag
|
|
209
|
+
if (typeof document !== 'undefined' && typeof document.getElementById === 'function') {
|
|
210
|
+
const scriptEl = document.getElementById(scriptId);
|
|
211
|
+
if (scriptEl) {
|
|
212
|
+
scriptEl.remove();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (!isStoreStateObject(state)) {
|
|
217
|
+
return {};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
for (const value of Object.values(state)) {
|
|
221
|
+
if (!isStoreStateObject(value)) {
|
|
222
|
+
return {};
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const sanitizedStateMap = Object.create(null) as DeserializedStoreState;
|
|
227
|
+
|
|
228
|
+
for (const [storeId, storeState] of Object.entries(state)) {
|
|
229
|
+
if (isPrototypePollutionKey(storeId) || !isStoreStateObject(storeState)) {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
sanitizedStateMap[storeId] = sanitizeHydrationState(storeState);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return sanitizedStateMap;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Hydrates a store with pre-serialized state from SSR.
|
|
241
|
+
*
|
|
242
|
+
* If the store exists and has a `$patch` method, this applies the
|
|
243
|
+
* deserialized state as a patch. Otherwise, the state is ignored.
|
|
244
|
+
*
|
|
245
|
+
* @param storeId - The store ID to hydrate
|
|
246
|
+
* @param state - The plain state object to apply
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```ts
|
|
250
|
+
* import { hydrateStore, deserializeStoreState } from '@bquery/bquery/ssr';
|
|
251
|
+
* import { createStore } from '@bquery/bquery/store';
|
|
252
|
+
*
|
|
253
|
+
* // 1. Deserialize state from SSR script tag
|
|
254
|
+
* const ssrState = deserializeStoreState();
|
|
255
|
+
*
|
|
256
|
+
* // 2. Create store (gets initial values from factory)
|
|
257
|
+
* const store = createStore({
|
|
258
|
+
* id: 'counter',
|
|
259
|
+
* state: () => ({ count: 0 }),
|
|
260
|
+
* });
|
|
261
|
+
*
|
|
262
|
+
* // 3. Apply SSR state
|
|
263
|
+
* if (ssrState.counter) {
|
|
264
|
+
* hydrateStore('counter', ssrState.counter);
|
|
265
|
+
* }
|
|
266
|
+
* // store.count is now 42 (from SSR)
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
export const hydrateStore = (storeId: string, state: Record<string, unknown>): void => {
|
|
270
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
271
|
+
const store = getStore<{ $patch?: (partial: any) => void }>(storeId);
|
|
272
|
+
if (store && typeof store.$patch === 'function') {
|
|
273
|
+
store.$patch(sanitizeHydrationState(state));
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Hydrates all stores at once from a deserialized state map.
|
|
279
|
+
*
|
|
280
|
+
* Convenience wrapper that calls `hydrateStore` for each entry in the state map.
|
|
281
|
+
*
|
|
282
|
+
* @param stateMap - Map of store IDs to their state objects
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```ts
|
|
286
|
+
* import { hydrateStores, deserializeStoreState } from '@bquery/bquery/ssr';
|
|
287
|
+
*
|
|
288
|
+
* const ssrState = deserializeStoreState();
|
|
289
|
+
* hydrateStores(ssrState);
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
export const hydrateStores = (stateMap: DeserializedStoreState): void => {
|
|
293
|
+
for (const [storeId, state] of Object.entries(stateMap)) {
|
|
294
|
+
hydrateStore(storeId, state);
|
|
295
|
+
}
|
|
296
|
+
};
|
package/src/ssr/types.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { HydrateMountOptions } from './hydrate';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Public types for the SSR / Pre-rendering module.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options for server-side rendering a template to an HTML string.
|
|
9
|
+
*/
|
|
10
|
+
export type RenderOptions = {
|
|
11
|
+
/**
|
|
12
|
+
* Prefix for directive attributes.
|
|
13
|
+
* @default 'bq'
|
|
14
|
+
*/
|
|
15
|
+
prefix?: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Whether to strip directive attributes from the output HTML.
|
|
19
|
+
* When `true`, attributes like `bq-text`, `bq-if`, etc. are removed
|
|
20
|
+
* from the rendered output for cleaner HTML.
|
|
21
|
+
* @default false
|
|
22
|
+
*/
|
|
23
|
+
stripDirectives?: boolean;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Whether to include a serialized store state `<script>` tag in the output.
|
|
27
|
+
* When `true`, all registered store states are serialized and appended.
|
|
28
|
+
* You can also pass an array of store IDs to serialize only specific stores.
|
|
29
|
+
* @default false
|
|
30
|
+
*/
|
|
31
|
+
includeStoreState?: boolean | string[];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Result of a `renderToString` call.
|
|
36
|
+
*/
|
|
37
|
+
export type SSRResult = {
|
|
38
|
+
/** The rendered HTML string */
|
|
39
|
+
html: string;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Serialized store state string, typically the `<script>` tag payload
|
|
43
|
+
* produced when `includeStoreState` is enabled.
|
|
44
|
+
*/
|
|
45
|
+
storeState?: string;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** @deprecated Use `HydrateMountOptions` instead. */
|
|
49
|
+
export type HydrationOptions = HydrateMountOptions;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Options for serializing store state.
|
|
53
|
+
*/
|
|
54
|
+
export type SerializeOptions = {
|
|
55
|
+
/**
|
|
56
|
+
* The ID attribute for the generated `<script>` tag.
|
|
57
|
+
* @default '__BQUERY_STORE_STATE__'
|
|
58
|
+
*/
|
|
59
|
+
scriptId?: string;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The global variable name where state will be assigned.
|
|
63
|
+
* @default '__BQUERY_INITIAL_STATE__'
|
|
64
|
+
*/
|
|
65
|
+
globalKey?: string;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Store IDs to serialize. If omitted, all registered stores are serialized.
|
|
69
|
+
*/
|
|
70
|
+
storeIds?: string[];
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Custom serializer function. Defaults to `JSON.stringify`.
|
|
74
|
+
*/
|
|
75
|
+
serialize?: (data: unknown) => string;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Deserialized store state map: store ID → plain state object.
|
|
80
|
+
*/
|
|
81
|
+
export type DeserializedStoreState = Record<string, Record<string, unknown>>;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Store creation logic.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
import { isPromise } from '../core/utils/type-guards';
|
|
5
6
|
import {
|
|
6
7
|
batch,
|
|
7
8
|
computed,
|
|
@@ -13,7 +14,14 @@ import {
|
|
|
13
14
|
import { notifyDevtoolsStateChange, registerDevtoolsStore } from './devtools';
|
|
14
15
|
import { applyPlugins } from './plugins';
|
|
15
16
|
import { getStore, hasStore, registerStore } from './registry';
|
|
16
|
-
import type {
|
|
17
|
+
import type {
|
|
18
|
+
ActionContext,
|
|
19
|
+
Getters,
|
|
20
|
+
OnActionCallback,
|
|
21
|
+
Store,
|
|
22
|
+
StoreDefinition,
|
|
23
|
+
StoreSubscriber,
|
|
24
|
+
} from './types';
|
|
17
25
|
import { deepClone, detectNestedMutations, isDev } from './utils';
|
|
18
26
|
|
|
19
27
|
/**
|
|
@@ -53,6 +61,63 @@ export const createStore = <
|
|
|
53
61
|
// Subscribers for $subscribe
|
|
54
62
|
const subscribers: Array<StoreSubscriber<S>> = [];
|
|
55
63
|
|
|
64
|
+
// Action lifecycle hooks for $onAction
|
|
65
|
+
const actionListeners: Array<OnActionCallback<S, G, A>> = [];
|
|
66
|
+
|
|
67
|
+
const reportOnActionError = (
|
|
68
|
+
phase: 'listener' | 'after' | 'onError',
|
|
69
|
+
actionName: string,
|
|
70
|
+
error: unknown
|
|
71
|
+
): void => {
|
|
72
|
+
if (!isDev() || typeof console === 'undefined' || typeof console.error !== 'function') return;
|
|
73
|
+
console.error(
|
|
74
|
+
`[bQuery store "${id}"] Error in $onAction ${phase} for action "${actionName}"`,
|
|
75
|
+
error
|
|
76
|
+
);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const warnedAsyncOnActionListeners = new WeakSet<OnActionCallback<S, G, A>>();
|
|
80
|
+
|
|
81
|
+
const warnAsyncOnActionListener = (
|
|
82
|
+
listener: OnActionCallback<S, G, A>,
|
|
83
|
+
actionName: string
|
|
84
|
+
): void => {
|
|
85
|
+
if (!isDev() || typeof console === 'undefined' || typeof console.warn !== 'function') return;
|
|
86
|
+
if (warnedAsyncOnActionListeners.has(listener)) return;
|
|
87
|
+
warnedAsyncOnActionListeners.add(listener);
|
|
88
|
+
console.warn(
|
|
89
|
+
`[bQuery store "${id}"] Async $onAction listener detected for action "${actionName}". If it awaits, register after()/onError() before the first await; late registrations will not affect the current action.`
|
|
90
|
+
);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Executes an action observer callback without allowing observer failures to
|
|
95
|
+
* affect the action result. Handles both synchronous exceptions and async
|
|
96
|
+
* rejections, routing all failures through the standard $onAction logger.
|
|
97
|
+
*
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
const runOnActionCallback = (
|
|
101
|
+
phase: 'listener' | 'after' | 'onError',
|
|
102
|
+
actionName: string,
|
|
103
|
+
callback: () => unknown,
|
|
104
|
+
listener?: OnActionCallback<S, G, A>
|
|
105
|
+
): void => {
|
|
106
|
+
try {
|
|
107
|
+
const result = callback();
|
|
108
|
+
if (isPromise(result)) {
|
|
109
|
+
if (phase === 'listener' && listener) {
|
|
110
|
+
warnAsyncOnActionListener(listener, actionName);
|
|
111
|
+
}
|
|
112
|
+
void result.catch((error) => {
|
|
113
|
+
reportOnActionError(phase, actionName, error);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
} catch (error) {
|
|
117
|
+
reportOnActionError(phase, actionName, error);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
56
121
|
/**
|
|
57
122
|
* Gets the current state.
|
|
58
123
|
*
|
|
@@ -172,12 +237,13 @@ export const createStore = <
|
|
|
172
237
|
});
|
|
173
238
|
}
|
|
174
239
|
|
|
175
|
-
// Bind actions to the store context
|
|
240
|
+
// Bind actions to the store context, with $onAction lifecycle support
|
|
176
241
|
for (const key of Object.keys(actions) as Array<keyof A>) {
|
|
177
242
|
const actionFn = actions[key];
|
|
243
|
+
const actionName = key as keyof A & string;
|
|
178
244
|
|
|
179
|
-
// Wrap action to enable 'this' binding
|
|
180
|
-
(store as Record<string, unknown>)[
|
|
245
|
+
// Wrap action to enable 'this' binding and $onAction hooks
|
|
246
|
+
(store as Record<string, unknown>)[actionName] = function (...args: unknown[]) {
|
|
181
247
|
// Create a context that allows 'this.property' access
|
|
182
248
|
const context = new Proxy(store, {
|
|
183
249
|
get: (target, prop) => {
|
|
@@ -198,7 +264,67 @@ export const createStore = <
|
|
|
198
264
|
},
|
|
199
265
|
});
|
|
200
266
|
|
|
201
|
-
|
|
267
|
+
// Run $onAction hooks if any listeners are registered
|
|
268
|
+
if (actionListeners.length === 0) {
|
|
269
|
+
return actionFn.apply(context, args);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const afterHooks: Array<(result: unknown) => void> = [];
|
|
273
|
+
const errorHooks: Array<(error: unknown) => void> = [];
|
|
274
|
+
const listenerSnapshot = [...actionListeners];
|
|
275
|
+
|
|
276
|
+
const listenerContext = {
|
|
277
|
+
name: actionName,
|
|
278
|
+
store,
|
|
279
|
+
args: args as Parameters<A[typeof actionName]>,
|
|
280
|
+
after: (callback: (result: Awaited<ReturnType<A[typeof actionName]>>) => void) => {
|
|
281
|
+
afterHooks.push((result) =>
|
|
282
|
+
callback(result as Awaited<ReturnType<A[typeof actionName]>>)
|
|
283
|
+
);
|
|
284
|
+
},
|
|
285
|
+
onError: (callback: (error: unknown) => void) => {
|
|
286
|
+
errorHooks.push(callback);
|
|
287
|
+
},
|
|
288
|
+
} satisfies ActionContext<S, G, A, typeof actionName>;
|
|
289
|
+
|
|
290
|
+
// Notify all action listeners (before phase)
|
|
291
|
+
for (const listener of listenerSnapshot) {
|
|
292
|
+
runOnActionCallback('listener', actionName, () => listener(listenerContext), listener);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
let result: unknown;
|
|
296
|
+
try {
|
|
297
|
+
result = actionFn.apply(context, args);
|
|
298
|
+
} catch (error) {
|
|
299
|
+
for (const hook of errorHooks) {
|
|
300
|
+
runOnActionCallback('onError', actionName, () => hook(error));
|
|
301
|
+
}
|
|
302
|
+
throw error;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Handle async actions (promises)
|
|
306
|
+
if (isPromise(result)) {
|
|
307
|
+
return result.then(
|
|
308
|
+
(resolved) => {
|
|
309
|
+
for (const hook of afterHooks) {
|
|
310
|
+
runOnActionCallback('after', actionName, () => hook(resolved));
|
|
311
|
+
}
|
|
312
|
+
return resolved;
|
|
313
|
+
},
|
|
314
|
+
(error) => {
|
|
315
|
+
for (const hook of errorHooks) {
|
|
316
|
+
runOnActionCallback('onError', actionName, () => hook(error));
|
|
317
|
+
}
|
|
318
|
+
throw error;
|
|
319
|
+
}
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Sync action — run after hooks immediately
|
|
324
|
+
for (const hook of afterHooks) {
|
|
325
|
+
runOnActionCallback('after', actionName, () => hook(result));
|
|
326
|
+
}
|
|
327
|
+
return result;
|
|
202
328
|
};
|
|
203
329
|
}
|
|
204
330
|
|
|
@@ -233,13 +359,25 @@ export const createStore = <
|
|
|
233
359
|
writable: false,
|
|
234
360
|
enumerable: false,
|
|
235
361
|
},
|
|
362
|
+
$onAction: {
|
|
363
|
+
value: (callback: OnActionCallback<S, G, A>) => {
|
|
364
|
+
actionListeners.push(callback);
|
|
365
|
+
return () => {
|
|
366
|
+
const index = actionListeners.indexOf(callback);
|
|
367
|
+
if (index > -1) actionListeners.splice(index, 1);
|
|
368
|
+
};
|
|
369
|
+
},
|
|
370
|
+
writable: false,
|
|
371
|
+
enumerable: false,
|
|
372
|
+
},
|
|
236
373
|
$patch: {
|
|
237
374
|
value: (partial: Partial<S> | ((state: S) => void)) => {
|
|
238
375
|
batch(() => {
|
|
239
376
|
if (typeof partial === 'function') {
|
|
240
377
|
// Capture state before mutation for nested mutation detection
|
|
241
|
-
const
|
|
242
|
-
const
|
|
378
|
+
const devMode = isDev();
|
|
379
|
+
const stateBefore = devMode ? deepClone(getCurrentState()) : null;
|
|
380
|
+
const signalValuesBefore = devMode
|
|
243
381
|
? new Map(Array.from(stateSignals.entries()).map(([k, s]) => [k, s.value]))
|
|
244
382
|
: null;
|
|
245
383
|
|
|
@@ -248,7 +386,7 @@ export const createStore = <
|
|
|
248
386
|
partial(state);
|
|
249
387
|
|
|
250
388
|
// Detect nested mutations in development mode
|
|
251
|
-
if (
|
|
389
|
+
if (devMode && stateBefore && signalValuesBefore) {
|
|
252
390
|
const mutatedKeys = detectNestedMutations(stateBefore, state, signalValuesBefore);
|
|
253
391
|
if (mutatedKeys.length > 0) {
|
|
254
392
|
console.warn(
|
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Store factory helpers.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { createStore } from './create-store';
|
|
6
|
-
import { getStore, hasStore } from './registry';
|
|
7
|
-
import type { Store, StoreDefinition } from './types';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Creates a store factory that returns the store instance.
|
|
11
|
-
*
|
|
12
|
-
* The store is lazily created on first call and cached in the global store
|
|
13
|
-
* registry. Subsequent calls return the same instance. After calling
|
|
14
|
-
* `destroyStore(id)`, the next factory call will create a fresh store.
|
|
15
|
-
*
|
|
16
|
-
* @param id - Store identifier
|
|
17
|
-
* @param definition - Store definition without id
|
|
18
|
-
* @returns A function that returns the store instance
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* const useCounter = defineStore('counter', {
|
|
23
|
-
* state: () => ({ count: 0 }),
|
|
24
|
-
* actions: { increment() { this.count++; } },
|
|
25
|
-
* });
|
|
26
|
-
*
|
|
27
|
-
* const counter = useCounter();
|
|
28
|
-
* counter.increment();
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
export const defineStore = <
|
|
32
|
-
S extends Record<string, unknown>,
|
|
33
|
-
G extends Record<string, unknown> = Record<string, never>,
|
|
34
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- actions may declare specific parameter types
|
|
35
|
-
A extends Record<string, (...args: any[]) => any> = Record<string, never>,
|
|
36
|
-
>(
|
|
37
|
-
id: string,
|
|
38
|
-
definition: Omit<StoreDefinition<S, G, A>, 'id'>
|
|
39
|
-
): (() => Store<S, G, A>) => {
|
|
40
|
-
// Check registry first to avoid noisy warnings from createStore()
|
|
41
|
-
// when the factory is called multiple times (intended usage pattern).
|
|
42
|
-
// createStore() only called when store doesn't exist or was destroyed.
|
|
43
|
-
return () => {
|
|
44
|
-
if (hasStore(id)) {
|
|
45
|
-
return getStore(id) as Store<S, G, A>;
|
|
46
|
-
}
|
|
47
|
-
return createStore({ id, ...definition });
|
|
48
|
-
};
|
|
49
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Store factory helpers.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { createStore } from './create-store';
|
|
6
|
+
import { getStore, hasStore } from './registry';
|
|
7
|
+
import type { Store, StoreDefinition } from './types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Creates a store factory that returns the store instance.
|
|
11
|
+
*
|
|
12
|
+
* The store is lazily created on first call and cached in the global store
|
|
13
|
+
* registry. Subsequent calls return the same instance. After calling
|
|
14
|
+
* `destroyStore(id)`, the next factory call will create a fresh store.
|
|
15
|
+
*
|
|
16
|
+
* @param id - Store identifier
|
|
17
|
+
* @param definition - Store definition without id
|
|
18
|
+
* @returns A function that returns the store instance
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const useCounter = defineStore('counter', {
|
|
23
|
+
* state: () => ({ count: 0 }),
|
|
24
|
+
* actions: { increment() { this.count++; } },
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* const counter = useCounter();
|
|
28
|
+
* counter.increment();
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export const defineStore = <
|
|
32
|
+
S extends Record<string, unknown>,
|
|
33
|
+
G extends Record<string, unknown> = Record<string, never>,
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- actions may declare specific parameter types
|
|
35
|
+
A extends Record<string, (...args: any[]) => any> = Record<string, never>,
|
|
36
|
+
>(
|
|
37
|
+
id: string,
|
|
38
|
+
definition: Omit<StoreDefinition<S, G, A>, 'id'>
|
|
39
|
+
): (() => Store<S, G, A>) => {
|
|
40
|
+
// Check registry first to avoid noisy warnings from createStore()
|
|
41
|
+
// when the factory is called multiple times (intended usage pattern).
|
|
42
|
+
// createStore() only called when store doesn't exist or was destroyed.
|
|
43
|
+
return () => {
|
|
44
|
+
if (hasStore(id)) {
|
|
45
|
+
return getStore(id) as Store<S, G, A>;
|
|
46
|
+
}
|
|
47
|
+
return createStore({ id, ...definition });
|
|
48
|
+
};
|
|
49
|
+
};
|
package/src/store/index.ts
CHANGED
|
@@ -3,13 +3,18 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
export type {
|
|
6
|
+
ActionContext,
|
|
6
7
|
Actions,
|
|
7
8
|
Getters,
|
|
9
|
+
OnActionCallback,
|
|
10
|
+
PersistedStoreOptions,
|
|
8
11
|
StateFactory,
|
|
12
|
+
StorageBackend,
|
|
9
13
|
Store,
|
|
10
14
|
StoreDefinition,
|
|
11
15
|
StorePatch,
|
|
12
16
|
StorePlugin,
|
|
17
|
+
StoreSerializer,
|
|
13
18
|
StoreSubscriber,
|
|
14
19
|
} from './types';
|
|
15
20
|
|