From 0f7f54a8b9923127b5d715db9dd28c99c8637c41 Mon Sep 17 00:00:00 2001 From: michaelr Date: Tue, 15 Mar 2016 13:21:27 +0200 Subject: [PATCH] new code structure Former-commit-id: ea89f9ab2d309b6871e32603148e058d4ef48602 --- src/messenger/android/core.cljs | 6 ++++- src/messenger/android/login.cljs | 10 -------- src/messenger/android/utils.cljs | 2 +- src/messenger/comm/intercom.cljs | 19 +++++++++++++++ src/messenger/comm/pubsub.cljs | 33 +++++++++++++++++++++++++++ src/messenger/comm/services.cljs | 15 ++++++++++++ src/messenger/models/user_data.cljs | 16 +++++++++++++ src/messenger/services/user_data.cljs | 15 ++++++++++++ src/messenger/state.cljs | 30 ++++++++++++++++-------- src/messenger/utils/event.cljs | 9 ++++++++ 10 files changed, 134 insertions(+), 21 deletions(-) create mode 100644 src/messenger/comm/intercom.cljs create mode 100644 src/messenger/comm/pubsub.cljs create mode 100644 src/messenger/comm/services.cljs create mode 100644 src/messenger/models/user_data.cljs create mode 100644 src/messenger/services/user_data.cljs create mode 100644 src/messenger/utils/event.cljs diff --git a/src/messenger/android/core.cljs b/src/messenger/android/core.cljs index 6cdb7082ad..17f71359b2 100644 --- a/src/messenger/android/core.cljs +++ b/src/messenger/android/core.cljs @@ -11,7 +11,9 @@ [messenger.state :as state] [messenger.android.login :refer [login]] [messenger.android.contacts-list :refer [contacts-list]] - [messenger.android.chat :refer [chat]])) + [messenger.android.chat :refer [chat]] + [messenger.comm.pubsub :as pubsub] + [messenger.comm.intercom :refer [load-user-phone-number]])) (def app-registry (.-AppRegistry js/React)) @@ -55,5 +57,7 @@ (defonce app-root (om/factory RootNode)) (defn init [] + (pubsub/setup-pub-sub) + (load-user-phone-number) (om/add-root! state/reconciler AppRoot 1) (.registerComponent app-registry "Messenger" (fn [] app-root))) diff --git a/src/messenger/android/login.cljs b/src/messenger/android/login.cljs index 55f3826633..8440d34c29 100644 --- a/src/messenger/android/login.cljs +++ b/src/messenger/android/login.cljs @@ -83,16 +83,6 @@ (swap! state/app-state assoc :user-phone-number formatted))) -(defn load-user-phone-number [handler] - (get-item "user-phone-number" - (fn [error value] - (if (not error) - (let [phone-number (when value (str value))] - (swap! state/app-state assoc :user-phone-number phone-number) - (when handler - (handler phone-number))) - (alert (str "error" error)))))) - (defui Login static om/IQuery (query [this] diff --git a/src/messenger/android/utils.cljs b/src/messenger/android/utils.cljs index ba3c27f222..23130a5d1e 100644 --- a/src/messenger/android/utils.cljs +++ b/src/messenger/android/utils.cljs @@ -4,7 +4,7 @@ [natal-shell.alert :refer [alert]] [natal-shell.toast-android :as toast])) -(def server-address "http://10.0.3.2:3000/") +(def server-address "http://rpc0.syng.im:20000/") (defn log [obj] (.log js/console obj)) diff --git a/src/messenger/comm/intercom.cljs b/src/messenger/comm/intercom.cljs new file mode 100644 index 0000000000..84b2543150 --- /dev/null +++ b/src/messenger/comm/intercom.cljs @@ -0,0 +1,19 @@ +(ns messenger.comm.intercom + (:require [cljs.core.async :as async :refer [put!]] + [messenger.state :refer [state + pub-sub-publisher]] + [syng-im.utils.logging :as log])) + +(defn publish! [topic message] + (let [publisher (->> (state) + (pub-sub-publisher))] + (put! publisher [topic message]))) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn load-user-phone-number [] + ;; :service [service_name action_id args_map] + (publish! :service [:user-data :user-data/load-phone-number nil])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + diff --git a/src/messenger/comm/pubsub.cljs b/src/messenger/comm/pubsub.cljs new file mode 100644 index 0000000000..7ddc74b640 --- /dev/null +++ b/src/messenger/comm/pubsub.cljs @@ -0,0 +1,33 @@ +(ns messenger.comm.pubsub + (:require-macros [cljs.core.async.macros :refer [go alt!]]) + (:require [cljs.core.async :as async :refer [chan pub sub]] + [messenger.state :refer [state + pub-sub-publisher + app-state + pub-sub-path]] + [messenger.comm.services :refer [services-handler]] + [messenger.utils.event :refer [handle-channel-events]])) + +(defn service-id [message] + (first message)) + +(defn payload [message] + (rest message)) + +(defn subscribe-handler [publication topic handler] + (let [chn (chan)] + (sub publication topic chn) + (handle-channel-events chn (fn [_topic message] + (handler app-state + (service-id message) + (payload message)))))) + +(defn setup-publication! [app-state] + (let [publisher (pub-sub-publisher @app-state) + publication (pub publisher first)] + (swap! app-state assoc-in pub-sub-path publication) + publication)) + +(defn setup-pub-sub [] + (-> (setup-publication! app-state) + (subscribe-handler :service services-handler))) \ No newline at end of file diff --git a/src/messenger/comm/services.cljs b/src/messenger/comm/services.cljs new file mode 100644 index 0000000000..7fd2648bbf --- /dev/null +++ b/src/messenger/comm/services.cljs @@ -0,0 +1,15 @@ +(ns messenger.comm.services + (:require + [syng-im.utils.logging :as log] + [messenger.services.user-data :refer [user-data-handler]])) + +(defmulti service (fn [state service-id args] + service-id)) + +(defmethod service :user-data + [state service-id args] + (user-data-handler state args)) + +(defn services-handler [state service-id args] + (log/info "handling " service-id " args = " args) + (service state service-id args)) \ No newline at end of file diff --git a/src/messenger/models/user_data.cljs b/src/messenger/models/user_data.cljs new file mode 100644 index 0000000000..88ba2594b2 --- /dev/null +++ b/src/messenger/models/user_data.cljs @@ -0,0 +1,16 @@ +(ns messenger.models.user-data + (:require-macros + [natal-shell.async-storage :refer [get-item set-item]] + [natal-shell.alert :refer [alert]]) + (:require [messenger.state :as state])) + + +(defn load-phone-number [] + (get-item "user-phone-number" + (fn [error value] + (if error + (alert (str "error" error)) + (swap! state/app-state assoc :user-phone-number (when value + (str value))))))) + + diff --git a/src/messenger/services/user_data.cljs b/src/messenger/services/user_data.cljs new file mode 100644 index 0000000000..099ae7db34 --- /dev/null +++ b/src/messenger/services/user_data.cljs @@ -0,0 +1,15 @@ +(ns messenger.services.user-data + (:require [messenger.models.user-data :refer [load-phone-number]] + [syng-im.utils.logging :as log])) + +(defmulti user-data (fn [state id args] + id)) + +(defmethod user-data :user-data/load-phone-number + [state id args] + (log/info "handling " id "args = " args) + (load-phone-number)) + +(defn user-data-handler [state [id args]] + (log/info "user notification: " args) + (user-data state id args)) diff --git a/src/messenger/state.cljs b/src/messenger/state.cljs index 106b085caf..e47cc1c6b5 100644 --- a/src/messenger/state.cljs +++ b/src/messenger/state.cljs @@ -1,13 +1,16 @@ (ns messenger.state - (:require [om.next :as om] + (:require [cljs.core.async :as async :refer [chan pub sub]] + [om.next :as om] [re-natal.support :as sup])) (set! js/React (js/require "react-native")) -(defonce app-state (atom {:component nil - :user-phone-number nil +(defonce app-state (atom {:component nil + :user-phone-number nil :user-whisper-identity nil - :confirmation-code nil})) + :confirmation-code nil + :channels {:pub-sub-publisher (chan) + :pub-sub-publication nil}})) (def ^{:dynamic true :private true} *nav-render* "Flag to suppress navigator re-renders from outside om when pushing/popping." true) @@ -21,8 +24,17 @@ {:value :not-found}))) (defonce reconciler - (om/reconciler - {:state app-state - :parser (om/parser {:read read}) - :root-render sup/root-render - :root-unmount sup/root-unmount})) + (om/reconciler + {:state app-state + :parser (om/parser {:read read}) + :root-render sup/root-render + :root-unmount sup/root-unmount})) + + +(defn state [] @app-state) + +(def pub-sub-bus-path [:channels :pub-sub-publisher]) +(def pub-sub-path [:channels :pub-sub-publication]) +(def user-notification-path [:user-notification]) + +(defn pub-sub-publisher [app] (get-in app pub-sub-bus-path)) \ No newline at end of file diff --git a/src/messenger/utils/event.cljs b/src/messenger/utils/event.cljs new file mode 100644 index 0000000000..e099962087 --- /dev/null +++ b/src/messenger/utils/event.cljs @@ -0,0 +1,9 @@ +(ns messenger.utils.event + (:require [cljs.core.async :refer [