Retry creation of web3 object if js/ethereum is not available right away

This commit is contained in:
Roman Volosovskyi 2022-01-20 13:50:51 +02:00
parent 1392f803e4
commit 5684b632e3
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
2 changed files with 30 additions and 11 deletions

View File

@ -4,7 +4,8 @@
[reagent "0.7.0" :exclusions [cljsjs/react cljsjs/react-dom]]
[re-frame "0.10.2"]
[cljs-web3 "0.19.0-0-9"]
[day8.re-frame/http-fx "0.1.5"]]
[day8.re-frame/http-fx "0.1.5"]
[javax.xml.bind/jaxb-api "2.3.1"]]
:plugins [[lein-cljsbuild "1.1.7"]]
:min-lein-version "2.5.3"

View File

@ -3,17 +3,35 @@
(def dapp-store? (re-find #"#dapp-store" (-> js/window .-location .-href)))
(def web3 (or dapp-store? (when (exists? js/web3)
js/web3)
(when (exists? js/ethereum)
(js/setTimeout (fn []
(.then (.enable js/ethereum)
#(re-frame/dispatch [:set-default-account (first %1)]))
(when js/ethereum.status
(.then (.getContactCode js/ethereum.status) #(re-frame/dispatch [:on-status-api :contact %]))))
100)
(js/Web3. js/ethereum))))
(defn get-web3 []
(or dapp-store? (if (exists? js/web3)
js/web3
(do (re-frame/dispatch [:set-in [:foo] "w3 doesn't exist"])
nil))
(if (exists? js/ethereum)
(do
(js/setTimeout (fn []
(.then (.enable js/ethereum)
#(re-frame/dispatch [:set-default-account (first %1)]))
(when js/ethereum.status
(.then (.getContactCode js/ethereum.status) #(re-frame/dispatch [:on-status-api :contact %]))))
100)
(js/Web3. js/ethereum))
(re-frame/dispatch [:set-in [:foo] "ethereum doesn exists"]))))
(def web3 (get-web3))
;; NOTE(rasom): injection of js code into Android's webview might take some time
;; and if js/web3 or js/ethereum are not available rignt away it makes sense to
;; retry a bit later
(when (nil? web3)
(js/setTimeout
(fn []
(when-let [web3 (get-web3)]
(re-frame/dispatch [:set-in [:web3] web3])
(when-not dapp-store?
(re-frame/dispatch [:set-in [:view-id] :web3]))))
300))
(def all
[{:title "Exchanges"