🐛 Disable broadcast-channel when it is not available (mainly safari)

This commit is contained in:
Andrey Antukh 2022-10-04 23:19:28 +02:00 committed by Alejandro Alonso
parent 953607fc4a
commit 5a06749664

View file

@ -19,24 +19,29 @@
;; The main broadcast channel instance, used for emit data ;; The main broadcast channel instance, used for emit data
(defonce default-channel (defonce default-channel
(js/BroadcastChannel. default-topic)) (when (exists? js/BroadcastChannel)
(js/BroadcastChannel. default-topic)))
(defonce stream (defonce stream
(if (exists? js/BroadcastChannel)
(->> (rx/create (fn [subs] (->> (rx/create (fn [subs]
(let [chan (js/BroadcastChannel. default-topic)] (let [chan (js/BroadcastChannel. default-topic)]
(unchecked-set chan "onmessage" #(rx/push! subs (unchecked-get % "data"))) (unchecked-set chan "onmessage" #(rx/push! subs (unchecked-get % "data")))
(fn [] (.close ^js chan))))) (fn [] (.close ^js chan)))))
(rx/map t/decode-str) (rx/map t/decode-str)
(rx/map map->BroadcastMessage) (rx/map map->BroadcastMessage)
(rx/share))) (rx/share))
(rx/subject)))
(defn emit! (defn emit!
([type data] ([type data]
(when default-channel
(.postMessage ^js default-channel (t/encode-str {:id nil :type type :data data})) (.postMessage ^js default-channel (t/encode-str {:id nil :type type :data data}))
nil)
([id type data]
(.postMessage ^js default-channel (t/encode-str {:id id :type type :data data}))
nil)) nil))
([id type data]
(when default-channel
(.postMessage ^js default-channel (t/encode-str {:id id :type type :data data}))
nil)))
(defn type? (defn type?
([type] ([type]