olova 2.0.9 → 2.0.10
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/hooks/callback.js +5 -3
- package/dist/hooks/context.js +24 -20
- package/dist/hooks/effect.js +19 -10
- package/dist/hooks/memo.js +16 -14
- package/dist/hooks/reducer.js +20 -10
- package/dist/hooks/ref.js +6 -4
- package/dist/hooks/state.js +20 -18
- package/dist/olova.js +1 -1
- package/package.json +1 -1
package/dist/hooks/callback.js
CHANGED
package/dist/hooks/context.js
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
n.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
export function createContextHook(e) {
|
|
2
|
+
return function (n) {
|
|
3
|
+
const s = e.getCurrentInstance(),
|
|
4
|
+
t = s.currentHook++;
|
|
5
|
+
if (!s.hooks[t]) {
|
|
6
|
+
s.hooks[t] = n._currentValue;
|
|
7
|
+
const e = { instance: s, hookIndex: t, version: n._version };
|
|
8
|
+
n.subscribers.set(s, e),
|
|
9
|
+
s.contextSubscriptions.add(() => {
|
|
10
|
+
n.subscribers.delete(s);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
const r = n.subscribers.get(s);
|
|
14
|
+
return (
|
|
15
|
+
r &&
|
|
16
|
+
r.version !== n._version &&
|
|
17
|
+
((s.hooks[t] = n._currentValue),
|
|
18
|
+
(r.version = n._version),
|
|
19
|
+
(e.dirtyInstances = e.dirtyInstances || new Set()),
|
|
20
|
+
e.dirtyInstances.add(s),
|
|
21
|
+
e.scheduleUpdate()),
|
|
22
|
+
n._currentValue
|
|
23
|
+
);
|
|
24
|
+
};
|
|
21
25
|
}
|
package/dist/hooks/effect.js
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
export function createEffectHook(n) {
|
|
2
|
+
return function (e, t) {
|
|
3
|
+
const o = n.getCurrentInstance(),
|
|
4
|
+
u = o.currentHook++;
|
|
5
|
+
o.hooks[u] ||
|
|
6
|
+
(o.hooks[u] = { deps: null, cleanup: null, effect: e, lastRun: 0 });
|
|
7
|
+
const c = o.hooks[u];
|
|
8
|
+
(t
|
|
9
|
+
? c.deps &&
|
|
10
|
+
n.areArraysEqual(
|
|
11
|
+
t.map((n) => ("function" == typeof n ? n() : n)),
|
|
12
|
+
c.deps
|
|
13
|
+
)
|
|
14
|
+
: 0 !== c.lastRun) ||
|
|
15
|
+
("function" == typeof c.cleanup && c.cleanup(),
|
|
16
|
+
(c.cleanup = e()),
|
|
17
|
+
(c.deps = t?.map((n) => ("function" == typeof n ? n() : n))),
|
|
18
|
+
(c.lastRun = Date.now()));
|
|
19
|
+
};
|
|
11
20
|
}
|
package/dist/hooks/memo.js
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
export function createMemoHook(o) {
|
|
2
|
+
return function (n, t) {
|
|
3
|
+
const e = o.getCurrentInstance(),
|
|
4
|
+
r = e.currentHook++,
|
|
5
|
+
[u, c] = e.hooks[r] || [void 0, void 0];
|
|
6
|
+
if (
|
|
7
|
+
!c ||
|
|
8
|
+
!t ||
|
|
9
|
+
t.length !== c.length ||
|
|
10
|
+
t.some((n, t) => !o.shallowEqual(n, c[t]))
|
|
11
|
+
) {
|
|
12
|
+
const o = n();
|
|
13
|
+
return (e.hooks[r] = [o, t]), o;
|
|
14
|
+
}
|
|
15
|
+
return u;
|
|
16
|
+
};
|
|
15
17
|
}
|
package/dist/hooks/reducer.js
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
export function createReducerHook(t) {
|
|
2
|
+
return function (s, e, o) {
|
|
3
|
+
const n = t.getCurrentInstance(),
|
|
4
|
+
c = n.currentHook++;
|
|
5
|
+
if (!n.hooks[c]) {
|
|
6
|
+
const a = o ? o(e) : e;
|
|
7
|
+
n.hooks[c] = {
|
|
8
|
+
state: a,
|
|
9
|
+
dispatch: (e) => {
|
|
10
|
+
const o = s(n.hooks[c].state, e);
|
|
11
|
+
o !== n.hooks[c].state &&
|
|
12
|
+
((n.hooks[c].state = o),
|
|
13
|
+
t.dirtyInstances || (t.dirtyInstances = new Set()),
|
|
14
|
+
t.dirtyInstances.add(n),
|
|
15
|
+
t.scheduleUpdate());
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
return [n.hooks[c].state, n.hooks[c].dispatch];
|
|
20
|
+
};
|
|
11
21
|
}
|
package/dist/hooks/ref.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
r =
|
|
4
|
-
|
|
1
|
+
export function createRefHook(o) {
|
|
2
|
+
return function (n) {
|
|
3
|
+
const r = o.getCurrentInstance(),
|
|
4
|
+
t = r.currentHook++;
|
|
5
|
+
return r.hooks[t] || (r.hooks[t] = { current: n }), r.hooks[t];
|
|
6
|
+
};
|
|
5
7
|
}
|
package/dist/hooks/state.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
export function createStateHook(e) {
|
|
2
|
+
return function (t) {
|
|
3
|
+
const o = e.getCurrentInstance(),
|
|
4
|
+
n = o.currentHook++;
|
|
5
|
+
return (
|
|
6
|
+
o.hooks[n] ||
|
|
7
|
+
(o.hooks[n] = {
|
|
8
|
+
value: t,
|
|
9
|
+
setValue: (t) => {
|
|
10
|
+
const s = "function" == typeof t ? t(o.hooks[n].value) : t;
|
|
11
|
+
s !== o.hooks[n].value &&
|
|
12
|
+
((o.hooks[n].value = s),
|
|
13
|
+
e.dirtyInstances || (e.dirtyInstances = new Set()),
|
|
14
|
+
e.dirtyInstances.add(o),
|
|
15
|
+
e.scheduleUpdate());
|
|
16
|
+
},
|
|
17
|
+
}),
|
|
18
|
+
[() => o.hooks[n].value, o.hooks[n].setValue]
|
|
19
|
+
);
|
|
20
|
+
};
|
|
19
21
|
}
|
package/dist/olova.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import{createStateHook}from"./hooks/state";import{createEffectHook}from"./hooks/effect";import{createMemoHook}from"./hooks/memo";import{createRefHook}from"./hooks/ref";import{createCallbackHook}from"./hooks/callback";import{createContextHook}from"./hooks/context";class ReactLite{constructor(){this.rootElement=null,this.components=new Map,this.componentStack=[],this.componentInstances=new Map,this.pendingUpdates=[],this.pendingEffects=[],this.contextSubscriptions=new WeakMap,this.isBatchingUpdates=!1,this.hasScheduledFlush=!1,this.dirtyInstances=null,this.mountedComponents=new Map,this.$state=createStateHook(this),this.$effect=createEffectHook(this),this.$memo=createMemoHook(this),this.$ref=createRefHook(this),this.$callback=createCallbackHook(this),this.$context=createContextHook(this)}createElement(e,t,...n){if(null==e)return console.error("Element type cannot be null or undefined"),null;const o=n.flat(),r=[];return o.forEach((e=>{"string"==typeof e||"number"==typeof e?r.length>0&&("string"==typeof r[r.length-1]||"number"==typeof r[r.length-1])?r[r.length-1]=String(r[r.length-1])+String(e):r.push(e):null!=e&&r.push(e)})),e===Fragment?{type:Fragment,props:{...t,children:r}}:{type:e,props:{...t,children:r}}}render(e,t){try{if(null==e)return;if("string"==typeof e||"number"==typeof e)return void t.appendChild(document.createTextNode(e));if(Array.isArray(e))return void e.forEach((e=>this.render(e,t)));if(!e.type)return void console.error("Invalid element:",e);if(e.type===Fragment)return void(e.props?.children||[]).forEach((e=>this.render(e,t)));if("function"==typeof e.type){const n=e.type,o=this.getComponentInstance(n);this.componentStack.push(o),o.currentHook=0,o.lastProps=e.props;const r=n(e.props);return o.lastResult=r,this.componentStack.pop(),void this.render(r,t)}if("string"==typeof e.type){const n=document.createElement(e.type);return this.applyProps(n,e.props),(e.props?.children||[]).forEach((e=>this.render(e,n))),void t.appendChild(n)}console.error("Unhandled element type:",e.type)}catch(t){console.error("Render error:",t),console.error("Element:",e)}}applyProps(e,t){Object.keys(t||{}).forEach((n=>{if("ref"===n&&t[n])t[n].current=e;else if(n.startsWith("on")){const o=n.toLowerCase().substring(2);e.addEventListener(o,t[n])}else"children"!==n&&("className"===n?e.setAttribute("class",t[n]):e[n]=t[n])}))}getComponentInstance(e){return this.componentInstances.has(e)||this.componentInstances.set(e,{hooks:[],currentHook:0,effects:new Map,cleanups:new Set,pendingEffects:[],contextSubscriptions:new Set,lastProps:null,lastResult:null}),this.componentInstances.get(e)}renderComponent(e,t){const n=this.getComponentInstance(e);this.componentStack.push(n),n.currentHook=0;const o=e();n.lastResult=o,this.render(o,t),this.componentStack.pop()}scheduleUpdate(){this.isBatchingUpdates||(this.isBatchingUpdates=!0,queueMicrotask((()=>{const e=new Set(this.dirtyInstances);this.dirtyInstances=new Set,e.forEach((e=>{const t=Array.from(this.components.keys()).find((t=>this.componentInstances.get(t)===e));if(t){this.componentStack.push(e),e.currentHook=0;const n=t(),o=e.lastResult;this.areNodesEqual(n,o)||(e.lastResult=n,this.rootElement&&this.updateDOM(this.rootElement,o,n)),this.componentStack.pop()}})),this.isBatchingUpdates=!1})))}updateDOM(e,t,n){if(this.areNodesEqual(t,n))return;const o=(e,t,n,r=0)=>{if("string"==typeof t||"number"==typeof t){const n=e.childNodes[r];return void(n?3===n.nodeType?n.textContent!==String(t)&&(n.textContent=String(t)):e.replaceChild(document.createTextNode(String(t)),n):e.appendChild(document.createTextNode(String(t))))}if(n&&this.areNodesEqual(n,t))return;if(!t)return void(e.childNodes[r]&&e.removeChild(e.childNodes[r]));if(!n||n.type!==t.type){const n=this.createElementFromVNode(t);return void(e.childNodes[r]?e.replaceChild(n,e.childNodes[r]):e.appendChild(n))}const s=e.childNodes[r];"string"==typeof t.type&&this.updateProps(s,n.props||{},t.props||{});const i=Array.isArray(t.props?.children)?t.props.children:[t.props?.children].filter(Boolean),c=Array.isArray(n.props?.children)?n.props.children:[n.props?.children].filter(Boolean),a=Math.max(i.length,c.length);for(let e=0;e<a;e++)o(s,i[e],c[e],e)};o(e,n,t)}updateProps(e,t,n){Object.keys(t).forEach((o=>{if("children"!==o&&!(o in n))if(o.startsWith("on")){const n=o.toLowerCase().substring(2);e.removeEventListener(n,t[o])}else"className"===o?e.removeAttribute("class"):e.removeAttribute(o)})),Object.keys(n).forEach((o=>{if("children"!==o&&n[o]!==t[o])if(o.startsWith("on")){const r=o.toLowerCase().substring(2);t[o]&&e.removeEventListener(r,t[o]),e.addEventListener(r,n[o])}else"className"===o?e.setAttribute("class",n[o]):o in e?e[o]=n[o]:e.setAttribute(o,n[o])}))}flushUpdates(){for(;this.pendingUpdates.length>0;){this.pendingUpdates.shift()()}this.hasScheduledFlush=!1}mount(e,t){this.rootElement=t,this.components.set(e,e),this.mountedComponents.has(t)&&this.unmount(t),t.innerHTML="",this.mountedComponents.set(t,new Set),this.renderComponent(e,t)}unmount(e){console.log("Unmounting component...");Array.from(this.componentInstances.values()).forEach((e=>{e.cleanups&&(e.cleanups.forEach((e=>{try{console.log("Running cleanup on unmount"),e()}catch(e){console.error("Error in unmount cleanup:",e)}})),e.cleanups.clear())})),this.componentInstances.clear(),e.innerHTML=""}createElementFromVNode(e){if("string"==typeof e||"number"==typeof e)return document.createTextNode(e);if(e.type===Fragment){const t=document.createDocumentFragment();return(e.props&&e.props.children?e.props.children:[]).forEach((e=>{t.appendChild(this.createElementFromVNode(e))})),t}if("string"==typeof e.type){const t=document.createElement(e.type);if(e.props){Object.keys(e.props).forEach((n=>{if("children"!==n)if("className"===n)t.setAttribute("class",e.props[n]);else if(n.startsWith("on")){const o=n.toLowerCase().substring(2);t.addEventListener(o,e.props[n])}else t[n]=e.props[n]}));(e.props.children||[]).forEach((e=>{t.appendChild(this.createElementFromVNode(e))}))}return t}return document.createTextNode("")}createContext(e){const t={currentValue:e,defaultValue:e,subscribers:new Map,version:0,Provider:({value:e,children:n})=>{const o=this.getCurrentInstance(),r=o.currentHook++,s=o.hooks[r];return this.shallowEqual(s,e)||(t._currentValue=e,t._version++,t.subscribers.forEach((t=>{t.instance.hooks[t.hookIndex]=e,this.dirtyInstances=this.dirtyInstances||new Set,this.dirtyInstances.add(t.instance),this.scheduleUpdate()}))),o.hooks[r]=e,n},Consumer:({children:e})=>{if("function"!=typeof e)throw new Error("Context.Consumer expects a function as a child");return e(t._currentValue)}};return t}memo(e){const t=t=>{const n=this.getCurrentInstance();if(!n)return e(t);const o=n.currentHook++,r=n.hooks[o]||{props:null,result:null},s=!r.props||!this.deepEqual(t,r.props);if(!r.result||s){const r=e(t);return n.hooks[o]={props:t,result:r},r}return r.result};return t.__isMemoized=!0,t.__original=e,t}shallowEqual(e,t){if(e===t)return!0;if(!e||!t)return!1;if("object"!=typeof e||"object"!=typeof t)return e===t;const n=Object.keys(e),o=Object.keys(t);return n.length===o.length&&n.every((n=>t.hasOwnProperty(n)&&e[n]===t[n]))}deepEqual(e,t){if(e===t)return!0;if(typeof e!=typeof t)return!1;if("object"!=typeof e||null===e||null===t)return!1;const n=Object.keys(e),o=Object.keys(t);return n.length===o.length&&n.every((n=>this.deepEqual(e[n],t[n])))}areArraysEqual(e,t){return e===t||!(!e||!t)&&(e.length===t.length&&e.every(((e,n)=>e===t[n])))}areNodesEqual(e,t){return e===t||!(!e||!t)&&(typeof e==typeof t&&("string"==typeof e||"number"==typeof e?e===t:e.type===t.type&&this.areObjectsEqual(e.props,t.props)))}areObjectsEqual(e,t){if(e===t)return!0;if(!e||!t)return!1;const n=Object.keys(e),o=Object.keys(t);return n.length===o.length&&n.every((n=>"children"===n?this.areChildrenEqual(e[n],t[n]):e[n]===t[n]))}areChildrenEqual(e,t){if(e===t)return!0;if(!e||!t)return!1;const n=Array.isArray(e)?e:[e],o=Array.isArray(t)?t:[t];return n.length===o.length&&n.every(((e,t)=>this.areNodesEqual(e,o[t])))}getCurrentInstance(){const e=this.componentStack[this.componentStack.length-1];if(!e)throw new Error("Hooks can only be called inside function components");return e}}const Olova=new ReactLite;export const h=Olova.createElement.bind(Olova);export const Fragment=Symbol("Fragment");export const $state=Olova.$state.bind(Olova);export const $effect=Olova.$effect.bind(Olova);export const $ref=Olova.$ref.bind(Olova);export const $memo=Olova.$memo.bind(Olova);export const $callback=Olova.$callback.bind(Olova);export const $context=Olova.$context.bind(Olova);export const createContext=Olova.createContext.bind(Olova);export const memo=Olova.memo.bind(Olova);export default Olova;function isFunctionComponent(e){return"function"==typeof e}function renderComponent(e,t){try{return"function"==typeof e.type?e.type(e.props):"string"==typeof e?e:Array.isArray(e)?e.map((e=>renderComponent(e,t))):null==e?"":"object"==typeof e?e:String(e)}catch(e){return console.error("Error rendering component:",e),null}}
|