|
| 1 | +export type DemoItem = { |
| 2 | + name: string; |
| 3 | + children?: string[]; |
| 4 | +}; |
| 5 | + |
| 6 | +export const data: Record<string, DemoItem> = { |
| 7 | + root: { |
| 8 | + name: "Root", |
| 9 | + children: ["fruit", "vegetables", "meals", "dessert", "drinks"], |
| 10 | + }, |
| 11 | + fruit: { |
| 12 | + name: "Fruit", |
| 13 | + children: ["apple", "banana", "orange", "berries", "lemon"], |
| 14 | + }, |
| 15 | + apple: { name: "Apple" }, |
| 16 | + banana: { name: "Banana" }, |
| 17 | + orange: { name: "Orange" }, |
| 18 | + lemon: { name: "Lemon" }, |
| 19 | + berries: { name: "Berries", children: ["red", "blue", "black"] }, |
| 20 | + red: { name: "Red", children: ["strawberry", "raspberry"] }, |
| 21 | + strawberry: { name: "Strawberry" }, |
| 22 | + raspberry: { name: "Raspberry" }, |
| 23 | + blue: { name: "Blue", children: ["blueberry"] }, |
| 24 | + blueberry: { name: "Blueberry" }, |
| 25 | + black: { name: "Black", children: ["blackberry"] }, |
| 26 | + blackberry: { name: "Blackberry" }, |
| 27 | + vegetables: { |
| 28 | + name: "Vegetables", |
| 29 | + children: ["tomato", "carrot", "cucumber", "potato"], |
| 30 | + }, |
| 31 | + tomato: { name: "Tomato" }, |
| 32 | + carrot: { name: "Carrot" }, |
| 33 | + cucumber: { name: "Cucumber" }, |
| 34 | + potato: { name: "Potato" }, |
| 35 | + meals: { |
| 36 | + name: "Meals", |
| 37 | + children: ["america", "europe", "asia", "australia"], |
| 38 | + }, |
| 39 | + america: { name: "America", children: ["burger", "hotdog", "pizza"] }, |
| 40 | + burger: { name: "Burger" }, |
| 41 | + hotdog: { name: "Hotdog" }, |
| 42 | + pizza: { name: "Pizza" }, |
| 43 | + europe: { |
| 44 | + name: "Europe", |
| 45 | + children: ["pasta", "paella", "schnitzel", "risotto", "weisswurst"], |
| 46 | + }, |
| 47 | + pasta: { name: "Pasta" }, |
| 48 | + paella: { name: "Paella" }, |
| 49 | + schnitzel: { name: "Schnitzel" }, |
| 50 | + risotto: { name: "Risotto" }, |
| 51 | + weisswurst: { name: "Weisswurst" }, |
| 52 | + asia: { name: "Asia", children: ["sushi", "ramen", "curry", "noodles"] }, |
| 53 | + sushi: { name: "Sushi" }, |
| 54 | + ramen: { name: "Ramen" }, |
| 55 | + curry: { name: "Curry" }, |
| 56 | + noodles: { name: "Noodles" }, |
| 57 | + australia: { |
| 58 | + name: "Australia", |
| 59 | + children: ["potatowedges", "pokebowl", "lemoncurd", "kumarafries"], |
| 60 | + }, |
| 61 | + potatowedges: { name: "Potato Wedges" }, |
| 62 | + pokebowl: { name: "Poke Bowl" }, |
| 63 | + lemoncurd: { name: "Lemon Curd" }, |
| 64 | + kumarafries: { name: "Kumara Fries" }, |
| 65 | + dessert: { |
| 66 | + name: "Dessert", |
| 67 | + children: ["icecream", "cake", "pudding", "cookies"], |
| 68 | + }, |
| 69 | + icecream: { name: "Icecream" }, |
| 70 | + cake: { name: "Cake" }, |
| 71 | + pudding: { name: "Pudding" }, |
| 72 | + cookies: { name: "Cookies" }, |
| 73 | + drinks: { name: "Drinks", children: ["water", "juice", "beer", "wine"] }, |
| 74 | + water: { name: "Water" }, |
| 75 | + juice: { name: "Juice" }, |
| 76 | + beer: { name: "Beer" }, |
| 77 | + wine: { name: "Wine" }, |
| 78 | +}; |
| 79 | + |
| 80 | +const wait = (ms: number) => |
| 81 | + new Promise((resolve) => { |
| 82 | + setTimeout(resolve, ms); |
| 83 | + }); |
| 84 | + |
| 85 | +export const syncDataLoader = { |
| 86 | + getItem: (id: string) => data[id], |
| 87 | + getChildren: (id: string) => data[id]?.children ?? [], |
| 88 | +}; |
| 89 | + |
| 90 | +export const asyncDataLoader = { |
| 91 | + getItem: (itemId: string) => wait(500).then(() => data[itemId]), |
| 92 | + getChildren: (itemId: string) => |
| 93 | + wait(800).then(() => data[itemId]?.children ?? []), |
| 94 | +}; |
0 commit comments