Comparing version 0.0.9 to 0.0.10
{ | ||
"name": "realdom", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"author": "Fedot Kriutchenko <fodyadev@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "", |
import { r } from './state.js'; | ||
import { argsToArray, isReactive, toString, kebab } from './common.js'; | ||
import { argsToArray, toString, isReactive, kebab } from './common.js'; | ||
const symbol = Symbol(); | ||
const builder = Symbol(); | ||
const metadata = Symbol(); | ||
export const Builder = (effect, tasks=[], names=[]) => new Proxy( | ||
(...args) => args[0] === symbol | ||
(...args) => args[0] === builder | ||
? (args[0] = tasks, effect(...args)) | ||
: Builder(effect, [...tasks, { names, args }], []), { | ||
get: (_, k) => k === symbol | ||
get: (_, k) => typeof k === 'symbol' | ||
? effect[k] | ||
: Builder(effect, tasks, [...names, k]) | ||
}); | ||
Builder.launch = (f, ...a) => f(symbol, ...a); | ||
Builder.launch = (f, ...a) => f(builder, ...a); | ||
const elementBuilder = func => { | ||
const elementCache = {}; | ||
const elementBuilder = (tag, func) => { | ||
const effect = (tasks, node, init) => { | ||
let res; | ||
for (const {names, args} of tasks) res ??= func(args, node, init); | ||
let res; for (const {args} of tasks) res ??= func(args, node, init); | ||
return res; | ||
} | ||
effect[symbol] = 1; | ||
effect[builder] = 1; | ||
return Builder(effect); | ||
} | ||
export const Operator = (func) => { | ||
let op = 0; | ||
const ops = {}; | ||
export const Operator = (defer, func) => { | ||
const effect = (tasks, node, init) => { | ||
for (const {names, args} of tasks) { | ||
const reactive = isReactive(args); | ||
if (init || reactive) r.effect(() => func(node, names, args)); | ||
if (init ? !defer : isReactive(args)) func(node, names, args, init) | ||
} | ||
} | ||
effect[symbol] = 2; | ||
effect[builder] = 2; | ||
return Builder(effect); | ||
} | ||
const textContent = Operator((node, _, args) => { | ||
const textContent = Operator(false, (node, _, args, init) => r.effect(() => { | ||
node.textContent = toString(args); | ||
}); | ||
})); | ||
export const Element = tag => elementBuilder((content, node, init) => { | ||
export const Element = tag => elementBuilder(tag, (content, node, init) => { | ||
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); | ||
@@ -49,16 +50,21 @@ } | ||
const append = item => node.appendChild(Builder.launch(item, null, init)); | ||
const apply = item => Builder.launch(item, node, init); | ||
const hydrate = item => { | ||
const hydrate = (item) => { | ||
const next = child?.nextSibling; | ||
Builder.launch(item, child, init); | ||
child = child.nextSibling; | ||
child = next; | ||
} | ||
const apply = item => Builder.launch(item, node, init); | ||
for (const item of argsToArray(content)) switch (item[symbol]) { | ||
for (const item of argsToArray(content)) switch (item[builder]) { | ||
case 1: (init ? append : hydrate)(item); break; | ||
case 2: apply(item); break; | ||
default: | ||
const text = textContent(item); | ||
if (tag === '' || tag === '!') apply(text); | ||
else if (init) append(Element('')(text)); | ||
else hydrate(text); | ||
if (tag === '' || tag === '!') apply(textContent(item)); | ||
else if (init) { | ||
if (item instanceof Node) {} | ||
else append(Element('')(textContent(item))); | ||
} else { | ||
if (item instanceof Node) child.before(item); | ||
else hydrate(textContent(item)); | ||
} | ||
} | ||
@@ -70,8 +76,8 @@ return node; | ||
let tmpl; | ||
return (...args) => { | ||
return func.component ??= (...args) => { | ||
const elem = func(...args); | ||
return !tmpl | ||
? tmpl = Builder.launch(elem, null) | ||
: Builder.launch(elem, tmpl.cloneNode(true)); | ||
if (elem instanceof Node) return elem; | ||
tmpl ??= Builder.launch(elem, null); | ||
return Builder.launch(elem, tmpl.cloneNode(true)); | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3826
74