diff --git a/src/file-handler.ts b/src/file-handler.ts index 3f53bbd..1835b1a 100644 --- a/src/file-handler.ts +++ b/src/file-handler.ts @@ -122,18 +122,17 @@ const loadCameraPoses = async (url: string, filename: string, events: Events) => // initialize file handler events const initFileHandler = async (scene: Scene, events: Events, dropTarget: HTMLElement, remoteStorageDetails: RemoteStorageDetails) => { + // returns a promise that resolves when the file is loaded const handleLoad = async (url: string, filename: string) => { try { const lowerFilename = (filename || url).toLowerCase(); if (lowerFilename.endsWith('.json')) { await loadCameraPoses(url, filename, events); } else if (lowerFilename.endsWith('.ply') || lowerFilename.endsWith('.splat')) { - await scene.assetLoader.loadModel({ url, filename }) - .then((model) => { - scene.add(model); - scene.camera.focus(); - events.fire('loaded', filename); - }); + const model = await scene.assetLoader.loadModel({ url, filename }); + scene.add(model); + scene.camera.focus(); + events.fire('loaded', filename); } else { throw new Error(`Unsupported file type`); } @@ -333,40 +332,48 @@ const initFileHandler = async (scene: Scene, events: Events, dropTarget: HTMLEle }); events.function('scene.write', async (options: SceneWriteOptions) => { - const splats = getSplats(); - startSpinner(); - // setTimeout so spinner has a chance to activate - await new Promise((resolve) => { - setTimeout(resolve); - }); + try { + const splats = getSplats(); - const data = (() => { - switch (options.type) { - case 'ply': - return convertPly(splats); - case 'compressed-ply': - return convertPlyCompressed(splats); - case 'splat': - return convertSplat(splats); - default: - return null; + // setTimeout so spinner has a chance to activate + await new Promise((resolve) => { + setTimeout(resolve); + }); + + const data = (() => { + switch (options.type) { + case 'ply': + return convertPly(splats); + case 'compressed-ply': + return convertPlyCompressed(splats); + case 'splat': + return convertSplat(splats); + default: + return null; + } + })(); + + if (options.stream) { + // write to stream + await writeToFile(options.stream, data); + } else if (remoteStorageDetails) { + // write data to remote storage + await sendToRemoteStorage(options.filename, data, remoteStorageDetails); + } else if (options.filename) { + // download file to local machine + download(options.filename, data); } - })(); - - if (options.stream) { - // write to stream - await writeToFile(options.stream, data); - } else if (remoteStorageDetails) { - // write data to remote storage - await sendToRemoteStorage(options.filename, data, remoteStorageDetails); - } else if (options.filename) { - // download file to local machine - download(options.filename, data); + } catch (err) { + events.invoke('showPopup', { + type: 'error', + header: localize('popup.error-loading'), + message: `${err.message ?? err} while saving file` + }); + } finally { + stopSpinner(); } - - stopSpinner(); }); };