refactor: centralize source API requests, polling, and path parsing
- Add request_source_response and request_source_json helpers with 429 retry logic - Introduce shared split_path_segments and parse_provider_model_url utilities - Migrate CivitAI, CivArchive, HuggingFace, and ModelList sources to centralized helpers - Add generic pollBackgroundTask utility for frontend progress polling - Add tests for request_source_json, split_path_segments, parse_provider_model_url - Add huggingface-hub to requirements
This commit is contained in:
@@ -119,3 +119,43 @@ export function sanitizeDescriptionHtml(html) {
|
||||
Array.from(root.childNodes).forEach(child => sanitizeNode(child));
|
||||
return root.innerHTML;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic background task progress poller with token cancellation checks.
|
||||
*/
|
||||
export async function pollBackgroundTask({
|
||||
endpoint,
|
||||
tokenCheck,
|
||||
onProgress,
|
||||
isTerminal,
|
||||
onTerminal,
|
||||
onError,
|
||||
intervalMs = 250,
|
||||
fetchJson,
|
||||
filterIgnoredStatus
|
||||
}) {
|
||||
while (tokenCheck()) {
|
||||
try {
|
||||
const data = await fetchJson(endpoint, { silent: true }, 'Poll background task');
|
||||
|
||||
if (filterIgnoredStatus && filterIgnoredStatus(data)) {
|
||||
await new Promise(resolve => setTimeout(resolve, intervalMs));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tokenCheck()) {
|
||||
onProgress(data);
|
||||
if (isTerminal(data)) {
|
||||
onTerminal(data);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
if (tokenCheck()) {
|
||||
onError(error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
await new Promise(resolve => setTimeout(resolve, intervalMs));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user