kv store implementation over realm

Former-commit-id: 95d0f566d7
This commit is contained in:
michaelr 2016-03-17 20:15:24 +02:00
parent 62642eadc1
commit 85f7a08380
7 changed files with 47 additions and 28 deletions

View File

@ -17,7 +17,8 @@
[messenger.comm.intercom :as intercom :refer [load-user-phone-number
load-user-whisper-identity]]
[messenger.protocol.protocol-handler :refer [make-handler]]
[syng-im.protocol.api :refer [init-protocol]]))
[syng-im.protocol.api :refer [init-protocol]]
[messenger.init :refer [init-simple-store]]))
(def app-registry (.-AppRegistry js/React))
@ -62,6 +63,7 @@
(defonce app-root (om/factory RootNode))
(defn init []
(init-simple-store)
(pubsub/setup-pub-sub)
(init-protocol (make-handler))
(load-user-phone-number)

6
src/messenger/init.cljs Normal file
View File

@ -0,0 +1,6 @@
(ns messenger.init
(:require [messenger.persistence.simple-kv-store :as kv]
[messenger.state :as state]))
(defn init-simple-store []
(swap! state/app-state assoc-in state/simple-store-path (kv/->SimpleKvStore)))

View File

@ -1,6 +1,6 @@
(ns messenger.models.protocol
(:require [messenger.state :as state]
[messenger.persistence.realm :as r]))
[syng-im.protocol.state.storage :as s]))
(defn set-initialized [initialized?]
(swap! state/app-state assoc-in state/protocol-initialized-path initialized?))
@ -8,12 +8,7 @@
;; TODO at least the private key has to be encrypted with user's password
(defn update-identity [identity]
(r/write
(fn []
(r/create :kv-store {:key :identity
:value (str identity)} true))))
(s/put (state/kv-store) :identity identity))
(defn current-identity []
(-> (r/get-by-field :kv-store :key :identity)
(r/single-cljs)
(r/decode-value)))
(s/get (state/kv-store) :identity))

View File

@ -61,23 +61,9 @@
(defn decode-value [{:keys [key value]}]
(read-string value))
(comment
(use 'figwheel-sidecar.repl-api)
(cljs-repl)
(def x (-> (get-by-field :kv-store :key :identity)
(single)))
(aget x 1)
(defn delete [obj]
(write (fn []
(.delete realm (-> (get-by-field :kv-store :key :identity)
(single)))))
(log/info (.keys js/Object realm))
(log/info (clj->js opts))
(clj->js (clj->js {:a [{:b 123}]}))
(.delete realm obj))))
(comment
)

View File

@ -0,0 +1,26 @@
(ns messenger.persistence.simple-kv-store
(:require [syng-im.protocol.state.storage :as st]
[messenger.persistence.realm :as r]))
(defrecord SimpleKvStore []
st/Storage
(put [_ key value]
(r/write
(fn []
(r/create :kv-store {:key key
:value (str value)} true))))
(get [_ key]
(-> (r/get-by-field :kv-store :key key)
(r/single-cljs)
(r/decode-value)))
(contains-key? [_ key]
(= 0
(.-length (r/get-by-field :kv-store :key key))))
(delete [_ key]
(-> (r/get-by-field :kv-store :key key)
(r/single)
(r/delete))))
(comment
)

View File

@ -2,12 +2,13 @@
(:require [syng-im.utils.logging :as log]
[messenger.constants :refer [ethereum-rpc-url]]
[messenger.comm.intercom :refer [protocol-initialized]]
[messenger.models.protocol :refer [current-identity]]))
[messenger.models.protocol :refer [current-identity]]
[messenger.state :refer [kv-store]]))
(defn make-handler []
{:ethereum-rpc-url ethereum-rpc-url
:identity (current-identity)
:storage nil
:storage (kv-store)
:handler (fn [{:keys [event-type] :as event}]
(log/info "Event:" (clj->js event))
(case event-type

View File

@ -38,5 +38,8 @@
(def pub-sub-path [:channels :pub-sub-publication])
(def user-notification-path [:user-notification])
(def protocol-initialized-path [:protocol-initialized])
(def simple-store-path [:simple-store])
(defn pub-sub-publisher [app] (get-in app pub-sub-bus-path))
(defn kv-store []
(get-in @app-state simple-store-path))