0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-19 11:11:21 -05:00

Improved performance for obtaining the image size.

This commit is contained in:
Andrey Antukh 2016-11-12 11:49:43 +01:00
parent dcb35084a1
commit c38079e03b
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95

View file

@ -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)))