From f477de962d581c319a3f56a94261960b0cb8d6c3 Mon Sep 17 00:00:00 2001 From: Aitor Date: Wed, 29 Nov 2023 15:27:17 +0100 Subject: [PATCH] :zap: Cache rasterizer resources --- frontend/src/app/rasterizer.cljs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/frontend/src/app/rasterizer.cljs b/frontend/src/app/rasterizer.cljs index 7c68c0208..e844bfd68 100644 --- a/frontend/src/app/rasterizer.cljs +++ b/frontend/src/app/rasterizer.cljs @@ -25,6 +25,8 @@ (declare send-success!) (declare send-failure!) +(defonce data-uri-cache (js/Map.)) + (defonce parent-origin (dm/str cf/public-uri)) @@ -86,13 +88,18 @@ (defn- fetch-as-data-uri "Fetches a URL as a Data URI." [uri] - (->> (http/send! {:uri uri - :response-type :blob - :method :get - :mode :cors - :omit-default-headers true}) - (rx/map :body) - (rx/mapcat wapi/read-file-as-data-url))) + (if (.has data-uri-cache uri) + (let [blob (.get data-uri-cache uri)] + (rx/from (.text blob))) + (->> (http/send! {:uri uri + :response-type :blob + :method :get + :mode :cors + :omit-default-headers true}) + (rx/map :body) + (rx/mapcat wapi/read-file-as-data-url) + (rx/tap (fn [data-uri] + (.set data-uri-cache uri (wapi/create-blob data-uri "text/plain"))))))) (defn- svg-update-image! "Updates an image in an SVG to a Data URI."