@unsetsoft/ryunixjs 1.1.6-canary.3 → 1.1.6-canary.4
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/dist/Ryunix.js +39 -35
- package/dist/Ryunix.min.js +1 -1
- package/package.json +1 -1
package/dist/Ryunix.js
CHANGED
|
@@ -170,36 +170,40 @@
|
|
|
170
170
|
* `setState` function that can be used to update the state.
|
|
171
171
|
*/
|
|
172
172
|
const useStore = (initial) => {
|
|
173
|
-
const oldHook =
|
|
173
|
+
const oldHook =
|
|
174
|
+
vars.wipFiber.alternate &&
|
|
175
|
+
vars.wipFiber.alternate.hooks &&
|
|
176
|
+
vars.wipFiber.alternate.hooks[vars.hookIndex];
|
|
174
177
|
const hook = {
|
|
175
178
|
state: oldHook ? oldHook.state : initial,
|
|
176
179
|
queue: oldHook ? [...oldHook.queue] : [],
|
|
177
180
|
};
|
|
178
181
|
|
|
179
182
|
hook.queue.forEach((action) => {
|
|
180
|
-
|
|
183
|
+
hook.state =
|
|
181
184
|
typeof action === STRINGS.function ? action(hook.state) : action;
|
|
182
|
-
if (newState !== hook.state) {
|
|
183
|
-
hook.state = newState;
|
|
184
|
-
}
|
|
185
185
|
});
|
|
186
186
|
|
|
187
|
+
hook.queue = [];
|
|
188
|
+
|
|
187
189
|
const setState = (action) => {
|
|
188
190
|
hook.queue.push(action);
|
|
189
191
|
|
|
190
|
-
|
|
191
|
-
vars.
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
192
|
+
vars.wipRoot = {
|
|
193
|
+
dom: vars.currentRoot.dom,
|
|
194
|
+
props: {
|
|
195
|
+
...vars.currentRoot.props,
|
|
196
|
+
},
|
|
197
|
+
alternate: vars.currentRoot,
|
|
198
|
+
};
|
|
199
|
+
vars.nextUnitOfWork = vars.wipRoot;
|
|
200
|
+
vars.deletions = [];
|
|
199
201
|
};
|
|
200
202
|
|
|
201
|
-
vars.wipFiber.hooks
|
|
202
|
-
|
|
203
|
+
if (vars.wipFiber && vars.wipFiber.hooks) {
|
|
204
|
+
vars.wipFiber.hooks.push(hook);
|
|
205
|
+
vars.hookIndex++;
|
|
206
|
+
}
|
|
203
207
|
|
|
204
208
|
return [hook.state, setState]
|
|
205
209
|
};
|
|
@@ -341,51 +345,51 @@
|
|
|
341
345
|
const useRouter = (routes) => {
|
|
342
346
|
const [location, setLocation] = useStore(window.location.pathname);
|
|
343
347
|
|
|
344
|
-
const routeCache = new Map();
|
|
345
|
-
|
|
346
348
|
const findRoute = (routes, path) => {
|
|
347
|
-
if (routeCache.has(path)) return routeCache.get(path)
|
|
348
|
-
|
|
349
349
|
const pathname = path.split('?')[0];
|
|
350
|
-
|
|
350
|
+
|
|
351
|
+
const notFoundRoute = routes.find((route) => route.NotFound);
|
|
351
352
|
const notFound = notFoundRoute
|
|
352
353
|
? { route: { component: notFoundRoute.NotFound }, params: {} }
|
|
353
354
|
: { route: { component: null }, params: {} };
|
|
354
355
|
|
|
355
356
|
for (const route of routes) {
|
|
356
357
|
if (route.subRoutes) {
|
|
357
|
-
const
|
|
358
|
-
if (
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
358
|
+
const childRoute = findRoute(route.subRoutes, path);
|
|
359
|
+
if (childRoute) return childRoute
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (route.path === '*') {
|
|
363
|
+
return notFound
|
|
362
364
|
}
|
|
363
365
|
|
|
364
366
|
if (!route.path || typeof route.path !== 'string') {
|
|
365
367
|
console.warn('Invalid route detected:', route);
|
|
368
|
+
console.info(
|
|
369
|
+
"if you are using { NotFound: NotFound } please add { path: '*', NotFound: NotFound }",
|
|
370
|
+
);
|
|
366
371
|
continue
|
|
367
372
|
}
|
|
368
373
|
|
|
369
374
|
const keys = [];
|
|
370
|
-
const pattern =
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
375
|
+
const pattern = new RegExp(
|
|
376
|
+
`^${route.path.replace(/:\w+/g, (match) => {
|
|
377
|
+
keys.push(match.substring(1));
|
|
378
|
+
return '([^/]+)'
|
|
379
|
+
})}$`,
|
|
380
|
+
);
|
|
374
381
|
|
|
375
|
-
const match = pathname.match(
|
|
382
|
+
const match = pathname.match(pattern);
|
|
376
383
|
if (match) {
|
|
377
384
|
const params = keys.reduce((acc, key, index) => {
|
|
378
385
|
acc[key] = match[index + 1];
|
|
379
386
|
return acc
|
|
380
387
|
}, {});
|
|
381
388
|
|
|
382
|
-
|
|
383
|
-
routeCache.set(path, routeData);
|
|
384
|
-
return routeData
|
|
389
|
+
return { route, params }
|
|
385
390
|
}
|
|
386
391
|
}
|
|
387
392
|
|
|
388
|
-
routeCache.set(path, notFound);
|
|
389
393
|
return notFound
|
|
390
394
|
};
|
|
391
395
|
|
package/dist/Ryunix.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Ryunix={})}(this,(function(e){"use strict";let t={containerRoot:{},nextUnitOfWork:{},currentRoot:{},wipRoot:{},deletions:[],wipFiber:{},hookIndex:0};const o=/[A-Z]/g,n=Object.freeze({TEXT_ELEMENT:Symbol("text.element"),Ryunix_ELEMENT:Symbol("ryunix.element"),RYUNIX_EFFECT:Symbol("ryunix.effect"),RYUNIX_MEMO:Symbol("ryunix.memo"),RYUNIX_URL_QUERY:Symbol("ryunix.urlQuery"),RYUNIX_REF:Symbol("ryunix.ref"),RYUNIX_STORE:Symbol("ryunix.store"),RYUNIX_REDUCE:Symbol("ryunix.reduce")}),r=Object.freeze({object:"object",function:"function",style:"ryunix-style",className:"ryunix-class",children:"children",boolean:"boolean",string:"string"}),i=Object.freeze({PLACEMENT:Symbol("ryunix.reconciler.status.placement").toString(),UPDATE:Symbol("ryunix.reconciler.status.update").toString(),DELETION:Symbol("ryunix.reconciler.status.deletion").toString()}),s=(e,t)=>{if(!e||!t||e.length!==t.length)return!1;for(let o=0;o<e.length;o++)if(e[o]!==t[o])return!1;return!0},
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Ryunix={})}(this,(function(e){"use strict";let t={containerRoot:{},nextUnitOfWork:{},currentRoot:{},wipRoot:{},deletions:[],wipFiber:{},hookIndex:0};const o=/[A-Z]/g,n=Object.freeze({TEXT_ELEMENT:Symbol("text.element"),Ryunix_ELEMENT:Symbol("ryunix.element"),RYUNIX_EFFECT:Symbol("ryunix.effect"),RYUNIX_MEMO:Symbol("ryunix.memo"),RYUNIX_URL_QUERY:Symbol("ryunix.urlQuery"),RYUNIX_REF:Symbol("ryunix.ref"),RYUNIX_STORE:Symbol("ryunix.store"),RYUNIX_REDUCE:Symbol("ryunix.reduce")}),r=Object.freeze({object:"object",function:"function",style:"ryunix-style",className:"ryunix-class",children:"children",boolean:"boolean",string:"string"}),i=Object.freeze({PLACEMENT:Symbol("ryunix.reconciler.status.placement").toString(),UPDATE:Symbol("ryunix.reconciler.status.update").toString(),DELETION:Symbol("ryunix.reconciler.status.deletion").toString()}),s=(e,t)=>{if(!e||!t||e.length!==t.length)return!1;for(let o=0;o<e.length;o++)if(e[o]!==t[o])return!1;return!0},l=(e,t=[])=>(null==e||(Array.isArray(e)?e.forEach((e=>l(e,t))):t.push(e)),t),c=(e,t,...o)=>{const i=(e=>{const t={};for(const o in e)t[o]=typeof e[o]===r.function?e[o]():e[o];return t})({...t});i.children=l(o,[]);const s=i.key||`${n.Ryunix_ELEMENT.toString()}-${Math.random().toString(36).substring(2,9)}`;return{type:e,props:{...i,key:s,children:i.children.map((e=>typeof e===r.object?e:p(e)))}}},p=e=>({type:n.TEXT_ELEMENT,props:{nodeValue:e,children:[]}}),a=(e,o)=>{t.wipRoot={dom:o,props:{children:[e]},alternate:t.currentRoot},t.deletions=[],t.nextUnitOfWork=t.wipRoot},u=e=>{const o=t.wipFiber.alternate&&t.wipFiber.alternate.hooks&&t.wipFiber.alternate.hooks[t.hookIndex],n={state:o?o.state:e,queue:o?[...o.queue]:[]};n.queue.forEach((e=>{n.state=typeof e===r.function?e(n.state):e})),n.queue=[];return t.wipFiber&&t.wipFiber.hooks&&(t.wipFiber.hooks.push(n),t.hookIndex++),[n.state,e=>{n.queue.push(e),t.wipRoot={dom:t.currentRoot.dom,props:{...t.currentRoot.props},alternate:t.currentRoot},t.nextUnitOfWork=t.wipRoot,t.deletions=[]}]},f=(e,o)=>{const r=t.wipFiber.alternate?.hooks?.[t.hookIndex],i={type:n.RYUNIX_EFFECT,deps:o,cleanup:r?.cleanup},l=!r||!s(r.deps,o);l&&i.cleanup&&i.cleanup(),l&&(i.cleanup=e()),t.wipFiber.hooks.push(i),t.hookIndex++},d=(e,o)=>{const r=t.wipFiber.alternate?.hooks?.[t.hookIndex],i={type:n.RYUNIX_MEMO,value:r?r.value:e(),deps:o};return r&&s(r.deps,o)||(i.value=e()),t.wipFiber.hooks.push(i),t.hookIndex++,i.value},h=()=>{const e=new URLSearchParams(window.location.search),t={};for(let[o,n]of e.entries())t[o]=n;return t},m=e=>e.startsWith("on"),y=e=>e!==r.children&&!m(e),E=(e,t)=>o=>e[o]!==t[o],b=e=>t=>!(t in e),R=e=>{e.hooks&&e.hooks.filter((e=>e.type===n.RYUNIX_EFFECT&&e.cancel)).forEach((e=>{e.cancel()}))},k=e=>{e.hooks&&e.hooks.filter((e=>e.type===n.RYUNIX_EFFECT&&e.effect)).forEach((e=>{e.cancel=e.effect()}))},w=e=>{const t=e.type===n.TEXT_ELEMENT?document.createTextNode(e.props.nodeValue):document.createElement(e.type);return x(t,{},e.props),t},x=(e,t,o)=>{Object.keys(t).filter(m).filter((e=>b(o)(e)||E(t,o)(e))).forEach((o=>{const n=o.toLowerCase().substring(2);e.removeEventListener(n,t[o])})),Object.keys(o).filter(m).filter(E(t,o)).forEach((t=>{const n=t.toLowerCase().substring(2);e.addEventListener(n,o[t])})),Object.keys(t).filter(y).filter(b(o)).forEach((t=>{e[t]=""})),Object.keys(o).filter(y).filter(E(t,o)).forEach((n=>{n===r.style?_(e,o[n]):n===r.className?o[n]!==t[n]&&(e.className=o[n]):e[n]=o[n]}))},_=(e,t)=>{for(const n in t){const r=n.replace(o,(e=>"-"+e.toLowerCase()));e.style[r]!==t[n]&&(e.style[r]=t[n])}};var g=Object.freeze({__proto__:null,DomStyle:_,createDom:w,updateDom:x});const F=()=>{t.deletions.forEach(T),t.wipRoot&&t.wipRoot.child&&(T(t.wipRoot.child),t.currentRoot=t.wipRoot),t.wipRoot=null},T=e=>{if(!e)return;let t=e.parent;for(;t&&!t.dom;)t=t.parent;const o=t?.dom;if(e.effectTag===i.PLACEMENT&&e.dom)o.appendChild(e.dom),k(e);else if(e.effectTag===i.UPDATE&&e.dom)R(e),x(e.dom,e.alternate.props,e.props),k(e);else if(e.effectTag===i.DELETION)return N(e,o),void R(e);T(e.child),T(e.sibling)},N=(e,t)=>{e.dom?t.removeChild(e.dom):e.child&&N(e.child,t),e.sibling&&N(e.sibling,t)};var I=Object.freeze({__proto__:null,commitDeletion:N,commitRoot:F,commitWork:T});const O=(e,o)=>{let n=0,r=null;const l=new Map;let c=e.alternate?.child;for(;c;){const e=c.props.key||c.type;l.set(e,c),c=c.sibling}for(;n<o.length;){const t=o[n],c=t.props.key||t.type,p=l.get(c);let a;const u=p&&t&&t.type===p.type;u&&s(p.props,t.props)?a=p:u?(a={...p,props:t.props,effectTag:i.UPDATE},l.delete(c)):t&&(a={type:t.type,props:t.props,effectTag:i.PLACEMENT,parent:e}),0===n?e.child=a:r&&(r.sibling=a),r=a,n++}l.forEach((e=>{e.effectTag=i.DELETION,t.deletions.push(e)}))};var U=Object.freeze({__proto__:null,reconcileChildren:O});const v=e=>{t.wipFiber=e,t.hookIndex=0,t.wipFiber.hooks=[];const o=e.type(e.props);O(e,Array.isArray(o)?o:[o])},C=e=>{e.dom||(e.dom=w(e)),O(e,e.props.children)};var L=Object.freeze({__proto__:null,updateFunctionComponent:v,updateHostComponent:C});const S=e=>{let o=!1;for(;t.nextUnitOfWork&&!o;)t.nextUnitOfWork=j(t.nextUnitOfWork),o=e.timeRemaining()<1;!t.nextUnitOfWork&&t.wipRoot&&F(),(t.nextUnitOfWork||t.wipRoot)&&requestIdleCallback(S)};requestIdleCallback(S);const j=e=>{if(e.type instanceof Function?v(e):C(e),e.child)return e.child;let t=e;for(;t;){if(t.sibling)return t.sibling;t=t.parent}};var M={createElement:c,render:a,init:(e,o="__ryunix")=>{t.containerRoot=document.getElementById(o),a(e,t.containerRoot)},Fragment:e=>e.children,Dom:g,Workers:Object.freeze({__proto__:null,performUnitOfWork:j,workLoop:S}),Reconciler:U,Components:L,Commits:I};window.Ryunix=M,e.default=M,e.useCallback=(e,t)=>d((()=>e),t),e.useEffect=f,e.useMemo=d,e.useQuery=h,e.useRef=e=>{const o=t.wipFiber.alternate?.hooks?.[t.hookIndex],r={type:n.RYUNIX_REF,value:o?o.value:{current:e}};return t.wipFiber.hooks.push(r),t.hookIndex++,r.value},e.useRouter=e=>{const[t,o]=u(window.location.pathname),n=(e,t)=>{const o=t.split("?")[0],r=e.find((e=>e.NotFound)),i=r?{route:{component:r.NotFound},params:{}}:{route:{component:null},params:{}};for(const r of e){if(r.subRoutes){const e=n(r.subRoutes,t);if(e)return e}if("*"===r.path)return i;if(!r.path||"string"!=typeof r.path){console.warn("Invalid route detected:",r),console.info("if you are using { NotFound: NotFound } please add { path: '*', NotFound: NotFound }");continue}const e=[],s=new RegExp(`^${r.path.replace(/:\w+/g,(t=>(e.push(t.substring(1)),"([^/]+)")))}$`),l=o.match(s);if(l){return{route:r,params:e.reduce(((e,t,o)=>(e[t]=l[o+1],e)),{})}}}return i},i=e=>{window.history.pushState({},"",e),s(e)},s=e=>{const t=e.split("?")[0];o(t)};f((()=>{const e=()=>s(window.location.pathname);return window.addEventListener("popstate",e),()=>window.removeEventListener("popstate",e)}),[]);const l=n(e,t)||{};return{Children:()=>{const e=h(),{route:t}=l;return t&&t.component&&typeof t.component===r.function?t.component({params:l.params||{},query:e}):(console.error("Component not found for current path or the component is not a valid function:",l),null)},NavLink:({to:e,...t})=>c("a",{href:e,onClick:t=>{t.preventDefault(),i(e)},...t},t.children),navigate:i}},e.useStore=u,Object.defineProperty(e,"__esModule",{value:!0})}));
|