@deijose/nix-js 2.5.0 → 2.5.2
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 +2 -2
- package/dist/lib/async.cjs +1 -1
- package/dist/lib/async.js +3 -3
- package/dist/lib/component.cjs +1 -1
- package/dist/lib/component.js +1 -1
- package/dist/lib/form.cjs +1 -1
- package/dist/lib/form.js +1 -1
- package/dist/lib/nix/async.d.ts +9 -5
- package/dist/lib/nix/plugins.d.ts +1 -5
- package/dist/lib/nix/reactivity.d.ts +1 -2
- package/dist/lib/nix/router.d.ts +4 -2
- package/dist/lib/nix/store.d.ts +5 -0
- package/dist/lib/plugins.cjs +1 -1
- package/dist/lib/plugins.js +1 -1
- package/dist/lib/router.cjs +5 -5
- package/dist/lib/router.js +5 -5
- package/dist/lib/signals.cjs +1 -1
- package/dist/lib/signals.js +1 -1
- package/dist/lib/store.cjs +1 -1
- package/dist/lib/store.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
5
|
[]()
|
|
6
6
|
[]()
|
|
7
|
-
[]()
|
|
8
8
|
[]()
|
|
9
9
|
[](https://nix-js.dev/)
|
|
10
10
|
|
|
@@ -13,7 +13,7 @@ A lightweight, fully reactive micro-framework for building modern web UIs — no
|
|
|
13
13
|
**[→ Documentation & Live Demo](https://nix-js.dev/)**
|
|
14
14
|
|
|
15
15
|
```
|
|
16
|
-
~24 KB minified · ~
|
|
16
|
+
~24 KB minified · ~12 KB gzipped · zero dependencies · TypeScript-first · ES2022
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## Installation
|
package/dist/lib/async.cjs
CHANGED
|
@@ -12,4 +12,4 @@ Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=requi
|
|
|
12
12
|
<span style="color:#f87171;font-size:13px">
|
|
13
13
|
⚠ ${e instanceof Error?e.message:String(e)}
|
|
14
14
|
</span>
|
|
15
|
-
`}var a=new Map,o=3e5,s=null,c=o;function l(){null===s&&(s=setInterval(()=>{let e=Date.now();for(let[t,s]of a)s.subscribers<=0&&e-s.fetchedAt>c&&a.delete(t);0===a.size&&null!==s&&(clearInterval(s),s=null)},6e4))}function u(e){let t=a.get(e);if(t&&t.fetchedAt>0)return t}function d(e,t){let s=a.get(e);a.set(e,{data:t,fetchedAt:Date.now(),subscribers:s?.subscribers??0}),l()}function f(e){let t=a.get(e);t?t.subscribers++:a.set(e,{fetchedAt:0,subscribers:1})}function p(e){let t=a.get(e);t&&(t.subscribers=Math.max(0,t.subscribers-1))}function m(e,t){let s=a.get(e);return!!s&&Date.now()-s.fetchedAt<t}function h(s,l,o={}){let{fallback:c,errorFallback:h,resetOnRefresh:b=!1,invalidate:g,cacheKey:v,staleTime:x=0}=o,
|
|
15
|
+
`}var a=new Map,o=3e5,s=null,c=o;function l(){null===s&&(s=setInterval(()=>{let e=Date.now();for(let[t,s]of a)s.subscribers<=0&&e-s.fetchedAt>c&&a.delete(t);0===a.size&&null!==s&&(clearInterval(s),s=null)},6e4))}function u(e){let t=a.get(e);if(t&&t.fetchedAt>0)return t}function d(e,t){let s=a.get(e);a.set(e,{data:t,fetchedAt:Date.now(),subscribers:s?.subscribers??0}),l()}function f(e){let t=a.get(e);t?t.subscribers++:a.set(e,{fetchedAt:0,subscribers:1})}function p(e){let t=a.get(e);t&&(t.subscribers=Math.max(0,t.subscribers-1))}function m(e,t){let s=a.get(e);return!!s&&Date.now()-s.fetchedAt<t}function h(s,l,o={}){let{fallback:c,errorFallback:h,resetOnRefresh:b=!1,invalidate:g,cacheKey:v,staleTime:x=0}=o,_=c??r(),y=h??i;class w extends n.NixComponent{_state;_disposeWatcher;constructor(){super();let t=v?u(v):void 0;this._state=e.signal(t&&void 0!==t.data?{status:"resolved",data:t.data}:{status:"pending"})}onMount(){v&&f(v);let t=v?u(v):void 0;if(t&&m(v,x)||(t?this._fetch():this._run()),g){let t=!0;this._disposeWatcher=e.effect(()=>{g.value,t?t=!1:(v&&a.delete(v),this._run())})}return()=>{this._disposeWatcher?.(),v&&p(v)}}_run(){(b||"pending"===this._state.peek().status)&&(this._state.value={status:"pending"}),this._fetch()}_fetch(){s().then(e=>{v&&d(v,e),this._state.value={status:"resolved",data:e}},e=>{this._state.value={status:"error",error:e}})}render(){return t.l`<div class="nix-suspense" style="display:contents">${()=>{let e=this._state.value;return"pending"===e.status?_:"error"===e.status?y(e.error):l(e.data)}}</div>`}}return new w}function g(e,s){let n=void 0===s||t._(s)?{fallback:s}:s,a=n.selector??(e=>e.default),r=null;return()=>r?new r:h(async()=>(r=a(await e()),r),e=>new e,{fallback:n.fallback})}exports.lazy=g,exports.suspend=h;
|
package/dist/lib/async.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{effect as e,signal as t}from"./signals.js";import{l as
|
|
1
|
+
import{effect as e,signal as t}from"./signals.js";import{_ as n,l as r}from"./template2.js";import{NixComponent as i}from"./lifecycle.js";function a(){return r`
|
|
2
2
|
<span style="color:#52525b;font-size:13px;display:inline-flex;align-items:center;gap:6px">
|
|
3
3
|
<span class="nix-spinner" style="
|
|
4
4
|
display:inline-block;width:14px;height:14px;border-radius:50%;
|
|
@@ -8,8 +8,8 @@ import{effect as e,signal as t}from"./signals.js";import{l as n}from"./template2
|
|
|
8
8
|
Loading…
|
|
9
9
|
</span>
|
|
10
10
|
<style>@keyframes nix-spin{to{transform:rotate(360deg)}}</style>
|
|
11
|
-
`}function
|
|
11
|
+
`}function o(e){return r`
|
|
12
12
|
<span style="color:#f87171;font-size:13px">
|
|
13
13
|
⚠ ${e instanceof Error?e.message:String(e)}
|
|
14
14
|
</span>
|
|
15
|
-
`}var
|
|
15
|
+
`}var s=new Map,c=3e5,l=null,u=c;function d(){null===l&&(l=setInterval(()=>{let e=Date.now();for(let[t,n]of s)n.subscribers<=0&&e-n.fetchedAt>u&&s.delete(t);0===s.size&&null!==l&&(clearInterval(l),l=null)},6e4))}function f(e){let t=s.get(e);if(t&&t.fetchedAt>0)return t}function p(e,t){let n=s.get(e);s.set(e,{data:t,fetchedAt:Date.now(),subscribers:n?.subscribers??0}),d()}function m(e){let t=s.get(e);t?t.subscribers++:s.set(e,{fetchedAt:0,subscribers:1})}function h(e){let t=s.get(e);t&&(t.subscribers=Math.max(0,t.subscribers-1))}function g(e,t){let n=s.get(e);return!!n&&Date.now()-n.fetchedAt<t}function _(n,l,u={}){let{fallback:c,errorFallback:d,resetOnRefresh:b=!1,invalidate:_,cacheKey:v,staleTime:x=0}=u,y=c??a(),w=d??o;return new class extends i{_state;_disposeWatcher;constructor(){super();let e=v?f(v):void 0;this._state=t(e&&void 0!==e.data?{status:"resolved",data:e.data}:{status:"pending"})}onMount(){v&&m(v);let t=v?f(v):void 0;if(t&&g(v,x)||(t?this._fetch():this._run()),_){let t=!0;this._disposeWatcher=e(()=>{_.value,t?t=!1:(v&&s.delete(v),this._run())})}return()=>{this._disposeWatcher?.(),v&&h(v)}}_run(){(b||"pending"===this._state.peek().status)&&(this._state.value={status:"pending"}),this._fetch()}_fetch(){n().then(e=>{v&&p(v,e),this._state.value={status:"resolved",data:e}},e=>{this._state.value={status:"error",error:e}})}render(){return r`<div class="nix-suspense" style="display:contents">${()=>{let e=this._state.value;return"pending"===e.status?y:"error"===e.status?w(e.error):l(e.data)}}</div>`}}}function v(e,t){let s=void 0===t||n(t)?{fallback:t}:t,a=s.selector??(e=>e.default),r=null;return()=>r?new r:_(async()=>(r=a(await e()),r),e=>new e,{fallback:s.fallback})}export{v as lazy,_ as suspend};
|
package/dist/lib/component.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./lifecycle.cjs"),t=require("./context.cjs"),n=require("./router.cjs");function r(t){let o="string"==typeof t?document.querySelector(t):t;if(!o)throw Error(`[Nix] mount: container not found: ${t}`);return o}function i(o,u,i){if(e.isNixComponent(o)){let c
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./lifecycle.cjs"),t=require("./context.cjs"),n=require("./router.cjs");function r(t){let o="string"==typeof t?document.querySelector(t):t;if(!o)throw Error(`[Nix] mount: container not found: ${t}`);return o}function i(o,u,i){if(e.isNixComponent(o)){let c=r(u);e._debugComponentMountStart(o),t._pushComponentContext();let p,l=()=>{},f=!1;try{i?.router&&t.provide(n.RouterKey,i.router);try{o.onInit?.()}catch(t){if(!o.onError)throw t;o.onError(t)}try{l=o.render()._render(c,null)}catch(t){if(!o.onError)throw t;o.onError(t),l=()=>{},f=!0}}finally{e._debugComponentMountEnd(o),t._popComponentContext()}if(!f)try{let t=o.onMount?.();"function"==typeof t&&(p=t)}catch(t){if(!o.onError)throw t;o.onError(t)}return{unmount(){try{o.onUnmount?.()}catch{}try{p?.()}catch{}l(),e._debugComponentUnmount(o)}}}if(!i?.router)return o.mount(u);let c,p=r(u);t._pushComponentContext();try{t.provide(n.RouterKey,i.router),c=o._render(p,null)}finally{t._popComponentContext()}return{unmount(){c()}}}exports.mount=i;
|
package/dist/lib/component.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{_debugComponentMountEnd as e,_debugComponentMountStart as t,_debugComponentUnmount as n,isNixComponent as r}from"./lifecycle.js";import{_popComponentContext as i,_pushComponentContext as a,provide as o}from"./context.js";import{RouterKey as s}from"./router.js";function c(o){let
|
|
1
|
+
import{_debugComponentMountEnd as e,_debugComponentMountStart as t,_debugComponentUnmount as n,isNixComponent as r}from"./lifecycle.js";import{_popComponentContext as i,_pushComponentContext as a,provide as o}from"./context.js";import{RouterKey as s}from"./router.js";function c(o){let r="string"==typeof o?document.querySelector(o):o;if(!r)throw Error(`[Nix] mount: container not found: ${o}`);return r}function l(u,m,f){if(r(u)){let r=c(m);t(u),a();let l,p=()=>{},y=!1;try{f?.router&&o(s,f.router);try{u.onInit?.()}catch(o){if(!u.onError)throw o;u.onError(o)}try{p=u.render()._render(r,null)}catch(o){if(!u.onError)throw o;u.onError(o),p=()=>{},y=!0}}finally{e(u),i()}if(!y)try{let o=u.onMount?.();"function"==typeof o&&(l=o)}catch(o){if(!u.onError)throw o;u.onError(o)}return{unmount(){try{u.onUnmount?.()}catch{}try{l?.()}catch{}p(),n(u)}}}if(!f?.router)return u.mount(m);let l,p=c(m);a();try{o(s,f.router),l=u._render(p,null)}finally{i()}return{unmount(){l()}}}export{l as mount};
|
package/dist/lib/form.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs");function t(e="Required"){return t=>null==t||""===t||Array.isArray(t)&&0===t.length?e:null}function n(e,t){return r=>"string"==typeof r&&r.length<e?t??`Minimum ${e} characters`:null}function r(e,t){return r=>"string"==typeof r&&r.length>e?t??`Maximum ${e} characters`:null}function i(e,t="Invalid format"){return r=>"string"!=typeof r||e.test(r)?null:t}function a(e="Invalid email"){return i(/^[^\s@]+@[^\s@]+\.[^\s@]+$/,e)}function o(e,t){return r=>"number"==typeof r&&r<e?t??`Minimum value is ${e}`:null}function s(e,t){return r=>"number"==typeof r&&r>e?t??`Maximum value is ${e}`:null}function c(e){return e}var l={required:t,minLength:n,maxLength:r,email:a,pattern:i,min:o,max:s};function u(e,t){return{...e,...t}}function d(t,r=[],n="blur",l){let u=e.signal(t),i=e.signal(!1),o=e.signal(!1),a=e.signal(null),s=e.signal(!1),f=!0,c=e.effect(()=>{u.value,f?f=!1:null!==a.peek()&&(a.value=null)}),
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs");function t(e="Required"){return t=>null==t||""===t||Array.isArray(t)&&0===t.length?e:null}function n(e,t){return r=>"string"==typeof r&&r.length<e?t??`Minimum ${e} characters`:null}function r(e,t){return r=>"string"==typeof r&&r.length>e?t??`Maximum ${e} characters`:null}function i(e,t="Invalid format"){return r=>"string"!=typeof r||e.test(r)?null:t}function a(e="Invalid email"){return i(/^[^\s@]+@[^\s@]+\.[^\s@]+$/,e)}function o(e,t){return r=>"number"==typeof r&&r<e?t??`Minimum value is ${e}`:null}function s(e,t){return r=>"number"==typeof r&&r>e?t??`Maximum value is ${e}`:null}function c(e){return e}var l={required:t,minLength:n,maxLength:r,email:a,pattern:i,min:o,max:s};function u(e,t){return{...e,...t}}function d(t,r=[],n="blur",l){let u=e.signal(t),i=e.signal(!1),o=e.signal(!1),a=e.signal(null),s=e.signal(!1),f=!0,c=e.effect(()=>{u.value,f?f=!1:null!==a.peek()&&(a.value=null)}),p=e.computed(()=>{if(a.value)return a.value;let e=l?.();for(let t of r){let r=t(u.value,e);if(r)return r}return null}),v=e.computed(()=>("input"===n?o.value||i.value:"submit"===n?s.value:i.value)?p.value:null);return{value:u,error:v,rawError:p,touched:i,dirty:o,onInput:e=>{u.value=function(e){if(!e||!("value"in e))return t;let r=e,n=r.value;return"boolean"==typeof t?!0===r.checked||!function(e){return"checkbox"===e.type||"radio"===e.type}(r)&&("true"===n||"1"===n||"false"!==n&&"0"!==n&&""!==n&&t):"number"==typeof t?""===n?NaN:Number(n):n}(e.target),o.value=!0},onBlur:()=>{i.value=!0},reset:function(){e.batch(()=>{u.value=t,i.value=!1,o.value=!1,a.value=null,s.value=!1})},_setExternalError:function(e){a.value=e,e&&(i.value=!0)},_forceVisible:function(){i.value=!0,s.value=!0},_dispose:function(){c(),v.dispose(),p.dispose()}}}function f(t,r={},n="blur"){function l(e){let t={};for(let l in e){let u=r[l]??[];t[l]=d(e[l],u,n)}return t}let u=e.signal(t.map(l)),i=e.computed(()=>u.value.length);return{fields:u,append:function(e){u.value=[...u.value,l(e)]},remove:function(e){let t=u.value;if(!(e<0||e>=t.length)){for(let r in t[e])t[e][r]._dispose();u.value=t.filter((t,r)=>r!==e)}},move:function(e,t){let r=[...u.value];if(e<0||e>=r.length||t<0||t>=r.length||e===t)return;let[n]=r.splice(e,1);r.splice(t,0,n),u.value=r},replace:function(e,t){let r=[...u.value];if(!(e<0||e>=r.length)){for(let t in r[e])r[e][t]._dispose();r[e]=l(t),u.value=r}},length:i,reset:function(){for(let e of u.value)for(let t in e)e[t]._dispose();u.value=t.map(l)},_dispose:function(){for(let e of u.value)for(let t in e)e[t]._dispose();i.dispose()}}}function p(e){return"object"==typeof e&&!!e&&!Array.isArray(e)}function m(e,t="",r=[]){for(let[n,l]of Object.entries(e)){let e=t?`${t}.${n}`:n;p(l)&&Object.keys(l).length>0?m(l,e,r):r.push([e,l])}return r}function h(e,t,r){let n=t.split("."),l=e;for(let e=0;e<n.length-1;e++){let t=n[e];p(l[t])||(l[t]={}),l=l[t]}l[n[n.length-1]]=r}function g(t,r={}){let n=r.validateOn??"blur",l={},u=r.validators;function i(){let e={};for(let t in l)h(e,t,l[t].value.value);return e}for(let[e,r]of m(t))l[e]=d(r,u?.[e]??[],n,i);let o=e.signal(!1),a=e.signal(0),s=e.computed(()=>i()),f=e.computed(()=>{let e={};for(let t in l){let r=l[t].error.value;r&&(e[t]=r)}return e}),c=e.computed(()=>{for(let e in l)if(l[e].rawError.value)return!1;return!0}),p=e.computed(()=>{for(let e in l)if(l[e].error.value)return!1;return!0}),v=e.computed(()=>{for(let e in l)if(l[e].dirty.value)return!0;return!1}),g=e.computed(()=>{for(let e in l)if(l[e].touched.value)return!0;return!1});function x(e){for(let t in e)l[t]?._setExternalError(e[t]??null)}return{fields:l,values:s,errors:f,canSubmit:c,valid:p,dirty:v,touched:g,isSubmitting:o,submitCount:a,handleSubmit:function(e){return t=>{t.preventDefault(),a.value++;for(let e in l)l[e]._forceVisible();let n=s.value;if(r.validate){let e=r.validate(n);if(e){let t={},r=!1;for(let n in e){let l=e[n],u=Array.isArray(l)?l[0]??null:l??null;u&&(t[n]=u,r=!0)}if(r)return void x(t)}}for(let e in l)if(l[e].rawError.value)return;let u=e(n);u instanceof Promise&&(o.value=!0,u.finally(()=>{o.value=!1}).catch(()=>{}))}},reset:function(){for(let e in l)l[e].reset();o.value=!1,a.value=0},setErrors:x,dispose:function(){s.dispose(),f.dispose(),c.dispose(),p.dispose(),v.dispose(),g.dispose();for(let e in l)l[e]._dispose()}}}exports.createForm=g,exports.createValidator=c,exports.email=a,exports.extendValidators=u,exports.max=s,exports.maxLength=r,exports.min=o,exports.minLength=n,exports.nixField=d,exports.nixFieldArray=f,exports.pattern=i,exports.required=t,exports.validators=l;
|
package/dist/lib/form.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{batch as e,computed as t,effect as n,signal as r}from"./signals.js";function i(e="Required"){return t=>null==t||""===t||Array.isArray(t)&&0===t.length?e:null}function a(e,t){return r=>"string"==typeof r&&r.length<e?t??`Minimum ${e} characters`:null}function o(e,t){return r=>"string"==typeof r&&r.length>e?t??`Maximum ${e} characters`:null}function s(e,t="Invalid format"){return r=>"string"!=typeof r||e.test(r)?null:t}function c(e="Invalid email"){return s(/^[^\s@]+@[^\s@]+\.[^\s@]+$/,e)}function l(e,t){return r=>"number"==typeof r&&r<e?t??`Minimum value is ${e}`:null}function u(e,t){return r=>"number"==typeof r&&r>e?t??`Maximum value is ${e}`:null}function d(e){return e}var f={required:i,minLength:a,maxLength:o,email:c,pattern:s,min:l,max:u};function p(e,t){return{...e,...t}}function m(l,u=[],i="blur",a){let o=r(l),s=r(!1),f=r(!1),v=r(null),c=r(!1),d=!0,p=n(()=>{o.value,d?d=!1:null!==v.peek()&&(v.value=null)}),m=t(()=>{if(v.value)return v.value;let e=a?.();for(let t of u){let r=t(o.value,e);if(r)return r}return null}),h=t(()=>("input"===i?f.value||s.value:"submit"===i?c.value:s.value)?m.value:null);return{value:o,error:h,rawError:m,touched:s,dirty:f,onInput:e=>{o.value=function(e){if(!e||!("value"in e))return l;let t=e;return"boolean"==typeof l
|
|
1
|
+
import{batch as e,computed as t,effect as n,signal as r}from"./signals.js";function i(e="Required"){return t=>null==t||""===t||Array.isArray(t)&&0===t.length?e:null}function a(e,t){return r=>"string"==typeof r&&r.length<e?t??`Minimum ${e} characters`:null}function o(e,t){return r=>"string"==typeof r&&r.length>e?t??`Maximum ${e} characters`:null}function s(e,t="Invalid format"){return r=>"string"!=typeof r||e.test(r)?null:t}function c(e="Invalid email"){return s(/^[^\s@]+@[^\s@]+\.[^\s@]+$/,e)}function l(e,t){return r=>"number"==typeof r&&r<e?t??`Minimum value is ${e}`:null}function u(e,t){return r=>"number"==typeof r&&r>e?t??`Maximum value is ${e}`:null}function d(e){return e}var f={required:i,minLength:a,maxLength:o,email:c,pattern:s,min:l,max:u};function p(e,t){return{...e,...t}}function m(l,u=[],i="blur",a){let o=r(l),s=r(!1),f=r(!1),v=r(null),c=r(!1),d=!0,p=n(()=>{o.value,d?d=!1:null!==v.peek()&&(v.value=null)}),m=t(()=>{if(v.value)return v.value;let e=a?.();for(let t of u){let r=t(o.value,e);if(r)return r}return null}),h=t(()=>("input"===i?f.value||s.value:"submit"===i?c.value:s.value)?m.value:null);return{value:o,error:h,rawError:m,touched:s,dirty:f,onInput:e=>{o.value=function(e){if(!e||!("value"in e))return l;let t=e,r=t.value;return"boolean"==typeof l?!0===t.checked||!function(e){return"checkbox"===e.type||"radio"===e.type}(t)&&("true"===r||"1"===r||"false"!==r&&"0"!==r&&""!==r&&l):"number"==typeof l?""===r?NaN:Number(r):r}(e.target),f.value=!0},onBlur:()=>{s.value=!0},reset:function(){e(()=>{o.value=l,s.value=!1,f.value=!1,v.value=null,c.value=!1})},_setExternalError:function(e){v.value=e,e&&(s.value=!0)},_forceVisible:function(){s.value=!0,c.value=!0},_dispose:function(){p(),h.dispose(),m.dispose()}}}function h(e,n={},l="blur"){function u(e){let t={};for(let r in e){let u=n[r]??[];t[r]=m(e[r],u,l)}return t}let i=r(e.map(u)),a=t(()=>i.value.length);return{fields:i,append:function(e){i.value=[...i.value,u(e)]},remove:function(e){let t=i.value;if(!(e<0||e>=t.length)){for(let r in t[e])t[e][r]._dispose();i.value=t.filter((t,r)=>r!==e)}},move:function(e,t){let r=[...i.value];if(e<0||e>=r.length||t<0||t>=r.length||e===t)return;let[n]=r.splice(e,1);r.splice(t,0,n),i.value=r},replace:function(e,t){let r=[...i.value];if(!(e<0||e>=r.length)){for(let t in r[e])r[e][t]._dispose();r[e]=u(t),i.value=r}},length:a,reset:function(){for(let e of i.value)for(let t in e)e[t]._dispose();i.value=e.map(u)},_dispose:function(){for(let e of i.value)for(let t in e)e[t]._dispose();a.dispose()}}}function g(e){return"object"==typeof e&&!!e&&!Array.isArray(e)}function _(e,t="",r=[]){for(let[n,l]of Object.entries(e)){let e=t?`${t}.${n}`:n;g(l)&&Object.keys(l).length>0?_(l,e,r):r.push([e,l])}return r}function v(e,t,r){let n=t.split("."),l=e;for(let e=0;e<n.length-1;e++){let t=n[e];g(l[t])||(l[t]={}),l=l[t]}l[n[n.length-1]]=r}function y(e,n={}){let l=n.validateOn??"blur",u={},i=n.validators;function a(){let e={};for(let t in u)v(e,t,u[t].value.value);return e}for(let[t,r]of _(e))u[t]=m(r,i?.[t]??[],l,a);let o=r(!1),s=r(0),f=t(()=>a()),c=t(()=>{let e={};for(let t in u){let r=u[t].error.value;r&&(e[t]=r)}return e}),d=t(()=>{for(let e in u)if(u[e].rawError.value)return!1;return!0}),p=t(()=>{for(let e in u)if(u[e].error.value)return!1;return!0}),h=t(()=>{for(let e in u)if(u[e].dirty.value)return!0;return!1}),g=t(()=>{for(let e in u)if(u[e].touched.value)return!0;return!1});function y(e){for(let t in e)u[t]?._setExternalError(e[t]??null)}return{fields:u,values:f,errors:c,canSubmit:d,valid:p,dirty:h,touched:g,isSubmitting:o,submitCount:s,handleSubmit:function(e){return t=>{t.preventDefault(),s.value++;for(let e in u)u[e]._forceVisible();let r=f.value;if(n.validate){let e=n.validate(r);if(e){let t={},r=!1;for(let n in e){let l=e[n],u=Array.isArray(l)?l[0]??null:l??null;u&&(t[n]=u,r=!0)}if(r)return void y(t)}}for(let e in u)if(u[e].rawError.value)return;let l=e(r);l instanceof Promise&&(o.value=!0,l.finally(()=>{o.value=!1}).catch(()=>{}))}},reset:function(){for(let e in u)u[e].reset();o.value=!1,s.value=0},setErrors:y,dispose:function(){f.dispose(),c.dispose(),d.dispose(),p.dispose(),h.dispose(),g.dispose();for(let e in u)u[e]._dispose()}}}export{y as createForm,d as createValidator,c as email,p as extendValidators,u as max,o as maxLength,l as min,a as minLength,m as nixField,h as nixFieldArray,s as pattern,i as required,f as validators};
|
package/dist/lib/nix/async.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Signal } from "./reactivity";
|
|
2
2
|
import { NixComponent } from "./lifecycle";
|
|
3
|
-
import type
|
|
3
|
+
import { type NixTemplate } from "./template";
|
|
4
4
|
export interface SuspenseOptions {
|
|
5
5
|
fallback?: NixTemplate;
|
|
6
6
|
errorFallback?: (err: unknown) => NixTemplate;
|
|
@@ -19,10 +19,14 @@ export interface SuspenseOptions {
|
|
|
19
19
|
* ```
|
|
20
20
|
*/
|
|
21
21
|
export declare function suspend<T>(asyncFn: () => Promise<T>, renderFn: (data: T) => NixTemplate | NixComponent, options?: SuspenseOptions): NixComponent;
|
|
22
|
+
export interface LazyOptions {
|
|
23
|
+
/** Component selector when the module uses a named export. Defaults to `mod.default`. */
|
|
24
|
+
selector?: (mod: Record<string, unknown>) => new () => NixComponent;
|
|
25
|
+
/** Template shown while the component is loading. */
|
|
26
|
+
fallback?: NixTemplate;
|
|
27
|
+
}
|
|
22
28
|
/**
|
|
23
29
|
* Wraps a dynamic import for lazy-loading route components.
|
|
24
|
-
* The module is loaded once and cached.
|
|
30
|
+
* The module is loaded once and cached. Supports both default and named exports.
|
|
25
31
|
*/
|
|
26
|
-
export declare function lazy(importFn: () => Promise<
|
|
27
|
-
default: new () => NixComponent;
|
|
28
|
-
}>, fallback?: NixTemplate): () => NixComponent;
|
|
32
|
+
export declare function lazy(importFn: () => Promise<Record<string, unknown>>, options?: NixTemplate | LazyOptions): () => NixComponent;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type NixPlugin, type Store } from "./store";
|
|
1
|
+
import { type NixPlugin, type Store, type GuardFn } from "./store";
|
|
2
2
|
/**
|
|
3
3
|
* Minimal interface that any storage adapter must implement.
|
|
4
4
|
* Compatible with localStorage, sessionStorage, AsyncStorage, IndexedDB, etc.
|
|
@@ -37,10 +37,6 @@ export declare function loggerPlugin<T extends Record<string, unknown>>(opts?: {
|
|
|
37
37
|
/** Optional filter to skip logging for specific changes. */
|
|
38
38
|
filter?: (diff: Partial<T>) => boolean;
|
|
39
39
|
}): NixPlugin<T>;
|
|
40
|
-
/**
|
|
41
|
-
* Function type for mutation guards.
|
|
42
|
-
*/
|
|
43
|
-
type GuardFn<T extends Record<string, unknown>> = (next: Partial<T>, current: T) => Partial<T> | void;
|
|
44
40
|
/**
|
|
45
41
|
* Intercepts $patch and $reset calls to validate or transform state before it is applied.
|
|
46
42
|
*/
|
|
@@ -38,8 +38,7 @@ export declare function signal<T>(initialValue: T): Signal<T>;
|
|
|
38
38
|
* and on disposal.
|
|
39
39
|
*/
|
|
40
40
|
export declare function effect(fn: () => void | (() => void)): () => void;
|
|
41
|
-
|
|
42
|
-
export declare function computed<T>(fn: () => T): Signal<T> & {
|
|
41
|
+
export declare function computed<T>(fn: () => T, equals?: (a: T, b: T) => boolean): Signal<T> & {
|
|
43
42
|
dispose(): void;
|
|
44
43
|
};
|
|
45
44
|
/** Groups multiple signal writes so effects flush once at the end. */
|
package/dist/lib/nix/router.d.ts
CHANGED
|
@@ -106,13 +106,15 @@ export declare function nixRouter(): Router;
|
|
|
106
106
|
export declare function _resetRouter(): void;
|
|
107
107
|
export declare class RouterView extends NixComponent {
|
|
108
108
|
private _depth;
|
|
109
|
-
|
|
109
|
+
private _router?;
|
|
110
|
+
constructor(depth?: number, router?: Router);
|
|
110
111
|
render(): NixTemplate;
|
|
111
112
|
}
|
|
112
113
|
export declare class Link extends NixComponent {
|
|
113
114
|
private _to;
|
|
114
115
|
private _label;
|
|
115
|
-
|
|
116
|
+
private _router?;
|
|
117
|
+
constructor(to: string, label: string, router?: Router);
|
|
116
118
|
render(): NixTemplate;
|
|
117
119
|
}
|
|
118
120
|
export interface _RouterDebugInternal {
|
package/dist/lib/nix/store.d.ts
CHANGED
|
@@ -71,6 +71,11 @@ export type GettersFactory<T extends object, G extends Record<string, Signal<unk
|
|
|
71
71
|
* computed(() => store.someSignal.value) — derive new nodes
|
|
72
72
|
* store.$snapshot() — passive read for logging/persistence
|
|
73
73
|
*/
|
|
74
|
+
/**
|
|
75
|
+
* Function type for mutation guards.
|
|
76
|
+
* Intercepts $patch and $reset to validate or transform state before it is applied.
|
|
77
|
+
*/
|
|
78
|
+
export type GuardFn<T extends object> = (next: Partial<T>, current: T) => Partial<T> | void;
|
|
74
79
|
export type NixPlugin<T extends object, A extends object = Record<never, never>, G extends Record<string, Signal<unknown>> = Record<never, never>> = (store: Store<T, A, G>) => (() => void) | void;
|
|
75
80
|
/**
|
|
76
81
|
* Options object for createStore. NoInfer<T> on factory parameters ensures
|
package/dist/lib/plugins.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs");function t(t,r={}){let{storage:n=localStorage,exclude:o=[],serialize:l=JSON.stringify,deserialize:i=JSON.parse,debounce:
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs");function t(t,r={}){let{storage:n=localStorage,exclude:o=[],serialize:l=JSON.stringify,deserialize:i=JSON.parse,debounce:s=0}=r;return r=>{let c;return e.untrack(async()=>{try{let e=await n.getItem(t);if(!e)return;let l=i(e),s={};for(let e of Object.keys(l))e in r.$state&&!o.includes(e)&&(s[e]=l[e]);Object.keys(s).length>0&&r.$patch(s)}catch{}}),e.watch(r.$stateSignal,e=>{let r=()=>{try{let r=0===o.length?e:Object.fromEntries(Object.entries(e).filter(([e])=>!o.includes(e)));n.setItem(t,l(r))}catch{}};s>0?(clearTimeout(c),c=setTimeout(r,s)):r()})}}function n(t={}){let{collapsed:r=!0,filter:n}=t;return t=>{let o=r?console.groupCollapsed:console.group;return e.watch(t.$stateSignal,(e,r)=>{if(!r)return;let l={};for(let t of Object.keys(e))Object.is(e[t],r[t])||(l[t]=e[t]);0!==Object.keys(l).length&&(n&&!n(l)||(o(`%c[nix:${t.$id}]%c ${Object.keys(l).join(", ")}`,"color:#7F77DD;font-weight:500","color:inherit;font-weight:400"),console.log("prev →",r),console.log("next →",e),console.log("diff →",l),console.groupEnd()))},{immediate:!0})}}function r(e){return t=>{let r=t._guardFns;for(let t of e)r.push(t);return()=>{for(let t of e){let e=r.indexOf(t);e>=0&&r.splice(e,1)}}}}function i(t,r){return n=>e.watch(t.$stateSignal,e=>{r(e,n)})}exports.bridgePlugin=i,exports.guardPlugin=r,exports.loggerPlugin=n,exports.persistPlugin=t;
|
package/dist/lib/plugins.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{untrack as e,watch as t}from"./signals.js";function n(n,r={}){let{storage:l=localStorage,exclude:o=[],serialize:i=JSON.stringify,deserialize:s=JSON.parse,debounce:c=0}=r;return r=>{let a;return e(async()=>{try{let e=await l.getItem(n);if(!e)return;let t=s(e),i={};for(let e of Object.keys(t))e in r.$state&&!o.includes(e)&&(i[e]=t[e]);Object.keys(i).length>0&&r.$patch(i)}catch{}}),t(r.$stateSignal,e=>{let t=()=>{try{let t=0===o.length?e:Object.fromEntries(Object.entries(e).filter(([e])=>!o.includes(e)));l.setItem(n,i(t))}catch{}};c>0?(clearTimeout(a),a=setTimeout(t,c)):t()})}}function r(e={}){let{collapsed:n=!0,filter:r}=e;return e=>{let l=n?console.groupCollapsed:console.group;return t(e.$stateSignal,(t,n)=>{if(!n)return;let o={};for(let e of Object.keys(t))Object.is(t[e],n[e])||(o[e]=t[e]);0!==Object.keys(o).length&&(r&&!r(o)||(l(`%c[nix:${e.$id}]%c ${Object.keys(o).join(", ")}`,"color:#7F77DD;font-weight:500","color:inherit;font-weight:400"),console.log("prev →",n),console.log("next →",t),console.log("diff →",o),console.groupEnd()))},{immediate:!0})}}function i(
|
|
1
|
+
import{untrack as e,watch as t}from"./signals.js";function n(n,r={}){let{storage:l=localStorage,exclude:o=[],serialize:i=JSON.stringify,deserialize:s=JSON.parse,debounce:c=0}=r;return r=>{let a;return e(async()=>{try{let e=await l.getItem(n);if(!e)return;let t=s(e),i={};for(let e of Object.keys(t))e in r.$state&&!o.includes(e)&&(i[e]=t[e]);Object.keys(i).length>0&&r.$patch(i)}catch{}}),t(r.$stateSignal,e=>{let t=()=>{try{let t=0===o.length?e:Object.fromEntries(Object.entries(e).filter(([e])=>!o.includes(e)));l.setItem(n,i(t))}catch{}};c>0?(clearTimeout(a),a=setTimeout(t,c)):t()})}}function r(e={}){let{collapsed:n=!0,filter:r}=e;return e=>{let l=n?console.groupCollapsed:console.group;return t(e.$stateSignal,(t,n)=>{if(!n)return;let o={};for(let e of Object.keys(t))Object.is(t[e],n[e])||(o[e]=t[e]);0!==Object.keys(o).length&&(r&&!r(o)||(l(`%c[nix:${e.$id}]%c ${Object.keys(o).join(", ")}`,"color:#7F77DD;font-weight:500","color:inherit;font-weight:400"),console.log("prev →",n),console.log("next →",t),console.log("diff →",o),console.groupEnd()))},{immediate:!0})}}function i(e){return t=>{let n=t._guardFns;for(let t of e)n.push(t);return()=>{for(let t of e){let e=n.indexOf(t);e>=0&&n.splice(e,1)}}}}function a(e,n){return r=>t(e.$stateSignal,e=>{n(e,r)})}export{a as bridgePlugin,i as guardPlugin,r as loggerPlugin,n as persistPlugin};
|
package/dist/lib/router.cjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs"),t=require("./template2.cjs"),n=require("./lifecycle.cjs"),r=require("./context.cjs");var i=r.createInjectionKey("nix:router"),a=null,o=null,s="__nix_scroll",c="__nix_pos";function l(){if(!a)throw Error("[Nix] No active router. Call createRouter() first, or instantiate an outlet that auto-bootstraps one (e.g. IonRouterOutlet).");return a}function u(){return null!==a}function d(){return{left:window.scrollX??window.pageXOffset??0,top:window.scrollY??window.pageYOffset??0}}function ee(e){if(!e||"object"!=typeof e)return null;let t=e[s];if(!t||"object"!=typeof t)return null;let r=t.left,n=t.top;return"number"!=typeof r||"number"!=typeof n?null:{left:r,top:n}}function f(e){if(!e||"object"!=typeof e)return null;let t=e[c];return"number"==typeof t?t:null}function p(e,t,r){let n=e&&"object"==typeof e?{...e}:{};return n[s]={left:t.left,top:t.top},n[c]=r,n}function m(e){let t={};return new URLSearchParams(e).forEach((e,r)=>{t[r]=e}),t}function h(e){let t=new URLSearchParams;for(let[r,n]of Object.entries(e))null!=n&&!1!==n&&t.set(r,String(n));let r=t.toString();return r?"?"+r:""}function g(e){return"*"===e?[{kind:"wildcard"}]:e.split("/").filter(Boolean).map(e=>"*"===e?{kind:"wildcard"}:e.startsWith(":")?{kind:"param",name:e.slice(1)}:{kind:"literal",value:e})}function _(e,t){return"*"===t?""===e?"*":e+"/*":(e+(t.startsWith("/")?t:"/"+t)).replace(/\/+/g,"/")||"/"}function v(e,t="",r=[]){let n=[];for(let o of e){let e=_(t,o.path),a=[...r,o.component],i=g(e);n.push({fullPath:e,segments:i,chain:a,name:o.name,meta:o.meta,beforeEnter:o.beforeEnter,record:o}),o.children?.length&&n.push(...v(o.children,e,a))}return n}function y(e,t){let r=e.split("/").filter(Boolean),n=t.segments;if(1===n.length&&"wildcard"===n[0].kind)return{};let o=n.length>0&&"wildcard"===n[n.length-1].kind,a=o?n.slice(0,-1):n;if(o){if(r.length<a.length)return null}else if(r.length!==a.length)return null;let i={};for(let e=0;e<a.length;e++){let t=a[e];if("literal"===t.kind){if(r[e]!==t.value)return null}else if("param"===t.kind)try{i[t.name]=decodeURIComponent(r[e]??"")}catch{i[t.name]=r[e]??""}}return i}function b(e){return e.segments.reduce((e,t)=>"literal"===t.kind?e+2:"param"===t.kind?e+1:e,0)}function x(e,t){let r,n={},o=-1;for(let a of t){let t=y(e,a);if(null===t)continue;let i=b(a);i>o&&(r=a,n=t,o=i)}return r?{route:r,params:n}:void 0}function S(e){let t=e.trim();return t&&"/"!==t?(t.startsWith("/")||(t="/"+t),t.endsWith("/")&&(t=t.slice(0,-1)),t):""}function te(){if(typeof document>"u")return"";let e=document.querySelector("base");if(!e)return"";let t=e.getAttribute("href")||"";try{return S(new URL(t,window.location.origin).pathname)}catch{return S(t)}}function C(e){return!1===e?{allow:!1}:!0===e||null==e?{allow:!0}:"string"==typeof e?{allow:!1,redirect:e}:"object"==typeof e&&"redirect"in e&&"string"==typeof e.redirect?{allow:!1,redirect:e.redirect}:{allow:!0}}function w(t,r){let n=null==r?.base?te():S(r.base),i=r?.mode??"history",l="hash"===i,u=r?.scrollBehavior,s=new Map,c=!1;function g(e){return e?e.startsWith("/")?e:"/"+e:"/"}function w(e){let t=g(e||"/");if(n&&t.startsWith(n)){let e=t.slice(n.length);return""===e?"/":g(e)}return t}function y(){return l?function(){let e=window.location.hash||"";if(e.startsWith("#")&&(e=e.slice(1)),!e)return{pathname:"/",search:""};e.startsWith("/")||(e="/"+e);let t=e.indexOf("?"),r=-1===t?e:e.slice(0,t),n=-1===t?"":e.slice(t);return{pathname:w(r),search:n}}():{pathname:w(window.location.pathname||"/"),search:window.location.search||""}}function b(e,t){let r=function(e){let t=g(e);return n?(n+t).replace(/\/+/g,"/")||"/":t}(e)+h(t);return l?"#"+r:r}function _(e,t){return g(e)+h(t)}let k=y(),E=k.pathname,R=m(k.search),j=v(t),q=new Map;for(let e of j)e.name&&(q.has(e.name)&&console.warn(`[Nix Router] Duplicate route name: "${e.name}"`),q.set(e.name,e));let O=x(E,j),W=e.signal(E),$=e.signal(O?.params??{}),N=e.signal(R),L=f(history.state)??0,P=e.signal({action:"initial",direction:"none"}),M=e.signal(L>0);function T(e){window.scrollTo(e.left,e.top)}function B(e,t,r){if(u){let n=u(e,t,r);if(!n)return;return void T(n)}T(r??{left:0,top:0})}l?s.set(_(E,R),d()):history.replaceState(p(history.state,d(),L),"");let I=[],U=[],A=0;function D(e,t,r,n,o){let a=[...I];r&&a.push(r);let i=++A;if(0===a.length)return void n();let l=0;!function r(u){if(i!==A)return;let s=C(u);if(!s.allow)return s.redirect&&s.redirect!==e?void z(s.redirect):s.redirect===e?void n():void o?.();if(l>=a.length)return void n();let c=a[l++](e,t);c instanceof Promise?c.then(r):r(c)}(void 0)}let G=!1;function Q(e,t){let r=e.indexOf("?"),n=g((-1===r?e:e.slice(0,r))||"/"),o=-1===r?{}:m(e.slice(r)),a=t?{...o,...t}:o,i={};for(let[e,t]of Object.entries(a))null!=t&&!1!==t&&(i[e]=String(t));return{pathname:n,stringQuery:i}}function K(e,t){return"string"==typeof e?Q(e,t?.query):Q(function(e){let t=q.get(e.name);if(!t)throw Error(`[Nix Router] No route with name "${e.name}"`);return"/"+t.segments.map(t=>{if("literal"===t.kind)return t.value;if("wildcard"===t.kind)return"";let r=e.params?.[t.name];if(null==r)throw Error(`[Nix Router] Missing param "${t.name}" for route "${e.name}"`);return encodeURIComponent(String(r))}).filter(Boolean).join("/")}(e),{...e.query??{},...t?.query??{}})}o&&=(o(),null);let X,Y=(e,t,r,n,o)=>{let a=W.value,i={...N.value},l=x(e,j),u="none";null!=n&&(n<L?u="back":n>L&&(u="forward")),D(e,a,l?.route.beforeEnter,()=>{null!=n&&(L=n);let o=X;X=void 0,P.value={action:"pop",direction:u,animation:o},$.value=l?.params??{},N.value=t,W.value=e,M.value=L>0,B(e,a,r);for(let t of U)try{t(e,a)}catch{}},()=>o(a,i))};if(l){let e=()=>{if(c)return void(c=!1);let e=y(),t=m(e.search),r=s.get(_(e.pathname,t))??null;Y(e.pathname,t,r,null,(e,t)=>{c=!0,window.location.hash=b(e,t).slice(1),queueMicrotask(()=>{c=!1})})};window.addEventListener("hashchange",e),o=()=>window.removeEventListener("hashchange",e)}else{let e=e=>{let t=y(),r=m(t.search),n=ee(e.state??history.state),o=f(e.state??history.state);Y(t.pathname,r,n,o,(e,t)=>{history.pushState(p({},d(),L),"",b(e,t))})};window.addEventListener("popstate",e),o=()=>window.removeEventListener("popstate",e)}function V(e,t,r,n,o,a,i){i||(function(e,t){let r=d();l?s.set(_(e,t),r):history.replaceState(p(history.state,r,L),"")}(r,n),L+=1),P.value=a,$.value=o?.params??{},N.value=t,W.value=e,M.value=L>0;let u=b(e,t);if(l)s.set(_(e,t),{left:0,top:0}),i?history.replaceState(history.state,"",u):(c=!0,window.location.hash=u.slice(1),queueMicrotask(()=>{c=!1}));else{let e=p({},{left:0,top:0},L);i?history.replaceState(e,"",u):history.pushState(e,"",u)}B(e,r,null);for(let t of U)try{t(e,r)}catch{}}function z(e,t){G=!0;let{pathname:r,stringQuery:n}=K(e,t),o=W.value,a={...N.value},i=x(r,j),l={action:"push",direction:t?.direction??"forward",animation:t?.animation};D(r,o,i?.route.beforeEnter,()=>V(r,n,o,a,i,l,!1))}let F={current:W,params:$,query:N,intent:P,canGoBack:M,base:n||"/",navigate:z,replace:function(e,t){G=!0;let{pathname:r,stringQuery:n}=K(e,t),o=W.value,a={...N.value},i=x(r,j),l={action:"replace",direction:t?.direction??"root",animation:t?.animation};D(r,o,i?.route.beforeEnter,()=>V(r,n,o,a,i,l,!0))},back:function(e){void 0!==e&&(X=e),history.back()},forward:function(e){void 0!==e&&(X=e),history.forward()},go:function(e){history.go(e)},isActive:function(e,t=!0){let r=W.value;return t?r===e:r===e||r.startsWith(e.endsWith("/")?e:e+"/")},resolve:function(e){let t=x(e,j);return t?{matched:!0,params:t.params,route:t.route.record}:{matched:!1,params:{},route:void 0}},beforeEach:function(e){return I.push(e),()=>{let t=I.indexOf(e);-1!==t&&I.splice(t,1)}},afterEach:function(e){return U.push(e),()=>{let t=U.indexOf(e);-1!==t&&U.splice(t,1)}},routes:t,_flat:j,_guards:I,_base:n,_mode:i};return a&&console.warn("[Nix] A router already exists. The previous router is being replaced. Only one router instance should be active at a time."),a=F,queueMicrotask(()=>{G||D(E,"",x(E,j)?.route.beforeEnter,()=>{},()=>{let e=b("/",{});l?(s.set(_("/",{}),{left:0,top:0}),history.replaceState(history.state,"",e)):history.replaceState(p({},{left:0,top:0},L),"",e);let t=x("/",j);P.value={action:"replace",direction:"root"},W.value="/",$.value=t?.params??{},N.value={},M.value=L>0,B("/",E,null)})}),F}function T(){return r.inject(i)||l()}function E(){o&&=(o(),null),a=null}var D=class extends n.NixComponent{_depth;constructor(e=0){super(),this._depth=e}render(){let e=this._depth;return t.l`<div class="router-view">${()=>{let r
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs"),t=require("./template2.cjs"),n=require("./lifecycle.cjs"),r=require("./context.cjs");var i=r.createInjectionKey("nix:router"),a=null,o=null,s="__nix_scroll",c="__nix_pos";function l(){if(!a)throw Error("[Nix] No active router. Call createRouter() first, or instantiate an outlet that auto-bootstraps one (e.g. IonRouterOutlet).");return a}function u(){return null!==a}function d(){return{left:window.scrollX??window.pageXOffset??0,top:window.scrollY??window.pageYOffset??0}}function ee(e){if(!e||"object"!=typeof e)return null;let t=e[s];if(!t||"object"!=typeof t)return null;let r=t.left,n=t.top;return"number"!=typeof r||"number"!=typeof n?null:{left:r,top:n}}function f(e){if(!e||"object"!=typeof e)return null;let t=e[c];return"number"==typeof t?t:null}function p(e,t,r){let n=e&&"object"==typeof e?{...e}:{};return n[s]={left:t.left,top:t.top},n[c]=r,n}function m(e){let t={};return new URLSearchParams(e).forEach((e,r)=>{t[r]=e}),t}function h(e){let t=new URLSearchParams;for(let[r,n]of Object.entries(e))null!=n&&!1!==n&&t.set(r,String(n));let r=t.toString();return r?"?"+r:""}function g(e){return"*"===e?[{kind:"wildcard"}]:e.split("/").filter(Boolean).map(e=>"*"===e?{kind:"wildcard"}:e.startsWith(":")?{kind:"param",name:e.slice(1)}:{kind:"literal",value:e})}function _(e,t){return"*"===t?""===e?"*":e+"/*":(e+(t.startsWith("/")?t:"/"+t)).replace(/\/+/g,"/")||"/"}function v(e,t="",r=[]){let n=[];for(let o of e){let e=_(t,o.path),a=[...r,o.component],i=g(e);n.push({fullPath:e,segments:i,chain:a,name:o.name,meta:o.meta,beforeEnter:o.beforeEnter,record:o}),o.children?.length&&n.push(...v(o.children,e,a))}return n}function y(e,t){let r=e.split("/").filter(Boolean),n=t.segments;if(1===n.length&&"wildcard"===n[0].kind)return{};let o=n.length>0&&"wildcard"===n[n.length-1].kind,a=o?n.slice(0,-1):n;if(o){if(r.length<a.length)return null}else if(r.length!==a.length)return null;let i={};for(let e=0;e<a.length;e++){let t=a[e];if("literal"===t.kind){if(r[e]!==t.value)return null}else if("param"===t.kind)try{i[t.name]=decodeURIComponent(r[e]??"")}catch{i[t.name]=r[e]??""}}return i}function b(e){return e.segments.reduce((e,t)=>"literal"===t.kind?e+2:"param"===t.kind?e+1:e,0)}function x(e,t){let r,n={},o=-1;for(let a of t){let t=y(e,a);if(null===t)continue;let i=b(a);i>o&&(r=a,n=t,o=i)}return r?{route:r,params:n}:void 0}function S(e){let t=e.trim();return t&&"/"!==t?(t.startsWith("/")||(t="/"+t),t.endsWith("/")&&(t=t.slice(0,-1)),t):""}function te(){if(typeof document>"u")return"";let e=document.querySelector("base");if(!e)return"";let t=e.getAttribute("href")||"";try{return S(new URL(t,window.location.origin).pathname)}catch{return S(t)}}function C(e){return!1===e?{allow:!1}:!0===e||null==e?{allow:!0}:"string"==typeof e?{allow:!1,redirect:e}:"object"==typeof e&&"redirect"in e&&"string"==typeof e.redirect?{allow:!1,redirect:e.redirect}:{allow:!0}}function w(t,r){let n=null==r?.base?te():S(r.base),i=r?.mode??"history",l="hash"===i,u=r?.scrollBehavior,s=new Map,c=!1;function g(e){return e?e.startsWith("/")?e:"/"+e:"/"}function w(e){let t=g(e||"/");if(n&&t.startsWith(n)){let e=t.slice(n.length);return""===e?"/":g(e)}return t}function y(){return l?function(){let e=window.location.hash||"";if(e.startsWith("#")&&(e=e.slice(1)),!e)return{pathname:"/",search:""};e.startsWith("/")||(e="/"+e);let t=e.indexOf("?"),r=-1===t?e:e.slice(0,t),n=-1===t?"":e.slice(t);return{pathname:w(r),search:n}}():{pathname:w(window.location.pathname||"/"),search:window.location.search||""}}function b(e,t){let r=function(e){let t=g(e);return n?(n+t).replace(/\/+/g,"/")||"/":t}(e)+h(t);return l?"#"+r:r}function _(e,t){return g(e)+h(t)}let k=y(),E=k.pathname,R=m(k.search),j=v(t),q=new Map;for(let e of j)e.name&&(q.has(e.name)&&console.warn(`[Nix Router] Duplicate route name: "${e.name}"`),q.set(e.name,e));let O=x(E,j),W=e.signal(E),$=e.signal(O?.params??{}),N=e.signal(R),L=f(history.state)??0,P=e.signal({action:"initial",direction:"none"}),M=e.signal(L>0);function T(e){window.scrollTo(e.left,e.top)}function B(e,t,r){if(u){let n=u(e,t,r);if(!n)return;return void T(n)}T(r??{left:0,top:0})}l?s.set(_(E,R),d()):history.replaceState(p(history.state,d(),L),"");let I=[],U=[],A=0;function D(e,t,r,n,o){let a=[...I];r&&a.push(r);let i=++A;if(0===a.length)return void n();let l=0;!function r(u){if(i!==A)return;let s=C(u);if(!s.allow)return s.redirect&&s.redirect!==e?void z(s.redirect):s.redirect===e?void n():void o?.();if(l>=a.length)return void n();let c=a[l++](e,t);c instanceof Promise?c.then(r):r(c)}(void 0)}let G=!1;function Q(e,t){let r=e.indexOf("?"),n=g((-1===r?e:e.slice(0,r))||"/"),o=-1===r?{}:m(e.slice(r)),a=t?{...o,...t}:o,i={};for(let[e,t]of Object.entries(a))null!=t&&!1!==t&&(i[e]=String(t));return{pathname:n,stringQuery:i}}function K(e,t){return"string"==typeof e?Q(e,t?.query):Q(function(e){let t=q.get(e.name);if(!t)throw Error(`[Nix Router] No route with name "${e.name}"`);return"/"+t.segments.map(t=>{if("literal"===t.kind)return t.value;if("wildcard"===t.kind)return"";let r=e.params?.[t.name];if(null==r)throw Error(`[Nix Router] Missing param "${t.name}" for route "${e.name}"`);return encodeURIComponent(String(r))}).filter(Boolean).join("/")}(e),{...e.query??{},...t?.query??{}})}o&&=(o(),null);let X,Y=(e,t,r,n,o)=>{let a=W.value,i={...N.value},l=x(e,j),u="none";null!=n&&(n<L?u="back":n>L&&(u="forward")),D(e,a,l?.route.beforeEnter,()=>{null!=n&&(L=n);let o=X;X=void 0,P.value={action:"pop",direction:u,animation:o},$.value=l?.params??{},N.value=t,W.value=e,M.value=L>0,B(e,a,r);for(let t of U)try{t(e,a)}catch{}},()=>o(a,i))};if(l){let e=()=>{if(c)return void(c=!1);let e=y(),t=m(e.search),r=s.get(_(e.pathname,t))??null;Y(e.pathname,t,r,null,(e,t)=>{c=!0,window.location.hash=b(e,t).slice(1),queueMicrotask(()=>{c=!1})})};window.addEventListener("hashchange",e),o=()=>window.removeEventListener("hashchange",e)}else{let e=e=>{let t=y(),r=m(t.search),n=ee(e.state??history.state),o=f(e.state??history.state);Y(t.pathname,r,n,o,(e,t)=>{history.pushState(p({},d(),L),"",b(e,t))})};window.addEventListener("popstate",e),o=()=>window.removeEventListener("popstate",e)}function V(e,t,r,n,o,a,i){i||(function(e,t){let r=d();l?s.set(_(e,t),r):history.replaceState(p(history.state,r,L),"")}(r,n),L+=1),P.value=a,$.value=o?.params??{},N.value=t,W.value=e,M.value=L>0;let u=b(e,t);if(l)s.set(_(e,t),{left:0,top:0}),i?history.replaceState(history.state,"",u):(c=!0,window.location.hash=u.slice(1),queueMicrotask(()=>{c=!1}));else{let e=p({},{left:0,top:0},L);i?history.replaceState(e,"",u):history.pushState(e,"",u)}B(e,r,null);for(let t of U)try{t(e,r)}catch{}}function z(e,t){G=!0;let{pathname:r,stringQuery:n}=K(e,t),o=W.value,a={...N.value},i=x(r,j),l={action:"push",direction:t?.direction??"forward",animation:t?.animation};D(r,o,i?.route.beforeEnter,()=>V(r,n,o,a,i,l,!1))}let F={current:W,params:$,query:N,intent:P,canGoBack:M,base:n||"/",navigate:z,replace:function(e,t){G=!0;let{pathname:r,stringQuery:n}=K(e,t),o=W.value,a={...N.value},i=x(r,j),l={action:"replace",direction:t?.direction??"root",animation:t?.animation};D(r,o,i?.route.beforeEnter,()=>V(r,n,o,a,i,l,!0))},back:function(e){void 0!==e&&(X=e),history.back()},forward:function(e){void 0!==e&&(X=e),history.forward()},go:function(e){history.go(e)},isActive:function(e,t=!0){let r=W.value;return t?r===e:r===e||r.startsWith(e.endsWith("/")?e:e+"/")},resolve:function(e){let t=x(e,j);return t?{matched:!0,params:t.params,route:t.route.record}:{matched:!1,params:{},route:void 0}},beforeEach:function(e){return I.push(e),()=>{let t=I.indexOf(e);-1!==t&&I.splice(t,1)}},afterEach:function(e){return U.push(e),()=>{let t=U.indexOf(e);-1!==t&&U.splice(t,1)}},routes:t,_flat:j,_guards:I,_base:n,_mode:i};return a&&console.warn("[Nix] A router already exists. The previous router is being replaced. Only one router instance should be active at a time."),a=F,queueMicrotask(()=>{G||D(E,"",x(E,j)?.route.beforeEnter,()=>{},()=>{let e=b("/",{});l?(s.set(_("/",{}),{left:0,top:0}),history.replaceState(history.state,"",e)):history.replaceState(p({},{left:0,top:0},L),"",e);let t=x("/",j);P.value={action:"replace",direction:"root"},W.value="/",$.value=t?.params??{},N.value={},M.value=L>0,B("/",E,null)})}),F}function T(){return r.inject(i)||l()}function E(){o&&=(o(),null),a=null}var D=class extends n.NixComponent{_depth;_router;constructor(e=0,t){super(),this._depth=e,this._router=t}render(){let e=this._depth,r=this._router;return t.l`<div class="router-view">${()=>{let n=r??T(),o=x(n.current.value,n._flat);if(!o)return t.l`
|
|
2
2
|
<div style="color:#f87171;padding:16px 0">
|
|
3
|
-
404 — Route not found: <strong>${
|
|
3
|
+
404 — Route not found: <strong>${n.current.value}</strong>
|
|
4
4
|
</div>
|
|
5
|
-
`;if(e>=
|
|
5
|
+
`;if(e>=o.route.chain.length)return t.l`
|
|
6
6
|
<span></span>
|
|
7
|
-
`;let o
|
|
7
|
+
`;let a=o.route.chain[e];return a?a():t.l`
|
|
8
8
|
<span></span>
|
|
9
|
-
`}}</div>`}},O=class extends n.NixComponent{_to;_label;constructor(e,t){super(),this._to=e,this._label=t}render(){let e=this._to,r=this._label,n=T(),o=e.startsWith("/")?e:"/"+e,a=(n._base?n._base+o:o).replace(/\/+/g,"/");return t.l`
|
|
9
|
+
`}}</div>`}},O=class extends n.NixComponent{_to;_label;_router;constructor(e,t,r){super(),this._to=e,this._label=t,this._router=r}render(){let e=this._to,r=this._label,n=this._router??T(),o=e.startsWith("/")?e:"/"+e,a=(n._base?n._base+o:o).replace(/\/+/g,"/");return t.l`
|
|
10
10
|
<a href=${"hash"===n._mode?"#"+a:a} style=${()=>n.current.value===e?"color:#38bdf8;font-weight:700;text-decoration:none;cursor:pointer;padding:4px 10px;border-radius:4px;background:#0c2a3a":"color:#a3a3a3;text-decoration:none;cursor:pointer;padding:4px 10px;border-radius:4px"} @click=${t=>{t.preventDefault(),n.navigate(e)}}>${r}</a>
|
|
11
11
|
`}};function k(){if(!a)return null;let e=a.current.value,t=x(e,a._flat),r=t?.route.beforeEnter,n=a._guards.map((e,t)=>e.name||`beforeEach#${t+1}`);return r&&n.push(r.name||"beforeEnter"),{mode:a._mode,base:a._base||"/",currentPath:e,params:{...a.params.value},query:{...a.query.value},matchedPath:t?.route.fullPath??null,activeGuards:{globalCount:a._guards.length,hasRouteGuard:!!r,names:n}}}exports.Link=O,exports.RouterKey=i,exports.RouterView=D,exports._debugGetRouterInternal=k,exports._hasActiveRouter=u,exports._resetRouter=E,exports.createRouter=w,exports.nixRouter=T;
|
package/dist/lib/router.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import{signal as e}from"./signals.js";import{l as t}from"./template2.js";import{NixComponent as n}from"./lifecycle.js";import{createInjectionKey as r,inject as i}from"./context.js";var a=r("nix:router"),o=null,s=null,c="__nix_scroll",l="__nix_pos";function u(){if(!o)throw Error("[Nix] No active router. Call createRouter() first, or instantiate an outlet that auto-bootstraps one (e.g. IonRouterOutlet).");return o}function d(){return null!==o}function f(){return{left:window.scrollX??window.pageXOffset??0,top:window.scrollY??window.pageYOffset??0}}function ee(e){if(!e||"object"!=typeof e)return null;let t=e[c];if(!t||"object"!=typeof t)return null;let r=t.left,n=t.top;return"number"!=typeof r||"number"!=typeof n?null:{left:r,top:n}}function p(e){if(!e||"object"!=typeof e)return null;let t=e[l];return"number"==typeof t?t:null}function m(e,t,r){let n=e&&"object"==typeof e?{...e}:{};return n[c]={left:t.left,top:t.top},n[l]=r,n}function h(e){let t={};return new URLSearchParams(e).forEach((e,r)=>{t[r]=e}),t}function g(e){let t=new URLSearchParams;for(let[r,n]of Object.entries(e))null!=n&&!1!==n&&t.set(r,String(n));let r=t.toString();return r?"?"+r:""}function _(e){return"*"===e?[{kind:"wildcard"}]:e.split("/").filter(Boolean).map(e=>"*"===e?{kind:"wildcard"}:e.startsWith(":")?{kind:"param",name:e.slice(1)}:{kind:"literal",value:e})}function v(e,t){return"*"===t?""===e?"*":e+"/*":(e+(t.startsWith("/")?t:"/"+t)).replace(/\/+/g,"/")||"/"}function y(e,t="",r=[]){let n=[];for(let o of e){let e=v(t,o.path),a=[...r,o.component],i=_(e);n.push({fullPath:e,segments:i,chain:a,name:o.name,meta:o.meta,beforeEnter:o.beforeEnter,record:o}),o.children?.length&&n.push(...y(o.children,e,a))}return n}function b(e,t){let r=e.split("/").filter(Boolean),n=t.segments;if(1===n.length&&"wildcard"===n[0].kind)return{};let o=n.length>0&&"wildcard"===n[n.length-1].kind,a=o?n.slice(0,-1):n;if(o){if(r.length<a.length)return null}else if(r.length!==a.length)return null;let i={};for(let e=0;e<a.length;e++){let t=a[e];if("literal"===t.kind){if(r[e]!==t.value)return null}else if("param"===t.kind)try{i[t.name]=decodeURIComponent(r[e]??"")}catch{i[t.name]=r[e]??""}}return i}function x(e){return e.segments.reduce((e,t)=>"literal"===t.kind?e+2:"param"===t.kind?e+1:e,0)}function S(e,t){let r,n={},o=-1;for(let a of t){let t=b(e,a);if(null===t)continue;let i=x(a);i>o&&(r=a,n=t,o=i)}return r?{route:r,params:n}:void 0}function C(e){let t=e.trim();return t&&"/"!==t?(t.startsWith("/")||(t="/"+t),t.endsWith("/")&&(t=t.slice(0,-1)),t):""}function te(){if(typeof document>"u")return"";let e=document.querySelector("base");if(!e)return"";let t=e.getAttribute("href")||"";try{return C(new URL(t,window.location.origin).pathname)}catch{return C(t)}}function ne(e){return!1===e?{allow:!1}:!0===e||null==e?{allow:!0}:"string"==typeof e?{allow:!1,redirect:e}:"object"==typeof e&&"redirect"in e&&"string"==typeof e.redirect?{allow:!1,redirect:e.redirect}:{allow:!0}}function w(t,r){let n=null==r?.base?te():C(r.base),a=r?.mode??"history",i="hash"===a,l=r?.scrollBehavior,u=new Map,c=!1;function d(e){return e?e.startsWith("/")?e:"/"+e:"/"}function v(e){let t=d(e||"/");if(n&&t.startsWith(n)){let e=t.slice(n.length);return""===e?"/":d(e)}return t}function w(){return i?function(){let e=window.location.hash||"";if(e.startsWith("#")&&(e=e.slice(1)),!e)return{pathname:"/",search:""};e.startsWith("/")||(e="/"+e);let t=e.indexOf("?"),r=-1===t?e:e.slice(0,t),n=-1===t?"":e.slice(t);return{pathname:v(r),search:n}}():{pathname:v(window.location.pathname||"/"),search:window.location.search||""}}function b(e,t){let r=function(e){let t=d(e);return n?(n+t).replace(/\/+/g,"/")||"/":t}(e)+g(t);return i?"#"+r:r}function _(e,t){return d(e)+g(t)}let x=w(),k=x.pathname,E=h(x.search),R=y(t),j=new Map;for(let e of R)e.name&&(j.has(e.name)&&console.warn(`[Nix Router] Duplicate route name: "${e.name}"`),j.set(e.name,e));let O=S(k,R),W=e(k),$=e(O?.params??{}),q=e(E),L=p(history.state)??0,N=e({action:"initial",direction:"none"}),P=e(L>0);function M(e){window.scrollTo(e.left,e.top)}function T(e,t,r){if(l){let n=l(e,t,r);if(!n)return;return void M(n)}M(r??{left:0,top:0})}i?u.set(_(k,E),f()):history.replaceState(m(history.state,f(),L),"");let B=[],I=[],U=0;function A(e,t,r,n,o){let a=[...B];r&&a.push(r);let i=++U;if(0===a.length)return void n();let l=0;!function r(u){if(i!==U)return;let s=ne(u);if(!s.allow)return s.redirect&&s.redirect!==e?void V(s.redirect):s.redirect===e?void n():void o?.();if(l>=a.length)return void n();let c=a[l++](e,t);c instanceof Promise?c.then(r):r(c)}(void 0)}let D=!1;function G(e,t){let r=e.indexOf("?"),n=d((-1===r?e:e.slice(0,r))||"/"),o=-1===r?{}:h(e.slice(r)),a=t?{...o,...t}:o,i={};for(let[e,t]of Object.entries(a))null!=t&&!1!==t&&(i[e]=String(t));return{pathname:n,stringQuery:i}}function Q(e,t){return"string"==typeof e?G(e,t?.query):G(function(e){let t=j.get(e.name);if(!t)throw Error(`[Nix Router] No route with name "${e.name}"`);return"/"+t.segments.map(t=>{if("literal"===t.kind)return t.value;if("wildcard"===t.kind)return"";let r=e.params?.[t.name];if(null==r)throw Error(`[Nix Router] Missing param "${t.name}" for route "${e.name}"`);return encodeURIComponent(String(r))}).filter(Boolean).join("/")}(e),{...e.query??{},...t?.query??{}})}s&&=(s(),null);let K,X=(e,t,r,n,o)=>{let a=W.value,i={...q.value},l=S(e,R),u="none";null!=n&&(n<L?u="back":n>L&&(u="forward")),A(e,a,l?.route.beforeEnter,()=>{null!=n&&(L=n);let o=K;K=void 0,N.value={action:"pop",direction:u,animation:o},$.value=l?.params??{},q.value=t,W.value=e,P.value=L>0,T(e,a,r);for(let t of I)try{t(e,a)}catch{}},()=>o(a,i))};if(i){let e=()=>{if(c)return void(c=!1);let e=w(),t=h(e.search),r=u.get(_(e.pathname,t))??null;X(e.pathname,t,r,null,(e,t)=>{c=!0,window.location.hash=b(e,t).slice(1),queueMicrotask(()=>{c=!1})})};window.addEventListener("hashchange",e),s=()=>window.removeEventListener("hashchange",e)}else{let e=e=>{let t=w(),r=h(t.search),n=ee(e.state??history.state),o=p(e.state??history.state);X(t.pathname,r,n,o,(e,t)=>{history.pushState(m({},f(),L),"",b(e,t))})};window.addEventListener("popstate",e),s=()=>window.removeEventListener("popstate",e)}function Y(e,t,r,n,o,a,l){l||(function(e,t){let r=f();i?u.set(_(e,t),r):history.replaceState(m(history.state,r,L),"")}(r,n),L+=1),N.value=a,$.value=o?.params??{},q.value=t,W.value=e,P.value=L>0;let s=b(e,t);if(i)u.set(_(e,t),{left:0,top:0}),l?history.replaceState(history.state,"",s):(c=!0,window.location.hash=s.slice(1),queueMicrotask(()=>{c=!1}));else{let e=m({},{left:0,top:0},L);l?history.replaceState(e,"",s):history.pushState(e,"",s)}T(e,r,null);for(let t of I)try{t(e,r)}catch{}}function V(e,t){D=!0;let{pathname:r,stringQuery:n}=Q(e,t),o=W.value,a={...q.value},i=S(r,R),l={action:"push",direction:t?.direction??"forward",animation:t?.animation};A(r,o,i?.route.beforeEnter,()=>Y(r,n,o,a,i,l,!1))}let z={current:W,params:$,query:q,intent:N,canGoBack:P,base:n||"/",navigate:V,replace:function(e,t){D=!0;let{pathname:r,stringQuery:n}=Q(e,t),o=W.value,a={...q.value},i=S(r,R),l={action:"replace",direction:t?.direction??"root",animation:t?.animation};A(r,o,i?.route.beforeEnter,()=>Y(r,n,o,a,i,l,!0))},back:function(e){void 0!==e&&(K=e),history.back()},forward:function(e){void 0!==e&&(K=e),history.forward()},go:function(e){history.go(e)},isActive:function(e,t=!0){let r=W.value;return t?r===e:r===e||r.startsWith(e.endsWith("/")?e:e+"/")},resolve:function(e){let t=S(e,R);return t?{matched:!0,params:t.params,route:t.route.record}:{matched:!1,params:{},route:void 0}},beforeEach:function(e){return B.push(e),()=>{let t=B.indexOf(e);-1!==t&&B.splice(t,1)}},afterEach:function(e){return I.push(e),()=>{let t=I.indexOf(e);-1!==t&&I.splice(t,1)}},routes:t,_flat:R,_guards:B,_base:n,_mode:a};return o&&console.warn("[Nix] A router already exists. The previous router is being replaced. Only one router instance should be active at a time."),o=z,queueMicrotask(()=>{D||A(k,"",S(k,R)?.route.beforeEnter,()=>{},()=>{let e=b("/",{});i?(u.set(_("/",{}),{left:0,top:0}),history.replaceState(history.state,"",e)):history.replaceState(m({},{left:0,top:0},L),"",e);let t=S("/",R);N.value={action:"replace",direction:"root"},W.value="/",$.value=t?.params??{},q.value={},P.value=L>0,T("/",k,null)})}),z}function T(){return i(a)||u()}function E(){s&&=(s(),null),o=null}var D=class extends n{_depth;constructor(e=0){super(),this._depth=e}render(){let e=this._depth;return t`<div class="router-view">${()=>{let r
|
|
1
|
+
import{signal as e}from"./signals.js";import{l as t}from"./template2.js";import{NixComponent as n}from"./lifecycle.js";import{createInjectionKey as r,inject as i}from"./context.js";var a=r("nix:router"),o=null,s=null,c="__nix_scroll",l="__nix_pos";function u(){if(!o)throw Error("[Nix] No active router. Call createRouter() first, or instantiate an outlet that auto-bootstraps one (e.g. IonRouterOutlet).");return o}function d(){return null!==o}function f(){return{left:window.scrollX??window.pageXOffset??0,top:window.scrollY??window.pageYOffset??0}}function ee(e){if(!e||"object"!=typeof e)return null;let t=e[c];if(!t||"object"!=typeof t)return null;let r=t.left,n=t.top;return"number"!=typeof r||"number"!=typeof n?null:{left:r,top:n}}function p(e){if(!e||"object"!=typeof e)return null;let t=e[l];return"number"==typeof t?t:null}function m(e,t,r){let n=e&&"object"==typeof e?{...e}:{};return n[c]={left:t.left,top:t.top},n[l]=r,n}function h(e){let t={};return new URLSearchParams(e).forEach((e,r)=>{t[r]=e}),t}function g(e){let t=new URLSearchParams;for(let[r,n]of Object.entries(e))null!=n&&!1!==n&&t.set(r,String(n));let r=t.toString();return r?"?"+r:""}function _(e){return"*"===e?[{kind:"wildcard"}]:e.split("/").filter(Boolean).map(e=>"*"===e?{kind:"wildcard"}:e.startsWith(":")?{kind:"param",name:e.slice(1)}:{kind:"literal",value:e})}function v(e,t){return"*"===t?""===e?"*":e+"/*":(e+(t.startsWith("/")?t:"/"+t)).replace(/\/+/g,"/")||"/"}function y(e,t="",r=[]){let n=[];for(let o of e){let e=v(t,o.path),a=[...r,o.component],i=_(e);n.push({fullPath:e,segments:i,chain:a,name:o.name,meta:o.meta,beforeEnter:o.beforeEnter,record:o}),o.children?.length&&n.push(...y(o.children,e,a))}return n}function b(e,t){let r=e.split("/").filter(Boolean),n=t.segments;if(1===n.length&&"wildcard"===n[0].kind)return{};let o=n.length>0&&"wildcard"===n[n.length-1].kind,a=o?n.slice(0,-1):n;if(o){if(r.length<a.length)return null}else if(r.length!==a.length)return null;let i={};for(let e=0;e<a.length;e++){let t=a[e];if("literal"===t.kind){if(r[e]!==t.value)return null}else if("param"===t.kind)try{i[t.name]=decodeURIComponent(r[e]??"")}catch{i[t.name]=r[e]??""}}return i}function x(e){return e.segments.reduce((e,t)=>"literal"===t.kind?e+2:"param"===t.kind?e+1:e,0)}function S(e,t){let r,n={},o=-1;for(let a of t){let t=b(e,a);if(null===t)continue;let i=x(a);i>o&&(r=a,n=t,o=i)}return r?{route:r,params:n}:void 0}function C(e){let t=e.trim();return t&&"/"!==t?(t.startsWith("/")||(t="/"+t),t.endsWith("/")&&(t=t.slice(0,-1)),t):""}function te(){if(typeof document>"u")return"";let e=document.querySelector("base");if(!e)return"";let t=e.getAttribute("href")||"";try{return C(new URL(t,window.location.origin).pathname)}catch{return C(t)}}function ne(e){return!1===e?{allow:!1}:!0===e||null==e?{allow:!0}:"string"==typeof e?{allow:!1,redirect:e}:"object"==typeof e&&"redirect"in e&&"string"==typeof e.redirect?{allow:!1,redirect:e.redirect}:{allow:!0}}function w(t,r){let n=null==r?.base?te():C(r.base),a=r?.mode??"history",i="hash"===a,l=r?.scrollBehavior,u=new Map,c=!1;function d(e){return e?e.startsWith("/")?e:"/"+e:"/"}function v(e){let t=d(e||"/");if(n&&t.startsWith(n)){let e=t.slice(n.length);return""===e?"/":d(e)}return t}function w(){return i?function(){let e=window.location.hash||"";if(e.startsWith("#")&&(e=e.slice(1)),!e)return{pathname:"/",search:""};e.startsWith("/")||(e="/"+e);let t=e.indexOf("?"),r=-1===t?e:e.slice(0,t),n=-1===t?"":e.slice(t);return{pathname:v(r),search:n}}():{pathname:v(window.location.pathname||"/"),search:window.location.search||""}}function b(e,t){let r=function(e){let t=d(e);return n?(n+t).replace(/\/+/g,"/")||"/":t}(e)+g(t);return i?"#"+r:r}function _(e,t){return d(e)+g(t)}let x=w(),k=x.pathname,E=h(x.search),R=y(t),j=new Map;for(let e of R)e.name&&(j.has(e.name)&&console.warn(`[Nix Router] Duplicate route name: "${e.name}"`),j.set(e.name,e));let O=S(k,R),W=e(k),$=e(O?.params??{}),q=e(E),L=p(history.state)??0,N=e({action:"initial",direction:"none"}),P=e(L>0);function M(e){window.scrollTo(e.left,e.top)}function T(e,t,r){if(l){let n=l(e,t,r);if(!n)return;return void M(n)}M(r??{left:0,top:0})}i?u.set(_(k,E),f()):history.replaceState(m(history.state,f(),L),"");let B=[],I=[],U=0;function A(e,t,r,n,o){let a=[...B];r&&a.push(r);let i=++U;if(0===a.length)return void n();let l=0;!function r(u){if(i!==U)return;let s=ne(u);if(!s.allow)return s.redirect&&s.redirect!==e?void V(s.redirect):s.redirect===e?void n():void o?.();if(l>=a.length)return void n();let c=a[l++](e,t);c instanceof Promise?c.then(r):r(c)}(void 0)}let D=!1;function G(e,t){let r=e.indexOf("?"),n=d((-1===r?e:e.slice(0,r))||"/"),o=-1===r?{}:h(e.slice(r)),a=t?{...o,...t}:o,i={};for(let[e,t]of Object.entries(a))null!=t&&!1!==t&&(i[e]=String(t));return{pathname:n,stringQuery:i}}function Q(e,t){return"string"==typeof e?G(e,t?.query):G(function(e){let t=j.get(e.name);if(!t)throw Error(`[Nix Router] No route with name "${e.name}"`);return"/"+t.segments.map(t=>{if("literal"===t.kind)return t.value;if("wildcard"===t.kind)return"";let r=e.params?.[t.name];if(null==r)throw Error(`[Nix Router] Missing param "${t.name}" for route "${e.name}"`);return encodeURIComponent(String(r))}).filter(Boolean).join("/")}(e),{...e.query??{},...t?.query??{}})}s&&=(s(),null);let K,X=(e,t,r,n,o)=>{let a=W.value,i={...q.value},l=S(e,R),u="none";null!=n&&(n<L?u="back":n>L&&(u="forward")),A(e,a,l?.route.beforeEnter,()=>{null!=n&&(L=n);let o=K;K=void 0,N.value={action:"pop",direction:u,animation:o},$.value=l?.params??{},q.value=t,W.value=e,P.value=L>0,T(e,a,r);for(let t of I)try{t(e,a)}catch{}},()=>o(a,i))};if(i){let e=()=>{if(c)return void(c=!1);let e=w(),t=h(e.search),r=u.get(_(e.pathname,t))??null;X(e.pathname,t,r,null,(e,t)=>{c=!0,window.location.hash=b(e,t).slice(1),queueMicrotask(()=>{c=!1})})};window.addEventListener("hashchange",e),s=()=>window.removeEventListener("hashchange",e)}else{let e=e=>{let t=w(),r=h(t.search),n=ee(e.state??history.state),o=p(e.state??history.state);X(t.pathname,r,n,o,(e,t)=>{history.pushState(m({},f(),L),"",b(e,t))})};window.addEventListener("popstate",e),s=()=>window.removeEventListener("popstate",e)}function Y(e,t,r,n,o,a,l){l||(function(e,t){let r=f();i?u.set(_(e,t),r):history.replaceState(m(history.state,r,L),"")}(r,n),L+=1),N.value=a,$.value=o?.params??{},q.value=t,W.value=e,P.value=L>0;let s=b(e,t);if(i)u.set(_(e,t),{left:0,top:0}),l?history.replaceState(history.state,"",s):(c=!0,window.location.hash=s.slice(1),queueMicrotask(()=>{c=!1}));else{let e=m({},{left:0,top:0},L);l?history.replaceState(e,"",s):history.pushState(e,"",s)}T(e,r,null);for(let t of I)try{t(e,r)}catch{}}function V(e,t){D=!0;let{pathname:r,stringQuery:n}=Q(e,t),o=W.value,a={...q.value},i=S(r,R),l={action:"push",direction:t?.direction??"forward",animation:t?.animation};A(r,o,i?.route.beforeEnter,()=>Y(r,n,o,a,i,l,!1))}let z={current:W,params:$,query:q,intent:N,canGoBack:P,base:n||"/",navigate:V,replace:function(e,t){D=!0;let{pathname:r,stringQuery:n}=Q(e,t),o=W.value,a={...q.value},i=S(r,R),l={action:"replace",direction:t?.direction??"root",animation:t?.animation};A(r,o,i?.route.beforeEnter,()=>Y(r,n,o,a,i,l,!0))},back:function(e){void 0!==e&&(K=e),history.back()},forward:function(e){void 0!==e&&(K=e),history.forward()},go:function(e){history.go(e)},isActive:function(e,t=!0){let r=W.value;return t?r===e:r===e||r.startsWith(e.endsWith("/")?e:e+"/")},resolve:function(e){let t=S(e,R);return t?{matched:!0,params:t.params,route:t.route.record}:{matched:!1,params:{},route:void 0}},beforeEach:function(e){return B.push(e),()=>{let t=B.indexOf(e);-1!==t&&B.splice(t,1)}},afterEach:function(e){return I.push(e),()=>{let t=I.indexOf(e);-1!==t&&I.splice(t,1)}},routes:t,_flat:R,_guards:B,_base:n,_mode:a};return o&&console.warn("[Nix] A router already exists. The previous router is being replaced. Only one router instance should be active at a time."),o=z,queueMicrotask(()=>{D||A(k,"",S(k,R)?.route.beforeEnter,()=>{},()=>{let e=b("/",{});i?(u.set(_("/",{}),{left:0,top:0}),history.replaceState(history.state,"",e)):history.replaceState(m({},{left:0,top:0},L),"",e);let t=S("/",R);N.value={action:"replace",direction:"root"},W.value="/",$.value=t?.params??{},q.value={},P.value=L>0,T("/",k,null)})}),z}function T(){return i(a)||u()}function E(){s&&=(s(),null),o=null}var D=class extends n{_depth;_router;constructor(e=0,t){super(),this._depth=e,this._router=t}render(){let e=this._depth,r=this._router;return t`<div class="router-view">${()=>{let n=r??T(),o=S(n.current.value,n._flat);if(!o)return t`
|
|
2
2
|
<div style="color:#f87171;padding:16px 0">
|
|
3
|
-
404 — Route not found: <strong>${
|
|
3
|
+
404 — Route not found: <strong>${n.current.value}</strong>
|
|
4
4
|
</div>
|
|
5
|
-
`;if(e>=
|
|
5
|
+
`;if(e>=o.route.chain.length)return t`
|
|
6
6
|
<span></span>
|
|
7
|
-
`;let o
|
|
7
|
+
`;let a=o.route.chain[e];return a?a():t`
|
|
8
8
|
<span></span>
|
|
9
|
-
`}}</div>`}},O=class extends n{_to;_label;constructor(e,t){super(),this._to=e,this._label=t}render(){let e=this._to,r=this._label,n=T(),o=e.startsWith("/")?e:"/"+e,a=(n._base?n._base+o:o).replace(/\/+/g,"/");return t`
|
|
9
|
+
`}}</div>`}},O=class extends n{_to;_label;_router;constructor(e,t,r){super(),this._to=e,this._label=t,this._router=r}render(){let e=this._to,r=this._label,n=this._router??T(),o=e.startsWith("/")?e:"/"+e,a=(n._base?n._base+o:o).replace(/\/+/g,"/");return t`
|
|
10
10
|
<a href=${"hash"===n._mode?"#"+a:a} style=${()=>n.current.value===e?"color:#38bdf8;font-weight:700;text-decoration:none;cursor:pointer;padding:4px 10px;border-radius:4px;background:#0c2a3a":"color:#a3a3a3;text-decoration:none;cursor:pointer;padding:4px 10px;border-radius:4px"} @click=${t=>{t.preventDefault(),n.navigate(e)}}>${r}</a>
|
|
11
11
|
`}};function k(){if(!o)return null;let e=o.current.value,t=S(e,o._flat),r=t?.route.beforeEnter,n=o._guards.map((e,t)=>e.name||`beforeEach#${t+1}`);return r&&n.push(r.name||"beforeEnter"),{mode:o._mode,base:o._base||"/",currentPath:e,params:{...o.params.value},query:{...o.query.value},matchedPath:t?.route.fullPath??null,activeGuards:{globalCount:o._guards.length,hasRouteGuard:!!r,names:n}}}export{O as Link,a as RouterKey,D as RouterView,k as _debugGetRouterInternal,d as _hasActiveRouter,E as _resetRouter,w as createRouter,T as nixRouter};
|
package/dist/lib/signals.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function e(){return{ctxPool:[],ctxStack:[],activeEffect:null,activeDeps:null,activeErrorHandler:null,errorHandlerStack:[],batchLevel:0,pendingEffectsSet:new Set,pendingEffectsArr:[],effectDepth:0,notifyBuf:[],notifyBase:0,signalDebugHooks:null}}Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var t=Symbol.for("@deijose/nix-js/reactivity-state"),n=globalThis,r=(()=>{let r=n[t];if(r)return r;let l=e();return n[t]=l,l})();function i(e){r.errorHandlerStack.push(r.activeErrorHandler),r.activeErrorHandler=e}function a(){r.activeErrorHandler=r.errorHandlerStack.pop()??null}var o=100,s=64,c=16,l=32;function u(e){r.signalDebugHooks=e}function d(){return r.notifyBuf.length}var f=class{_value;_subs=new Set;constructor(e){this._value=e,r.signalDebugHooks?.onCreate?.(this,e)}get value(){return r.activeEffect&&(this._subs.add(r.activeEffect),r.activeDeps?.add(this)),this._value}set value(e){Object.is(this._value,e)||(this._value=e,r.signalDebugHooks?.onWrite?.(this,e),this._notify())}update(e){this.value=e(this._value)}peek(){return this._value}_removeSub(e){this._subs.delete(e)}_notify(){if(r.batchLevel>0){for(let e of this._subs)r.pendingEffectsSet.has(e)||(r.pendingEffectsSet.add(e),r.pendingEffectsArr.push(e));return}let e=r.notifyBase,t=0;for(let n of this._subs)r.notifyBuf[e+t++]=n;r.notifyBase=e+t;try{for(let n=0;n<t;n++){let t=r.notifyBuf[e+n];r.notifyBuf[e+n]=null,t?.()}}finally{r.notifyBase=e,0===e&&r.notifyBuf.length>s&&t<c&&(r.notifyBuf.length=l)}}dispose(){this._subs.clear()}};function p(e){return new f(e)}function m(e){let t,n=!1,l=new Set,
|
|
1
|
+
function e(){return{ctxPool:[],ctxStack:[],activeEffect:null,activeDeps:null,activeErrorHandler:null,errorHandlerStack:[],batchLevel:0,pendingEffectsSet:new Set,pendingEffectsArr:[],effectDepth:0,notifyBuf:[],notifyBase:0,signalDebugHooks:null}}Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var t=Symbol.for("@deijose/nix-js/reactivity-state"),n=globalThis,r=(()=>{let r=n[t];if(r)return r;let l=e();return n[t]=l,l})();function i(e){r.errorHandlerStack.push(r.activeErrorHandler),r.activeErrorHandler=e}function a(){r.activeErrorHandler=r.errorHandlerStack.pop()??null}var o=100,s=64,c=16,l=32;function u(e){r.signalDebugHooks=e}function d(){return r.notifyBuf.length}var f=class{_value;_subs=new Set;constructor(e){this._value=e,r.signalDebugHooks?.onCreate?.(this,e)}get value(){return r.activeEffect&&(this._subs.add(r.activeEffect),r.activeDeps?.add(this)),this._value}set value(e){Object.is(this._value,e)||(this._value=e,r.signalDebugHooks?.onWrite?.(this,e),this._notify())}update(e){this.value=e(this._value)}peek(){return this._value}_removeSub(e){this._subs.delete(e)}_notify(){if(r.batchLevel>0){for(let e of this._subs)r.pendingEffectsSet.has(e)||(r.pendingEffectsSet.add(e),r.pendingEffectsArr.push(e));return}let e=r.notifyBase,t=0;for(let n of this._subs)r.notifyBuf[e+t++]=n;r.notifyBase=e+t;try{for(let n=0;n<t;n++){let t=r.notifyBuf[e+n];r.notifyBuf[e+n]=null,t?.()}}finally{r.notifyBase=e,0===e&&r.notifyBuf.length>s&&t<c&&(r.notifyBuf.length=l)}}dispose(){this._subs.clear()}};function p(e){return new f(e)}function m(e){let t,n=!1,l=new Set,i=new Set,f=r.activeErrorHandler,c=()=>{if(n)return;"function"==typeof t&&t();let s=l;l=i,i=s,i.clear();let a=r.ctxPool.length>0?r.ctxPool.pop():{effect:null,deps:null};if(a.effect=r.activeEffect,a.deps=r.activeDeps,r.ctxStack.push(a),r.activeEffect=c,r.activeDeps=i,r.effectDepth++,r.effectDepth>o){r.effectDepth=0;let e=r.ctxStack.pop();throw r.activeEffect=e.effect,r.activeDeps=e.deps,e.effect=null,e.deps=null,r.ctxPool.push(e),Error("[Nix] Maximum effect re-execution depth exceeded (possible infinite loop).")}try{t=e()}catch(e){if(!f)throw e;f(e)}finally{r.effectDepth--;let e=r.ctxStack.pop();r.activeEffect=e.effect,r.activeDeps=e.deps,e.effect=null,e.deps=null,r.ctxPool.push(e);for(let e of l)i.has(e)||e._removeSub(c)}};return c(),()=>{n=!0,"function"==typeof t&&t();for(let e of i)e._removeSub(c);i.clear(),l.clear()}}var h=Symbol("nix-computed-uninitialized");function g(e,t=Object.is){let r=new f(h),n=null,l=!1,i=!1,o=Object.getOwnPropertyDescriptor(Object.getPrototypeOf(r),"value");if(!o?.get||!o?.set)throw Error("[Nix] Internal error: Signal.value descriptor not found.");Object.defineProperty(r,"value",{get(){return l||i||(l=!0,v(()=>{n=m(()=>{let n=e(),l=r.peek();(l===h||!t(n,l))&&(r.value=n)})})),o.get.call(this)},set(e){o.set.call(this,e)},configurable:!0});let c=r.dispose;return r.dispose=()=>{i=!0,n?.(),n=null,c.call(r)},r}function _(e){r.batchLevel++;try{e()}finally{if(r.batchLevel--,0===r.batchLevel&&r.pendingEffectsArr.length>0){let e=r.pendingEffectsArr.length;for(let t=0;t<e;t++)r.pendingEffectsArr[t]();r.pendingEffectsArr.length=0,r.pendingEffectsSet.clear()}}}function v(e){let t=r.activeEffect,n=r.activeDeps;r.activeEffect=null,r.activeDeps=null;try{return e()}finally{r.activeEffect=t,r.activeDeps=n}}function y(e,t,r={}){let n,{immediate:l=!1,once:i=!1}=r,o=e instanceof f?()=>e.value:e,c=!0,s=!1,a=m(()=>{let e=o();if(c){if(c=!1,l&&!s){let r=e;v(()=>t(r,void 0)),i&&(s=!0,Promise.resolve().then(a))}n=e}else if(!s){let r=e,l=n;n=e,v(()=>t(r,l)),i&&(s=!0,Promise.resolve().then(a))}});return()=>{s=!0,a()}}function b(e){return e?Promise.resolve().then(e):Promise.resolve()}exports.Signal=f,exports._getNotifyBufSize=d,exports._popErrorHandler=a,exports._pushErrorHandler=i,exports._setSignalDebugHooks=u,exports.batch=_,exports.computed=g,exports.effect=m,exports.nextTick=b,exports.signal=p,exports.untrack=v,exports.watch=y;
|
package/dist/lib/signals.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function e(){return{ctxPool:[],ctxStack:[],activeEffect:null,activeDeps:null,activeErrorHandler:null,errorHandlerStack:[],batchLevel:0,pendingEffectsSet:new Set,pendingEffectsArr:[],effectDepth:0,notifyBuf:[],notifyBase:0,signalDebugHooks:null}}var t=Symbol.for("@deijose/nix-js/reactivity-state"),n=globalThis,r=(()=>{let r=n[t];if(r)return r;let
|
|
1
|
+
function e(){return{ctxPool:[],ctxStack:[],activeEffect:null,activeDeps:null,activeErrorHandler:null,errorHandlerStack:[],batchLevel:0,pendingEffectsSet:new Set,pendingEffectsArr:[],effectDepth:0,notifyBuf:[],notifyBase:0,signalDebugHooks:null}}var t=Symbol.for("@deijose/nix-js/reactivity-state"),n=globalThis,r=(()=>{let r=n[t];if(r)return r;let l=e();return n[t]=l,l})();function i(e){r.errorHandlerStack.push(r.activeErrorHandler),r.activeErrorHandler=e}function a(){r.activeErrorHandler=r.errorHandlerStack.pop()??null}var o=100,s=64,c=16,l=32;function u(e){r.signalDebugHooks=e}function d(){return r.notifyBuf.length}var f=class{_value;_subs=new Set;constructor(e){this._value=e,r.signalDebugHooks?.onCreate?.(this,e)}get value(){return r.activeEffect&&(this._subs.add(r.activeEffect),r.activeDeps?.add(this)),this._value}set value(e){Object.is(this._value,e)||(this._value=e,r.signalDebugHooks?.onWrite?.(this,e),this._notify())}update(e){this.value=e(this._value)}peek(){return this._value}_removeSub(e){this._subs.delete(e)}_notify(){if(r.batchLevel>0){for(let e of this._subs)r.pendingEffectsSet.has(e)||(r.pendingEffectsSet.add(e),r.pendingEffectsArr.push(e));return}let e=r.notifyBase,t=0;for(let n of this._subs)r.notifyBuf[e+t++]=n;r.notifyBase=e+t;try{for(let n=0;n<t;n++){let t=r.notifyBuf[e+n];r.notifyBuf[e+n]=null,t?.()}}finally{r.notifyBase=e,0===e&&r.notifyBuf.length>s&&t<c&&(r.notifyBuf.length=l)}}dispose(){this._subs.clear()}};function p(e){return new f(e)}function m(e){let t,n=!1,l=new Set,i=new Set,f=r.activeErrorHandler,c=()=>{if(n)return;"function"==typeof t&&t();let s=l;l=i,i=s,i.clear();let a=r.ctxPool.length>0?r.ctxPool.pop():{effect:null,deps:null};if(a.effect=r.activeEffect,a.deps=r.activeDeps,r.ctxStack.push(a),r.activeEffect=c,r.activeDeps=i,r.effectDepth++,r.effectDepth>o){r.effectDepth=0;let e=r.ctxStack.pop();throw r.activeEffect=e.effect,r.activeDeps=e.deps,e.effect=null,e.deps=null,r.ctxPool.push(e),Error("[Nix] Maximum effect re-execution depth exceeded (possible infinite loop).")}try{t=e()}catch(e){if(!f)throw e;f(e)}finally{r.effectDepth--;let e=r.ctxStack.pop();r.activeEffect=e.effect,r.activeDeps=e.deps,e.effect=null,e.deps=null,r.ctxPool.push(e);for(let e of l)i.has(e)||e._removeSub(c)}};return c(),()=>{n=!0,"function"==typeof t&&t();for(let e of i)e._removeSub(c);i.clear(),l.clear()}}var h=Symbol("nix-computed-uninitialized");function g(e,t=Object.is){let r=new f(h),n=null,l=!1,i=!1,c=Object.getOwnPropertyDescriptor(Object.getPrototypeOf(r),"value");if(!c?.get||!c?.set)throw Error("[Nix] Internal error: Signal.value descriptor not found.");Object.defineProperty(r,"value",{get(){return l||i||(l=!0,v(()=>{n=m(()=>{let n=e(),l=r.peek();(l===h||!t(n,l))&&(r.value=n)})})),c.get.call(this)},set(e){c.set.call(this,e)},configurable:!0});let o=r.dispose;return r.dispose=()=>{i=!0,n?.(),n=null,o.call(r)},r}function _(e){r.batchLevel++;try{e()}finally{if(r.batchLevel--,0===r.batchLevel&&r.pendingEffectsArr.length>0){let e=r.pendingEffectsArr.length;for(let t=0;t<e;t++)r.pendingEffectsArr[t]();r.pendingEffectsArr.length=0,r.pendingEffectsSet.clear()}}}function v(e){let t=r.activeEffect,n=r.activeDeps;r.activeEffect=null,r.activeDeps=null;try{return e()}finally{r.activeEffect=t,r.activeDeps=n}}function y(e,t,r={}){let n,{immediate:l=!1,once:i=!1}=r,c=e instanceof f?()=>e.value:e,o=!0,s=!1,a=m(()=>{let e=c();if(o){if(o=!1,l&&!s){let r=e;v(()=>t(r,void 0)),i&&(s=!0,Promise.resolve().then(a))}n=e}else if(!s){let r=e,l=n;n=e,v(()=>t(r,l)),i&&(s=!0,Promise.resolve().then(a))}});return()=>{s=!0,a()}}function b(e){return e?Promise.resolve().then(e):Promise.resolve()}export{f as Signal,d as _getNotifyBufSize,a as _popErrorHandler,i as _pushErrorHandler,u as _setSignalDebugHooks,_ as batch,g as computed,m as effect,b as nextTick,p as signal,v as untrack,y as watch};
|
package/dist/lib/store.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs");var t=class extends e.Signal{label;constructor(e,t="ReadonlySignal"){super(e.peek()),this.label=t,Object.defineProperty(this,"value",{get:()=>e.value,set:()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},configurable:!1}),this.update=()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},this.dispose=()=>{throw Error(`[Nix] Cannot dispose "${this.label}" directly.`)}}},n=new Set(["$id","$state","$stateSignal","$snapshot","$reset","$patch","$watch","$dispose"]);function r(e){if("__proto__"===e||"constructor"===e||"prototype"===e)throw Error(`[Nix] Store key "${e}" is not allowed for security reasons.`);if(n.has(e))throw Error(`[Nix] Store key "${e}" is reserved.`)}function i(e,t){return!n.has(e)||(console.warn(`[Nix] Store ${t} "${e}" is reserved and will be ignored.`),!1)}function a(e,r){return new t(e,r)}function o(t,o){let{name:l="store",actions:s,getters:c,plugins:f=[]}=o??{},u=Object.keys(t),d={};for(let o of u)r(o),d[o]=e.signal(t[o]);let
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs");var t=class extends e.Signal{label;constructor(e,t="ReadonlySignal"){super(e.peek()),this.label=t,Object.defineProperty(this,"value",{get:()=>e.value,set:()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},configurable:!1}),this.update=()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},this.dispose=()=>{throw Error(`[Nix] Cannot dispose "${this.label}" directly.`)}}},n=new Set(["$id","$state","$stateSignal","$snapshot","$reset","$patch","$watch","$dispose"]);function r(e){if("__proto__"===e||"constructor"===e||"prototype"===e)throw Error(`[Nix] Store key "${e}" is not allowed for security reasons.`);if(n.has(e))throw Error(`[Nix] Store key "${e}" is reserved.`)}function i(e,t){return!n.has(e)||(console.warn(`[Nix] Store ${t} "${e}" is reserved and will be ignored.`),!1)}function a(e,r){return new t(e,r)}function o(t,o){let{name:l="store",actions:s,getters:c,plugins:f=[]}=o??{},u=Object.keys(t),d={};for(let o of u)r(o),d[o]=e.signal(t[o]);let b,g=d,$=e.computed(()=>{let e={};for(let t of u)e[t]=d[t].value;return e}),h=a($,`store "${l}".$stateSignal`);try{b=structuredClone(t)}catch(e){throw Error(`[Nix] Store "${l}" initialState contains non-serializable data (functions, DOM nodes, Symbols, or WeakRefs). Remove these before creating the store. Original error: ${e}`)}let p=[];function y(){let e={};for(let t of u)e[t]=d[t].peek();return e}let w=Object.assign(Object.create(null),g,{$reset:function(){let t=y();for(let e of p)e(t,t);e.batch(()=>{for(let e of u)d[e].value=b[e]})},$patch:function(t){let r=t,o=y();for(let e of p){let t=e(r,o);void 0!==t&&(r=t)}e.batch(()=>{for(let e of Object.keys(r))Object.prototype.hasOwnProperty.call(d,e)&&(d[e].value=r[e])})},$watch:function(t,r){return e.watch($,t,r)},$snapshot:y});Object.defineProperty(w,"$id",{value:l,writable:!1,enumerable:!1,configurable:!1}),Object.defineProperty(w,"$state",{get:()=>$.value,enumerable:!0,configurable:!1}),Object.defineProperty(w,"$stateSignal",{value:h,writable:!1,enumerable:!1,configurable:!1}),Object.defineProperty(w,"_guardFns",{value:p,writable:!1,enumerable:!1,configurable:!1});let S=new Set([...u,...Array.from(n)]);if(s){let e=s(g);for(let t of Object.keys(e))if(i(t,"action")){if(S.has(t)){console.warn(`[Nix] Store "${l}": action "${t}" collides with an existing signal or getter and will be ignored.`);continue}S.add(t),w[t]=e[t]}}if(c){let t=c(g);for(let r of Object.keys(t)){if(!i(r,"getter"))continue;if(S.has(r)){console.warn(`[Nix] Store "${l}": getter "${r}" collides with an existing signal or action and will be ignored.`);continue}let o=t[r];if(!(o instanceof e.Signal))throw TypeError(`[Nix] Store "${l}": getter "${r}" must return a Signal (wrap it with computed()). Got: ${typeof o}`);S.add(r),w[r]=a(o,`getter "${r}" in store "${l}"`)}}let x=[()=>$.dispose()];for(let e of f)try{let t=e(w);"function"==typeof t&&x.push(t)}catch(e){console.error(`[Nix] Plugin initialization failed for store "${l}":`,e)}return w.$dispose=()=>{for(let e of x)e()},w}exports.ReadonlySignal=t,exports.createStore=o;
|
package/dist/lib/store.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Signal as e,batch as t,computed as n,signal as r,watch as i}from"./signals.js";var a=class extends e{label;constructor(e,t="ReadonlySignal"){super(e.peek()),this.label=t,Object.defineProperty(this,"value",{get:()=>e.value,set:()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},configurable:!1}),this.update=()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},this.dispose=()=>{throw Error(`[Nix] Cannot dispose "${this.label}" directly.`)}}},o=new Set(["$id","$state","$stateSignal","$snapshot","$reset","$patch","$watch","$dispose"]);function s(e){if("__proto__"===e||"constructor"===e||"prototype"===e)throw Error(`[Nix] Store key "${e}" is not allowed for security reasons.`);if(o.has(e))throw Error(`[Nix] Store key "${e}" is reserved.`)}function c(e,t){return!o.has(e)||(console.warn(`[Nix] Store ${t} "${e}" is reserved and will be ignored.`),!1)}function l(e,t){return new a(e,t)}function u(a,f){let{name:u="store",actions:d,getters:$,plugins:
|
|
1
|
+
import{Signal as e,batch as t,computed as n,signal as r,watch as i}from"./signals.js";var a=class extends e{label;constructor(e,t="ReadonlySignal"){super(e.peek()),this.label=t,Object.defineProperty(this,"value",{get:()=>e.value,set:()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},configurable:!1}),this.update=()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},this.dispose=()=>{throw Error(`[Nix] Cannot dispose "${this.label}" directly.`)}}},o=new Set(["$id","$state","$stateSignal","$snapshot","$reset","$patch","$watch","$dispose"]);function s(e){if("__proto__"===e||"constructor"===e||"prototype"===e)throw Error(`[Nix] Store key "${e}" is not allowed for security reasons.`);if(o.has(e))throw Error(`[Nix] Store key "${e}" is reserved.`)}function c(e,t){return!o.has(e)||(console.warn(`[Nix] Store ${t} "${e}" is reserved and will be ignored.`),!1)}function l(e,t){return new a(e,t)}function u(a,f){let{name:u="store",actions:d,getters:$,plugins:b=[]}=f??{},g=Object.keys(a),h={};for(let e of g)s(e),h[e]=r(a[e]);let p,w=h,y=n(()=>{let e={};for(let t of g)e[t]=h[t].value;return e}),S=l(y,`store "${u}".$stateSignal`);try{p=structuredClone(a)}catch(e){throw Error(`[Nix] Store "${u}" initialState contains non-serializable data (functions, DOM nodes, Symbols, or WeakRefs). Remove these before creating the store. Original error: ${e}`)}let x=[];function O(){let e={};for(let t of g)e[t]=h[t].peek();return e}let v=Object.assign(Object.create(null),w,{$reset:function(){let e=O();for(let t of x)t(e,e);t(()=>{for(let e of g)h[e].value=p[e]})},$patch:function(e){let r=e,o=O();for(let e of x){let t=e(r,o);void 0!==t&&(r=t)}t(()=>{for(let e of Object.keys(r))Object.prototype.hasOwnProperty.call(h,e)&&(h[e].value=r[e])})},$watch:function(e,t){return i(y,e,t)},$snapshot:O});Object.defineProperty(v,"$id",{value:u,writable:!1,enumerable:!1,configurable:!1}),Object.defineProperty(v,"$state",{get:()=>y.value,enumerable:!0,configurable:!1}),Object.defineProperty(v,"$stateSignal",{value:S,writable:!1,enumerable:!1,configurable:!1}),Object.defineProperty(v,"_guardFns",{value:x,writable:!1,enumerable:!1,configurable:!1});let j=new Set([...g,...Array.from(o)]);if(d){let e=d(w);for(let t of Object.keys(e))if(c(t,"action")){if(j.has(t)){console.warn(`[Nix] Store "${u}": action "${t}" collides with an existing signal or getter and will be ignored.`);continue}j.add(t),v[t]=e[t]}}if($){let t=$(w);for(let r of Object.keys(t)){if(!c(r,"getter"))continue;if(j.has(r)){console.warn(`[Nix] Store "${u}": getter "${r}" collides with an existing signal or action and will be ignored.`);continue}let o=t[r];if(!(o instanceof e))throw TypeError(`[Nix] Store "${u}": getter "${r}" must return a Signal (wrap it with computed()). Got: ${typeof o}`);j.add(r),v[r]=l(o,`getter "${r}" in store "${u}"`)}}let m=[()=>y.dispose()];for(let e of b)try{let t=e(v);"function"==typeof t&&m.push(t)}catch(e){console.error(`[Nix] Plugin initialization failed for store "${u}":`,e)}return v.$dispose=()=>{for(let e of m)e()},v}export{a as ReadonlySignal,u as createStore};
|
package/package.json
CHANGED