🐛 Fix problem when loading fonts

This commit is contained in:
alonso.torres 2023-03-08 14:30:25 +01:00
parent e170011e3c
commit b2c3dc1504

View file

@ -194,10 +194,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn ensure-loaded! (defn ensure-loaded!
([id] [id]
(p/create (fn [resolve]
(ensure-loaded! id resolve))))
([id on-loaded]
(log/debug :action "try-ensure-loaded!" :font-id id) (log/debug :action "try-ensure-loaded!" :font-id id)
(if-not (exists? js/window) (if-not (exists? js/window)
;; If we are in the worker environment, we just mark it as loaded ;; If we are in the worker environment, we just mark it as loaded
@ -206,25 +203,24 @@
(swap! loaded conj id) (swap! loaded conj id)
(p/resolved id)) (p/resolved id))
(when-let [font (get @fontsdb id)] (let [font (get @fontsdb id)]
(cond (cond
(nil? font)
(p/resolved id)
;; Font already loaded, we just continue ;; Font already loaded, we just continue
(contains? @loaded id) (contains? @loaded id)
(p/do (p/resolved id)
(on-loaded id)
id)
;; Font is currently downloading. We attach the caller to the promise ;; Font is currently downloading. We attach the caller to the promise
(contains? @loading id) (contains? @loading id)
(-> (get @loading id) (p/resolved (get @loading id))
(p/then #(do (on-loaded id) id)))
;; First caller, we create the promise and then wait ;; First caller, we create the promise and then wait
:else :else
(let [on-load (fn [resolve] (let [on-load (fn [resolve]
(swap! loaded conj id) (swap! loaded conj id)
(swap! loading dissoc id) (swap! loading dissoc id)
(on-loaded id)
(resolve id)) (resolve id))
load-p (p/create load-p (p/create
@ -234,7 +230,7 @@
(load-font))))] (load-font))))]
(swap! loading assoc id load-p) (swap! loading assoc id load-p)
load-p)))))) load-p)))))
(defn ready (defn ready
[cb] [cb]