+1
-1
| { | ||
| "name": "realdom", | ||
| "version": "0.0.5", | ||
| "version": "0.0.6", | ||
| "author": "Fedot Kriutchenko <fodyadev@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "", |
+30
-23
| import { runWithState, getCurrentHandler } from './state.js'; | ||
| import { argsToArray, toString, kebab } from './common.js'; | ||
| import { argsToArray, isReactive, toString, kebab } from './common.js'; | ||
@@ -32,3 +32,3 @@ const symbol = Symbol(); | ||
| const r = typeof reactive == 'function' ? reactive(args) : reactive; | ||
| if (init || r) func(node, names, args); | ||
| if (init && !r || r) func(node, names, args); | ||
| } | ||
@@ -42,2 +42,6 @@ effect[symbol] = 2; | ||
| const textContent = Operator(true, (node, _, args) => { | ||
| node.textContent = toString(args); | ||
| }); | ||
| const elementBuilder = effect => { | ||
@@ -48,22 +52,25 @@ effect[symbol] = 1; | ||
| export const Element = tag => elementBuilder((_, content, node, init) => { | ||
| init ??= !node; | ||
| node ??= ( | ||
| tag === '' ? document.createTextNode('') : | ||
| document.createElement(tag) | ||
| ); | ||
| const node_ = node; | ||
| if (!node && (init = true)) switch (tag) { | ||
| case '': node = document.createTextNode(''); break; | ||
| case '!': node = document.createComment(''); break; | ||
| case ':': node = document.createDocumentFragment(); break; | ||
| default: node = document.createElement(tag); | ||
| } | ||
| let childNode; | ||
| for (const item of argsToArray(content)) { | ||
| const type = typeof item == 'function' && item[symbol]; | ||
| if (type === 1) { | ||
| if (init) { | ||
| node.appendChild(item(symbol, null, init)); | ||
| } else { | ||
| item(symbol, childNode ??= node.firstChild, init); | ||
| childNode = childNode.nextSibling; | ||
| let child = init ? null : node.firstChild; | ||
| for (const item of argsToArray(content)) switch (item[symbol]) { | ||
| case 1: | ||
| if (init) node.appendChild(item(symbol, null, init)); | ||
| else (item(symbol, child, init), child = child.nextSibling); | ||
| break; | ||
| case 2: item(symbol, node, init); break; | ||
| default: | ||
| if (tag === '' || tag === '!') textContent(item)(symbol, node, init); | ||
| else if (init) | ||
| node.appendChild(Element('')(textContent(item))(symbol, null, init)); | ||
| else { | ||
| textContent(item)(symbol, child, init); | ||
| child = child.nextSibling; | ||
| } | ||
| } | ||
| else if (type === 2) item(symbol, node, init); | ||
| else if (tag === '') node.textContent = toString(item); | ||
| else Element('')(item)(symbol, node, init); | ||
| } | ||
@@ -76,6 +83,6 @@ return node; | ||
| return (...args) => { | ||
| const clone = func(...args)(symbol, tmpl?.cloneNode?.(true)); | ||
| tmpl ??= clone; | ||
| return clone; | ||
| const node = func(...args)(symbol, tmpl ? tmpl.cloneNode(true) : null); | ||
| tmpl ??= node; | ||
| return node; | ||
| } | ||
| } |
3684
12.21%77
8.45%