hi
- User
hi
- Assistant
Hello! How can I assist you today?
- User
thoughts on this? import { Observable, Executable } from "../Kinds/Kinds";
type Hook = Observable<unknown, unknown, unknown> | Executable<E, unknown>;
const Render = (thunk: () => E, hooks: Hook[]): E => { let res = thunk();
const executables: Executable<E, unknown>[] = [];
const len = hooks.length; for (let i = 0; i < len; i++) { const h = hooks[i]!;
if ("sub" in h && typeof h.sub === "function") { h.sub(() => { requestAnimationFrame(() => { const newRes = thunk(); const len = executables.length; for (let i = 0; i < len; i++) executables[i]!.ex(newRes); if (newRes instanceof HTMLElement && res instanceof HTMLElement) res.parentNode?.replaceChild(newRes, res); res = newRes; }); }); } else if ("ex" in h && typeof h.ex === "function") { h.ex(res); executables.push(h); }}
return res; };
export { Render };
- Assistant
- User
the observables only support one subscruber so theres no memeory leaks
- Assistant
Got it, thanks for clarifying that your
Observablesupports only a single subscriber—this definitely simplifies the subscription management and alleviates concerns about memory leaks from multiple callbacks.In that case, your approach of setting the subscription inside the
Renderfunction without explicit cleanup is reasonable, as eachObservablecan only have onesubat a time, so you won't accumulate subscriptions inadvertently.
A few additional thoughts given that constraint:
-
Since there's only one subscriber allowed, you might want to clearly document this invariant in your
Observableinterface or type so future maintainers understand why you don't need unsubscription logic. -
It also implies that calling
Rendermultiple times with the same hooks might replace previous subscriptions implicitly. Just make sure this behavior aligns with your usage pattern. -
If you ever decide to extend
Observableto support multiple subscribers in the future, you'll want to revisit this design to manage subscriptions carefully.
-
- User
i wonder if we should get rid of executables al together and only make it take observables array since HTML dsl function already takes executable hooks?
- Assistant
- User
anything else ? import { Observable } from "../Kinds/Kinds";
const Render = ( thunk: () => E, observables: Observable<unknown, unknown, unknown>[], ): E => { let res = thunk();
const l = observables.length;
for (let i = 0; i < l; i++) { const h = observables[i]!;
if ("sub" in h && typeof h.sub === "function") { h.sub(() => { requestAnimationFrame(() => { const newRes = thunk(); if (newRes instanceof HTMLElement && res instanceof HTMLElement) { res.parentNode?.replaceChild(newRes, res); } res = newRes; }); }); }}
return res; };
export { Render };
- Assistant
- User
is this ok? import { Observable } from "../Kinds/Kinds";
let SCHEDULED = false;
const Render = ( thunk: () => E, observables: Observable<unknown, unknown, unknown>[], ): E => { let res = thunk();
const RENDER = () => { if (SCHEDULED) { requestAnimationFrame(RENDER); return; } SCHEDULED = true; const newRes = thunk(); if (newRes instanceof HTMLElement && res instanceof HTMLElement) { res.parentNode?.replaceChild(newRes, res); } res = newRes; SCHEDULED = false; };
const l = observables.length;
for (let i = 0; i < l; i++) { const h = observables[i]!; if ("sub" in h && typeof h.sub === "function") { h.sub(() => requestAnimationFrame(RENDER)); } }
return res; };
export { Render };
- Assistant
- User
better? import { Observable } from "../Kinds/Kinds";
const Render = ( thunk: () => E, observables: Observable<unknown, unknown, unknown>[], ): E => { let res = thunk(); let scheduled = false; const l = observables.length;
const RENDER = () => { if (scheduled) { requestAnimationFrame(RENDER); return; } scheduled = true; const next = thunk(); if (next instanceof HTMLElement && res instanceof HTMLElement) { res.parentNode?.replaceChild(next, res); } res = next; scheduled = false; };
for (let i = 0; i < l; i++) { const h = observables[i]!; if ("sub" in h && typeof h.sub === "function") { h.sub(() => requestAnimationFrame(RENDER)); } }
return res; };
export { Render };
- Assistant
You're almost there! However, the main problem with this version is still the recursive call:
if (scheduled) { requestAnimationFrame(RENDER); return; }This will cause an infinite loop once
scheduledbecomestrue, because it keeps schedulingRENDERover and over without waiting forscheduledto becomefalse.
What you want:
- User
this seem pointless
- Assistant
I get what you mean — it can look like a bit of ceremony just to throttle updates on every observable notification, especially in a simple render function.
But the main reasons for this pattern are:
-
Avoiding redundant renders during rapid updates. If multiple observables trigger changes within the same frame, you only want to run the expensive render logic once per animation frame, not multiple times.
-
- User
this makes no sense because it will jsut skip frame if called before the thunk computes
- Assistant
You're absolutely right in noticing that if the
scheduledflag is set before the deferred thunk computation and DOM update happen, then any new triggers before the RAF callback runs will just be ignored/skipped. This behavior effectively batches multiple update triggers into a single render per frame — which is usually exactly what you want for UI rendering to avoid excessive and unnecessary renders. - User
they dont get coalesced because it returns from render function.
- Assistant
- User
how about we use an array for scheduled frames ? and run them after render ?
- Assistant
- User
but raf already uses a queue whats the point of it?
- Assistant
- User
import { Observable } from "../Kinds/Kinds";
const Render = ( thunk: () => E, observables: Observable<unknown, unknown, unknown>[], ): E => { let res = thunk(); let scheduled = false; let frameQueu: (() => void)[] = [];
const RENDER = () => { scheduled = true; const next = thunk(); if (next instanceof HTMLElement && res instanceof HTMLElement) { res.parentNode?.replaceChild(next, res); } res = next; scheduled = false; };
for (let i = 0; i < observables.length; i++) { const h = observables[i]!; if ("sub" in h && typeof h.sub === "function") { h.sub(() => { if (scheduled) { frameQueu.push(RENDER); } else { requestAnimationFrame(RENDER); return; }
if (frameQueu.length) { requestAnimationFrame(() => { while (frameQueu.length) { frameQueu.shift()!(); } }); } }); }}
return res; };
export { Render };
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.