Skip to content

Commit c55038e

Browse files
committed
add abort/unmount guards
1 parent 53e2823 commit c55038e

2 files changed

Lines changed: 41 additions & 23 deletions

File tree

docs/src/components/sdk/object-documentation.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,32 +160,43 @@ export function ObjectFromJson({ objectName }: { objectName: string }) {
160160
const [error, setError] = React.useState<string | null>(null);
161161

162162
React.useEffect(() => {
163+
const controller = new AbortController();
164+
163165
async function loadObjectInfo() {
164-
try {
165-
setLoading(true);
166-
setError(null);
166+
setLoading(true);
167+
setError(null);
167168

168-
// Load the objects.json file
169-
const response = await fetch('/sdk-docs/objects.json');
169+
try {
170+
const response = await fetch('/sdk-docs/objects.json', { signal: controller.signal });
170171
if (!response.ok) {
171172
throw new Error(`Failed to load objects.json: ${response.statusText}`);
172173
}
173174

174-
const objectsData = await response.json();
175+
const objectsData: Partial<Record<string, ObjectInfo>> = await response.json();
175176
const foundObject = objectsData[objectName];
176177

177178
if (!foundObject) {
178-
throw new Error(`Object "${objectName}" not found in objects.json`);
179+
const availableKeys = Object.keys(objectsData);
180+
const preview = availableKeys.slice(0, 10).join(', ');
181+
const suffix = availableKeys.length > 10 ? `, ... (${availableKeys.length} total)` : '';
182+
throw new Error(`Object "${objectName}" not found in objects.json. Available objects: ${preview}${suffix}`);
179183
}
180184

181-
setObjectInfo(foundObject);
185+
if (!controller.signal.aborted) {
186+
setObjectInfo(foundObject);
187+
}
182188
} catch (err) {
189+
if (controller.signal.aborted) return;
183190
setError(err instanceof Error ? err.message : 'Unknown error');
184191
} finally {
185-
setLoading(false);
192+
if (!controller.signal.aborted) {
193+
setLoading(false);
194+
}
186195
}
187196
}
188197
runAsynchronously(loadObjectInfo());
198+
199+
return () => controller.abort();
189200
}, [objectName]);
190201

191202
if (loading) {

docs/src/components/sdk/type-documentation.tsx

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,39 +1112,46 @@ export function TypeFromJson({
11121112
const [error, setError] = React.useState<string | null>(null);
11131113

11141114
React.useEffect(() => {
1115+
const controller = new AbortController();
1116+
11151117
async function loadTypeInfo() {
1116-
try {
1117-
setLoading(true);
1118-
setError(null);
1118+
setLoading(true);
1119+
setError(null);
11191120

1120-
// Try types.json first, then mixins.json
1121-
const typesResponse = await fetch('/sdk-docs/types.json');
1121+
try {
1122+
const typesResponse = await fetch('/sdk-docs/types.json', { signal: controller.signal });
11221123
if (typesResponse.ok) {
1123-
const typesData = await typesResponse.json();
1124-
if (typesData[typeName]) {
1125-
setTypeInfo(typesData[typeName]);
1124+
const typesData: Partial<Record<string, TypeInfo>> = await typesResponse.json();
1125+
const found = typesData[typeName];
1126+
if (found) {
1127+
if (!controller.signal.aborted) setTypeInfo(found);
11261128
return;
11271129
}
11281130
}
11291131

1130-
// Try mixins.json
1131-
const mixinsResponse = await fetch('/sdk-docs/mixins.json');
1132+
const mixinsResponse = await fetch('/sdk-docs/mixins.json', { signal: controller.signal });
11321133
if (mixinsResponse.ok) {
1133-
const mixinsData = await mixinsResponse.json();
1134-
if (mixinsData[typeName]) {
1135-
setTypeInfo(mixinsData[typeName]);
1134+
const mixinsData: Partial<Record<string, TypeInfo>> = await mixinsResponse.json();
1135+
const found = mixinsData[typeName];
1136+
if (found) {
1137+
if (!controller.signal.aborted) setTypeInfo(found);
11361138
return;
11371139
}
11381140
}
11391141

11401142
throw new Error(`Type "${typeName}" not found in types.json or mixins.json`);
11411143
} catch (err) {
1144+
if (controller.signal.aborted) return;
11421145
setError(err instanceof Error ? err.message : 'Unknown error');
11431146
} finally {
1144-
setLoading(false);
1147+
if (!controller.signal.aborted) {
1148+
setLoading(false);
1149+
}
11451150
}
11461151
}
11471152
runAsynchronously(loadTypeInfo());
1153+
1154+
return () => controller.abort();
11481155
}, [typeName]);
11491156

11501157
if (loading) {

0 commit comments

Comments
 (0)