store commands.js

Former-commit-id: 72768fc7dd
This commit is contained in:
Roman Volosovskyi 2016-06-09 14:41:25 +03:00
parent 2920984b0c
commit 6a9d1af886
3 changed files with 31 additions and 15 deletions

View File

@ -4,7 +4,8 @@
[status-im.utils.handlers :as u]
[status-im.components.react :as r]
[status-im.utils.utils :refer [http-get toast]]
[clojure.string :as s]))
[clojure.string :as s]
[status-im.persistence.realm :as realm]))
;; commands loading flow
; ┌────────────────────────────┐
@ -44,8 +45,8 @@
; │ │ │ │
; │ │ │ ▼
; │ │ │ ┌────────────────────────────┐
; │ │ ▼ │ save commands
; │ │ ┌────────────────────────┐ │ save js ?
; │ │ ▼ │
; │ │ ┌────────────────────────┐ │ save commands-js │
; │ │ │the dapp emit a message │ │ add some API object form │
; │ └─▶│ saying js is broken │ │ otto to app-db │
; │ └────────────────────────┘ └────────────────────────────┘
@ -68,6 +69,12 @@
(def commands-js "commands.js")
(defn load-commands!
[_ [identity]]
(if-let [{:keys [file]} (realm/get-one-by-field :commands :chat-id identity)]
(dispatch [::parse-commands! identity file])
(dispatch [::fetch-commands! identity])))
(defn fetch-commands!
[db [identity]]
(when-let [url (:dapp-url (get-in db [:chats identity]))]
@ -103,27 +110,26 @@
(parse file
(fn [result]
(let [commands (json->clj result)]
(dispatch [::add-commands identity commands])))
(dispatch [::add-commands identity file commands])))
#(dispatch [::loading-failed! identity ::error-in-jail %])))
(defn validate-hash
[db [identity file]]
(let [valid? (= (get-hash-by-dentity db identity)
(get-hash-by-file file))]
(println "hsh " (hash file))
(assoc db ::valid-hash valid?)))
(defn save-commands!
[db]
#_(realm/save (::new-commands db)))
(defn add-commands
[db [id commands-obj]]
[db [id _ commands-obj]]
(let [commands (:commands commands-obj)]
(-> db
(update-in [:chats id :commands] merge commands)
(assoc db ::new-commands commands))))
(defn save-commands-js!
[_ [id file]]
(realm/create-object :commands {:chat-id id :file file}))
(defn loading-failed
[db [id reason details]]
(let [url (get-in db [:chats id :dapp-url])]
@ -133,7 +139,8 @@
(name reason)
details]))))
(reg-handler :load-commands! (u/side-effect! fetch-commands!))
(reg-handler :load-commands! (u/side-effect! load-commands!))
(reg-handler ::fetch-commands! (u/side-effect! fetch-commands!))
(reg-handler ::validate-hash
(after dispatch-loaded!)
@ -142,7 +149,7 @@
(reg-handler ::parse-commands! (u/side-effect! parse-commands!))
(reg-handler ::add-commands
(after save-commands!)
(after save-commands-js!)
add-commands)
(reg-handler ::loading-failed! (u/side-effect! loading-failed))

View File

@ -1,6 +1,5 @@
(ns status-im.navigation.handlers
(:require [re-frame.core :refer [register-handler dispatch debug enrich
after]]))
(:require [re-frame.core :refer [register-handler dispatch debug enrich after]]))
(defn push-view [db view-id]
(-> db

View File

@ -1,7 +1,6 @@
(ns status-im.persistence.realm
(:require [cljs.reader :refer [read-string]]
[status-im.components.styles :refer [default-chat-color]]
[status-im.utils.logging :as log]
[status-im.utils.types :refer [to-string]]
[status-im.utils.utils :as u])
(:refer-clojure :exclude [exists?]))
@ -56,6 +55,10 @@
:dapp-hash {:type :int
:optional true}
:last-msg-id "string"}}
{:name :commands
:primaryKey :chat-id
:properties {:chat-id "string"
:file "string"}}
{:name :tag
:primaryKey :name
:properties {:name "string"
@ -98,6 +101,10 @@
([schema-name obj update?]
(.create realm (to-string schema-name) (clj->js obj) update?)))
(defn create-object
[schema-name obj]
(write (fn [] (create schema-name obj true))))
(defmulti to-query (fn [schema-name operator field value]
operator))
@ -162,3 +169,6 @@
(defn collection->map [collection]
(-> (.map collection (fn [object _ _] object))
(js->clj :keywordize-keys true)))
(defn get-one-by-field [schema-name field value]
(single-cljs (get-by-field schema-name field value)))