Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

non-destructive renderer #388

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/playground/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Solid Playground</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.jsx"></script>
</body>
</html>
20 changes: 20 additions & 0 deletions apps/playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "playground",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"dom-expressions": "workspace:*",
"s-js": "0.4.9"
},
"devDependencies": {
"babel-plugin-jsx-dom-expressions": "workspace:*",
"vite": "^5.4.10",
"vite-plugin-solid": "^2.10.2"
}
}
1 change: 1 addition & 0 deletions apps/playground/src/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "dom-expressions/src/client.js";
24 changes: 24 additions & 0 deletions apps/playground/src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { render } from "./dom.js";
import * as s from "./rxcore.js";

const count = s.value(0);
const increment = () => count(count() + 1);
const s0 = () => (count() % 2 ? "ok" : [1, 2]);

render(function App() {
return (
<>
<button type="button" onClick={increment}>
{s0()}
</button>
<pre
ref={ref => {
ref.prepend("x");
queueMicrotask(() => ref.append("y"));
}}
>
{s0()}
</pre>
</>
);
}, document.getElementById("root"));
96 changes: 96 additions & 0 deletions apps/playground/src/rxcore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import S from "s-js";
const { root, value, sample } = S;
export { value, sample };

const getOwner = null;
const sharedConfig = {
getNextContextId() {
return this.context.id + this.context.count++;
}
};

function memo(fn) {
const s = value(sample(fn));
S(() => s(fn()));
return s;
}

function createComponent(Comp, props) {
if (Comp.prototype && Comp.prototype.isClassComponent) {
return sample(() => {
const comp = new Comp(props);
return comp.render(props);
});
}
return sample(() => Comp(props));
}

const propTraps = {
get(_, property) {
return _.get(property);
},
has(_, property) {
return _.has(property);
},
set: trueFn,
deleteProperty: trueFn,
getOwnPropertyDescriptor(_, property) {
return {
configurable: true,
enumerable: true,
get() {
return _.get(property);
},
set: trueFn,
deleteProperty: trueFn
};
},
ownKeys(_) {
return _.keys();
}
};

function trueFn() {
return true;
}

function resolveSource(s) {
return (s = typeof s === "function" ? s() : s) == null ? {} : s;
}

function mergeProps(...sources) {
return new Proxy(
{
get(property) {
for (let i = sources.length - 1; i >= 0; i--) {
const v = resolveSource(sources[i])[property];
if (v !== undefined) return v;
}
},
has(property) {
for (let i = sources.length - 1; i >= 0; i--) {
if (property in resolveSource(sources[i])) return true;
}
return false;
},
keys() {
const keys = [];
for (let i = 0; i < sources.length; i++)
keys.push(...Object.keys(resolveSource(sources[i])));
return [...new Set(keys)];
}
},
propTraps
);
}

export {
root,
S as effect,
memo,
createComponent,
getOwner,
sharedConfig,
sample as untrack,
mergeProps
};
42 changes: 42 additions & 0 deletions apps/playground/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { defineConfig } from "vite";
import { transformAsync } from "@babel/core";
import dom from "babel-plugin-jsx-dom-expressions";
export default defineConfig({
plugins: [
{
async transform(code, id) {
if (!/.[tj]sx$/.test(id)) return;
const result = await transformAsync(code, {
plugins: [
[
dom,
{
moduleName: "./dom",
generate: "dom",
hydratable: false,
delegateEvents: true,
delegatedEvents: [],
builtIns: [],
requireImportSource: false,
wrapConditionals: true,
omitNestedClosingTags: false,
contextToCustomElements: false,
staticMarker: "@once",
effectWrapper: "effect",
memoWrapper: "memo",
validate: true
}
]
]
});
return result.code;
}
}
],
esbuild: { jsx: "preserve" },
resolve: {
alias: {
rxcore: "/src/rxcore.js"
}
}
});
27 changes: 20 additions & 7 deletions packages/dom-expressions/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,26 @@ function insertExpression(parent, value, current, marker, unwrapArray) {
} else node = document.createTextNode(value);
current = cleanChildren(parent, current, marker, node);
} else {
if (current !== "" && typeof current === "string") {
current = parent.firstChild.data = value;
} else current = parent.textContent = value;
if (current && Array.isArray(current) && current.length) {
for (let i = current.length - 1; i--;) current[i].remove();
current = current[0];
}
if (current && current.nodeType === 3) {
current.data !== value && (current.data = value)
} else {
current && current.remove();
current = document.createTextNode(value);
parent.appendChild(current);
}
}
} else if (value == null || t === "boolean") {
if (hydrating) return current;
current = cleanChildren(parent, current, marker);
if (multi) {
current = cleanChildren(parent, current, marker);
} else {
current && current.remove()
current = value
}
} else if (t === "function") {
effect(() => {
let v = value();
Expand Down Expand Up @@ -513,8 +526,8 @@ function insertExpression(parent, value, current, marker, unwrapArray) {
appendNodes(parent, array, marker);
} else reconcileArrays(parent, current, array);
} else {
current && cleanChildren(parent);
appendNodes(parent, array);
appendNodes(parent, array, current);
current && current.remove();
}
current = array;
} else if (value.nodeType) {
Expand All @@ -524,7 +537,7 @@ function insertExpression(parent, value, current, marker, unwrapArray) {
cleanChildren(parent, current, null, value);
} else if (current == null || current === "" || !parent.firstChild) {
parent.appendChild(value);
} else parent.replaceChild(value, parent.firstChild);
} else parent.replaceChild(value, current);
current = value;
} else if ("_DX_DEV_") console.warn(`Unrecognized value. Skipped inserting`, value);

Expand Down
Loading
Loading