/// import { Cell, Default, derive, handler, recipe } from "commontools"; interface DelayedCounterArgs { value: Default; pending: Default; } const scheduleIncrement = handler( ( event: { amount?: number } | undefined, context: { pending: Cell }, ) => { const amount = typeof event?.amount === "number" ? event.amount : 1; const existing = context.pending.get() ?? []; context.pending.set([...existing, amount]); }, ); export const counterWithDelayedIncrement = recipe( "Counter With Delayed Increment", ({ value, pending }) => { const drainPending = derive( { pending, value }, ({ pending, value }) => { const queued = [...(pending ?? [])]; if (queued.length === 0) return value ?? 0; const total = queued.reduce((sum, amount) => sum + amount, 0); const current = value ?? 0; return current + total; }, ); return { value: drainPending, schedule: scheduleIncrement({ pending }), rawValue: value, }; }, );