♻️ Refactor embed resouces

This commit is contained in:
alonso.torres 2021-05-21 14:51:02 +02:00 committed by Andrey Antukh
parent 42072f2584
commit 6a68e9c118
15 changed files with 312 additions and 260 deletions

View file

@ -0,0 +1,39 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) UXBOX Labs SL
(ns app.main.ui.shapes.embed
(:require
[app.main.ui.hooks :as hooks]
[app.util.http :as http]
[beicon.core :as rx]
[rumext.alpha :as mf]))
(def context (mf/create-context false))
(defn use-data-uris [urls]
(let [embed? (mf/use-ctx context)
urls (hooks/use-equal-memo urls)
uri-data (mf/use-ref {})
state (mf/use-state 0)]
(hooks/use-effect-ssr
(mf/deps embed? urls)
(fn []
(let [sub (when embed?
(->> (rx/from urls)
(rx/merge-map http/fetch-data-uri)
(rx/reduce conj {})
(rx/subs (fn [data]
(when-not (= data (mf/ref-val uri-data))
(mf/set-ref-val! uri-data data)
(reset! state inc))))))]
#(when sub
(rx/dispose! sub)))))
;; Use ref so if the urls are cached will return inmediately instead of the
;; next render
(when embed?
(mf/ref-val uri-data))))