mirror of
https://github.com/penpot/penpot.git
synced 2025-01-24 07:29:08 -05:00
Merge pull request #1954 from penpot/alotor-fix-cache-embeds
🐛 Fix problems with embed data cache
This commit is contained in:
commit
0828d43f8f
2 changed files with 51 additions and 32 deletions
|
@ -10,17 +10,34 @@
|
||||||
[beicon.core :as rx]))
|
[beicon.core :as rx]))
|
||||||
|
|
||||||
(defonce cache (atom {}))
|
(defonce cache (atom {}))
|
||||||
|
(defonce pending (atom {}))
|
||||||
|
|
||||||
(defn with-cache
|
(defn with-cache
|
||||||
[{:keys [key max-age]} observable]
|
[{:keys [key max-age]} observable]
|
||||||
(let [entry (get @cache key)
|
(let [entry (get @cache key)
|
||||||
|
pending-entry (get @pending key)
|
||||||
|
|
||||||
age (when entry
|
age (when entry
|
||||||
(dt/diff (dt/now)
|
(dt/diff (dt/now)
|
||||||
(:created-at entry)))]
|
(:created-at entry)))]
|
||||||
(if (and (some? entry) (< age max-age))
|
(cond
|
||||||
|
(and (some? entry) (< age max-age))
|
||||||
(rx/of (:data entry))
|
(rx/of (:data entry))
|
||||||
(->> observable
|
|
||||||
(rx/tap
|
(some? pending-entry)
|
||||||
(fn [data]
|
pending-entry
|
||||||
(let [entry {:created-at (dt/now) :data data}]
|
|
||||||
(swap! cache assoc key entry))))))))
|
:else
|
||||||
|
(let [subject (rx/subject)]
|
||||||
|
(swap! pending assoc key subject)
|
||||||
|
(->> observable
|
||||||
|
(rx/catch #(do (rx/error! subject %)
|
||||||
|
(swap! pending dissoc key)
|
||||||
|
(rx/throw %)))
|
||||||
|
(rx/tap
|
||||||
|
(fn [data]
|
||||||
|
(let [entry {:created-at (dt/now) :data data}]
|
||||||
|
(swap! cache assoc key entry))
|
||||||
|
(rx/push! subject data)
|
||||||
|
(rx/end! subject)
|
||||||
|
(swap! pending dissoc key))))))))
|
||||||
|
|
|
@ -173,32 +173,34 @@
|
||||||
(fetch-data-uri uri false))
|
(fetch-data-uri uri false))
|
||||||
|
|
||||||
([uri throw-err?]
|
([uri throw-err?]
|
||||||
(c/with-cache {:key uri :max-age (dt/duration {:hours 4})}
|
(let [request-str
|
||||||
(let [request-stream
|
(->> (send! {:method :get
|
||||||
(send! {:method :get
|
:uri uri
|
||||||
:uri uri
|
:response-type :blob
|
||||||
:response-type :blob
|
:omit-default-headers true})
|
||||||
:omit-default-headers true})
|
(rx/tap
|
||||||
|
(fn [resp]
|
||||||
|
(when (or (< (:status resp) 200) (>= (:status resp) 300))
|
||||||
|
(rx/throw (js/Error. "Error fetching data uri" #js {:cause (clj->js resp)})))))
|
||||||
|
|
||||||
request-stream
|
(rx/map :body)
|
||||||
(if throw-err?
|
(rx/mapcat wapi/read-file-as-data-url)
|
||||||
(rx/tap #(when-not (and (>= (:status %) 200) (< (:status %) 300))
|
(rx/map #(hash-map uri %))
|
||||||
;; HTTP ERRROR
|
(c/with-cache {:key uri :max-age (dt/duration {:hours 4})}))]
|
||||||
(throw (js/Error. "Error fetching data uri" #js {:cause (clj->js %)})))
|
|
||||||
request-stream)
|
;; We need to check `throw-err?` after the cache is resolved otherwise we cannot cache request
|
||||||
(rx/filter #(= 200 (:status %))
|
;; with different values of throw-err. By default we throw always the exception and then we just
|
||||||
request-stream))]
|
;; ignore when `throw-err?` is `true`
|
||||||
(->> request-stream
|
(if (not throw-err?)
|
||||||
(rx/map :body)
|
(->> request-str (rx/catch #(rx/empty)))
|
||||||
(rx/mapcat wapi/read-file-as-data-url)
|
request-str))))
|
||||||
(rx/map #(hash-map uri %)))))))
|
|
||||||
|
|
||||||
(defn fetch-text [url]
|
(defn fetch-text [url]
|
||||||
(c/with-cache {:key url :max-age (dt/duration {:hours 4})}
|
(->> (send!
|
||||||
(->> (send!
|
{:method :get
|
||||||
{:method :get
|
:mode :cors
|
||||||
:mode :cors
|
:omit-default-headers true
|
||||||
:omit-default-headers true
|
:uri url
|
||||||
:uri url
|
:response-type :text})
|
||||||
:response-type :text})
|
(rx/map :body)
|
||||||
(rx/map :body))))
|
(c/with-cache {:key url :max-age (dt/duration {:hours 4})})))
|
||||||
|
|
Loading…
Add table
Reference in a new issue