mirror of
https://github.com/penpot/penpot.git
synced 2025-01-23 23:18:48 -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]))
|
||||
|
||||
(defonce cache (atom {}))
|
||||
(defonce pending (atom {}))
|
||||
|
||||
(defn with-cache
|
||||
[{:keys [key max-age]} observable]
|
||||
(let [entry (get @cache key)
|
||||
pending-entry (get @pending key)
|
||||
|
||||
age (when entry
|
||||
(dt/diff (dt/now)
|
||||
(:created-at entry)))]
|
||||
(if (and (some? entry) (< age max-age))
|
||||
(cond
|
||||
(and (some? entry) (< age max-age))
|
||||
(rx/of (:data entry))
|
||||
(->> observable
|
||||
(rx/tap
|
||||
(fn [data]
|
||||
(let [entry {:created-at (dt/now) :data data}]
|
||||
(swap! cache assoc key entry))))))))
|
||||
|
||||
(some? pending-entry)
|
||||
pending-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))
|
||||
|
||||
([uri throw-err?]
|
||||
(c/with-cache {:key uri :max-age (dt/duration {:hours 4})}
|
||||
(let [request-stream
|
||||
(send! {:method :get
|
||||
:uri uri
|
||||
:response-type :blob
|
||||
:omit-default-headers true})
|
||||
(let [request-str
|
||||
(->> (send! {:method :get
|
||||
:uri uri
|
||||
:response-type :blob
|
||||
: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
|
||||
(if throw-err?
|
||||
(rx/tap #(when-not (and (>= (:status %) 200) (< (:status %) 300))
|
||||
;; HTTP ERRROR
|
||||
(throw (js/Error. "Error fetching data uri" #js {:cause (clj->js %)})))
|
||||
request-stream)
|
||||
(rx/filter #(= 200 (:status %))
|
||||
request-stream))]
|
||||
(->> request-stream
|
||||
(rx/map :body)
|
||||
(rx/mapcat wapi/read-file-as-data-url)
|
||||
(rx/map #(hash-map uri %)))))))
|
||||
(rx/map :body)
|
||||
(rx/mapcat wapi/read-file-as-data-url)
|
||||
(rx/map #(hash-map uri %))
|
||||
(c/with-cache {:key uri :max-age (dt/duration {:hours 4})}))]
|
||||
|
||||
;; We need to check `throw-err?` after the cache is resolved otherwise we cannot cache request
|
||||
;; with different values of throw-err. By default we throw always the exception and then we just
|
||||
;; ignore when `throw-err?` is `true`
|
||||
(if (not throw-err?)
|
||||
(->> request-str (rx/catch #(rx/empty)))
|
||||
request-str))))
|
||||
|
||||
(defn fetch-text [url]
|
||||
(c/with-cache {:key url :max-age (dt/duration {:hours 4})}
|
||||
(->> (send!
|
||||
{:method :get
|
||||
:mode :cors
|
||||
:omit-default-headers true
|
||||
:uri url
|
||||
:response-type :text})
|
||||
(rx/map :body))))
|
||||
(->> (send!
|
||||
{:method :get
|
||||
:mode :cors
|
||||
:omit-default-headers true
|
||||
:uri url
|
||||
:response-type :text})
|
||||
(rx/map :body)
|
||||
(c/with-cache {:key url :max-age (dt/duration {:hours 4})})))
|
||||
|
|
Loading…
Add table
Reference in a new issue