// WARNING: This pattern is INTENTIONALLY non-idempotent. // It exists to test detectNonIdempotent() diagnosis tooling. // Do NOT use as a reference for correct pattern development. import { computed, Default, nonPrivateRandom, pattern, UI, Writable, } from "commonfabric"; interface Item { title: string; tag: string; } const preset: Item[] = [ { title: "Apples", tag: "fruit" }, { title: "Carrots", tag: "vegetable" }, { title: "Bananas", tag: "fruit" }, { title: "Broccoli", tag: "vegetable" }, { title: "Cherries", tag: "fruit" }, { title: "Spinach", tag: "vegetable" }, { title: "Grapes", tag: "fruit" }, { title: "Peppers", tag: "vegetable" }, ]; export default pattern<{ items: Writable>; }>(({ items }) => { // Anti-pattern: Random sort before Set insertion changes iteration order const uniqueTags = new Writable([]); computed(() => { const tags = items.get().map((i) => i.tag); const shuffled = tags.sort(() => nonPrivateRandom() - 0.5); const set = new Set(shuffled); uniqueTags.set([...set]); }); return { $NAME: "Non-Idempotent Set-to-Array", [UI]: (

Non-Idempotent Set-to-Array

Items:
    {items.map((item) => (
  • {item.title} [{item.tag}]
  • ))}
Unique tags (keeps reordering):
    {uniqueTags.map((tag) =>
  • {tag}
  • )}
Anti-pattern:{" "} Randomizing insertion order into a Set before converting to array.
Fix:{" "} Sort the array after Set conversion: [...set].sort().
), }; });