is it better to make render completely stateless and only use one subscriber observables…
- User
is it better to make render completely stateless and only use one subscriber observables and provide optional cleanup function that queues cleanups into cache?
- Assistant
- User
- Assistant
- User
no i came up with idea to provide a separate cleanup function it will queue up callbacks into cache and when its called it runs the queued callbcaks. and make all observables support only one subscriber to enforce one way data flow
- Assistant
- User
if needed a multi observable store can be implemented using id keyid memo by binding ids to a component.
- Assistant
- User
i think its better now. more functional and pure type FC<P = {}> = (props: P) => Node | Noop;
interface Executable { ex: (n: N | undefined) => any; }
interface Observable { sub: (f: (...a: any) => any) => any; }
type Noop = null | undefined | void;
type Hooks = Observable | Executable | Noop;
const Render = ( thunk: () => N | Noop, hooks: Hooks[], ): N | Noop => { let RES: N | Noop;
try { RES = thunk(); } catch (err) { console.error(err); return; }
const executables: Executable[] = [];
const RUN_EFFECTS = () => { for (let i = 0; i < executables.length; i++) { try { executables[i]!.ex(RES ?? undefined); } catch (err) { console.error(err); } } };
const RENDER = () => { requestAnimationFrame(() => { if (typeof thunk !== "function") return; try { const next = thunk(); if (next instanceof Node && RES instanceof Node && RES.parentNode) { RES.parentNode.replaceChild(next, RES); } RES = next; } catch (err) { console.error(err); } queueMicrotask(RUN_EFFECTS); }); };
for (let i = 0; i < hooks.length; i++) { const h = hooks[i]!; if ("sub" in h && typeof h.sub === "function") { h.sub(RENDER); } else if ("ex" in h && typeof h.ex === "function") { executables.push(h); } }
- Assistant
- User
is this ok? import { Maybe, Just, Nothing } from "../Kinds/Maybe";
interface Box { kind: "Box"; set: (f: (thing: T | undefined) => T) => void; sub: (f: (thing: T | undefined) => void) => void; ref: () => T | undefined; full: () => boolean; empty: () => boolean; maybe: () => Maybe; extract: () => T | undefined; }
interface TypeConstructor<K extends Box> { (thunk?: () => T): Box; of: (thunk?: () => T) => Box; }
const Box: TypeConstructor<Box> = ( thunk?: () => T, ): Box => { let thing = thunk ? new WeakRef(thunk()) : void 0; let observer: ((thing: T | undefined) => void) | undefined = void 0; let ref = () => thing?.deref() as T | undefined;
thunk = void 0 as never;
return { kind: "Box", ref, full: () => ref() !== undefined, empty: () => ref() === undefined, set: (f) => { thing = new WeakRef(f(ref())); if (typeof observer === "function") observer(ref()); }, sub: (f) => { if (typeof observer === "function") { throw new Error("Observer has already been set"); } else { observer = f; } }, maybe: (): Maybe => { const r = ref(); return r ? Just(r) : Nothing(); }, extract: () => { const tmp = ref(); thing = void 0 as never; observer = void 0; ref = () => void 0; return tmp; }, } as const; };
- Assistant
- User
thoughts on this? its its own module now isntead of being coupled to render const CLEANUPS = new Set<() => boolean>();
const runCleanup = "requestIdleCallback" in window ? requestIdleCallback : (f: () => void) => setTimeout(f, 1);
interface Cleanup { kind: "Cleanup"; ex: (el: Node) => void; }
const Cleanup = (f?: () => void): Cleanup => ({ kind: "Cleanup",
ex: (el?: Node) => { runCleanup(() => { CLEANUPS.forEach((f) => { if (f()) CLEANUPS.delete(f); }); });
if (typeof f === "function" && el instanceof Node) { CLEANUPS.add(() => { if (el.isConnected) { return false; } else { f(); return true; } }); }}, });
export { Cleanup };
- Assistant