Skip to content

Commit

Permalink
Start converting quests into a module
Browse files Browse the repository at this point in the history
- Add state module with ::register-state! and  ::valid?
- Module events now must end in !
- Move braid.client.quests.* -> braid.quests.*
- Remove modules having init fn
- Fix broken schema
- Print available events and subscriptions
  • Loading branch information
rafd committed Nov 17, 2017
1 parent 10511d3 commit 8ad32fc
Show file tree
Hide file tree
Showing 24 changed files with 157 additions and 103 deletions.
2 changes: 1 addition & 1 deletion scripts/video-convert.sh
Expand Up @@ -21,7 +21,7 @@
#
# 3. Move the gif to /resource/public/images/quests/
#
# 4. Add the image path to `braid.client.quests.list`
# 4. Add the image path to `braid.quests.list`
#
# For Linux, consider using byzanz: http://linux.die.net/man/1/byzanz-record
#
Expand Down
15 changes: 8 additions & 7 deletions src/braid/client/core/events.cljs
Expand Up @@ -2,16 +2,16 @@
(:require
[clojure.set :as set]
[clojure.string :as string]
[re-frame.core :refer [dispatch reg-event-db reg-event-fx reg-fx]]
[braid.client.quests.helpers :as quest-helpers]
[re-frame.core :refer [dispatch reg-event-fx reg-event-db reg-fx]]
[braid.quests.helpers :as quest-helpers]
[braid.client.router :as router]
[braid.client.routes :as routes]
[braid.client.schema :as schema]
[braid.client.state.helpers :as helpers :refer [key-by-id]]
[braid.client.store :as store]
[braid.client.sync :as sync]
[braid.client.xhr :refer [edn-xhr]]
[braid.common.util :as util]))
[braid.common.util :as util]
[braid.state.core :as braid.state.core]))

; TODO: handle callbacks declaratively too?
(reg-fx :websocket-send (fn [args] (when args (apply sync/chsk-send! args))))
Expand Down Expand Up @@ -90,12 +90,13 @@
; regex doesn't have lookbehind
(str (second (re-matches #"^(\s).*" m))
"#" (or (name->open-tag-id state tag-name)
tag-name))))))
tag-name))))))


(reg-event-fx
:initialize-db
(fn [_ _]
{:db store/initial-state
(fn [{db :db} _]
{:db (braid.state.core/initialize-state db)
:dispatch [:braid.client.gateway.events/initialize :log-in]}))

(reg-event-db
Expand Down
8 changes: 2 additions & 6 deletions src/braid/client/desktop/core.cljs
Expand Up @@ -17,9 +17,8 @@
[braid.client.ui.views.app :refer [app-view]]
[braid.client.uploads.events]
[braid.client.uploads.subs]
[braid.client.quests.events]
[braid.client.quests.handler :as quests]
[braid.client.quests.subs]
[braid.quests.handler :as quests]
; need to reference so that it loads its dependencies
[braid.core.modules]))

(enable-console-print!)
Expand All @@ -34,9 +33,6 @@
(fn [e]
(dispatch [:set-window-visibility
(= "visible" (.-visibilityState js/document))])))

(braid.core.modules/init)

(render)

(router/init)
Expand Down
11 changes: 0 additions & 11 deletions src/braid/client/quests/schema.cljs

This file was deleted.

1 change: 0 additions & 1 deletion src/braid/client/state/remote_handlers.cljs
Expand Up @@ -2,7 +2,6 @@
(:require
[re-frame.core :refer [dispatch]]
[braid.client.desktop.notify :as notify]
[braid.client.quests.remote-handlers]
[braid.client.router :as router]
[braid.client.sync :as sync]))

Expand Down
83 changes: 50 additions & 33 deletions src/braid/client/store.cljs
@@ -1,39 +1,26 @@
(ns braid.client.store
(:require
[schema.core :as s :include-macros true]
[braid.core.api :refer [subscribe dispatch]]
[braid.client.invites.schema :as invites]
[braid.client.mobile.auth-flow.schema :as mobile-auth-flow]
[braid.client.quests.schema :as quests]
[braid.common.schema :as app-schema]))

