hi
- User
hi
- Assistant
Hello! How can I assist you today?
- User
review this type Cell = { thing: T | null; next: Cell | null; };
type Observer = (next: () => T | null, done: () => void) => void;
interface Queue { kind: "Queue"; sub: (f: Observer) => void; unsub: () => void; push: (thing: T) => void; pull: () => T | null; len: () => number; unwrap: () => Cell; }
const cell = (thing: T | null): Cell => ({ thing: thing, next: null });
const Queue = (): Queue => { let head = cell(null); let tail = head; let len = 0; let observer: Observer | undefined; let processing = false;
const pull = () => { if (len > 0) { const thing = head.thing; head = head.next!; len--; return thing; } else { return null; } };
return { kind: "Queue", len: () => len, sub: (f) => (observer = f), unsub: () => (observer = void 0), push: (thing) => { if (len > 0) { tail.next = cell(thing); tail = tail.next; } else if (len === 0) { head = tail; head.thing = thing; } else { return; } len++; if (!processing && observer) { processing = true; observer( () => pull(), () => (processing = false), ); } }, pull: pull, unwrap: () => { const tmp = head; head = void 0 as never; tail = void 0 as never; observer = void 0; return tmp; }, }; };
export { Queue };
- Assistant
- User
thoughts on this refactor? type Cell = { thing: T | null; next: Cell | null; };
const cell = (thing: T | null): Cell => ({ thing: thing, next: null });
type Observer = (next: () => T | null, done: () => void) => void;
interface Queue { kind: "Queue"; sub: (f: Observer) => void; unsub: () => void; push: (...things: T[]) => void; pull: () => T | null; len: () => number; unwrap: () => Cell; }
const Queue = (): Queue => { let head = cell(null); let tail = head; let len = 0; let observer: Observer | undefined; let processing = false;
const push = (thing: T) => { if (len > 0) { tail.next = cell(thing); tail = tail.next; } else if (len === 0) { head = tail; head.thing = thing; } len++; };
const pull = () => { if (len > 0) { const thing = head.thing; head = head.next!; len--; return thing; } else { return null; } };
return { kind: "Queue", len: () => len, sub: (f) => (observer = f), unsub: () => (observer = void 0), push: (...things: T[]) => { things.forEach((x) => { push(x); if (!processing && observer) { processing = true; observer( () => pull(), () => (processing = false), ); } }); }, pull: pull, unwrap: () => { const tmp = head; head = void 0 as never; tail = void 0 as never; observer = void 0; return tmp; }, }; };
- Assistant
- User
its async now type Cell = { thing: T | null; next: Cell | null; };
const cell = (thing: T | null): Cell => ({ thing: thing, next: null });
type Observer = (next: () => T | null, done: () => void) => void;
interface Queue { kind: "Queue"; sub: (f: Observer) => void; unsub: () => void; push: (...things: T[]) => void; pull: () => T | null; len: () => number; unwrap: () => Cell; }
const Queue = (): Queue => { let head = cell(null); let tail = head; let len = 0; let observer: Observer | undefined; let processing = false;
const push = (thing: T) => { if (len > 0) { tail.next = cell(thing); tail = tail.next; } else if (len === 0) { head = tail; head.thing = thing; } len++; };
const pull = () => { if (len > 0) { const thing = head.thing; head = head.next!; len--; return thing; } else { return null; } };
return { kind: "Queue", len: () => len, sub: (f) => (observer = f), unsub: () => (observer = void 0), push: (...things: T[]) => { things.forEach((x) => { queueMicrotask(() => push(x)); queueMicrotask(() => { if (!processing && observer) { processing = true; observer( () => pull(), () => (processing = false), ); } }); }); }, pull: pull, unwrap: () => { const tmp = head; head = void 0 as never; tail = void 0 as never; observer = void 0; return tmp; }, }; };
- Assistant
- User
everything is one microtask now push: (...things: T[]) => queueMicrotask(() => { things.forEach((x) => { push(x); if (!processing && observer) { processing = true; observer( () => pull(), () => (processing = false), ); } }); }),
- Assistant
- User
it doesnt call it multiple time because it first checks if its processing the queue
- Assistant
You're absolutely right that the
processingflag prevents multiple observer calls within the same microtask/run. So your current approach does not call the observer multiple times per batch, because onceprocessingis set totrueat the first item, the subsequent items skip the observer call in that same microtask. - User
push: (...things: T[]) => queueMicrotask(() => { things.forEach(push); if (!processing && observer) { processing = true; observer( () => pull(), () => (processing = false), ); } }),
- Assistant
Yes, that looks great — concise, clear, and safe! Here's why:
- You batch all pushes together via
things.forEach(push). - Then, after all pushes, you check if
processingis false and anobserveris set. - If so, mark
processing = trueand call the observer exactly once. - The observer can then consume the queue using
pull()until empty. - When the observer signals completion via the
done()callback, you resetprocessingto false.
This approach ensures:
- No premature invocation of observer before items are enqueued.
- Observer is only triggered once per batch.
- No redundant calls or race conditions.
Recap of your push method:
push: (...things: T[]) => queueMicrotask(() => { things.forEach(push); if (!processing && observer) { processing = true; observer(() => pull(), () => (processing = false)); } }),This pattern is ideal for asynchronous queues where producers push items asynchronously and a single observer consumes until depleted.
- You batch all pushes together via
- User
i need this queue for console widget
- Assistant
- User
im building my own reactive ui framework and a dev tools with console and css editor and other things in a floating resizable window
- Assistant
- User
the reactive render core turned out to be 60loc can u believe this?
- Assistant
That's fantastic—and absolutely believable! Sometimes the most elegant reactive cores are surprisingly small because they leverage clever minimalism and make the right abstractions.
Many popular frameworks started with tiny cores before scaling up:
- Svelte’s reactive store is just a handful of lines for basic subscriptions.
- SolidJS manages fine-grained reactivity with a lean core.
- Early React clones focused on minimal reconciliation and batching.
Having a concise reactive render core (~60 LOC) is a great sign your design is clean and focused.
If you want, I’d love to see that core! Also happy to help you test, expand, or optimize it further.
Congrats on that milestone! 🎉 What features does your core cover? (e.g., signal tracking, dependency tracking, DOM mounting?)
- User
type FC<P extends Record<string, unknown>> = (props: P) => HTMLElement | void;
interface Observable { sub(f: (...a: unknown[]) => unknown): unknown; }
interface Executable { ex(...a: unknown[]): unknown; }
interface Applicative { ap(...a: unknown[]): unknown; }
interface Setter { set(f: (...a: unknown[]) => unknown): unknown; }
const check = (x: any, f: string) => f in x && typeof x[f] === "function";
const filter = (hooks: (Observable | Executable | Applicative | Setter)[]) => { const observables = hooks.filter((x) => check(x, "sub")) as Observable[]; const executables = hooks.filter((x) => check(x, "ex")) as Executable[]; const applicatives = hooks.filter((x) => check(x, "ap")) as Applicative[]; const setters = hooks.filter((x) => check(x, "sub") ? false : check(x, "set"), ) as Setter[]; return { observables, executables, applicatives, setters }; };
- Assistant
- User
thoughts on this kind of thing? interface Executable<R, E> { ex(...a: R[]): E; }
- Assistant
Your interface:
interface Executable<R, E> { ex(...a: R[]): E; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.