/// import { Cell, computed, Default, handler, NAME, navigateTo, pattern, UI, } from "commontools"; interface RecipeState { value: Default; } const increment = handler }>((_, state) => { state.value.set(state.value.get() + 1); }); const decrement = handler((_, state: { value: Cell }) => { state.value.set(state.value.get() - 1); }); function previous(value: number) { return value - 1; } function nth(value: number) { if (value === 1) { return "1st"; } if (value === 2) { return "2nd"; } if (value === 3) { return "3rd"; } return `${value}th`; } export const Counter = pattern((state) => { return { [NAME]: computed(() => `Simple counter: ${state.value}`), [UI]: (
dec to {previous(state.value)} Counter is the {nth(state.value)} number inc to {state.value + 1}
), value: state.value, }; }); interface FactoryInput { // Provided by the shell; not used directly here allCharms: Default; } // No additional outputs beyond name and UI type FactoryOutput = { [NAME]: string; [UI]: any; }; type InputEvent = { detail: { message: string } }; const newCounter = handler>((_, __) => { const charm = Counter({ value: Math.round(Math.random() * 10) }); return navigateTo(charm); }); export default pattern((_) => { return { [NAME]: "Counter Factory", [UI]: (
), }; });