Skip to content

Instantly share code, notes, and snippets.

@thosmos
Created February 16, 2019 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thosmos/93ee78b2d508630b13e31cc9e858bdde to your computer and use it in GitHub Desktop.
Save thosmos/93ee78b2d508630b13e31cc9e858bdde to your computer and use it in GitHub Desktop.
fulcro code-split routing
(defsc AboutComp [this {:keys [about/hello]}]
{:query [:about/hello]
:initial-state {:about/hello "Hello from the About module!"}}
(do
(debug "Rendering AboutComp")
(segment
(header
"ABOUT COMP")
(message
hello))))
(fm/defmutation set-about-state [params]
(action [{:keys [state]}]
(try (let [{:keys [class]} (:about @st/pending-route)]
(swap! st/pending-route assoc-in [:about :is-loaded] true)
(swap! state (fn [s] (as-> s state-map
(assoc-in state-map [:PAGE/by-id :about :about-comp :about/hello] "Hello from the About module!")
(prim/set-query* state-map class {:query [{:about-comp (prim/get-query AboutComp state-map)}]})))))
(catch js/Error err (js/console.error "ABOUT TRANSACT ERROR" err)))))
(defn init-module []
(js/console.log ":about (init-module)!")
(try (let [{:keys [reconciler is-loaded] :as info} (:about @st/pending-route)]
(if is-loaded
(js/console.error "already loaded!?!?")
(if-not info
(js/console.error "AboutComp was unable to find pending-route info!")
(do
(js/console.log "setting :about :is-loaded")
(prim/ptransact! reconciler `[(set-about-state)
(dr/target-ready {:target [:PAGE/by-id :about]})])
(js/console.log "about code finished")))))
(catch js/Error err (js/console.error "ERROR IN ABOUT INIT" err))))
;; initialize during module loading
(js/console.log "initializing :about module")
(init-module)
(defsc-route-target AboutPage [this props]
{:query [{:about-comp '[*]}]
;:query [{:about-comp (prim/get-query AboutComp)}]
:ident (fn [] [:PAGE/by-id :about])
:initial-state (fn [params] {:about-comp {}})
;:initial-state (fn [params] {:about-comp (prim/get-initial-state AboutComp {})})
:route-segment (fn [] ["about"])
:route-cancelled (fn [_])
:will-enter (fn [reconciler route-params]
(if (get-in @st/pending-route [:about :is-loaded])
(dr/route-immediate [:PAGE/by-id :about])
(dr/route-deferred [:PAGE/by-id :about]
(fn []
(js/console.log "triggering load of :about")
(swap! st/pending-route assoc :about {:reconciler reconciler
:class AboutPage})
;; trigger a code load
(-> (loader/load "about")
(.then
(fn []
(js/console.log "module load succeeded"))
(fn [error]
(js/console.warn "about module failed to load" error))))))))
:will-leave (fn [_]
(js/console.log "Leaving about")
true)}
(do
(js/console.log "Render AboutPage")
(dom/div
(dom/h3 "AboutPage!")
(let [factory (some-> this (prim/get-query (prim/component->state-map this)) prim/query->ast1 :component prim/factory)]
(if factory
(do
(js/console.log "factory is present")
(cljs.pprint/pprint (some-> this (prim/get-query (prim/component->state-map this)) prim/query->ast1 :component))
(factory (:about-comp props)))
(do
(js/console.log "no factory")
(cljs.pprint/pprint (some-> this prim/get-query prim/query->ast))
(js/console.log "about-comp props" (:about-comp props))
(dom/div "NO component!")))))))
(defrouter RootPageRouter [this {:keys [current-state pending-path-segment path-segment]}]
{:router-targets [LoadingPage IndexPage AlternativesPage BlogPage AboutPage FAQPage TermsPage PrivacyPage ContactPage MediaPage FourOhFour]
:componentDidMount (fn []
(debug "RootPageRouter" :componentDidMount)
(debug-router this))
:componentDidUpdate (fn [pp ps] (debug-router this))}
(do
(debug "Rendering RootPageRouter" :current-state current-state :pending-path pending-path-segment
:current-path path-segment)
(case current-state
:initial (do
(debug "RootPageRouter initial state" current-state)
(dom/div "initial"))
:pending (dom/div "Loading a page ...")
:failed (dom/div "Ooops!")
(dom/div ""))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment