50 lines
2.2 KiB
JavaScript
50 lines
2.2 KiB
JavaScript
// The cloud document is authoritative; initial STEP is only an unsaved bootstrap source.
|
|
import { CAD } from './config.js';
|
|
import { cadError, downloadSource, initialStepSource } from './api.js';
|
|
|
|
export async function openProjectSource({ api, bridge, project, signal, onStage = () => {}, assertCurrent = () => {},
|
|
recover = async () => ({}), downloader = downloadSource }) {
|
|
const check = () => { signal?.throwIfAborted(); assertCurrent(); };
|
|
const download = async (source, maxBytes, stage) => {
|
|
check(); onStage(stage);
|
|
let bytes;
|
|
try {
|
|
bytes = await downloader(source, maxBytes, (done, total) => {
|
|
check(); onStage(stage, Math.round(done / total * 100) + '%');
|
|
}, signal);
|
|
} catch (error) {
|
|
if (stage === 'stepDownload' && error.code === 'SOURCE_TOO_LARGE') throw cadError('STEP_TOO_LARGE');
|
|
throw error;
|
|
}
|
|
check(); return bytes;
|
|
};
|
|
check();
|
|
if (project.hasDocument) {
|
|
const source = await api.document(project.id, signal);
|
|
check();
|
|
const bytes = await download(source, CAD.maxSourceBytes, 'download');
|
|
onStage('rebuild');
|
|
const opened = await bridge.openProject({ projectId: project.id, bytes, saved: true });
|
|
check();
|
|
return { opened, version: source.version, kind: 'native' };
|
|
}
|
|
const recovery = await recover();
|
|
check();
|
|
if (recovery.restored || recovery.blocked) return { ...recovery, kind: 'recovery' };
|
|
if (project.initialStep) {
|
|
const source = initialStepSource(project.initialStep);
|
|
const capabilities = await bridge.ready();
|
|
check();
|
|
if (capabilities.capabilities?.stepImport !== true || typeof bridge.openProjectFromStep !== 'function'
|
|
|| typeof bridge.cancelOpenProject !== 'function') throw cadError('STEP_ENGINE_UPDATE');
|
|
const bytes = await download(source, CAD.maxStepSourceBytes, 'stepDownload');
|
|
onStage('stepImport');
|
|
const opened = await bridge.openProjectFromStep({ projectId: project.id, bytes, fileName: source.fileName });
|
|
check();
|
|
return { opened, version: 0, kind: 'step', warnings: opened.warnings || [] };
|
|
}
|
|
const opened = await bridge.openProject({ projectId: project.id, bytes: null, saved: false });
|
|
check();
|
|
return { opened, version: 0, kind: 'empty' };
|
|
}
|