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

View File

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

View File

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