2026-07-05 07:57:04 +08:00
|
|
|
import { syncGeneratedPagesFromPublicAssets } from './mindspace-page-sync.mjs';
|
|
|
|
|
import { resolvePublishDir } from './user-publish.mjs';
|
|
|
|
|
|
|
|
|
|
export function createPageSyncService({
|
|
|
|
|
pool,
|
|
|
|
|
pageService,
|
|
|
|
|
assetService,
|
|
|
|
|
h5Root,
|
|
|
|
|
syncWorkspaceAssets = null,
|
|
|
|
|
logger = console,
|
|
|
|
|
} = {}) {
|
|
|
|
|
const syncInFlight = new Map();
|
|
|
|
|
|
2026-07-13 15:29:29 +08:00
|
|
|
async function syncUserGeneratedPages(userId, { onlyRelativePaths = null } = {}) {
|
2026-07-05 07:57:04 +08:00
|
|
|
if (!pool || !pageService || !userId) {
|
|
|
|
|
return { created: 0, skipped: 0, updated: 0 };
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-13 15:29:29 +08:00
|
|
|
const scopeKey = onlyRelativePaths == null
|
|
|
|
|
? '*'
|
|
|
|
|
: [...new Set(onlyRelativePaths)].sort().join('|');
|
|
|
|
|
const inFlightKey = `${userId}:${scopeKey}`;
|
|
|
|
|
let inFlight = syncInFlight.get(inFlightKey);
|
2026-07-05 07:57:04 +08:00
|
|
|
if (!inFlight) {
|
|
|
|
|
inFlight = syncGeneratedPagesFromPublicAssets({
|
|
|
|
|
pool,
|
|
|
|
|
pageService,
|
|
|
|
|
assetService,
|
|
|
|
|
userId,
|
|
|
|
|
publishDir: h5Root ? resolvePublishDir(h5Root, { id: userId }) : null,
|
|
|
|
|
syncWorkspaceAssets,
|
2026-07-13 15:29:29 +08:00
|
|
|
onlyRelativePaths,
|
2026-07-05 07:57:04 +08:00
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
logger.warn?.('[MindSpace] page sync failed:', error?.message ?? error);
|
|
|
|
|
return { created: 0, skipped: 0, updated: 0 };
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
2026-07-13 15:29:29 +08:00
|
|
|
if (syncInFlight.get(inFlightKey) === inFlight) {
|
|
|
|
|
syncInFlight.delete(inFlightKey);
|
2026-07-05 07:57:04 +08:00
|
|
|
}
|
|
|
|
|
});
|
2026-07-13 15:29:29 +08:00
|
|
|
syncInFlight.set(inFlightKey, inFlight);
|
2026-07-05 07:57:04 +08:00
|
|
|
}
|
|
|
|
|
return inFlight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { syncUserGeneratedPages };
|
|
|
|
|
}
|