• Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    Clojure
  • License
    Mozilla Public Li...
  • Created over 8 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🌱 Your companion to create derived values from a single source (atom)

Derivatives CircleCI cljdoc badge

Subscriptions distilled.

usage | comparisons | change log | API docs

A note on terminology: There are a lot of things with similar meanings/use-cases around: subscriptions, reactions, derived atoms, view models. I'll introduce another to make things even worse: derivative. A derivative implements IWatchable and it's value is the result of applying a function to the value of other things (sources) implementing IWatchable. Whenever any of the sources change the value of the derivative is updated.

Why this library

Let's assume you're hooked about the idea of storing all your application state in a single atom (db). (Why this is a great idea is covered elsewhere.)

Most of your components don't need the entirety of this state and instead receive a small selection of it that is enough to allow the components to do their job. Now that data is not always a subtree (i.e. (get-in db ...)) but might be a combination of various parts of your state. To transform the data in a way that it becomes useful for components you can use a function: (f @db).

Now you want to re-render your application whenever db changes so the views are representing the data in db. You end up calling f a lot, and remember, f has to do all the transformation for all components that could be rendered on the page, pretty inefficient!

To optimise we can create derivatives that contain data in shapes ideal to specific components and re-render those components when the derivative supplying the data changes.

These derivatives may depend on other derivatives, all ultimately leading up to your single db atom. To keep things efficient we only recalculate the value of a derivative when any of it's sources changes.

The intention of this library is to make the creation and usage of these interdependent references (derivatives) simple and efficient.

A secondary objective is also to achieve the above without relying on global state being defined at the namespace level of this library. (See re-frame vs. pure-frame.)

What this library helps with

  • transform db into shapes suited for rendering (a.k.a. view models)
  • managing a pool of derivatives so only needed derivatives are created and freed as soon as they become unused (currently Rum specific)
  • server-side rendering (to some degree)

What this library doesn't help with

Usage

[org.martinklepsch/derivatives "0.3.1-alpha"] ;; latest release

Derivatives of your application state can be defined via a kind of specification like the one below:

(def *db-atom (atom 0))

