olova 2.0.9 → 2.0.11

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.
@@ -1,18 +1,18 @@
1
- name: Publish to NPM
2
-
3
- on:
4
- push:
5
- branches: [ main ]
6
-
7
- jobs:
8
- publish:
9
- runs-on: ubuntu-latest
10
- steps:
11
- - uses: actions/checkout@v2
12
- - uses: actions/setup-node@v2
13
- with:
14
- node-version: '14'
15
- registry-url: 'https://registry.npmjs.org'
16
- - run: yarn publish
17
- env:
18
- NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
1
+ name: Publish to NPM
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+
7
+ jobs:
8
+ publish:
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v2
12
+ - uses: actions/setup-node@v2
13
+ with:
14
+ node-version: '14'
15
+ registry-url: 'https://registry.npmjs.org'
16
+ - run: yarn publish
17
+ env:
18
+ NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
package/README.md CHANGED
@@ -1,54 +1,54 @@
1
- # Olova JavaScript Framework
2
-
3
- Olova is a lightweight and reactive JavaScript framework that simplifies UI
4
- development with a clean, intuitive syntax. It provides a reactivity system and
5
- hooks to manage state and side effects, allowing developers to build modern web
6
- applications with ease.
7
-
8
- ## Features
9
-
10
- - **State Management**: Use the `State` hook to manage reactive state in your
11
- components.
12
- - **Side Effects**: Use the `Effect` hook to run side effects in response to
13
- state changes.
14
- - **JSX-style Syntax**: Write UI components using a simple, declarative
15
- JSX-style syntax.
16
- - **Reactivity**: Automatically re-render components when state changes.
17
-
18
- ## Installation
19
-
20
- To get started with Olova, first install the core library via npm or yarn.
21
-
22
- ```bash
23
- npm install olova
24
- ```
25
-
26
- or
27
-
28
- ```bash
29
- yarn add olova
30
- ```
31
-
32
- ## Example Usage
33
-
34
- Here is an example of a basic component in Olova:
35
-
36
- ```js
37
- import Olova, { State, Effect } from "olova";
38
-
39
- export default function Home() {
40
- const [count, setCount] = State(0);
41
-
42
- Effect(() => {
43
- console.log("Home is rendered");
44
- console.log(count);
45
- }, [count]);
46
-
47
- return (
48
- <>
49
- <h1>{count}</h1>
50
- <button onClick={() => setCount(count + 1)}>Increment</button>
51
- </>
52
- );
53
- }
54
- ```
1
+ # Olova JavaScript Framework
2
+
3
+ Olova is a lightweight and reactive JavaScript framework that simplifies UI
4
+ development with a clean, intuitive syntax. It provides a reactivity system and
5
+ hooks to manage state and side effects, allowing developers to build modern web
6
+ applications with ease.
7
+
8
+ ## Features
9
+
10
+ - **State Management**: Use the `State` hook to manage reactive state in your
11
+ components.
12
+ - **Side Effects**: Use the `Effect` hook to run side effects in response to
13
+ state changes.
14
+ - **JSX-style Syntax**: Write UI components using a simple, declarative
15
+ JSX-style syntax.
16
+ - **Reactivity**: Automatically re-render components when state changes.
17
+
18
+ ## Installation
19
+
20
+ To get started with Olova, first install the core library via npm or yarn.
21
+
22
+ ```bash
23
+ npm install olova
24
+ ```
25
+
26
+ or
27
+
28
+ ```bash
29
+ yarn add olova
30
+ ```
31
+
32
+ ## Example Usage
33
+
34
+ Here is an example of a basic component in Olova:
35
+
36
+ ```js
37
+ import Olova, { State, Effect } from "olova";
38
+
39
+ export default function Home() {
40
+ const [count, setCount] = State(0);
41
+
42
+ Effect(() => {
43
+ console.log("Home is rendered");
44
+ console.log(count);
45
+ }, [count]);
46
+
47
+ return (
48
+ <>
49
+ <h1>{count}</h1>
50
+ <button onClick={() => setCount(count + 1)}>Increment</button>
51
+ </>
52
+ );
53
+ }
54
+ ```
@@ -0,0 +1,14 @@
1
+ import { currentEffect, setCurrentEffect } from "./createSignal";
2
+ function createEffect(t) {
3
+ const e = () => {
4
+ f(), setCurrentEffect(e);
5
+ try {
6
+ f = t() || (() => {});
7
+ } finally {
8
+ setCurrentEffect(null);
9
+ }
10
+ };
11
+ let f = () => {};
12
+ e();
13
+ }
14
+ export { createEffect };
@@ -0,0 +1,21 @@
1
+ import { $state, $effect } from "../olova.js";
2
+ function createMemo(t, e = []) {
3
+ const [o, s] = $state(void 0),
4
+ [c, a] = $state(e),
5
+ [i, r] = $state(void 0);
6
+ return (
7
+ $effect(() => {
8
+ if (
9
+ void 0 === i ||
10
+ ((f = c),
11
+ (o = e).length !== f.length || o.some((t, e) => !Object.is(t, f[e])))
12
+ ) {
13
+ const o = t();
14
+ Object.is(o, i) || (r(o), s(o)), a(e);
15
+ }
16
+ var o, f;
17
+ }),
18
+ o
19
+ );
20
+ }
21
+ export { createMemo };
@@ -0,0 +1,13 @@
1
+ import { createSignal } from "./createSignal";
2
+ function createRef(e = null) {
3
+ const [r, t] = createSignal({ current: e });
4
+ return {
5
+ get current() {
6
+ return r().current;
7
+ },
8
+ set current(e) {
9
+ t({ current: e });
10
+ },
11
+ };
12
+ }
13
+ export { createRef };
@@ -0,0 +1,13 @@
1
+ let currentEffect = null;
2
+ const setCurrentEffect = (t) => {
3
+ currentEffect = t;
4
+ };
5
+ function createSignal(t) {
6
+ let e = t;
7
+ const r = new Set();
8
+ return [
9
+ () => (currentEffect && r.add(currentEffect), e),
10
+ (t) => ((e = "function" == typeof t ? t(e) : t), r.forEach((t) => t()), e),
11
+ ];
12
+ }
13
+ export { createSignal, currentEffect, setCurrentEffect };
package/dist/olova.d.ts CHANGED
@@ -1 +1 @@
1
- declare module "olova";
1
+ declare module "olova";
package/dist/olova.js CHANGED
@@ -1 +1 @@
1
- import stateHook from"./hooks/state.js";import effectHook from"./hooks/effect.js";import memoHook from"./hooks/memo.js";import callbackHook from"./hooks/callback.js";import reducerHook from"./hooks/reducer.js";import refHook from"./hooks/ref.js";import contextHook from"./hooks/context.js";class Olovav2{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}createElement(e,t,...n){return null==e?(console.error("Element type cannot be null or undefined"),null):"function"==typeof e||"string"==typeof e?{type:e,props:{...t,children:n.flat()}}:(console.error("Invalid element type:",e),null)}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("function"==typeof e.type){const n=e.type,o=this.getComponentInstance(n);if(n.__isMemoized){const n=o.lastProps;if(n&&this.deepEqual(n,e.props)&&o.lastResult)return void this.render(o.lastResult,t)}this.componentStack.push(o),o.currentHook=0,o.lastProps=e.props;const s=n(e.props);return o.lastResult=s,this.componentStack.pop(),void this.render(s,t)}if("string"!=typeof e.type)return void console.error("Invalid element type:",e.type);const n=document.createElement(e.type);this.applyProps(n,e.props),(e.props.children||[]).forEach((e=>this.render(e,n))),t.appendChild(n)}catch(e){console.error("Render error:",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:[],cleanups:new Map,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;if(!n.lastResult||this.shouldComponentUpdate(n,e)){const o=e();n.lastResult=o,this.render(o,t),n.pendingEffects.length>0&&(this.pendingEffects.push(...n.pendingEffects),n.pendingEffects=[])}else this.render(n.lastResult,t);this.componentStack.pop()}shouldComponentUpdate(e,t){return!t.__isMemoized||!e.lastProps||!this.deepEqual(e.lastProps,t.props)}getCurrentInstance(){return this.componentStack[this.componentStack.length-1]}createContext(e){const t={_currentValue:e,_defaultValue:e,_subscribers:new Set,_version:0,Provider:({value:e,children:n})=>{const o=this.getCurrentInstance(),s=o.currentHook++,r=o.hooks[s];return this.shallowEqual(r,e)||(t._currentValue=e,t._version++,t._subscribers.forEach((t=>{t.instance.hooks[t.hookIndex]=e,this.scheduleUpdate()}))),o.hooks[s]=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}scheduleUpdate(){this.isBatchingUpdates||(this.isBatchingUpdates=!0,this.pendingUpdates.push((()=>{if(this.rootElement){const e=this.dirtyInstances||new Set;for(this.components.forEach(((t,n)=>{const o=this.getComponentInstance(t);e.has(o)&&(n.innerHTML="",this.renderComponent(t,n))})),this.dirtyInstances=new Set;this.pendingEffects.length>0;){this.pendingEffects.shift()()}}})),this.hasScheduledFlush||(this.hasScheduledFlush=!0,queueMicrotask((()=>{this.flushUpdates(),this.isBatchingUpdates=!1}))))}flushUpdates(){for(;this.pendingUpdates.length>0;){this.pendingUpdates.shift()()}this.hasScheduledFlush=!1}mount(e,t){for(this.rootElement=t,this.components.set(t,e),this.renderComponent(e,t);this.pendingEffects.length>0;){this.pendingEffects.shift()()}}unmount(e){const t=this.components.get(e);if(t){const n=this.componentInstances.get(t);n&&(n.contextSubscriptions.forEach((e=>e())),n.contextSubscriptions.clear(),n.cleanups.forEach((e=>e())),n.cleanups.clear(),this.componentInstances.delete(t)),this.components.delete(e)}e.innerHTML=""}memo(e){const t=t=>{const n=this.getCurrentInstance();if(!n)return e(t);const o=n.currentHook++,s=n.hooks[o]||{props:null,result:null},r=!s.props||!this.deepEqual(t,s.props);if(!s.result||r){const s=e(t);return n.hooks[o]={props:t,result:s},s}return s.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(!e||!t)return!1;if(typeof e!=typeof t)return!1;if("object"!=typeof e)return e===t;if(Array.isArray(e))return!(!Array.isArray(t)||e.length!==t.length)&&e.every(((e,n)=>this.deepEqual(e,t[n])));const n=Object.keys(e),o=Object.keys(t);return n.length===o.length&&n.every((n=>t.hasOwnProperty(n)&&this.deepEqual(e[n],t[n])))}}const Olova=new Olovav2;export const h=Olova.createElement.bind(Olova);export const Fragment=e=>e?e.children:null;export const $state=e=>stateHook(Olova,e);export const $effect=(e,t)=>effectHook(Olova,e,t);export const $memo=(e,t)=>memoHook(Olova,e,t);export const $callback=(e,t)=>callbackHook(Olova,e,t);export const $reducer=(e,t)=>reducerHook(Olova,e,t);export const $ref=e=>refHook(Olova,e);export const $context=e=>contextHook(Olova,e);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}}
1
+ import{createSignal as $state}from"./hooks/createSignal";import{createEffect as $effect}from"./hooks/createEffect";import{createMemo as $memo}from"./hooks/createMemo";import{createRef as $ref}from"./hooks/createRef";function h(e,t={},...n){if(e===h.Fragment){const e=document.createDocumentFragment(),t=n.flat();for(let n=0;n<t.length;n++){const o=t[n];if("function"==typeof o){const t=document.createTextNode("");$effect((()=>{const n=o();if(Array.isArray(n)){const o=t.previousSibling;o&&o.remove();const r=document.createDocumentFragment();for(let e=0;e<n.length;e++){const t=n[e];r.appendChild(t instanceof Node?t:document.createTextNode(String(t)))}e.insertBefore(r,t)}else t.textContent=String(n)})),e.appendChild(t)}else o instanceof Node?e.appendChild(o):null!=o&&e.appendChild(document.createTextNode(String(o)))}return e}const o="function"==typeof e?e(t):document.createElement(e);if("function"!=typeof e){if(t){const e=Object.entries(t);for(let t=0;t<e.length;t++){const[n,r]=e[t];"ref"===n?queueMicrotask((()=>{r&&"object"==typeof r&&(r.current=o)})):n.startsWith("on")?o.addEventListener(n.slice(2).toLowerCase(),r):"style"===n&&"object"==typeof r?Object.assign(o.style,r):"className"===n?o.className=r:o.setAttribute(n,r)}}const e=document.createTextNode(""),r=n.flat();for(let t=0;t<r.length;t++){const n=r[t];if("function"==typeof n)$effect((()=>{const t=n();if(Array.isArray(t)){o.textContent="";const e=document.createDocumentFragment();for(let n=0;n<t.length;n++){const o=t[n];e.appendChild(o instanceof Node?o:document.createTextNode(String(o)))}o.appendChild(e)}else e.textContent=String(t),e.parentNode||o.appendChild(e)}));else if(Array.isArray(n)){const e=document.createDocumentFragment();for(let t=0;t<n.length;t++){const o=n[t];o instanceof Node?e.appendChild(o):null!=o&&e.appendChild(document.createTextNode(String(o)))}o.appendChild(e)}else n instanceof Node?o.appendChild(n):null!=n&&o.appendChild(document.createTextNode(String(n)))}}return o}h.Fragment=Symbol("Fragment");const Fragment=h.Fragment;export{h,$state,$effect,$memo,Fragment,$ref};
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
- {
2
- "name": "olova",
3
- "version": "2.0.9",
4
- "description": "A lightweight JavaScript framework for building reactive applications.",
5
- "main": "dist/olova.js",
6
- "keywords": [
7
- "javascript",
8
- "framework",
9
- "reactive",
10
- "olova",
11
- "olovajs"
12
- ],
13
- "author": "Nazmul Hossain",
14
- "license": "MIT"
15
- }
1
+ {
2
+ "name": "olova",
3
+ "version": "2.0.11",
4
+ "description": "A lightweight JavaScript framework for building reactive applications.",
5
+ "main": "dist/olova.js",
6
+ "keywords": [
7
+ "javascript",
8
+ "framework",
9
+ "reactive",
10
+ "olova",
11
+ "olovajs"
12
+ ],
13
+ "author": "Nazmul Hossain",
14
+ "license": "MIT"
15
+ }
@@ -1,4 +0,0 @@
1
- import o from "./memo.js";
2
- export default function r(r, t, e) {
3
- return o(r, () => t, e);
4
- }
@@ -1,21 +0,0 @@
1
- export default function e(e, r) {
2
- const n = e.getCurrentInstance(),
3
- o = n.currentHook++;
4
- if (!n.hooks[o]) {
5
- n.hooks[o] = r._currentValue;
6
- const e = { instance: n, hookIndex: o, version: r._version };
7
- r._subscribers.add(e),
8
- n.contextSubscriptions.add(() => {
9
- r._subscribers.delete(e);
10
- });
11
- }
12
- const s = Array.from(r._subscribers).find(
13
- (e) => e.instance === n && e.hookIndex === o
14
- );
15
- return (
16
- s &&
17
- s.version !== r._version &&
18
- ((n.hooks[o] = r._currentValue), (s.version = r._version)),
19
- r._currentValue
20
- );
21
- }
@@ -1,11 +0,0 @@
1
- export default function e(e, n, t) {
2
- const o = e.getCurrentInstance(),
3
- s = o.currentHook++,
4
- c = o.hooks[s];
5
- (!c || !t || t.length !== c.length || t.some((e, n) => e !== c[n])) &&
6
- o.pendingEffects.push(() => {
7
- o.cleanups.has(s) && o.cleanups.get(s)();
8
- const e = n();
9
- (o.hooks[s] = t), "function" == typeof e && o.cleanups.set(s, e);
10
- });
11
- }
@@ -1,15 +0,0 @@
1
- export default function o(o, t, n) {
2
- const e = o.getCurrentInstance(),
3
- r = e.currentHook++,
4
- [s, u] = e.hooks[r] || [void 0, void 0];
5
- if (
6
- !u ||
7
- !n ||
8
- n.length !== u.length ||
9
- n.some((t, n) => !o.shallowEqual(t, u[n]))
10
- ) {
11
- const o = t();
12
- return (e.hooks[r] = [o, n]), o;
13
- }
14
- return s;
15
- }
@@ -1,11 +0,0 @@
1
- export default function o(o, e, t) {
2
- const n = o.getCurrentInstance(),
3
- s = n.currentHook++;
4
- n.hooks[s] = n.hooks[s] || t;
5
- return [
6
- n.hooks[s],
7
- (t) => {
8
- (n.hooks[s] = e(n.hooks[s], t)), o.scheduleUpdate();
9
- },
10
- ];
11
- }
package/dist/hooks/ref.js DELETED
@@ -1,5 +0,0 @@
1
- export default function o(o, t) {
2
- const n = o.getCurrentInstance(),
3
- r = n.currentHook++;
4
- return n.hooks[r] || (n.hooks[r] = { current: t }), n.hooks[r];
5
- }
@@ -1,19 +0,0 @@
1
- export default function e(e, o) {
2
- const t = e.getCurrentInstance(),
3
- s = t.currentHook++;
4
- return (
5
- t.hooks[s] ||
6
- (t.hooks[s] = {
7
- value: o,
8
- setValue: (o) => {
9
- const n = "function" == typeof o ? o(t.hooks[s].value) : o;
10
- n !== t.hooks[s].value &&
11
- ((t.hooks[s].value = n),
12
- (e.dirtyInstances = e.dirtyInstances || new Set()),
13
- e.dirtyInstances.add(t),
14
- e.scheduleUpdate());
15
- },
16
- }),
17
- [t.hooks[s].value, t.hooks[s].setValue]
18
- );
19
- }