Here is my code. I'm trying to get (rf/dispatch [::set-words-and-letters…
- User
Here is my code. I'm trying to get (rf/dispatch [::set-words-and-letters word-collection]) to work. I'm working with re-frame through shadowcljs.
(ns spelling-bee.core (:require [re-frame.core :as rf] [reagent.dom :as rdom] [spelling-bee.config :as config] [spelling-bee.events :as events] [spelling-bee.views :as views]))
(defn dev-setup [] (when config/debug? (println "dev mode")))
(defn ^:dev/after-load mount-root [] (rf/clear-subscription-cache!) (let [root-el (.getElementById js/document "app")] (rdom/unmount-component-at-node root-el) (rdom/render [views/main-panel] root-el)))
(defn init [] (rf/dispatch-sync [::events/initialize-db]) (dev-setup) (mount-root))
(ns spelling-bee.db)
(def default-db {:name "default" :words #{} :common-letter #{} :letters #{}})
(ns spelling-bee.views (:require [re-frame.core :as rf] [spelling-bee.subs :as subs]))
;; Later this can be substituted with a database call to pull a list of words. (def word-collection #{"babyproof" "afar" "affray" "afro" "arbor" "array" "arroyo" "barb" "barf" "boar" "boor" "bray" "farro" "fora" "foray" "fray" "parry" "poor" "pray" "proof" "prop" "pyro" "roar" "roof" "ropy"})
(rf/dispatch [::set-words-and-letters word-collection])
(defn main-panel [] (let [name (rf/subscribe [::subs/name]) words (rf/subscribe [::subs/words])] [:div [:h1 "Hello from " @name]
[:h2 "Here are the words you have found:"] [:p "test: here are the words: " @words] ]))(ns spelling-bee.subs (:require [re-frame.core :as rf]))
(rf/reg-sub ::name (fn [db] (:name db)))
(rf/reg-sub ::words (fn [db] (:words db)))
(ns spelling-bee.events (:require [clojure.set :as set] [clojure.string :as str] [re-frame.core :as rf] [spelling-bee.db :as db] ))
(defn get-unique-letter-collection [word-set] (-> word-set vec str/join seq set))
(defn find-common-letter [word-set] (reduce set/intersection (map set (seq word-set))))
(rf/reg-event-db ::initialize-db (fn [_ _] db/default-db))
(rf/reg-event-db ::set-words-and-letters (fn [db [_ word-set]] (assoc db :words word-set :common-letter (find-common-letter word-set) :letters (get-unique-letter-collection word-set))))
- Assistant
Your code is well-organized and separates concerns into different namespaces. However, there are a few points where your code may have issues or could be improved:
-
Make sure the keyword
::set-words-and-lettersis defined and registered before dispatching it. -
You're passing
word-collectiontorf/dispatchwhich is good, but ensure that the keyword used (::set-words-and-letters) matches the one you registered in your events namespace. It's also a common practice to define your keywords at the top of the namespace or in a separate namespace where you define all keys to avoid typos. -
Are you sure that
::set-words-and-lettersis the right keyword being used in yourrf/reg-event-db? It might be that the problem is the usage of::which refers to a namespaced keyword, and that the namespace is not correctly mapped to your event handler. Double-check that the keyword you use torf/dispatchis the same as the one you've registered withrf/reg-event-db. -
The
find-common-letterandget-unique-letter-collectionfunctions seem not to be called. Remember thatfind-common-lettershould return the common letter that is present in all words, andget-unique-letter-collectionshould return unique letters found across all words. Make sure these functions are called somewhere in your event handler for::set-words-and-lettersto update the application state with the necessary data. -
Remember to define your application's subscription and event handlers before initializing the application with
init. Otherwise, they won't be available for use.
Additionally, you might want to consider the following modifications for better code structure and clarity:
-
Define your keywords (e.g.,
::initialize-db,::set-words-and-letters) in a separate namespace, e.g.,spelling-bee.keys, or at the top of the namespaces they are used in to prevent typos and make the code clearer. -
Ensure the
word-collectionyou're passing with the dispatch is the same data structure expected in your event handler functions. It's defined as a set, which is fine, but check if that's whatget-unique-letter-collectionandfind-common-letterexpect. -
It's a good idea to align the keyword name with the function it's triggering to avoid confusion. For instance, if
::set-words-and-lettersis meant to set a list of words and corresponding letters in the application state, name the registration accordingly, and make sure it's straightforward to understand what's happening when the event is dispatched.
With these points in mind, make sure to check the keywords and their usage, and ensure that the
word-collectionis what you expect it to be for all calculations and dispatches. -
- User
Reminder that this is my events concern:
(ns spelling-bee.events (:require [clojure.set :as set] [clojure.string :as str] [re-frame.core :as rf] [spelling-bee.db :as db] ))
(defn get-unique-letter-collection [word-set] (-> word-set vec str/join seq set))
(defn find-common-letter [word-set] (reduce set/intersection (map set (seq word-set))))
(rf/reg-event-db ::initialize-db (fn [_ _] db/default-db))
(rf/reg-event-db ::set-words-and-letters (fn [db [_ word-set]] (assoc db :words word-set :common-letter (find-common-letter word-set) :letters (get-unique-letter-collection word-set))))
- Assistant
Since you have shown that the
::set-words-and-lettersevent is indeed registered in yourspelling-bee.eventsnamespace, let's take a closer look at your setup to ensure this will work as expected:-
Make sure the namespace containing the event registration,
spelling-bee.events, has been required in the namespace from which you're dispatching the event. It looks like you've done this ([spelling-bee.events :as events]) in thespelling-bee.corenamespace, so this is likely not the issue. -
Confirm that the
::set-words-and-letterskeyword is properly namespaced. If you're using the::syntax, it refers to a namespaced keyword that's resolved within the current namespace. Make sure that when you dispatch the event:
(rf/dispatch [::set-words-and-letters word-collection])...you are in a namespace where
::set-words-and-lettersresolves to:spelling-bee.events/set-words-and-letters. If you are dispatching it fromspelling-bee.core, then you should have::events/set-words-and-letters, which assumeseventsalias is correctly referring to yourspelling-bee.eventsnamespace.-
If the
rf/dispatchis in the correct namespace but it's still not working, then check for any typos and ensure the keyword is the same both in the dispatch and in the event handler registration. -
The functions
get-unique-letter-collectionandfind-common-letterlacksetwrapping for the output from theseqfunction, which would cause an exception when theset/intersectionfunction is called with non-set arguments. You should convert the sequences to sets before callingset/intersection. Here is a corrected version offind-common-letter:
(defn find-common-letter [word-set] (reduce set/intersection (map set (map seq word-set))))-
Ensure that the
:wordsparameter insideget-unique-letter-collectionfunction is returning a set (as per yourassoc), otherwise, your view subscriptions might not work as expected. -
Make sure that you haven't inadvertently created a syntax error or other runtime error in your code that could be preventing the event dispatch from executing.
Once you have confirmed all the above points, your code should dispatch the event correctly. Remember that the
mount-rootfunction inspelling-bee.coreshould be executed after the re-frame event and subscription handlers have been set up to ensure that the component rendering relies on an up-to-date app state. -