Skip to content

Commit

Permalink
playground
Browse files Browse the repository at this point in the history
  • Loading branch information
leonvisibee committed Dec 8, 2024
1 parent ec513ce commit 42febf6
Show file tree
Hide file tree
Showing 8 changed files with 1,050 additions and 212 deletions.
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"
}
}
});
Loading

0 comments on commit 42febf6

Please sign in to comment.