(def initial-state
(merge
{:login-state :gateway ; :ws-connect :app
:websocket-state {:connected? false
:next-reconnect nil}
:open-group-id nil
:threads {}
:group-threads {}
:users {}
:tags {}
:groups {}
:page {:type nil}
:session nil
:errors []
:preferences {}
:notifications {:window-visible? true
:unread-count 0}
:user {:open-thread-ids #{}
:subscribed-tag-ids #{}}
:focused-thread-id nil
:temp-threads {}}
quests/init-state
invites/init-state
mobile-auth-flow/init-state))

(def AppState
(merge
{:login-state (s/enum :auth-check :login-form :ws-connect :app)
{:login-state (s/enum :auth-check :login-form :ws-connect
:app :gateway)
:websocket-state {:connected? s/Bool
:next-reconnect (s/maybe s/Int)}
:csrf-token s/Str
:action s/Keyword
:user-auth {:user s/Any
:error s/Any
:checking? s/Bool
:mode (s/enum :register :log-in :request-password-reset)
:should-validate? s/Bool
:oauth-provider s/Any
:validations s/Any
:fields s/Any}
:open-group-id (s/maybe s/Uuid)
:threads {s/Uuid app-schema/MsgThread}
:group-threads {s/Uuid #{s/Uuid}}
Expand All @@ -56,10 +43,40 @@
:subscribed-tag-ids #{s/Uuid}}
:focused-thread-id (s/maybe s/Uuid)
:temp-threads {s/Uuid {:id s/Uuid
:new? s/Bool
:messages s/Any
:group-id s/Uuid
:mentioned-ids [s/Uuid]
:tag-ids [s/Uuid]
:new-message s/Str}}}
quests/QuestsAppState
invites/InvitesAppState
mobile-auth-flow/MobileAuthFlowAppState))
:new-message s/Str}}
:action-disabled? s/Bool}
invites/InvitesAppState))

(def initial-state
(merge
{:login-state :gateway ; :ws-connect :app
:websocket-state {:connected? false
:next-reconnect nil}
:open-group-id nil
:threads {}
:group-threads {}
:users {}
:tags {}
:groups {}
:page {:type nil}
:session nil
:errors []
:preferences {}
:notifications {:window-visible? true
:unread-count 0}
:user {:open-thread-ids #{}
:subscribed-tag-ids #{}}
:focused-thread-id nil
:temp-threads {}}
invites/init-state))

(dispatch [:braid.state/register-state!
initial-state
AppState])


(def check-app-state! (s/validator AppState))
2 changes: 1 addition & 1 deletion src/braid/client/ui/styles/header.cljs
Expand Up @@ -2,7 +2,7 @@
(:require
[garden.arithmetic :as m]
[garden.units :refer [em px rem]]
[braid.client.quests.styles :refer [quests-header]]
[braid.quests.styles :refer [quests-header]]
[braid.client.ui.styles.mixins :as mixins]
[braid.client.ui.styles.vars :as vars]))

Expand Down
14 changes: 8 additions & 6 deletions src/braid/client/ui/views/autocomplete.cljs
@@ -1,5 +1,6 @@
(ns braid.client.ui.views.autocomplete
(:require
[schema.core :as s]
[clojure.string :as string]
[clj-fuzzy.metrics :as fuzzy]
[goog.string :as gstring]
Expand Down Expand Up @@ -47,10 +48,11 @@
(or (simple-matches? m s)
(< (fuzzy/levenshtein m s) 2)))))

(api/dispatch [:braid.core/init-state
{::autocomplete-engines []}])
(api/dispatch [:braid.state/register-state!
{::autocomplete-engines []}
{::autocomplete-engines [s/Any]}])

(api/reg-event-fx :braid.core/register-autocomplete-engine
(api/reg-event-fx :braid.core/register-autocomplete-engine!
(fn [{db :db} [_ handler]]
{:db (update db ::autocomplete-engines conj handler)}))

Expand Down Expand Up @@ -80,7 +82,7 @@
[:div.extra "..."]])})))
@(subscribe [:group-bots] [open-group])))))

(api/dispatch [:braid.core/register-autocomplete-engine bot-autocomplete-engine])
(api/dispatch [:braid.core/register-autocomplete-engine! bot-autocomplete-engine])

; ... @<user> -> autocompletes user name
(defn user-autocomplete-engine [text]
Expand All @@ -105,7 +107,7 @@
[:div.name (user :nickname)]
[:div.extra (user :status)]])})))))))

(api/dispatch [:braid.core/register-autocomplete-engine user-autocomplete-engine])
(api/dispatch [:braid.core/register-autocomplete-engine! user-autocomplete-engine])

; ... #<tag> -> autocompletes tag
(defn tag-autocomplete-engine [text]
Expand Down Expand Up @@ -162,4 +164,4 @@
(remove nil?)
reverse)))))

