From f877dcaff92322c138d96bb0981cb672ea37e11f Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Mon, 5 Nov 2018 08:04:47 +0000 Subject: [PATCH] Caught a bit of repetition in our utils --- src/lib/util.ts | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/lib/util.ts b/src/lib/util.ts index 4e974782..cbb511ed 100644 --- a/src/lib/util.ts +++ b/src/lib/util.ts @@ -57,16 +57,32 @@ export async function canvasEncode(data: ImageData, type: string, quality?: numb return blob; } +async function decodeImage(url: string): Promise { + const img = new Image(); + img.decoding = 'async'; + img.src = url; + const loaded = new Promise((resolve, reject) => { + img.onload = () => resolve(); + img.onerror = () => reject(Error('Image loading error')); + }); + + if (img.decode) { + // Nice off-thread way supported in Safari/Chrome. + // Safari throws on decode if the source is SVG. + // https://bugs.webkit.org/show_bug.cgi?id=188347 + await img.decode().catch(() => null); + } + + // Always await loaded, as we may have bailed due to the Safari bug above. + await loaded; + return img; +} + /** * Attempts to load the given URL as an image. */ -export function canDecodeImage(data: string): Promise { - return new Promise((resolve) => { - const img = document.createElement('img'); - img.src = data; - img.onload = _ => resolve(true); - img.onerror = _ => resolve(false); - }); +export function canDecodeImage(url: string): Promise { + return decodeImage(url).then(() => true, () => false); } export function blobToArrayBuffer(blob: Blob): Promise { @@ -108,24 +124,7 @@ export async function blobToImg(blob: Blob): Promise { const url = URL.createObjectURL(blob); try { - const img = new Image(); - img.decoding = 'async'; - img.src = url; - const loaded = new Promise((resolve, reject) => { - img.onload = () => resolve(); - img.onerror = () => reject(Error('Image loading error')); - }); - - if (img.decode) { - // Nice off-thread way supported in Safari/Chrome. - // Safari throws on decode if the source is SVG. - // https://bugs.webkit.org/show_bug.cgi?id=188347 - await img.decode().catch(() => null); - } - - // Always await loaded, as we may have bailed due to the Safari bug above. - await loaded; - return img; + return await decodeImage(url); } finally { URL.revokeObjectURL(url); }