import { generateMap } from "./mapPipeline.js"; import { collectTransferableBuffers } from "./transferUtils.js"; import { compactRawPatchCandidate, summarizeRawPatchCandidate } from "./rawPatchCandidate.js"; function scopeTaskProgress(event = {}, taskId) { const scoped = { ...event }; if (event?.workUnitId != null && String(event.workUnitId).length > 0) { scoped.workUnitId = `${taskId}/${String(event.workUnitId)}`; } return scoped; } if (typeof self !== "undefined") { self.onmessage = (event) => { const message = event.data || {}; if (message.type !== "generate-raw-patch-candidate") return; const taskId = String(message.taskId || `raw-${message.id || 0}`); const startedAt = typeof performance !== "undefined" && performance.now ? performance.now() : Date.now(); try { let candidate = generateMap(Number(message.seed) >>> 0, { ...(message.mapOptions || {}), // A boolean sentinel is sufficient: mapPipeline only records whether a // boundary world exists. Actual seam/quality work remains in the parent // patch worker after this raw candidate has been transferred back. boundaryWorld: true, onProgress: (progress) => { self.postMessage({ type: "raw-patch-candidate-progress", id: message.id, taskId, progress: scopeTaskProgress(progress, taskId), }); }, }); const elapsedMs = (typeof performance !== "undefined" && performance.now ? performance.now() : Date.now()) - startedAt; // Transfer only the roots consumed by mapPatch. Full-map geography/debug // graphs can exceed the actual patch payload and are never consulted by // candidate merge/quality logic. Dropping them before structured clone // creates a hard cross-worker memory bound for large tiled operations. candidate = compactRawPatchCandidate(candidate); const transferSummary = summarizeRawPatchCandidate(candidate); const transferables = [...collectTransferableBuffers(candidate)]; self.postMessage({ type: "raw-patch-candidate-result", id: message.id, taskId, ok: true, elapsedMs, transferSummary, candidate, }, transferables); } catch (error) { self.postMessage({ type: "raw-patch-candidate-result", id: message.id, taskId, ok: false, code: error?.code || "raw-patch-candidate-error", error: error?.message || String(error), stack: error?.stack || "", }); } }; }