hi
- User
hi
- Assistant
Hello! How can I assist you today?
- User
what styles should i set for document body for a react ap?
- Assistant
- User
ive made my own react. thoughts on this? import { FC, Render, Root, Signal, CSS, HTML } from "../lib/index";
const counterCSS = CSS({ width: "10rem", height: "10rem", backgroundColor: "rebeccapurple", display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center", });
const Counter: FC<{}, HTMLElement> = () => { const count = Signal(0);
return Render( () => HTML( "main", counterCSS, HTML("p", count.ref()), HTML("button", "+", { onpointerdown: () => count.set((x) => x + 1), }), ), [count], ); };
Root(document.body, () => Counter({}));
- Assistant
- User
its a pure function. the whole thing lives in a scope import { Observable, Executable } from "../Kinds/Kinds";
type Noop = null | undefined | void;
type Hooks = | Observable<unknown, void, unknown> | Executable<E | void, unknown> | Noop;
const Render = (thunk: () => E, hooks: Hooks[]): E | never => { let res: E;
try { res = thunk(); } catch (err) { console.error(err); return void 0 as never; }
const l = hooks.length; const executables: Executable<E | void, unknown>[] = [];
const RUN_EFFECTS = (res: E) => { try { const l = executables.length; for (let i = 0; i < l; i++) executables[i]?.ex(res); } catch (err) { console.error(err); } };
const RENDER = () => { try { const next = thunk(); if (next instanceof Node && res instanceof Node && res.parentNode) { res.parentNode.replaceChild(next, res); } res = next; queueMicrotask(() => RUN_EFFECTS(res)); } catch (err) { console.error(err); } };
for (let i = 0; i < l; i++) { const h = hooks[i]!; if ("sub" in h && typeof h.sub === "function") { h.sub(() => requestAnimationFrame(RENDER)); } else if ("ex" in h && typeof h.ex === "function") { executables.push(h); } }
queueMicrotask(() => RUN_EFFECTS(res));
return res; };
export { Render };
- Assistant
- User
i want to keep it pure the only thing in the framewrok that uses global store is the memo caching function
- Assistant
- User
works correctly with nested stateful elements import { FC, Render, Root, Signal, CSS, HTML } from "./lib/index";
const counterCSS = CSS({ width: "10rem", height: "10rem", backgroundColor: "rebeccapurple", display: "flex", alignItems: "center", justifyContent: "center", gap: "1rem", });
const Counter: FC<{ count: Signal; label: string }, HTMLElement> = ({ count, label, }) => { const count2 = Signal(-7);
return Render(() => { return HTML( "div", HTML("p", count.ref()), HTML("button", label, { onclick: () => count.set((x) => x + 1), }), HTML("p", count2.ref()), HTML("button", "-", { onclick: () => count2.set((x) => x + 1), }), ); }, [count2]); };
const Wrap: FC<{}, HTMLElement> = () => { const count1 = Signal(7);
return Render(() => { return HTML("main", counterCSS, Counter({ count: count1, label: "+" })); }, [count1]); };
Root(document.body, () => Wrap({}));
- Assistant
- User
it might not look as nice as other frameworks returning just jsx tempalte but its tiny, pure and composable
- Assistant
- User
is it possible to call js class constructor like function without new? like instead of new Foo() just Foo()?
- Assistant
- User
the problem with this is that u get the type name of the class and i need the type to be the same name as factory function
- Assistant
- User
ive been avoiding classes for functional types altogether and doing this instead : an interface + factory returning scope with lambdas import { Kinds } from "./Kinds";
interface IO { kind: "IO"; ex: () => Promise; map: (f: (a: E) => F | Promise) => IO; chain: (f: (a: E) => IO) => IO; extract: () => () => E | Promise; }
interface TypeConstructor { (thunk: () => Promise | E): IO; of: (thunk: () => Promise | E) => IO; id: () => IO; }
const IO: TypeConstructor<"IO"> = (thunk) => { const ex = () => Promise.resolve(thunk()); return { kind: "IO", ex: ex, map: (f) => IO(() => ex().then(f)), chain: (f) => IO(() => ex().then((a) => f(a).ex())), extract: () => thunk, } as const; };
IO.of = (thunk) => IO(thunk); IO.id = () => IO(() => {});
export { IO };
- Assistant
- User
im tired of typescript, have been working on this lib for a week now whole day. should go and start building a static site documentation for it in rust ?
- Assistant
- User
no i want to build my own static site generator to customize everything
- Assistant
- User
it works with multiple subscribers! it updates diverging branches separateely. can react do this? import { FC, Render, Root, Signal, CSS, HTML, IO } from "./lib/index";
const counterCSS = CSS({ width: "10rem", height: "10rem", backgroundColor: "rebeccapurple", display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center", gap: "1rem", });
class Signal_ { observers = new Set<(a: any) => void>(); val: S;
constructor(val: S) { this.val = val; }
ref() { return this.val; }
sub(f: (a: any) => void) { console.log(this.observers); this.observers.add(f); }
set(f: (s: S) => S) { this.val = f(this.val); this.observers.forEach((x) => x(this.val)); } }
const Counter: FC<{ count: Signal_ }, HTMLElement> = ({ count }) => { return Render(() => { return HTML("button", count.ref(), { onclick: () => count.set((x) => x + 1), }); }, [count]); };
const Display: FC<{ count: Signal_ }, HTMLElement> = ({ count }) => { return Render(() => { return HTML("output", count.ref()); }, [count]); };
const Wrap: FC<{}, HTMLElement> = () => { const count1 = new Signal_(6); return Render(() => { return HTML("main", Display({ count: count1 }), Counter({ count: count1 })); }, [counterCSS]); };
Root(document.body, () => Wrap({}));
- Assistant
- User
i implemented unsubbing. thoughts on this? import { Observable, Executable } from "../Kinds/Kinds";
type Noop = null | undefined | void;
type Hooks = | Observable<unknown, void, unknown> | Executable<E | void, unknown> | Noop;
const Render = (thunk: () => E, hooks: Hooks[]): E | never => { let res: E;
try { res = thunk(); } catch (err) { console.error(err); return void 0 as never; }
const l = hooks.length; let executables: Executable<E | void, unknown>[] = []; let unsubs: Function[] = [];
const RUN_EFFECTS = (res: E) => { try { const l = executables.length; for (let i = 0; i < l; i++) executables[i]?.ex(res); } catch (err) { console.error(err); } };
const RENDER = () => { if (res instanceof Node) { console.log(res.isConnected); if (!res.isConnected) { unsubs.forEach((x) => x()); res = void 0 as never; executables = void 0 as never; unsubs = void 0 as never; return; } } try { const next = thunk(); if (next instanceof Node && res instanceof Node && res.parentNode) { res.parentNode.replaceChild(next, res); } res = next; queueMicrotask(() => RUN_EFFECTS(res)); } catch (err) { console.error(err); } };
- Assistant
- User
now u can subscribe to all components without causing memory leaks import { FC, Render, Root, Signal, CSS, HTML, IO } from "./lib/index";
const counterCSS = CSS({ width: "10rem", height: "10rem", backgroundColor: "rebeccapurple", display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center", gap: "1rem", });
class Signal_ { observers = new Set<(a: any) => void>(); val: S;
constructor(val: S) { this.val = val; }
ref() { return this.val; }
sub(f: (a: any) => void) { console.log(this.observers); this.observers.add(f); return () => { this.observers.delete(f); }; }
set(f: (s: S) => S) { this.val = f(this.val); this.observers.forEach((x) => x(this.val)); } }
const Counter: FC<{ count: Signal_ }, HTMLElement> = ({ count }) => { return Render(() => { return HTML("button", count.ref(), { onclick: () => count.set((x) => x + 1), }); }, [count]); };
const Display: FC<{ count: Signal_ }, HTMLElement> = ({ count }) => { return Render(() => { return HTML("output", count.ref()); }, [count]); };
const Wrap: FC<{}, HTMLElement> = () => { const count1 = new Signal_(6);
return Render(() => { return HTML( "main", Display({ count: count1 }), Counter({ count: count1 }), Counter({ count: count1 as any }), ); }, [counterCSS, count1]); };
- Assistant
- User
what about implementing on unmount lifecicle with scoped mutation observer? create observer and what changes to parent if chenges occure check if res is connected if not then its our socope that got thrown away then do teardown, call unsubs, call effect cleanup callbacks. is that a good plan?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.