Skip to content

Commit

Permalink
Added application hook before-render
Browse files Browse the repository at this point in the history
  • Loading branch information
awkay committed May 18, 2022
1 parent b2680e3 commit df511f2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
4 changes: 4 additions & 0 deletions src/main/com/fulcrologic/fulcro/algorithms/lookup.cljc
Expand Up @@ -31,6 +31,10 @@
- `:drop-component!` - The algorithm that removes a component from indexes when it unmounts.
- `:props-middleware` - Middleware that can modify `props` for all components.
- `:render-middleware` - Middlware that wraps all `render` methods of `defsc` components.
- `:before-render - A function `(fn [app RootClass])` that is called after a transaction completes, just BEFORE
rendering. This function is allowed to affect the state atom to do things like compute dynamic derived state. Prefer
this over an atom watch, since it will be called less frequently that an atom watch. This will be called EVEN IF
Fulcro is running \"headless\". So, it can be thought of as `after-transaction`.
Returns nil if the algorithm is currently undefined.
"
Expand Down
2 changes: 2 additions & 0 deletions src/main/com/fulcrologic/fulcro/application.cljc
Expand Up @@ -187,6 +187,8 @@
* `:props-middleware` - A function that can add data to the 4th (optional) argument of
`defsc`. Useful for allowing users to quickly destructure extra data created by
component extensions. See the fulcro-garden-css project on github for an example usage.
* `:before-render` - A `(fn [app RootClass])` which is called just before rendering (usually just after transactions).
This can be used as a convenient hook to update the state atom in `app` before it is rendered.
* `:render-middleware` - A `(fn [this real-render])`. If supplied it will be called for every Fulcro component
render, and *must* call (and return the result of) `real-render`. This can be used to wrap the real render
function in order to do things like measure performance, set dynamic vars, or augment the UI in arbitrary ways.
Expand Down
56 changes: 32 additions & 24 deletions src/main/com/fulcrologic/fulcro/raw/application.cljc
Expand Up @@ -95,7 +95,13 @@
([app {:keys [force-root?] :as options}]
(tick! app)
(let [{:com.fulcrologic.fulcro.application/keys [runtime-atom state-atom]} app
{:com.fulcrologic.fulcro.application/keys [root-class]} (some-> runtime-atom deref)]
{:com.fulcrologic.fulcro.application/keys [root-class]} (some-> runtime-atom deref)
before-render (ah/app-algorithm app :before-render)]
(when before-render
(try
(before-render app root-class)
(catch #?(:clj Throwable :cljs :default) e
(log/error e "Transaction middleware threw an exception!"))))
(when root-class
(let [core-render! (ah/app-algorithm app :core-render!)
root-props-changed? (root-props-changed? app)]
Expand Down Expand Up @@ -219,6 +225,7 @@
submit-transaction!
abort-transaction!
render-middleware
before-render
initial-db
client-will-mount
client-did-mount
Expand All @@ -241,29 +248,30 @@
:external-config external-config
:query-transform-default query-transform-default
:load-mutation load-mutation}
:com.fulcrologic.fulcro.application/algorithms {:com.fulcrologic.fulcro.algorithm/tx! tx!
:com.fulcrologic.fulcro.algorithm/abort! (or abort-transaction! txn/abort!)
:com.fulcrologic.fulcro.algorithm/batch-notifications batch-notifications
:com.fulcrologic.fulcro.algorithm/core-render! (or core-render! identity)
:com.fulcrologic.fulcro.algorithm/optimized-render! (or optimized-render! identity)
:com.fulcrologic.fulcro.algorithm/initialize-state! initialize-state!
:com.fulcrologic.fulcro.algorithm/shared-fn shared-fn
:com.fulcrologic.fulcro.algorithm/render-root! render-root!
:com.fulcrologic.fulcro.algorithm/hydrate-root! hydrate-root!
:com.fulcrologic.fulcro.algorithm/unmount-root! unmount-root!
:com.fulcrologic.fulcro.algorithm/refresh-component! refresh-component!
:com.fulcrologic.fulcro.algorithm/render! render!
:com.fulcrologic.fulcro.algorithm/remote-error? (or remote-error? default-remote-error?)
:com.fulcrologic.fulcro.algorithm/global-error-action global-error-action
:com.fulcrologic.fulcro.algorithm/merge* merge/merge*
:com.fulcrologic.fulcro.algorithm/default-result-action! (or default-result-action! mut/default-result-action!)
:com.fulcrologic.fulcro.algorithm/global-eql-transform (or global-eql-transform default-global-eql-transform)
:com.fulcrologic.fulcro.algorithm/index-root! indexing/index-root!
:com.fulcrologic.fulcro.algorithm/index-component! indexing/index-component!
:com.fulcrologic.fulcro.algorithm/drop-component! indexing/drop-component!
:com.fulcrologic.fulcro.algorithm/props-middleware props-middleware
:com.fulcrologic.fulcro.algorithm/render-middleware render-middleware
:com.fulcrologic.fulcro.algorithm/schedule-render! schedule-render!}
:com.fulcrologic.fulcro.application/algorithms (cond-> {:com.fulcrologic.fulcro.algorithm/tx! tx!
:com.fulcrologic.fulcro.algorithm/abort! (or abort-transaction! txn/abort!)
:com.fulcrologic.fulcro.algorithm/batch-notifications batch-notifications
:com.fulcrologic.fulcro.algorithm/core-render! (or core-render! identity)
:com.fulcrologic.fulcro.algorithm/optimized-render! (or optimized-render! identity)
:com.fulcrologic.fulcro.algorithm/initialize-state! initialize-state!
:com.fulcrologic.fulcro.algorithm/shared-fn shared-fn
:com.fulcrologic.fulcro.algorithm/render-root! render-root!
:com.fulcrologic.fulcro.algorithm/hydrate-root! hydrate-root!
:com.fulcrologic.fulcro.algorithm/unmount-root! unmount-root!
:com.fulcrologic.fulcro.algorithm/refresh-component! refresh-component!
:com.fulcrologic.fulcro.algorithm/render! render!
:com.fulcrologic.fulcro.algorithm/remote-error? (or remote-error? default-remote-error?)
:com.fulcrologic.fulcro.algorithm/global-error-action global-error-action
:com.fulcrologic.fulcro.algorithm/merge* merge/merge*
:com.fulcrologic.fulcro.algorithm/default-result-action! (or default-result-action! mut/default-result-action!)
:com.fulcrologic.fulcro.algorithm/global-eql-transform (or global-eql-transform default-global-eql-transform)
:com.fulcrologic.fulcro.algorithm/index-root! indexing/index-root!
:com.fulcrologic.fulcro.algorithm/index-component! indexing/index-component!
:com.fulcrologic.fulcro.algorithm/drop-component! indexing/drop-component!
:com.fulcrologic.fulcro.algorithm/props-middleware props-middleware
:com.fulcrologic.fulcro.algorithm/render-middleware render-middleware
:com.fulcrologic.fulcro.algorithm/schedule-render! schedule-render!}
(fn? before-render) (assoc :com.fulcrologic.fulcro.algorithm/before-render before-render))
:com.fulcrologic.fulcro.application/runtime-atom (atom
{:com.fulcrologic.fulcro.application/app-root nil
:com.fulcrologic.fulcro.application/mount-node nil
Expand Down

0 comments on commit df511f2

Please sign in to comment.