(def drv-spec
  {;; a source with no dependencies
   :db     [[]         *db-atom]
   ;; a derivative with a dependency
   :inc    [[:db]      (fn [db] (inc db))]
   ;; a derivative with multiple dependencies
   :as-map [[:db :inc] (fn [db inc] {:db db :inc inc})]}

A specification like the above can be easily turned into a map with the same keys where the values are derivatives (see org.martinklepsch.derivatives/build).

Also it can be turned into a registry that can help with only creating needed derivatives and freeing them up when they become unused (see org.martinklepsch.derivatives/derivatives-pool).

What follows is Rum specific and this library has a dependency on Rum but this pattern could be used with old Om apps, or even Reagent's reactions. I'm very open to changes in that direction.

In a Rum component tree you might use derivatives as follows (assuming *db-atom and drv-spec as above):

(rum/defcs derived-view < rum/reactive (d/drv :inc) (d/drv :as-map)
  [s]
  [:div
   [:p ":inc "    (-> (d/react s :inc) pr-str)]
   [:p ":as-map " (-> (d/react s :as-map) pr-str)]])

(rum/defc app < (d/rum-derivatives drv-spec)
  [spec]
  [:div
   [:button {:on-click #(swap! *db-atom inc)} "inc"]
   (derived-view)])

The rum-derivatives mixin adds two functions to the React context of all child components: one to get a derivative and one to release it. The drv mixin adds hooks to your components that do exactly that and allow you to access the derivatives via component state.

Comparisons

Plain rum.core/derived-atom

Rum's derived-atoms serve as building block in this library but there are some things which are (rightfully) not solved by derived-atoms:

  • Creation of interdependent derivative chains and
  • a mechanism to only create actually needed derived-atoms.

A small code sample should illustrate this well:

(def *db (atom {:count 0})) ; base db

(def *increased 
  (rum/derived-atom [*db]
                    ::increased 
                    (fn [db]
                      (inc (:count db)))))
  
(def *as-map
  (rum/derived-atom [*db *increased] 
                    ::as-map 
                    (fn [db incd] 
                      {:db db :increased incd})))

compared with the way this could be described using derivatives:

(def *db (atom {:count 0}))

(def spec
  ;; {name    [depends-upon     derive-fn]}
  {:db        [[]               *db]
   :increased [[:db]            (fn [db] (inc (:count db)))]
   :as-map    [[:db :increased] (fn [db incd] {:db db :increased incd})]})

The benefit here is that we don't use vars to make sure the dependencies are met and that we provide this information in a way that can easily be turned into a dependency graph (data FTW) which will later help us only calculating required derivatives (done by derivatives-pool). In comparison the first snippet will create derived-atoms and recalculate them whenever any of their dependencies change, no matter if you're using the derived-atom in any of your views.

Re-Frame Subscriptions

The way they work Re-Frame's dynamic subscriptions are not much different from the approach chosen here, they vary in two ways however:

  • In Re-Frame you can do (subscribe [:sub-id "a parameter"]), with derivatives you can't. Instead these parameters need to be put into db and be used (potentially via another derivative) from there.
  • In Re-Frame subscriptions may have side-effects to listen to remote changes etc. This library does not intend to solve this kind of problem and thus side effects are discouraged.

Why no parameterized subscriptions?

In my personal experience a lot of non-idiomatic, non performance optimal re-frame use comes from having subscriptions in every corner of the code. Parameterized subscriptions enable this even more.

While a bandaid more than a solution the lack of parameterized subscriptions in Derivatives is meant to discourage ad-hoc, throwaway subscription use and instead encourage thoughtful reshaping of data from your DB into a form suitable for rendering.

Contributing

Feedback and PRs welcome.

Tests can be run with boot --source-paths test test or boot -s test test.

--

License: MPLv2, see LICENSE.

More Repositories

1

tenzing

⚑️ Clojurescript application template using Boot
Clojure
403
star
2

electron-and-clojurescript

πŸ’‘ Make an electron app with nothing but ClojureScript!
Clojure
148
star
3

s3-beam

πŸš€ direct-to-S3 uploading using ClojureScript
Clojure
92
star
4

dotfiles

Configuration for Emacs, Fish, Tmux, Git etc.
Lua
50
star
5

boot-garden

Boot task to compile Garden stylesheets.
Clojure
37
star
6

boot-deps

lein-ancient like task for boot
Clojure
27
star
7

custom-comparator-set

πŸ”¬ A Clojure/Script library for custom comparator sets
Clojure
11
star
8

boot-gzip

Boot task to gzip files and nothing else.
CSS
9
star
9

prune-feature-flags.clj

A small utility script to remove no longer needed conditionals in Clojure code
Clojure
8
star
10

martinklepsch.org

personal pages
HTML
7
star
11

learnclojure

An attempt at easing the pain for Clojure newcomers
6
star
12

soundrain

download soundcloud songs with ease
Clojure
6
star
13

autochrome-action

HTML
6
star
14

tlog

toy cli time tracker written in fennel-lang
Makefile
5
star
15

perun-guide

4
star
16

vscode-joker-clojure-linter

Clojure Linter for VS Code
TypeScript
4
star
17

boot-checkouts-demo

A small tutorial on using boot's checkouts option
Clojure
3
star
18

goldman.clj

a small toolchain to fetch stock data & analyze it (future)
Clojure
2
star
19

acme-clj

Clojure
2
star
20

om-docset

A Dash Docset for Om
CSS
2
star
21

one-of-each

🎹 my little personal music blog
Clojure
2
star
22

.vim

my vim config
Vim Script
1
star
23

configure

Install your dotfiles on any unix system with ease
Ruby
1
star
24

rethink

Clojure
1
star
25

boot-as-library

Shell
1
star
26

mobiledoc-test

Clojure
1
star
27

glitter

Racket program to automate bucket creation as well as some other AWS things.
Racket
1
star
28

boot-cljs-multiple-builds

Clojure
1
star
29

suggest

Clojure
1
star