Gracefully handle unsupported preview operation

This commit is contained in:
Ingvar Stepanyan
2021-07-23 17:22:29 +00:00
parent 585001bd4e
commit fcb67b1517

View File

@@ -8,7 +8,7 @@ const Stats = isDebug
) )
: null; : null;
/** @extends Component<{ getPreview: () => Promise<Blob> }, null> */ /** @extends Component<{ getPreview: () => Promise<Blob> }, { error?: string }> */
export class Preview extends Component { export class Preview extends Component {
canvasHolderRef = createRef(); canvasHolderRef = createRef();
canvasRef = createRef(); canvasRef = createRef();
@@ -20,7 +20,13 @@ export class Preview extends Component {
return h( return h(
'div', 'div',
{ class: 'center-parent', ref: this.canvasHolderRef }, { class: 'center-parent', ref: this.canvasHolderRef },
h('canvas', { class: 'center', ref: this.canvasRef }) this.state.error
? h(
'div',
{ class: 'center' },
`Could not retrieve preview: ${this.state.error}`
)
: h('canvas', { class: 'center', ref: this.canvasRef })
); );
} }
@@ -64,30 +70,32 @@ export class Preview extends Component {
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise(resolve => setTimeout(resolve, 1000));
while (this.canvasRef.current) { while (this.canvasRef.current) {
let blob;
try { try {
let blob = await this.props.getPreview(); blob = await this.props.getPreview();
} catch (error) {
// If ratio is known; decode resized image right away - it's a bit faster. this.setState({ error: error.message });
// If it isn't known, retrieve entire image to calculate ratio from its dimensions. return;
let img = await createImageBitmap(
blob,
ratio
? {
resizeWidth: canvas.width,
resizeHeight: canvas.height
}
: {}
);
if (!ratio) {
ratio = img.width / img.height;
updateCanvasSize();
}
canvasCtx.transferFromImageBitmap(img);
this.stats?.update();
} catch (e) {
console.warn(e);
} }
// If ratio is known; decode resized image right away - it's a bit faster.
// If it isn't known, retrieve entire image to calculate ratio from its dimensions.
let img = await createImageBitmap(
blob,
ratio
? {
resizeWidth: canvas.width,
resizeHeight: canvas.height
}
: {}
);
if (!ratio) {
ratio = img.width / img.height;
updateCanvasSize();
}
canvasCtx.transferFromImageBitmap(img);
await new Promise(resolve => requestAnimationFrame(resolve)); await new Promise(resolve => requestAnimationFrame(resolve));
this.stats?.update();
} }
} }