///
import { Cell, Default, handler, lift, recipe, str } from "commontools";
interface PersistenceDefaultsArgs {
value: Default;
step: Default;
}
const applyIncrement = handler(
(
event: { amount?: number } | undefined,
context: { value: Cell; step: Cell },
) => {
const amount = typeof event?.amount === "number"
? event.amount
: context.step.get() ?? 1;
const next = (context.value.get() ?? 0) + amount;
context.value.set(next);
},
);
export const counterWithPersistenceDefaults = recipe(
"Counter With Persistence Defaults",
({ value, step }) => {
const safeStep = lift((input: number | undefined) =>
typeof input === "number" ? input : 1
)(step);
return {
value,
step,
currentStep: safeStep,
label: str`Value ${value} (step ${safeStep})`,
increment: applyIncrement({ value, step }),
};
},
);