feat: sync learning progress to backend
Deploy Speaking Practice / deploy (push) Successful in 44s

This commit is contained in:
TTlaji committed 2026-06-15 12:52:20 +08:00
1 parent 67dcea21d0
commit 64e7cc5d8b
14 files changed
+689 -50

No files matched your search

+31
View File
@@ -147,8 +147,12 @@ let libraryLoadMessage = "";
let lastProgressSaveAt = 0;
let pendingRestoreProgress = null;
let currentSessionUser = null;
let progressSyncQueue = null;
async function apiRequest(path, options = {}) {
if (IS_BROWSER && window.SpeakingAuth) {
return window.SpeakingAuth.apiRequest(path, options);
}
const response = await fetch(path, options);
if (!response.ok) {
let message = "请求失败,请重试。";
@@ -197,6 +201,11 @@ async function initializeApp() {
currentSessionUser = await window.SpeakingAuth.requireSession();
if (!currentSessionUser) return;
ensureLocalUserForSession(currentSessionUser);
progressSyncQueue = window.SpeakingProgressSync?.createProgressSyncQueue({
getUser: getCurrentUser,
apiRequest,
saveState,
}) || null;
try {
libraryLoadMessage = "";
@@ -211,6 +220,8 @@ async function initializeApp() {
libraryLoadMessage = "后端视频库加载失败,当前显示示例素材。";
}
await syncLearningProgressFromBackend();
const requestedVideoId = getRequestedVideoId();
if (requestedVideoId && getVideo(requestedVideoId)) {
activeVideoId = requestedVideoId;
@@ -649,6 +660,7 @@ function saveActiveProgress(partial = {}, immediate = false) {
lastProgressSaveAt = now;
saveState();
renderLibrary();
scheduleProgressSync();
}
}
@@ -683,6 +695,7 @@ function toggleLineStatus(index, status, event) {
progress.updatedAt = new Date().toISOString();
saveState();
scheduleProgressSync();
renderTranscript();
renderLibrary();
}
@@ -700,10 +713,28 @@ function toggleSaved(field) {
user[field] = [...list, activeVideoId];
}
saveState();
scheduleProgressSync();
renderVideoMeta();
renderSavedLists();
}
async function syncLearningProgressFromBackend() {
if (!window.SpeakingProgressSync) return;
try {
await window.SpeakingProgressSync.syncLearningState({
user: getCurrentUser(),
apiRequest,
saveState,
});
} catch (error) {
console.warn("Learning progress sync failed", error);
}
}
function scheduleProgressSync() {
progressSyncQueue?.schedule();
}
function renderSavedLists() {
if (!els.savedLists) return;