Simplify storages usage

This commit is contained in:
Julien Eluard 2018-09-24 15:35:02 +02:00
parent 82df4f6308
commit 83c9755d32
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
6 changed files with 52 additions and 44 deletions

View File

@ -4,27 +4,27 @@
:documentation "..."}
lifecycle
{:on-activated [@events/fetch-all-posts]}
{:on-activated [fetch-all-posts]}
events/fetch-all-posts
[@events/ethereum.logs {:address "0xfa28ec7198028438514b49a3cf353bca5541ce1d"
[ethereum.logs {:address "0xfa28ec7198028438514b49a3cf353bca5541ce1d"
:topics ["PeepEth()"]
:inputs [{:name :hash :type :string}] ;; Allows to decode transaction data
:on-log [@events/fetch-ipfs]}] ;; A map of decoded data will be injected
events/fetch-ipfs
(let [{:keys [hash]} @properties]
[@queries/ipfs.get {:hash hash
:on-success [@events/db.append {:path [:all-posts]}]}])
(let [{hash :hash} properties]
[ipfs.get {:hash hash
:on-success [db.append {:path [:all-posts]}]}])
queries/all-posts
[@queries/db.get {:path [:all-posts]
[db.get {:path [:all-posts]
:limit 20}]
views/post
;; TODO get account details
;; handle threads
(let [{content :content address :untrustedAddress timestamp :untrustedTimestamp} @properties]
(let [{content :content address :untrustedAddress timestamp :untrustedTimestamp} properties]
[view {}
[view {}
[text {}
@ -35,13 +35,13 @@
content]])
views/main
[screen {:style @styles/screen}
[screen {:style screen}
[toolbar {}
[text {}
[@i18n/title {}]]]
(let [posts @queries/all-posts]
[list {:data posts
:template [@views/post]}])]
:template [post]}])]
styles/screen
{:background-color #?(:android :green
@ -52,5 +52,5 @@
{:en "Peepeth !!"}
hooks/main.pipelette
{:view @views/main}}
{:view main}}

View File

@ -3,10 +3,7 @@
[pluto.components.html :as html]
[pluto.reader :as reader]
[pluto.host :as host]
[pluto.storage :as storage]
[pluto.storage.http :as http]
[pluto.storage.gist :as gist]
[pluto.storage.ipfs :as ipfs]
[pluto.storages :as storages]
[reagent.core :as reagent]
[reagent.dom :as dom]
[re-frame.core :as re-frame]
@ -42,18 +39,6 @@
[:li
[:span [:b (str type)] (pr-str (dissoc m :type))]]))]))
(defn storage-for [type]
(condp = type
"url" (http/HTTPStorage.)
"gist" (gist/GistStorage.)
"ipfs" (ipfs/IPFSStorage. "https://cors.io/?https://gateway.ipfs.io")))
(defn fetch [uri cb]
(let [[type id] (string/split uri "@")]
(storage/fetch
(storage-for type)
{:value id} cb)))
(def hook
(reify host/AppHook
(id [_] :main)
@ -86,4 +71,4 @@
[s el el-errors]
(dom/unmount-component-at-node el)
(dom/unmount-component-at-node el-errors)
(fetch s #(render-result % el el-errors)))
(storages/fetch s #(render-result % el el-errors)))

View File

@ -39,7 +39,7 @@
<main>
<div id="selection">
Load demo extension from
<button disabled onclick="load('ipfs@QmSKP6f2uUsFq4mk1Afe4ZktxwQifrLb4xRQYNE1LxidKz')">IPFS</button>
<button onclick="load('ipfs@QmSKP6f2uUsFq4mk1Afe4ZktxwQifrLb4xRQYNE1LxidKz')">IPFS</button>
<button onclick="load('url@assets/extensions/demo')">HTTP</button>
<button onclick="load('gist@jeluard/1b41c8194c1658e1b6fc0bde4aa6db95')">Gist</button>
</div>

View File

@ -1,5 +1,6 @@
clojure -A:examples -m cljs.main -O advanced -d website/static/js -o website/static/js/pluto.js -c pluto.examples
cp -R examples/resources/extensions website/static
cp figwheel/resources/public/index.html website/pages/examples.html
cd website
GIT_USER=jeluard\
CURRENT_BRANCH=#2 \

19
src/pluto/storages.cljc Normal file
View File

@ -0,0 +1,19 @@
(ns pluto.storages
(:require [clojure.string :as string]
[pluto.storage :as storage]
[pluto.storage.http :as http]
[pluto.storage.gist :as gist]
[pluto.storage.ipfs :as ipfs]))
(def all
{"url" (http/HTTPStorage.)
"gist" (gist/GistStorage.)
"ipfs" (ipfs/IPFSStorage. "https://gateway.ipfs.io")})
(defn fetch [uri cb]
(when (and uri cb))
(let [[type id] (string/split uri "@")]
(when-let [s (get all type)]
(storage/fetch
s
{:value id} cb))))

View File

@ -39,8 +39,9 @@
<main>
<div id="selection">
Load demo extension from
<button disabled onclick="load('ipfs:QmSKP6f2uUsFq4mk1Afe4ZktxwQifrLb4xRQYNE1LxidKz')">IPFS</button>
<button onclick="load('url:extensions/demo')">HTTP</button>
<button onclick="load('ipfs@QmSKP6f2uUsFq4mk1Afe4ZktxwQifrLb4xRQYNE1LxidKz')">IPFS</button>
<button onclick="load('url@assets/extensions/demo')">HTTP</button>
<button onclick="load('gist@jeluard/1b41c8194c1658e1b6fc0bde4aa6db95')">Gist</button>
</div>
<div id="extension">
<iframe id="frame" srcdoc="<body><main></main></body>"></iframe>
@ -49,3 +50,5 @@
</main>
</body>
</html>
<script src="cljs-out/figwheel/dev-main.js"></script>