-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceDemo.ts
More file actions
188 lines (165 loc) · 4.42 KB
/
Copy pathResourceDemo.ts
File metadata and controls
188 lines (165 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { screen } from "@intent-framework/core"
import type { AppServices } from "./types.js"
export let teamLoadCount = 0
export let auditLogLoadCount = 0
export let cachedTeamLoadCount = 0
export let dedupeReportLoadCount = 0
export let keyedTeamLoadCount = 0
export let timeEvictTeamLoadCount = 0
export let keyedTimeEvictLoadCount = 0
export type Team = {
id: string
name: string
members: number
version: number
}
export const ResourceDemo = screen<AppServices>("Resource Demo", $ => {
const team = $.resource("team", {
load: async ({ route }) => {
teamLoadCount++
return {
id: route.params.id,
name: "Core",
members: 3,
version: teamLoadCount,
} satisfies Team
},
})
const auditLog = $.resource("auditLog", {
load: async () => {
auditLogLoadCount++
return ["entry_1", "entry_2"]
},
autoLoad: false,
})
$.resource("unstableReport", {
load: async () => {
throw new Error("Report generation failed")
},
})
const cachedTeam = $.resource("cachedTeam", {
load: async ({ route }) => {
cachedTeamLoadCount++
return {
id: route.params.id,
name: "Cached",
members: 5,
version: cachedTeamLoadCount,
} satisfies Team
},
cache: { staleTime: 50 },
})
const dedupeReport = $.resource("dedupeReport", {
load: async () => {
dedupeReportLoadCount++
await new Promise(r => setTimeout(r, 20))
return { summary: "ok" }
},
autoLoad: false,
cache: { deduplicate: true },
})
const keyedTeam = $.resource("keyedTeam", {
load: async ({ route }) => {
keyedTeamLoadCount++
return {
id: route.params.id,
name: `Team-${route.params.id}`,
members: 3,
version: keyedTeamLoadCount,
} satisfies Team
},
cache: { key: ({ route }) => route.params.id },
})
const timeEvictTeam = $.resource("timeEvictTeam", {
load: async ({ route }) => {
timeEvictTeamLoadCount++
return {
id: route.params.id ?? "default",
name: "TimeEvict",
members: 7,
version: timeEvictTeamLoadCount,
} satisfies Team
},
cache: { cacheTime: 1000 },
})
const keyedTimeEvict = $.resource("keyedTimeEvict", {
load: async ({ route }) => {
keyedTimeEvictLoadCount++
return {
id: route.params.id,
name: `Evict-${route.params.id}`,
members: 2,
version: keyedTimeEvictLoadCount,
} satisfies Team
},
cache: { key: ({ route }) => route.params.id, cacheTime: 1000 },
})
const reloadTeam = $.act("Reload team")
.does(async () => {
await team.reload()
})
const invalidateTeam = $.act("Invalidate team")
.does(() => {
team.invalidate()
})
const saveTeam = $.act("Save team")
.does(async () => {})
.invalidates(team)
const brokenSave = $.act("Broken save")
.does(async () => {
throw new Error("Save failed")
})
.invalidates(team)
const loadAuditLog = $.act("Load audit log")
.does(async () => {
await auditLog.load()
})
const reloadCachedTeam = $.act("Reload cached team")
.does(async () => {
await cachedTeam.reload()
})
const loadDedupeReport = $.act("Load dedupe report")
.does(async () => {
await dedupeReport.load()
})
const loadKeyedTeamB = $.act("Load keyed team (team_b)")
.does(async ({ navigate }) => {
await keyedTeam.load({
route: { name: "demo", path: "/:id", params: { id: "team_b" } },
navigate,
})
})
const reloadKeyedTeam = $.act("Reload keyed team")
.does(async () => {
await keyedTeam.reload()
})
const invalidateTimeEvict = $.act("Invalidate time-evict team")
.does(() => {
timeEvictTeam.invalidate()
})
const loadKeyedTimeEvictB = $.act("Load keyed time-evict (team_b)")
.does(async ({ navigate }) => {
await keyedTimeEvict.load({
route: { name: "demo", path: "/:id", params: { id: "team_b" } },
navigate,
})
})
const invalidateKeyedTimeEvict = $.act("Invalidate keyed time-evict")
.does(() => {
keyedTimeEvict.invalidate()
})
$.surface("main").contains(
reloadTeam,
invalidateTeam,
saveTeam,
brokenSave,
loadAuditLog,
reloadCachedTeam,
loadDedupeReport,
loadKeyedTeamB,
reloadKeyedTeam,
invalidateTimeEvict,
loadKeyedTimeEvictB,
invalidateKeyedTimeEvict,
)
})