From 1fb6edc71344ade9fdb21d1030a6e4fec3d9e2ee Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 17 Mar 2025 14:58:53 +0100 Subject: [PATCH] Introduce a `fetchSync` helper function for the `IccColorSpace` classes This reduces code duplication a tiny bit, which shouldn't hurt. --- src/core/icc_colorspace.js | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/core/icc_colorspace.js b/src/core/icc_colorspace.js index b269f887f..d955e3da0 100644 --- a/src/core/icc_colorspace.js +++ b/src/core/icc_colorspace.js @@ -28,6 +28,17 @@ import { shadow, warn } from "../shared/util.js"; import { ColorSpace } from "./colorspace.js"; import { QCMS } from "../../external/qcms/qcms_utils.js"; +function fetchSync(url) { + // Parsing and using color spaces is still synchronous, + // so we must load the wasm module synchronously. + // TODO: Make the color space stuff asynchronous and use fetch. + const xhr = new XMLHttpRequest(); + xhr.open("GET", url, false); + xhr.responseType = "arraybuffer"; + xhr.send(null); + return xhr.response; +} + class IccColorSpace extends ColorSpace { #transformer; @@ -132,7 +143,9 @@ class IccColorSpace extends ColorSpace { if (this.#useWasm) { if (this.#wasmUrl) { try { - this._module = QCMS._module = this.#load(); + this._module = QCMS._module = initSync({ + module: fetchSync(`${this.#wasmUrl}qcms_bg.wasm`), + }); isUsable = !!this._module; } catch (e) { warn(`ICCBased color space: "${e}".`); @@ -144,30 +157,16 @@ class IccColorSpace extends ColorSpace { return shadow(this, "isUsable", isUsable); } - - static #load() { - // Parsing and using color spaces is still synchronous, - // so we must load the wasm module synchronously. - // TODO: Make the color space stuff asynchronous and use fetch. - const filename = "qcms_bg.wasm"; - const xhr = new XMLHttpRequest(); - xhr.open("GET", `${this.#wasmUrl}${filename}`, false); - xhr.responseType = "arraybuffer"; - xhr.send(null); - return initSync({ module: xhr.response }); - } } class CmykICCBasedCS extends IccColorSpace { static #iccUrl; constructor() { - const filename = "CGATS001Compat-v2-micro.icc"; - const xhr = new XMLHttpRequest(); - xhr.open("GET", `${CmykICCBasedCS.#iccUrl}${filename}`, false); - xhr.responseType = "arraybuffer"; - xhr.send(null); - super(new Uint8Array(xhr.response), "DeviceCMYK", 4); + const iccProfile = new Uint8Array( + fetchSync(`${CmykICCBasedCS.#iccUrl}CGATS001Compat-v2-micro.icc`) + ); + super(iccProfile, "DeviceCMYK", 4); } static setOptions({ iccUrl }) {