Skip to content

Commit

Permalink
handle save throws too
Browse files Browse the repository at this point in the history
  • Loading branch information
slimbuck committed Oct 17, 2024
1 parent ac2317f commit 30f59e7
Showing 1 changed file with 42 additions and 35 deletions.
77 changes: 42 additions & 35 deletions src/file-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand Down Expand Up @@ -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<void>((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<void>((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();
});
};

Expand Down

0 comments on commit 30f59e7

Please sign in to comment.