(api/dispatch [:braid.core/register-autocomplete-engine tag-autocomplete-engine])
(api/dispatch [:braid.core/register-autocomplete-engine! tag-autocomplete-engine])
2 changes: 1 addition & 1 deletion src/braid/client/ui/views/header.cljs
Expand Up @@ -3,7 +3,7 @@
[reagent.ratom :refer-macros [reaction]]
[re-frame.core :refer [subscribe]]
[braid.client.helpers :refer [->color]]
[braid.client.quests.views :refer [quests-header-view quests-menu-view]]
[braid.quests.views :refer [quests-header-view quests-menu-view]]
[braid.client.routes :as routes]
[braid.client.ui.views.search-bar :refer [search-bar-view]]))

Expand Down
4 changes: 4 additions & 0 deletions src/braid/common/schema.cljc
Expand Up @@ -85,10 +85,12 @@
(def Group
{:id s/Uuid
:name s/Str
:slug s/Str
:admins #{s/Uuid}
:intro (s/maybe s/Str)
:avatar (s/maybe s/Str)
:public? s/Bool
:users-count s/Int
:bots #{BotDisplay}})

(def UserId
Expand All @@ -99,6 +101,7 @@
:nickname s/Str
:avatar s/Str
:group-ids [s/Uuid]
(s/optional-key :email) (s/maybe s/Str)
(s/optional-key :status) (s/enum :online :offline)})

(def Tag
Expand Down Expand Up @@ -141,6 +144,7 @@
{:quest-record/id QuestId
:quest-record/quest-id s/Keyword
:quest-record/user-id UserId
:quest-record/foo s/Int
:quest-record/progress s/Int
:quest-record/state (s/enum :inactive
:active
Expand Down
15 changes: 10 additions & 5 deletions src/braid/core/api.cljs
Expand Up @@ -2,10 +2,15 @@
(:require
[re-frame.core :as re-frame]))

(enable-console-print!)

(def dispatch re-frame/dispatch)
(def reg-event-fx re-frame/reg-event-fx)
(def reg-sub re-frame/reg-sub)
(def subscribe re-frame/subscribe)

(defn reg-event-fx [key handler]
(println "api event:" key)
(re-frame/reg-event-fx key handler))

(reg-event-fx :braid.core/init-state
(fn [{db :db} [_ state]]
{:db (merge db state)}))
(defn reg-sub [key handler]
(println "api sub:" key)
(re-frame/reg-sub key handler))
7 changes: 3 additions & 4 deletions src/braid/core/modules.cljs
@@ -1,7 +1,6 @@
(ns braid.core.modules
(:require
[braid.client.ui.views.autocomplete]
[braid.emoji.core]))

(defn init []
(braid.emoji.core/init))
[braid.emoji.core]
[braid.state.core]
[braid.quests.core]))
3 changes: 1 addition & 2 deletions src/braid/emoji/core.cljs
Expand Up @@ -37,5 +37,4 @@
(str "(" emoji-name ")") k))
{:react-key k}])}))))))

(defn init []
(api/dispatch [:braid.core/register-autocomplete-engine autocomplete-handler]))
(api/dispatch [:braid.core/register-autocomplete-engine! autocomplete-handler])
11 changes: 11 additions & 0 deletions src/braid/quests/core.cljs
@@ -0,0 +1,11 @@
(ns braid.quests.core
(:require
[braid.core.api :as api]
[braid.quests.events]
[braid.quests.subs]
[braid.quests.remote-handlers]
[braid.common.schema :refer [QuestRecordId QuestRecord]]))

(api/dispatch [:braid.state/register-state!
{::quest-records {}}
{::quest-records {QuestRecordId QuestRecord}}])
@@ -1,9 +1,9 @@
(ns braid.client.quests.events
(ns braid.quests.events
(:require
[cljs-uuid-utils.core :as uuid]
[re-frame.core :refer [reg-event-db reg-event-fx]]
[braid.client.quests.helpers :as helpers]
[braid.client.quests.list :refer [quests-by-id]]))
[braid.quests.helpers :as helpers]
[braid.quests.list :refer [quests-by-id]]))

(defn make-quest-record [{:keys [quest-id]}]
{:quest-record/id (uuid/make-random-squuid)
Expand Down
@@ -1,4 +1,4 @@
(ns braid.client.quests.handler
(ns braid.quests.handler
(:require
[re-frame.core :as rf]))

Expand Down

0 comments on commit 8ad32fc

Please sign in to comment.