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