From c38079e03b5bb63b9defcc2396d3b91237b98614 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 12 Nov 2016 11:49:43 +0100 Subject: [PATCH] Improved performance for obtaining the image size. --- src/uxbox/util/dom/files.cljs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/uxbox/util/dom/files.cljs b/src/uxbox/util/dom/files.cljs index 672f42ebd..6e4adc504 100644 --- a/src/uxbox/util/dom/files.cljs +++ b/src/uxbox/util/dom/files.cljs @@ -7,7 +7,8 @@ (ns uxbox.util.dom.files "A interop helpers for work with files." (:require [beicon.core :as rx] - [cuerdas.core :as str])) + [cuerdas.core :as str] + [uxbox.util.blob :as blob])) (defn read-as-text [file] @@ -27,17 +28,15 @@ (.readAsDataURL fr file)) (constantly nil)))) -(defn- retrieve-image-size - [data] - (rx/create - (fn [sick] - (let [img (js/Image.)] - (aset img "onload" #(sick (rx/end [(.-width img) (.-height img)]))) - (set! (.-src img) data)) - (constantly nil)))) - (defn get-image-size - "Get the real image size." [file] - (->> (read-as-dataurl file) - (rx/flat-map retrieve-image-size))) + (letfn [(on-load [sink img] + (let [size [(.-width img) (.-height img)]] + (sink (rx/end size)))) + (on-subscribe [sink] + (let [img (js/Image.) + uri (blob/create-uri file)] + (set! (.-onload img) (partial on-load sink img)) + (set! (.-src img) uri) + #(blob/revoke-uri uri)))] + (rx/create on-subscribe)))