-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceDemo.ts
More file actions
110 lines (95 loc) · 2.35 KB
/
Copy pathResourceDemo.ts
File metadata and controls
110 lines (95 loc) · 2.35 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
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 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 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()
})
$.surface("main").contains(
reloadTeam,
invalidateTeam,
saveTeam,
brokenSave,
loadAuditLog,
reloadCachedTeam,
loadDedupeReport,
)
})