-
Notifications
You must be signed in to change notification settings - Fork 986
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from syng-im/new-structure
new code structure
- Loading branch information
Showing
10 changed files
with
134 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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])) | ||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))))))) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(ns messenger.utils.event | ||
(:require [cljs.core.async :refer [<!]]) | ||
(:require-macros [cljs.core.async.macros :refer [go]])) | ||
|
||
(defn handle-channel-events [chan handler] | ||
(go (loop [[msg args] (<! chan)] | ||
(when msg | ||
(handler msg args) | ||
(recur (<! chan)))))) |