re-frame/README.md

1187 lines
52 KiB
Markdown
Raw Normal View History

![logo](/images/logo/re-frame_256w.png?raw=true)
2016-07-23 00:31:23 +00:00
2015-03-04 04:41:22 +00:00
2016-07-15 13:52:42 +00:00
## Derived Values, Flowing
2014-12-12 14:20:41 +00:00
2015-02-10 13:31:41 +00:00
> This, milord, is my family's axe. We have owned it for almost nine hundred years, see. Of course,
sometimes it needed a new blade. And sometimes it has required a new handle, new designs on the
metalwork, a little refreshing of the ornamentation . . . but is this not the nine hundred-year-old
axe of my family? And because it has changed gently over time, it is still a pretty good axe,
y'know. Pretty good.
> -- Terry Pratchett, The Fifth Elephant
2015-02-10 13:31:41 +00:00
2016-07-15 13:52:42 +00:00
## Why Should You Care?
2015-02-20 05:46:50 +00:00
Either:
2015-02-20 13:45:48 +00:00
2015-03-07 10:35:32 +00:00
1. You want to develop an [SPA] in ClojureScript, and you are looking for a framework; or
2015-02-25 11:03:59 +00:00
2. You believe that, by early 2015, ReactJS had won the JavaScript framework wars and
2015-03-01 20:54:46 +00:00
you are curious about the bigger implications. Is the combination of
`reactive programming`, `functional programming` and `immutable data` going to
2015-03-07 10:35:32 +00:00
**completely change everything**? And, if so, what would that look like in a language
2015-02-25 11:03:59 +00:00
that embraces those paradigms?
2015-02-22 11:28:40 +00:00
2015-02-05 12:46:54 +00:00
2015-02-20 05:46:50 +00:00
## re-frame
re-frame is a pattern for writing [SPAs] in ClojureScript, using [Reagent].
2014-12-18 11:48:12 +00:00
This repo contains both a **description of this pattern** and a **reference implementation**.
2015-01-28 22:24:11 +00:00
2015-05-02 15:41:16 +00:00
To quote McCoy: "It's MVC, Jim, but not as we know it".
2015-02-10 13:31:41 +00:00
To build a re-frame app, you:
2015-02-20 13:45:48 +00:00
- design your app's data structure (data layer)
- write and register subscription functions (query layer)
- write Reagent component functions (view layer)
- write and register event handler functions (control layer and/or state transition layer)
2014-12-18 11:48:12 +00:00
2015-02-20 05:46:50 +00:00
Features:
2015-02-20 13:45:48 +00:00
2015-02-21 05:40:24 +00:00
1. The functions you write are pure, so the computational pieces of your app can
be described, understood and tested independently.
2016-07-15 13:52:42 +00:00
You won't need Dependency Injection to test. Think about that.
2015-02-21 05:40:24 +00:00
2. These computational parts are composed via reactive data flows - a dynamic,
unidirectional Signal graph.
3. The resulting architecture involves "derived data" flowing in a two-stage, reactive loop.
2015-02-22 13:17:49 +00:00
Without realising it, you will be explicitly modelling time.
4. It is fast, straight out of the box. You won't have to go through [this sort of pain](http://blog.scalyr.com/2013/10/angularjs-1200ms-to-35ms/).
2015-02-21 05:40:24 +00:00
5. The surprising thing about re-frame is how simple it is. Beautifully simple! Our reference
2015-03-01 01:04:48 +00:00
implementation is little more than 200 lines of (ClojureScript) code. Learn it in an afternoon.
2015-02-23 01:09:12 +00:00
6. But it scales up nicely to more complex apps. Frameworks are just pesky overhead at small
2015-02-23 06:36:02 +00:00
scale - measure them instead by how they help you tame the complexity of bigger apps.
2015-02-20 05:46:50 +00:00
7. Re-frame is impressively buzzword compliant: it has FRP-nature,
2015-02-23 01:09:12 +00:00
unidirectional data flow, pristinely pure functions, conveyor belts, statechart-friendliness (FSM)
2015-02-21 05:40:24 +00:00
and claims an immaculate hammock conception.
It also has a charming xkcd reference (soon)
and a hilarious, insiders-joke T-shirt, ideal for conferences (in design).
2015-02-23 06:36:02 +00:00
What could possibly go wrong?
2015-05-02 15:41:16 +00:00
[![Clojars Project](https://img.shields.io/clojars/v/re-frame.svg)](https://clojars.org/re-frame)
[![GitHub license](https://img.shields.io/github/license/Day8/re-frame.svg)](license.txt)
[![Circle CI](https://circleci.com/gh/Day8/re-frame/tree/develop.svg?style=shield&circle-token=:circle-ci-badge-token)](https://circleci.com/gh/Day8/re-frame/tree/develop)
[![Circle CI](https://circleci.com/gh/Day8/re-frame/tree/master.svg?style=shield&circle-token=:circle-ci-badge-token)](https://circleci.com/gh/Day8/re-frame/tree/master)
[![Sample Project](https://img.shields.io/badge/project-example-ff69b4.svg)](https://github.com/Day8/re-frame/tree/master/examples)
__Warning__: That was the summary. What follows is a long-ish tutorial/explanation.
2015-05-02 15:41:16 +00:00
## Tutorial Table of Contents
2015-04-21 14:18:31 +00:00
- [What Problem Does It Solve?](#what-problem-does-it-solve)
- [Guiding Philosophy](#guiding-philosophy)
- [FRP Clarifications](#frp-clarifications)
- [Explaining re-frame](#explaining-re-frame)
2015-05-02 15:41:16 +00:00
- [On Data](#on-data)
- [The Big Ratom](#the-big-ratom)
- [The Benefits Of Data-In-The-One-Place](#the-benefits-of-data-in-the-one-place)
2015-04-21 14:18:31 +00:00
- [Flow](#flow)
2015-05-02 15:41:16 +00:00
- [How Flow Happens In Reagent](#how-flow-happens-in-reagent)
2015-04-21 14:18:31 +00:00
- [Components](#components)
2015-05-02 15:41:16 +00:00
- [Truth Interlude](#truth-interlude)
- [Components Like Templates?](#components-like-templates)
- [React etc.](#react-etc)
2015-04-21 14:18:31 +00:00
- [Subscribe](#subscribe)
2015-05-02 15:41:16 +00:00
- [Just A Read-Only Cursor?](#just-a-read-only-cursor)
2015-04-21 14:18:31 +00:00
- [The Signal Graph](#the-signal-graph)
- [A More Efficient Signal Graph](#a-more-efficient-signal-graph)
- [The 2nd Flow](#the-2nd-flow)
- [Event Flow](#event-flow)
2015-05-02 15:41:16 +00:00
- [What are events?](#what-are-events)
- [Dispatching Events](#dispatching-events)
- [Event Handlers](#event-handlers)
- [Routing](#routing)
- [Control Via FSM](#control-via-fsm)
- [As A Reduce](#as-a-reduce)
- [Derived Data, Everywhere, flowing](#derived-data-everywhere-flowing)
- [Logging And Debugging](#logging-and-debugging)
- [Talking To A Server](#talking-to-a-server)
2015-04-21 14:18:31 +00:00
- [The CPU Hog Problem](#the-cpu-hog-problem)
2015-05-02 15:41:16 +00:00
- [In Summary](#in-summary)
- [Where Do I Go Next](#where-do-i-go-next)
2015-05-02 15:41:16 +00:00
- [Licence](#licence)
## What Problem Does It Solve?
2015-02-04 11:35:11 +00:00
2015-02-23 01:09:12 +00:00
First, we decided to build our SPA apps with ClojureScript, then we
2015-02-22 11:28:40 +00:00
choose [Reagent], then we had a problem.
2015-02-10 13:31:41 +00:00
2015-02-21 05:40:24 +00:00
For all its considerable brilliance, Reagent (+ ReactJS)
delivers only the 'V' part of a traditional MVC framework.
2015-02-10 13:31:41 +00:00
2015-02-21 05:40:24 +00:00
But apps involve much more than V. Where
2015-07-15 06:22:37 +00:00
does the control logic go? How is state stored & manipulated? etc.
2015-02-10 13:31:41 +00:00
2015-07-15 03:51:47 +00:00
We read up on [Flux], [Pedestal App],
2015-07-15 06:22:37 +00:00
[Hoplon], [OM], [Elm], etc and re-frame is the architecture that emerged.
2015-02-10 13:31:41 +00:00
2015-07-15 06:22:37 +00:00
re-frame does have M, V, and C parts but they aren't objects, they
2015-02-22 13:17:49 +00:00
are pure functions (or pure data),
2015-07-15 03:51:47 +00:00
and they are wired together via reactive data flows. It is sufficiently different in nature
from (traditional, Smalltalk) MVC that calling it MVC would be confusing. I'd
2015-02-20 05:46:50 +00:00
love an alternative.
2015-02-17 01:40:42 +00:00
Perhaps it is a RACES framework - Reactive-Atom Component Event
Subscription framework (I love the smell of acronym in the morning).
2015-02-23 01:09:12 +00:00
Or, if we distill to pure essence, `DDATWD` - Derived Data All The Way Down.
2015-02-17 01:40:42 +00:00
2015-02-21 05:40:24 +00:00
*TODO:* get acronym down to 3 chars! Get an image of stacked Turtles for `DDATWD`
2015-02-20 05:46:50 +00:00
insider's joke, conference T-Shirt.
2015-02-17 01:40:42 +00:00
2015-02-10 13:31:41 +00:00
2015-02-05 12:46:54 +00:00
## Guiding Philosophy
2014-12-08 21:46:34 +00:00
2015-02-21 05:40:24 +00:00
__First__, above all we believe in the one true [Dan Holmsand], creator of Reagent, and
2015-02-05 12:46:54 +00:00
his divine instrument the `ratom`. We genuflect towards Sweden once a day.
2014-12-08 21:46:34 +00:00
2015-02-21 05:40:24 +00:00
__Second__, we believe in ClojureScript, immutable data and the process of building
a system out of pure functions.
2015-01-11 21:51:55 +00:00
2015-02-21 05:40:24 +00:00
__Third__, we believe that [FRP] is one honking great idea. You might be tempted to see
Reagent as simply another of the React wrappers - a sibling to [OM] and
[quiescent](https://github.com/levand/quiescent).
2015-02-05 12:46:54 +00:00
But you'll only really "get"
2015-02-23 06:36:02 +00:00
Reagent when you view it as an FRP-ish library. To put that another way, I think
2015-02-10 13:31:41 +00:00
that Reagent, at its best, is closer in nature to [Hoplon] or [Elm] than it is OM.
2014-12-08 21:46:34 +00:00
2015-02-22 11:28:40 +00:00
__Finally__, we believe in one-way data flow. No two way data binding. We don't
like read/write `cursors` which
promote the two way flow of data. As programs get bigger, we've found that their
use seems to encourage control logic into all the wrong places.
2015-02-10 13:31:41 +00:00
## FRP Clarifications
2015-01-14 05:19:37 +00:00
2015-07-15 03:51:47 +00:00
We'll get to the meat in a second, I promise, but first one final, useful diversion ...
2015-02-10 13:31:41 +00:00
Terminology in the FRP world seems to get people hot under the collar. Those who believe in continuous-time
2015-02-21 05:40:24 +00:00
semantics might object to me describing re-frame as having FRP-nature. They'd claim that it does something
2015-02-20 13:45:48 +00:00
different from pure FRP, which is true.
2015-01-14 05:19:37 +00:00
2015-02-21 05:40:24 +00:00
But, these days, FRP seems to have become a
["big tent"](http://soft.vub.ac.be/Publications/2012/vub-soft-tr-12-13.pdf)
(a broad church?).
2015-02-18 08:43:13 +00:00
Broad enough perhaps that re-frame can be in the far, top, left paddock of the tent, via a series of
2015-02-20 05:46:50 +00:00
qualifications: re-frame has "discrete, dynamic, asynchronous, push FRP-ish-nature" without "glitch free" guarantees.
2015-02-05 12:46:54 +00:00
(Surprisingly, "glitch" has specific meaning in FRP).
2014-12-12 11:50:39 +00:00
2015-02-10 13:31:41 +00:00
**If you are new to FRP, or reactive programming generally**, browse these resources before
2015-02-20 05:46:50 +00:00
going further (certainly read the first two):
- [Creative Explanation](http://paulstovell.com/blog/reactive-programming)
- [Reactive Programming Backgrounder](https://gist.github.com/staltz/868e7e9bc2a7b8c1f754)
2015-01-20 22:40:49 +00:00
- [presentation (video)](http://www.infoq.com/presentations/ClojureScript-Javelin) by Alan Dipert (co-author of Hoplon)
2015-02-17 01:40:42 +00:00
- [serious pants Elm thesis](https://www.seas.harvard.edu/sites/default/files/files/archived/Czaplicki.pdf)
2015-01-20 22:40:49 +00:00
2015-02-21 05:40:24 +00:00
And for the love of all that is good, please watch this terrific
2015-02-05 12:46:54 +00:00
[StrangeLoop presentation ](https://www.youtube.com/watch?v=fU9hR3kiOK0) (40 mins). Watch what happens
2015-02-21 05:40:24 +00:00
when you re-imagine a database as a stream!! Look at all the problems that are solved.
Think about that: shared mutable state (the root of all evil),
2015-02-20 05:46:50 +00:00
re-imagined as a stream!! Blew my socks off.
2015-02-05 12:46:54 +00:00
re-frame tries to be `Derived Data everywhere, flowing`. Or perhaps,
2015-02-21 05:40:24 +00:00
`Derived Data All The Way Down` (an infinite loop of Derived Data).
More explanation on all these claims soon.
2015-02-04 11:35:11 +00:00
2015-01-20 22:40:49 +00:00
## Explaining re-frame
2014-12-08 21:46:34 +00:00
2015-02-21 05:40:24 +00:00
To explain re-frame, I'll incrementally develop a diagram, describing each part as it is added.
2014-12-08 21:46:34 +00:00
2015-02-20 05:46:50 +00:00
Initially, I'll be using [Reagent] at an intermediate to advanced level. But this is no introductory
2015-02-05 12:46:54 +00:00
reagent tutorial and you will need to have done one of those before continuing here. Try
2015-02-20 05:46:50 +00:00
[The Introductory Tutorial](http://reagent-project.github.io/) or
2015-01-20 22:40:49 +00:00
[this one](https://github.com/jonase/reagent-tutorial) or
[Building Single Page Apps with Reagent](http://yogthos.net/posts/2014-07-15-Building-Single-Page-Apps-with-Reagent.html).
2014-12-15 13:02:27 +00:00
2015-02-05 23:41:47 +00:00
### On Data
2015-01-11 12:09:17 +00:00
2014-12-08 21:46:34 +00:00
<blockquote class="twitter-tweet" lang="en"><p>Well-formed Data at rest is as close to perfection in programming as it gets. All the crap that had to happen to put it there however...</p>&mdash; Fogus (@fogus) <a href="https://twitter.com/fogus/status/454582953067438080">April 11, 2014</a></blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
2015-02-05 23:41:47 +00:00
### The Big Ratom
2014-12-12 11:50:39 +00:00
2015-01-20 22:40:49 +00:00
Our re-frame diagram starts (very modestly) with Fogus' ***well-formed data at rest*** bit:
2014-12-17 05:23:22 +00:00
2014-12-08 21:46:34 +00:00
```
2014-12-11 20:20:55 +00:00
app-db
2014-12-08 21:46:34 +00:00
```
2015-02-05 12:46:54 +00:00
re-frame says that you put your data into one place which we'll call `app-db`. Structure
the data in that place, of course, and [give it a schema](https://github.com/Prismatic/schema).
2014-12-08 21:46:34 +00:00
2015-02-20 13:45:48 +00:00
Now, this advice is not the slightest bit controversial for 'real' databases, right?
2015-02-10 13:31:41 +00:00
You'd happily put all your well-formed data into PostgreSQL or MySQL.
But within a running
2015-02-05 12:46:54 +00:00
application (in memory), it is different. If you have a background in OO, this data-in-one-place
2015-02-20 05:46:50 +00:00
business is a really, really hard one to swallow. You've
2014-12-10 15:33:57 +00:00
spent your life breaking systems into pieces, organised around behaviour and trying
to hide the data. I still wake up in a sweat some nights thinking about all
2014-12-17 02:41:06 +00:00
that Clojure data lying around exposed and passive.
2014-12-08 21:46:34 +00:00
2015-02-20 05:46:50 +00:00
But, as Fogus reminds us, data at rest is the easy bit. Believe.
2014-12-08 21:46:34 +00:00
2015-02-20 13:45:48 +00:00
From here on in this document, we'll assume `app-db` is one of these:
2014-12-17 05:23:22 +00:00
```Clojure
2014-12-17 14:42:16 +00:00
(def app-db (reagent/atom {})) ;; a Reagent atom, containing a map
2014-12-10 10:48:27 +00:00
```
2015-02-20 13:45:48 +00:00
Although it is a `Reagent atom` (hereafter `ratom`), I'd encourage you to think of it as an in-memory database.
It will contain structured data. You will need to query that data. You will perform CRUD
2014-12-10 15:33:57 +00:00
and other transformations on it. You'll often want to transact on this
database atomically, etc. So "in-memory database"
2015-01-20 22:40:49 +00:00
seems a more useful paradigm than plain old map-in-atom.
2014-12-08 21:46:34 +00:00
2015-02-10 13:31:41 +00:00
A clarification: `app-db` doesn't actually have to be a reagent/atom containing
a map. It could, for example, be a [datascript] database. In fact, any database which is reactive
2015-02-20 05:46:50 +00:00
(can tell you when it changes) would do. (We'd love! to be using [datascript] - so damn cool -
but we had too much
2015-02-23 06:36:02 +00:00
data in our apps. If you were to use it, you'd have to tweak the reference implementation a bit,
[perhaps using this inspiration](https://gist.github.com/allgress/11348685)). The reference implementation already creates and manages an internal `app-db` for you, you don't need to declare one yourself.
2015-02-10 13:31:41 +00:00
2014-12-16 22:29:12 +00:00
2015-02-23 01:09:12 +00:00
### The Benefits Of Data-In-The-One-Place
2014-12-16 22:29:12 +00:00
2014-12-17 02:41:06 +00:00
I'm going to quote verbatim from Elm's website:
2014-12-16 22:29:12 +00:00
2015-02-05 12:46:54 +00:00
1. There is a single source of truth. Traditional approaches force you to write a decent amount
of custom and error prone code to synchronize state between many different stateful components.
(The state of this widget needs to be synced with the application state, which needs to be
synced with some other widget, etc.) By placing all of your state in one location, you
eliminate an entire class of bugs in which two components get into inconsistent states. We
also think you will end up writing much less code. That has been our observation in Elm so far.
2014-12-16 22:29:12 +00:00
2015-02-05 12:46:54 +00:00
2. Save and Undo become quite easy. Many applications would benefit from the ability to save
all application state and send it off to the server so it can be reloaded at some later date.
This is extremely difficult when your application state is spread all over the place and
potentially tied to objects that cannot be serialized. With a central store, this becomes
very simple. Many applications would also benefit from the ability to easily undo user's
actions. For example, a painting app is better with Undo. Since everything is immutable in Elm,
this is also very easy. Saving past states is trivial, and you will automatically get pretty
good sharing guarantees to keep the size of the snapshots down.
2014-12-10 10:48:27 +00:00
2015-02-20 05:46:50 +00:00
2015-02-22 11:28:40 +00:00
To this list, I would briefly add two: the ability to genuinely model control via FSMs
2015-02-21 05:40:24 +00:00
and the ability to do time travel debugging, even in a production setting. More on both soon.
2015-02-20 05:46:50 +00:00
2015-02-05 12:46:54 +00:00
[Hoplon] takes the same approach via what they called `stem cells`, which is a root source of data.
2015-01-20 22:40:49 +00:00
2015-02-10 13:31:41 +00:00
## Flow
2015-02-20 05:46:50 +00:00
Arguments from authority ...
2015-02-05 12:46:54 +00:00
2015-01-25 23:42:45 +00:00
> Everything flows, nothing stands still. (Panta rhei)
> No man ever steps in the same river twice for it's not the same river and he's not the same man.
2015-02-20 05:46:50 +00:00
[Heraclitus 500 BC](http://en.wikiquote.org/wiki/Heraclitus). Who, being Greek, had never seen a frozen river. [alt version](http://farm6.static.flickr.com/5213/5477602206_ecb78559ed.jpg).
2015-01-11 12:09:17 +00:00
2015-01-14 05:19:37 +00:00
2015-02-05 12:46:54 +00:00
> Think of an experience from your childhood. Something you remember clearly, something you can see,
feel, maybe even smell, as if you were really there. After all you really were there at the time,
werent you? How else could you remember it? But here is the bombshell: you werent there. Not a
single atom that is in your body today was there when that event took place .... Matter flows
from place to place and momentarily comes together to be you. Whatever you are, therefore, you
are not the stuff of which you are made. If that does not make the hair stand up on the back of
your neck, read it again until it does, because it is important.
2015-01-14 05:19:37 +00:00
Steve Grand
2015-01-14 12:01:29 +00:00
2015-02-04 11:35:11 +00:00
2015-02-10 13:31:41 +00:00
### How Flow Happens In Reagent
2015-01-25 23:42:45 +00:00
2015-02-22 11:28:40 +00:00
To implement FRP, Reagent provides a `ratom` and a `reaction`.
re-frame uses both of these
building blocks, so let's now make sure we understand them.
2014-12-09 18:03:11 +00:00
2015-01-20 22:40:49 +00:00
`ratoms` behave just like normal ClojureScript atoms. You can `swap!` and `reset!` them, `watch` them, etc.
2014-12-16 22:29:12 +00:00
2015-02-10 13:31:41 +00:00
From a ClojureScript perspective, the purpose of an atom is to hold mutable data. From a re-frame
2015-03-01 01:04:48 +00:00
perspective, we'll tweak that paradigm slightly and **view a `ratom` as having a value that
2015-02-05 12:46:54 +00:00
changes over time.** Seems like a subtle distinction, I know, but because of it, re-frame sees a
`ratom` as a Signal. [Pause and read this](http://elm-lang.org:1234/guide/reactivity).
2014-12-16 22:29:12 +00:00
2015-02-05 12:46:54 +00:00
The 2nd building block, `reaction`, acts a bit like a function. It's a macro which wraps some
`computation` (a block of code) and returns a `ratom` holding the result of that `computation`.
2014-12-11 20:20:55 +00:00
2015-02-05 12:46:54 +00:00
The magic thing about a `reaction` is that the `computation` it wraps will be automatically
re-run whenever 'its inputs' change, producing a new output (return) value.
2014-12-11 20:20:55 +00:00
2015-01-20 22:40:49 +00:00
Eh, how?
2014-12-11 20:20:55 +00:00
2015-02-10 13:31:41 +00:00
Well, the `computation` is just a block of code, and if that code dereferences one or
2015-02-05 12:46:54 +00:00
more `ratoms`, it will be automatically re-run (recomputing a new return value) whenever any
of these dereferenced `ratoms` change.
2015-01-11 12:09:17 +00:00
2015-02-05 12:46:54 +00:00
To put that yet another way, a `reaction` detects a `computation's` input Signals (aka input `ratoms`)
and it will `watch` them, and when, later, it detects a change in one of them, it will re-run that
computation, and it will `reset!` the new result of that computation into the `ratom` originally returned.
2015-01-11 12:09:17 +00:00
2015-02-05 12:46:54 +00:00
So, the `ratom` returned by a `reaction` is itself a Signal. Its value will change over time when
the `computation` is re-run.
2015-01-11 12:09:17 +00:00
2015-02-05 12:46:54 +00:00
So, via the interplay between `ratoms` and `reactions`, values 'flow' into computations and out
2015-02-21 05:40:24 +00:00
again, and then into further computations, etc. "Values" flow (propagate) through the Signal graph.
2015-01-14 12:01:29 +00:00
2015-02-21 05:40:24 +00:00
But this Signal graph must be without cycles, because cycles cause mayhem! re-frame achieves
2015-02-05 12:46:54 +00:00
a unidirectional flow.
2014-12-12 11:50:39 +00:00
While the mechanics are different, `reaction` has the intent of `map` in [Elm] and `defc=` in [Hoplon].
2014-12-17 01:45:35 +00:00
2015-02-20 13:45:48 +00:00
Right, so that was a lot of words. Some code to clarify:
2014-12-09 18:03:11 +00:00
2014-12-17 02:41:06 +00:00
```Clojure
2014-12-09 18:03:11 +00:00
(ns example1
2015-02-20 13:45:48 +00:00
(:require-macros [reagent.ratom :refer [reaction]]) ;; reaction is a macro
(:require [reagent.core :as reagent]))
2014-12-16 22:29:12 +00:00
(def app-db (reagent/atom {:a 1})) ;; our root ratom (signal)
2014-12-10 10:48:27 +00:00
2014-12-16 22:29:12 +00:00
(def ratom2 (reaction {:b (:a @app-db)})) ;; reaction wraps a computation, returns a signal
2015-03-01 01:04:48 +00:00
(def ratom3 (reaction (condp = (:b @ratom2) ;; reaction wraps another computation
0 "World"
1 "Hello")))
2014-12-12 11:50:39 +00:00
2015-01-13 07:21:08 +00:00
;; Notice that both computations above involve de-referencing a ratom:
2014-12-16 22:29:12 +00:00
;; - app-db in one case
2015-01-14 00:17:19 +00:00
;; - ratom2 in the other
2015-01-13 07:21:08 +00:00
;; Notice that both reactions above return a ratom.
2014-12-17 05:23:22 +00:00
;; Those returned ratoms hold the (time varying) value of the computations.
2014-12-09 18:03:11 +00:00
2014-12-15 00:30:10 +00:00
(println @ratom2) ;; ==> {:b 1} ;; a computed result, involving @app-db
2014-12-16 22:29:12 +00:00
(println @ratom3) ;; ==> "Hello" ;; a computed result, involving @ratom2
2014-12-11 20:20:55 +00:00
2015-03-01 01:04:48 +00:00
(reset! app-db {:a 0}) ;; this change to app-db, triggers re-computation
2015-02-20 13:45:48 +00:00
;; of ratom2
;; which, in turn, causes a re-computation of ratom3
2014-12-11 20:20:55 +00:00
2014-12-15 00:30:10 +00:00
(println @ratom2) ;; ==> {:b 0} ;; ratom2 is result of {:b (:a @app-db)}
2014-12-11 10:06:48 +00:00
(println @ratom3) ;; ==> "World" ;; ratom3 is automatically updated too.
2014-12-09 18:03:11 +00:00
```
2015-02-21 05:40:24 +00:00
So, in FRP-ish terms, a `reaction` will produce a "stream" of values over time (it is a Signal),
accessible via the `ratom` it returns.
2014-12-11 20:20:55 +00:00
2015-02-10 13:31:41 +00:00
2015-01-20 22:40:49 +00:00
Okay, that was all important background information for what is to follow. Back to the diagram ...
2014-12-12 11:50:39 +00:00
2015-02-05 23:41:47 +00:00
## Components
2014-12-08 21:46:34 +00:00
2015-01-20 22:40:49 +00:00
Extending the diagram, we introduce `components`:
2014-12-17 05:23:22 +00:00
2014-12-08 21:46:34 +00:00
```
2015-02-10 13:31:41 +00:00
app-db --> components --> Hiccup
2014-12-08 21:46:34 +00:00
```
2014-12-17 05:23:22 +00:00
When using Reagent, your primary job is to write one or more `components`. This is the view layer.
2014-12-08 21:46:34 +00:00
2014-12-17 14:42:16 +00:00
Think about `components` as `pure functions` - data in, Hiccup out. `Hiccup` is
2014-12-15 06:48:20 +00:00
ClojureScript data structures which represent DOM. Here's a trivial component:
2014-12-17 05:23:22 +00:00
```Clojure
2014-12-11 20:20:55 +00:00
(defn greet
2015-02-20 13:45:48 +00:00
[]
[:div "Hello ratoms and reactions"])
2014-12-15 11:56:43 +00:00
```
2014-12-17 05:23:22 +00:00
2014-12-15 11:56:43 +00:00
And if we call it:
2014-12-17 05:23:22 +00:00
```Clojure
2015-02-20 05:46:50 +00:00
(greet)
;; ==> [:div "Hello ratoms and reactions"]
2014-12-11 20:20:55 +00:00
```
2015-02-05 12:46:54 +00:00
You'll notice that our component is a regular Clojure function, nothing special. In this case, it takes
no parameters and it returns a ClojureScript vector (formatted as Hiccup).
2014-12-11 20:20:55 +00:00
2015-02-05 12:46:54 +00:00
Here is a slightly more interesting (parameterised) component (function):
2014-12-17 05:23:22 +00:00
```Clojure
2015-05-02 15:41:16 +00:00
(defn greet ;; greet has a parameter now
2015-02-20 13:45:48 +00:00
[name] ;; 'name' is a ratom holding a string
[:div "Hello " @name]) ;; dereference 'name' to extract the contained value
2014-12-16 22:29:12 +00:00
2015-01-13 07:21:08 +00:00
;; create a ratom, containing a string
2015-02-20 13:45:48 +00:00
(def n (reagent/atom "re-frame"))
2014-12-11 20:20:55 +00:00
2015-01-13 07:21:08 +00:00
;; call our `component` function, passing in a ratom
(greet n)
2015-02-20 13:45:48 +00:00
;; ==> [:div "Hello " "re-frame"] returns a vector
2014-12-11 20:20:55 +00:00
```
So components are easy - at core they are a render function which turns data into
Hiccup (which will later become DOM).
2014-12-15 13:02:27 +00:00
2015-02-05 12:46:54 +00:00
Now, let's introduce `reaction` into this mix. On the one hand, I'm complicating things
by doing this, because Reagent allows you to be ignorant of the mechanics I'm about to show
you. (It invisibly wraps your components in a `reaction` allowing you to be blissfully
ignorant of how the magic happens.)
2014-12-12 11:50:39 +00:00
2015-02-05 12:46:54 +00:00
On the other hand, it is useful to understand exactly how the Reagent Signal graph is wired,
2015-02-20 05:46:50 +00:00
because in a minute, when we get to `subscriptions`, we'll be directly using `reaction`, so we
2015-02-05 12:46:54 +00:00
might as well bite the bullet here and now ... and, anyway, it is pretty easy...
2015-01-20 22:40:49 +00:00
2014-12-17 05:23:22 +00:00
```Clojure
2015-05-02 15:41:16 +00:00
(defn greet ;; a component - data in, Hiccup out.
2015-02-20 13:45:48 +00:00
[name] ;; name is a ratom
[:div "Hello " @name]) ;; dereference name here, to extract the value within
2014-12-11 20:20:55 +00:00
(def n (reagent/atom "re-frame"))
2014-12-17 14:42:16 +00:00
;; The computation '(greet n)' returns Hiccup which is stored into 'hiccup-ratom'
2015-01-11 12:09:17 +00:00
(def hiccup-ratom (reaction (greet n))) ;; <-- use of reaction !!!
2014-12-12 11:50:39 +00:00
;; what is the result of the initial computation ?
(println @hiccup-ratom)
2015-01-20 22:40:49 +00:00
;; ==> [:div "Hello " "re-frame"] ;; returns hiccup (a vector of stuff)
2014-12-11 20:20:55 +00:00
2014-12-16 22:29:12 +00:00
;; now change 'n'
2015-02-20 13:45:48 +00:00
;; 'n' is an input Signal for the reaction above.
;; Warning: 'n' is not an input signal because it is a parameter. Rather, it is
2015-01-11 12:09:17 +00:00
;; because 'n' is dereferenced within the execution of the reaction's computation.
;; reaction notices what ratoms are dereferenced in its computation, and watches
;; them for changes.
2015-01-20 22:40:49 +00:00
(reset! n "blah") ;; n changes
2014-12-12 11:50:39 +00:00
2015-02-20 13:45:48 +00:00
;; The reaction above will notice the change to 'n' ...
2015-01-20 22:40:49 +00:00
;; ... and will re-run its computation ...
;; ... which will have a new "return value"...
;; ... which will be "reset!" into "hiccup-ratom"
2014-12-12 11:50:39 +00:00
(println @hiccup-ratom)
2014-12-16 22:29:12 +00:00
;; ==> [:div "Hello " "blah"] ;; yep, there's the new value
2014-12-11 20:20:55 +00:00
```
2014-12-12 12:37:29 +00:00
2015-02-20 05:46:50 +00:00
So, as `n` changes value over time (via a `reset!`), the output of the computation `(greet n)`
2015-02-05 12:46:54 +00:00
changes, which in turn means that the value in `hiccup-ratom` changes. Both `n` and
`hiccup-ratom` are FRP Signals. The Signal graph we created causes data to flow from
`n` into `hiccup-ratom`.
2014-12-16 22:29:12 +00:00
2015-02-10 13:31:41 +00:00
Derived Data, flowing.
2014-12-11 20:20:55 +00:00
2014-12-16 22:29:12 +00:00
### Truth Interlude
2014-12-15 06:48:20 +00:00
2015-02-05 12:46:54 +00:00
I haven't been entirely straight with you:
2015-02-20 13:45:48 +00:00
1. Reagent re-runs `reactions` (re-computations) via requestAnimationFrame. So a
re-computation happens about 16ms after an input Signals change is detected, or after the
2015-02-05 12:46:54 +00:00
current thread of processing finishes, whichever is the greater. So if you are in a bREPL
and you run the lines of code above one after the other too quickly, you might not see the
re-computation done immediately after `n` gets reset!, because the next animationFrame
hasn't run (yet). But you could add a `(reagent.core/flush)` after the reset! to force
re-computation to happen straight away.
2015-02-20 13:45:48 +00:00
2. `reaction` doesn't actually return a `ratom`. But it returns something that has
2015-02-05 12:46:54 +00:00
ratom-nature, so we'll happily continue believing it is a `ratom` and no harm will come to us.
2014-12-12 11:50:39 +00:00
2014-12-17 05:23:22 +00:00
On with the rest of my lies and distortions...
2014-12-08 21:46:34 +00:00
2014-12-16 22:29:12 +00:00
### Components Like Templates?
2015-02-21 05:40:24 +00:00
A `component` such as `greet` is like the templates you'd find in
2015-01-14 12:01:29 +00:00
Django, Rails, Handlebars or Mustache -- it maps data to HTML -- except for two massive differences:
2015-02-21 05:40:24 +00:00
2015-02-23 01:45:04 +00:00
1. you have the full power of ClojureScript available to you (generating a Clojure data structure). The
downside is that these are not "designer friendly" HTML templates.
2. these templates are reactive. When their input Signals change, they
are automatically rerun, producing new DOM. Reagent adroitly shields you from the details, but
the renderer of any `component` is wrapped by a `reaction`. If any of the the "inputs"
to that render change, the render is rerun.
2014-12-12 11:50:39 +00:00
2014-12-17 02:41:06 +00:00
### React etc.
2014-12-11 01:48:41 +00:00
2015-02-20 05:46:50 +00:00
Okay, so we have some unidirectional, dynamic, async, discrete FRP-ish data flow happening here.
2014-12-17 14:42:16 +00:00
2015-02-10 13:31:41 +00:00
Question: To which ocean does this river of data flow? Answer: The DOM ocean.
2014-12-11 01:48:41 +00:00
2015-02-21 05:40:24 +00:00
The full picture:
2014-12-11 01:48:41 +00:00
```
2014-12-17 05:23:22 +00:00
app-db --> components --> Hiccup --> Reagent --> VDOM --> React --> DOM
2014-12-11 01:48:41 +00:00
```
Best to imagine this process as a pipeline of 3 functions. Each
function takes data from the
2015-02-10 13:31:41 +00:00
previous step, and produces (derived!) data for the next step. In the next
diagram, the three functions are marked (f1, f2, f3). The unmarked nodes are derived data,
2015-01-11 12:09:17 +00:00
produced by one step, to be input to the following step. Hiccup,
2014-12-11 01:48:41 +00:00
VDOM and DOM are all various forms of HTML markup (in our world that's data).
```
2015-02-05 23:41:47 +00:00
app-db --> components --> Hiccup --> Reagent --> VDOM --> React --> DOM
2015-02-20 13:45:48 +00:00
f1 f2 f3
2014-12-11 01:48:41 +00:00
```
2015-02-20 13:45:48 +00:00
In abstract ClojureScript syntax terms, you could squint and imagine the process as:
2014-12-17 05:23:22 +00:00
```Clojure
2015-02-20 13:45:48 +00:00
(-> app-db
components ;; produces Hiccup
Reagent ;; produces VDOM (virtual DOM that React understands)
React ;; produces HTML (which magically and efficiently appears on the page).
Browser ;; produces pixels
Monitor) ;; produces photons?
2014-12-11 01:48:41 +00:00
```
2014-12-11 07:11:01 +00:00
2015-02-05 12:46:54 +00:00
2015-02-10 13:31:41 +00:00
Via the interplay between `ratom` and `reaction`, changes to `app-db` stream into the pipeline, where it
undergoes successive transformations, until pixels colour the monitor you to see.
Derived Data, flowing. Every step is acting like a pure function and turning data into new data.
2015-02-21 05:40:24 +00:00
All well and good, and nice to know, but we don't have to bother ourselves with most of the pipeline.
We just write the `components`
part and Reagent/React will look after the rest. So back we go to that part of the picture ...
2014-12-12 14:01:28 +00:00
2014-12-12 13:19:14 +00:00
## Subscribe
2014-12-12 13:19:14 +00:00
2015-02-10 13:31:41 +00:00
`components` render the app's state as hiccup.
2014-12-17 05:23:22 +00:00
2014-12-12 13:19:14 +00:00
```
2015-02-20 13:45:48 +00:00
app-db --> components
2014-12-12 13:19:14 +00:00
```
2015-01-12 00:11:00 +00:00
2015-02-10 13:31:41 +00:00
`components` (view layer) need to query aspects of `app-db` (data layer).
2015-01-12 00:11:00 +00:00
But how?
2015-01-14 12:01:29 +00:00
Let's pause to consider **our dream solution** for this part of the flow. `components` would:
2015-02-20 13:45:48 +00:00
* obtain data from `app-db` (their job is to turn this data into hiccup).
* obtain this data via a (possibly parameterised) query over `app-db`. Think database kind of query.
* automatically recompute their hiccup output, as the data returned by the query changes, over time
* use declarative queries. Components should know as little as possible about the structure of `app-db`. SQL? Datalog?
2014-12-16 22:29:12 +00:00
2015-02-10 13:31:41 +00:00
re-frame's `subscriptions` are an attempt to live this dream. As you'll see, they fall short on the declarative
query part, but they comfortably meet the other requirements.
2015-02-20 05:46:50 +00:00
As a re-frame app developer, your job will be to write and register one or more
"subscription handlers" - functions that do a named query.
2014-12-12 13:19:14 +00:00
2015-02-10 13:31:41 +00:00
Your subscription functions must return a value that changes over time (a Signal). I.e. they'll
be returning a reaction or, at least, the `ratom` produced by a `reaction`.
2014-12-12 13:19:14 +00:00
2014-12-16 22:29:12 +00:00
Rules:
2015-02-20 13:45:48 +00:00
- `components` never source data directly from `app-db`, and instead, they use a subscription.
- subscriptions are only ever used by components (they are never used in, say, event handlers).
2014-12-15 06:48:20 +00:00
2014-12-16 22:29:12 +00:00
Here's a component using a subscription:
2014-12-17 05:23:22 +00:00
```Clojure
2014-12-15 11:56:43 +00:00
(defn greet ;; outer, setup function, called once
2015-02-20 13:45:48 +00:00
[]
(let [name-ratom (subscribe [:name-query])] ;; <---- subscribing happens here
(fn [] ;; the inner, render function, potentially called many times.
[:div "Hello" @name-ratom])))
2014-12-15 06:48:20 +00:00
```
2015-03-01 20:54:46 +00:00
First, note this is a [Form-2](https://github.com/Day8/re-frame/wiki/Creating-Reagent-Components#form-2--a-function-returning-a-function)
`component` ([there are 3 forms](https://github.com/Day8/re-frame/wiki/Creating-Reagent-Components)).
2015-01-11 12:09:17 +00:00
2015-02-10 13:31:41 +00:00
Previously in this document, we've used the simplest, `Form-1` components (no setup was required, just render).
With `Form-2` components, there's a function returning a function:
- the returned function is the render function. Behind the scenes, Reagent will wrap this render function
2015-02-20 13:45:48 +00:00
in a `reaction` to make it produce new Hiccup when its input Signals change. In our example above, that
means it will rerun every time `name-ratom` changes.
- the outer function is a setup function, called once for each instance of the component. Notice the use of
2015-02-20 13:45:48 +00:00
'subscribe' with the parameter `:name-query`. That creates a Signal through which new values are supplied
over time; each new value causing the returned function (the actual renderer) to be run.
>It is important to distinguish between a new instance of the component versus the same instance of a component reacting to a new value. Simplistically, a new component is returned for every unique value the setup function (i.e. the outer function) is called with. This allows subscriptions based on initialisation values to be created, for example:
``` Clojure
(defn my-cmp [row-id]
(let [row-state (subscribe [row-id])]
(fn [row-id]
[:div (str "Row: " row-id " is " @row-state)])))
```
In this example, `[my-cmp 1][my-cmp 2]` will create two instances of `my-cmp`. Each instance will re-render when its internal `row-state` signal changes.
2014-12-15 06:48:20 +00:00
2015-02-10 13:31:41 +00:00
`subscribe` is always called like this:
2014-12-17 05:23:22 +00:00
```Clojure
2015-02-20 13:45:48 +00:00
(subscribe [query-id some optional query parameters])
2014-12-15 11:56:43 +00:00
```
2014-12-17 05:23:22 +00:00
2015-02-10 13:31:41 +00:00
There is only one (global) `subscribe` function and it takes one parameter, assumed to be a vector.
2014-12-17 01:23:53 +00:00
The first element in the vector (shown as `query-id` above) identifies/names the query and the other elements are optional
2015-02-10 13:31:41 +00:00
query parameters. With a traditional database a query might be:
2014-12-17 05:23:22 +00:00
2014-12-16 22:29:12 +00:00
```
2014-12-17 14:42:16 +00:00
select * from customers where name="blah"
2014-12-16 22:29:12 +00:00
```
2014-12-17 05:23:22 +00:00
2015-02-10 13:31:41 +00:00
In re-frame, that would be done as follows:
2015-02-20 13:45:48 +00:00
(subscribe [:customer-query "blah"])
which would return a `ratom` holding the customer state (a value which might change over time!).
2014-12-15 06:48:20 +00:00
2015-02-10 13:31:41 +00:00
So let's now look at how to write and register the subscription handler for `:customer-query`
2014-12-17 05:23:22 +00:00
```Clojure
2014-12-17 01:23:53 +00:00
(defn customer-query ;; a query over 'app-db' which returns a customer
2015-02-20 13:45:48 +00:00
[db, [sid cid]] ;; query fns are given 'app-db', plus vector given to subscribe
(assert (= sid :customer-query)) ;; subscription id was the first element in the vector
2015-02-20 13:45:48 +00:00
(reaction (get-in @db [:path :to :a :map cid]))) ;; re-runs each time db changes
2014-12-17 01:23:53 +00:00
;; register our query handler
(register-sub
2015-02-20 13:45:48 +00:00
:customer-query ;; the id (the name of the query()
customer-query) ;; the function which will perform the query
2014-12-15 11:56:43 +00:00
```
2014-12-15 06:48:20 +00:00
2015-02-10 13:31:41 +00:00
Notice how the handler is registered to handle `:customer-query` subscriptions.
2014-12-16 22:29:12 +00:00
2015-02-10 13:31:41 +00:00
**Rules and Notes**:
2015-02-20 13:45:48 +00:00
- you'll be writing one or more handlers, and you will need to register each one.
- handlers are functions which take two parameters: the db atom, and the vector given to subscribe.
- `components` tend to be organised into a hierarchy, often with data flowing from parent to child via
2015-02-10 13:31:41 +00:00
parameters. So not every component needs a subscription. Very often the values passed in from a parent component
are sufficient.
2015-02-20 13:45:48 +00:00
- subscriptions can only be used in `Form-2` components and the subscription must be in the outer setup
2015-02-10 13:31:41 +00:00
function and not in the inner render function. So the following is **wrong** (compare to the correct version above)
2014-12-17 05:23:22 +00:00
```Clojure
2015-01-11 12:09:17 +00:00
(defn greet ;; a Form-1 component - no inner render function
2015-02-20 13:45:48 +00:00
[]
(let [name-ratom (subscribe [:name-query])] ;; Eek! subscription in renderer
[:div "Hello" @name-ratom]))
2014-12-16 22:29:12 +00:00
```
2014-12-15 13:29:33 +00:00
2015-03-17 11:51:41 +00:00
Why is this wrong? Well, this component would be re-rendered every time `app-db` changed, even if the value
2015-02-10 13:31:41 +00:00
in `name-ratom` (the result of the query) stayed the same. If you were to use a `Form-2` component instead, and put the
2015-02-22 13:17:49 +00:00
subscription in the outer functions, then there'll be no re-render unless the value queried (i.e. `name-ratom`) changed.
2015-02-10 13:31:41 +00:00
2015-02-23 01:09:12 +00:00
### Just A Read-Only Cursor?
2015-02-10 13:31:41 +00:00
2015-02-20 05:46:50 +00:00
Subscriptions are different to read-only cursors.
2015-02-10 13:31:41 +00:00
Yes, `subscriptions` abstract away (hide) the data source, like a Cursor, but they also allow
2015-02-20 05:46:50 +00:00
for computation. To put that another way, they can create
derived data from `app-db` (a Materialised View of `app-db`).
2015-02-10 13:31:41 +00:00
Imagine that our `app-db` contained `:items` - a vector of maps. And imagine that we wanted to
display these items sorted by one of their attributes. And that we only want to display the top 20 items.
2015-02-10 13:31:41 +00:00
2015-02-20 05:46:50 +00:00
This is the sort of "derived data" which a subscription can deliver.
(And as we'll see, more efficiently than a Cursor).
2015-01-20 22:40:49 +00:00
## The Signal Graph
2014-12-17 01:23:53 +00:00
2015-02-10 13:31:41 +00:00
Let's sketch out the situation described above ...
2014-12-15 13:29:33 +00:00
2015-01-20 22:40:49 +00:00
`app-db` would be a bit like this (`items` is a vector of maps):
2015-01-20 22:40:49 +00:00
```Clojure
2015-02-10 13:31:41 +00:00
(def L [{:name "a" :val 23 :flag "y"}
2015-02-20 13:45:48 +00:00
{:name "b" :val 81 :flag "n"}
{:name "c" :val 23 :flag "y"}])
2015-01-20 22:40:49 +00:00
2015-03-17 11:51:41 +00:00
(def app-db (reagent/atom {:items L
2015-04-15 14:33:14 +00:00
:sort-by :name})) ;; sorted by the :name attribute
2015-02-10 13:31:41 +00:00
```
2015-02-20 05:46:50 +00:00
The subscription-handler might be written:
2014-12-15 13:29:33 +00:00
2014-12-17 05:23:22 +00:00
```Clojure
(register-sub
2015-02-20 13:45:48 +00:00
:sorted-items ;; the query id (the name of the query)
(fn [db [_]] ;; the handler for the subscription
(reaction
(let [items (get-in @db [:items]) ;; extract items from db
sort-attr (get-in @db [:sort-by])] ;; extract sort key from db
2015-05-14 21:27:11 +00:00
(sort-by sort-attr items))))) ;; return them sorted
2014-12-15 13:29:33 +00:00
```
2014-12-17 05:23:22 +00:00
2015-01-20 22:40:49 +00:00
Subscription handlers are given two parameters:
2015-02-20 13:45:48 +00:00
2015-02-23 01:45:04 +00:00
1. `app-db` - that's a reagent/atom which holds ALL the app's state. This is the "database"
on which we perform the "query".
2. the vector originally supplied to `subscribe`. In our case, we ignore it.
2015-01-20 22:40:49 +00:00
In the example above, notice that the `reaction` depends on the input Signal: `db`.
2015-02-10 13:31:41 +00:00
If `db` changes, the query is re-run.
2014-12-15 13:29:33 +00:00
2015-01-11 12:09:17 +00:00
In a component, we could use this query via `subscribe`:
2014-12-17 05:23:22 +00:00
```Clojure
2015-01-20 22:40:49 +00:00
(defn items-list ;; Form-2 component - outer, setup function, called once
2015-02-20 13:45:48 +00:00
[]
(let [items (subscribe [:sorted-items]) ;; <-- subscribe called with name
num (reaction (count @items)) ;; Woh! a reaction based on the subscription
top-20 (reaction (take 20 @items))] ;; Another dependent reaction
(fn []
[:div
2015-02-20 13:45:48 +00:00
(str "there's " @num " of these suckers. Here's top 20") ;; rookie mistake to leave off the @
(into [:div ] (map item-render @top-20))]))) ;; item-render is another component, not shown
2014-12-15 13:29:33 +00:00
```
2014-12-08 21:46:34 +00:00
2015-02-10 13:31:41 +00:00
There's a bit going on in that `let`, most of it tortuously contrived, just so I can show off chained
2015-02-23 06:36:02 +00:00
reactions. Okay, okay, all I wanted really was an excuse to use the phrase "chained reactions".
2014-12-17 01:23:53 +00:00
The calculation of `num` is done by a `reaction` which has `items` as an input Signal. And,
2015-02-10 13:31:41 +00:00
as we saw, `items` is itself a reaction over two other signals (one of them the `app-db`).
2015-01-11 12:09:17 +00:00
2015-02-20 05:46:50 +00:00
So this is a Signal Graph. Data is flowing through computation into renderer, which produce Hiccup, etc.
## A More Efficient Signal Graph
2015-02-24 13:24:50 +00:00
But there is a small problem. The approach above might get inefficient, if `:items` gets long.
2014-12-17 01:23:53 +00:00
2015-02-20 05:46:50 +00:00
Every time `app-db` changes, the `:sorted-items` query is
going to be re-run and it's going to re-sort `:items`. But `:items` might not have changed. Some other
part of `app-db` may have changed.
We don't want to perform this computationally expensive re-sort
each time something unrelated in `app-db` changes.
Luckily, we can easily fix that up by tweaking our subscription function so
that it chains `reactions`:
2015-02-20 13:45:48 +00:00
2014-12-17 05:23:22 +00:00
```Clojure
(register-sub
2015-02-20 13:45:48 +00:00
:sorted-items ;; the query id
(fn [db [_]]
(let [items (reaction (get-in @db [:some :path :to :items]))] ;; reaction #1
sort-attr (reaction (get-in @db [:sort-by]))] ;; reaction #2
(reaction (sort-by @sort-attr @items))))) ;; reaction #3
2014-12-17 01:23:53 +00:00
```
The original version had only one `reaction` which would be re-run completely each time `app-db` changed.
2015-02-22 11:28:40 +00:00
This new version, has chained reactions.
2015-03-17 11:51:41 +00:00
The 1st and 2nd reactions just extract from `db`. They will run each time `app-db` changes.
2015-02-20 05:46:50 +00:00
But they are cheap. The 3rd one does the expensive
computation using the result from the first two.
2015-02-10 13:31:41 +00:00
That 3rd, expensive reaction will be re-run when either one of its two input Signals change, right? Not quite.
`reaction` will only re-run the computation when one of the inputs has **changed in value**.
2015-02-10 13:31:41 +00:00
2015-02-20 05:46:50 +00:00
`reaction` compares the old input Signal value with the new Signal value using `identical?`. Because we're
using immutable data structures
(thank you ClojureScript), `reaction` can perform near instant checks for change on even
deeply nested and complex
2015-02-10 13:31:41 +00:00
input Signals. And `reaction` will then stop unneeded propagation of `identical?` values through the
Signal graph.
2014-12-17 01:23:53 +00:00
In the example above, reaction #3 won't re-run until `:items` or `:sort-by` are different
(do not test `identical?`
2015-02-10 13:31:41 +00:00
to their previous value), even though `app-db` itself has changed (presumably somewhere else).
2015-01-11 12:09:17 +00:00
2015-02-20 05:46:50 +00:00
Hideously contrived example, but I hope you get the idea. It is all screamingly efficient.
2014-12-17 01:23:53 +00:00
Summary:
2015-02-20 13:45:48 +00:00
- you can chain reactions.
- a reaction will only be re-run when its input Signals test not identical? to previous value.
- As a result, unnecessary Signal propagation is eliminated using highly efficient checks,
even for large, deep nested data structures.
2014-12-17 01:23:53 +00:00
## The 2nd Flow
2014-12-17 01:23:53 +00:00
2015-01-13 07:21:08 +00:00
At the top, I said that re-frame had two data flows.
2014-12-10 15:33:57 +00:00
2015-02-20 05:46:50 +00:00
The data flow from `app-db` to the DOM is the first half of the story. We now need to consider
2015-02-23 01:09:12 +00:00
the 2nd part of the story: the flow in the opposite direction.
2014-12-11 01:48:41 +00:00
2015-02-10 13:31:41 +00:00
While the first flow has FRP-nature, the 2nd flow does not. Well, not at first glance anyway.
2014-12-17 01:23:53 +00:00
When I think about these two flows, I imagine one of those school diagrams showing the water cycle.
![logo](/images/the-water-cycle.png?raw=true)
Rivers taking water down to the oceans, and evaporation/clouds/wind taking water back over
the mountains to fall again as rain or snow. Repeat.
2014-12-17 01:23:53 +00:00
2015-01-20 22:40:49 +00:00
There is a cycle, but it is handled by two independent flows.
2015-01-11 12:09:17 +00:00
2015-07-15 03:51:47 +00:00
*With re-frame, it is not water that is flowing, it is data.*
## Event Flow
2014-12-17 01:23:53 +00:00
2015-01-20 22:40:49 +00:00
Events are what flow in the opposite direction.
2014-12-11 01:48:41 +00:00
In response to user interaction, a DOM will generate
2014-12-12 13:46:58 +00:00
events like "clicked delete button on item 42" or
"unticked the checkbox for 'send me spam'".
2014-12-10 15:33:57 +00:00
2014-12-17 14:42:16 +00:00
These events have to be "handled". The code doing this handling might
2015-02-10 13:31:41 +00:00
mutate app state (in `app-db`), or request more data from the server, or POST somewhere and wait for a response, etc.
2015-02-20 13:45:48 +00:00
2015-01-20 22:40:49 +00:00
In fact, all these actions ultimately result in changes to the `app-db`.
2014-12-10 15:33:57 +00:00
2015-01-20 22:40:49 +00:00
An application has many handlers, and collectively
2015-02-20 13:45:48 +00:00
they represent the **control layer of the application**.
2014-12-08 21:46:34 +00:00
2015-01-20 22:40:49 +00:00
In re-frame, the backwards data flow of events happens via a conveyor belt:
2014-12-08 21:46:34 +00:00
```
2014-12-17 05:23:22 +00:00
app-db --> components --> Hiccup --> Reagent --> VDOM --> React --> DOM
2015-02-20 13:45:48 +00:00
^ |
| v
handlers <------------------- events -----------------------------------------
a "conveyor belt" takes events
from the DOM to the handlers
2014-12-08 21:46:34 +00:00
```
2015-02-20 13:45:48 +00:00
Generally, when the user manipulates the GUI, the state of the application changes. In our case,
that means the `app-db` will change. After all, it **is** the state. And the DOM presented to
the user is a function of that state.
2015-01-11 12:09:17 +00:00
2015-02-10 13:31:41 +00:00
So that tends to be the cycle:
2015-02-20 13:45:48 +00:00
2015-02-23 06:36:02 +00:00
1. the user clicks something which causes an event to be dispatched
2. a handler manages the event
3. and causes `app-db` to change (mutation happens here!)
4. which then causes a re-render
5. the user sees something different
2015-02-20 13:45:48 +00:00
6. goto #1
2015-02-10 13:31:41 +00:00
That's our water cycle.
2014-12-10 15:33:57 +00:00
2015-01-20 22:40:49 +00:00
Because handlers are that part of the system which does `app-db` mutation, you
could almost imagine them as a "stored procedures" on a
2014-12-11 07:11:01 +00:00
database. Almost. Stretching it? We do like our in-memory
2014-12-11 01:48:41 +00:00
database analogies.
2014-12-10 15:33:57 +00:00
### What are events?
Events are data. You choose the format.
In our reference implementation we choose a vector format. For example:
2014-12-10 15:33:57 +00:00
2015-02-20 13:45:48 +00:00
[:delete-item 42]
2014-12-10 15:33:57 +00:00
2014-12-11 07:11:01 +00:00
The first item in the vector identifies the event and
2015-01-11 12:09:17 +00:00
the rest of the vector is the optional parameters -- in the example above, the id (42) of the item to delete.
2014-12-10 15:33:57 +00:00
2014-12-11 07:11:01 +00:00
Here are some other example events:
2014-12-17 05:23:22 +00:00
2014-12-17 02:41:06 +00:00
```Clojure
2015-02-20 13:45:48 +00:00
[:yes-button-clicked]
[:set-spam-wanted false]
[[:complicated :multi :part :key] "a parameter" "another one" 45.6]
2014-12-11 07:11:01 +00:00
```
2014-12-17 05:23:22 +00:00
**Rule**: events are pure data. No dirty tricks like putting callback functions on the wire.
You know who you are.
2014-12-10 15:33:57 +00:00
2014-12-12 13:46:58 +00:00
### Dispatching Events
2014-12-11 07:11:01 +00:00
2015-02-10 13:31:41 +00:00
Events tend to start in the DOM in response to user actions. They are `dispatched`.
2014-12-11 07:11:01 +00:00
2014-12-17 05:23:22 +00:00
For example, a button component might be like this:
2015-02-20 13:45:48 +00:00
2014-12-17 02:41:06 +00:00
```Clojure
2015-02-20 13:45:48 +00:00
(defn yes-button
[]
[:div {:class "button-class"
:on-click #(dispatch [:yes-button-clicked])}
"Yes"])
2014-12-11 07:11:01 +00:00
```
2015-01-20 22:40:49 +00:00
Notice the `on-click` DOM handler:
2014-12-17 05:23:22 +00:00
2014-12-17 02:41:06 +00:00
```Clojure
2015-02-20 13:45:48 +00:00
#(dispatch [:yes-button-clicked])
2014-12-11 07:11:01 +00:00
```
With re-frame, we try to keep the DOM as passive as possible. We do not
want our views containing any control logic. That "on-click" is as simple as we can make it.
2014-12-11 07:11:01 +00:00
2015-02-20 05:46:50 +00:00
There's a single `dispatch` function in the entire framework, and it takes one parameter:
the event (vector) to be
dispatched (which is pure simple, lovely data, flowing).
2014-12-12 13:19:14 +00:00
2015-01-11 12:09:17 +00:00
Let's update our diagram to show `dispatch`:
2014-12-17 05:23:22 +00:00
2014-12-12 13:19:14 +00:00
```
2014-12-17 05:23:22 +00:00
app-db --> components --> Hiccup --> Reagent --> VDOM --> React --> DOM
2015-02-20 13:45:48 +00:00
^ |
| v
handlers <---------------------------------------- (dispatch [event-id event params])
2014-12-12 13:19:14 +00:00
```
2014-12-11 07:11:01 +00:00
**Rule**: `components` are as passive and minimal as possible when it comes to handling events.
They `dispatch` pure data and nothing more.
2014-12-17 01:23:53 +00:00
2015-02-20 13:45:48 +00:00
### Event Handlers
2014-12-12 13:19:14 +00:00
2015-01-13 07:21:08 +00:00
Collectively, event handlers provide the control logic in a re-frame application.
2014-12-12 13:19:14 +00:00
2015-02-20 05:46:50 +00:00
An event handler is a pure function of two parameters:
2015-02-22 11:28:40 +00:00
2015-02-20 13:45:48 +00:00
1. current value in `app-db`. Note: that's the map **in** `app-db`, not the atom itself.
2015-04-15 14:26:49 +00:00
2. an event (represented as a vector)
2014-12-11 07:11:01 +00:00
2015-02-20 13:45:48 +00:00
It returns the new value which should be reset! into `app-db`.
2014-12-11 07:11:01 +00:00
2015-01-20 22:40:49 +00:00
An example handler:
2014-12-17 05:23:22 +00:00
```Clojure
2014-12-12 13:46:58 +00:00
(defn handle-delete
2015-02-20 13:45:48 +00:00
[app-state [_ item-id]] ;; notice how event vector is destructured -- 2nd parameter
(dissoc-in app-state [:some :path item-id])) ;; return a modified version of 'app-state'
2014-12-12 13:46:58 +00:00
```
2014-12-10 15:33:57 +00:00
2015-02-20 05:46:50 +00:00
Handling an event invariably involves mutating the value in `app-db`
(which is provided as the first parameter).
2015-02-10 13:31:41 +00:00
An item is added here, or one is deleted there. So, often simple CRUD, but sometimes much more,
and sometimes with async results.
2015-01-20 22:40:49 +00:00
2015-06-07 09:52:41 +00:00
But the `app-db` mutation is ultimately handled by re-frame (it does the `reset!`). That leaves your event
2015-02-10 13:31:41 +00:00
handlers pure. As a result, they tend to be easy to test and understand. Many are almost trivial.
2014-12-10 15:33:57 +00:00
There's more to event handlers than can be covered here in this introductory tutorial. Read up on
2015-04-21 13:42:05 +00:00
issues like Middleware [in the Wiki](https://github.com/Day8/re-frame/wiki#handler-middleware).
2014-12-17 01:23:53 +00:00
### Routing
2015-02-20 13:45:48 +00:00
When `dispatch` is passed an event vector, it just puts that event onto a conveyor belt.
2015-01-20 22:40:49 +00:00
2015-02-20 05:46:50 +00:00
The consumer on the end of the conveyor is a `router` which will organise for that
event to be processed by the right handler.
2015-01-20 22:40:49 +00:00
```
app-db --> components --> Hiccup --> Reagent --> VDOM --> React --> DOM
2015-02-20 13:45:48 +00:00
^ |
| v
handlers <----- router <----------------------- (dispatch [event-id event params])
2015-01-20 22:40:49 +00:00
```
The `router` will:
2015-02-22 11:28:40 +00:00
2015-01-20 22:40:49 +00:00
1. inspect the 1st element of the arriving vector
2015-02-10 13:31:41 +00:00
2. look in its registry for the handler which is registered for this kind of event
2015-01-20 22:40:49 +00:00
3. call that handler with two parameters: (1) the current value in `app-db` and (2) the event vector
4. reset! the returned value back into `app-db`.
As a re-frame app developer, your job is to write handlers for each kind of event, and
then to register those handlers with the router.
2015-01-20 22:40:49 +00:00
Here's how we would register our event handler:
2014-12-17 01:23:53 +00:00
2014-12-17 05:23:22 +00:00
```Clojure
2016-07-07 04:52:13 +00:00
(reg-event-pure
2015-02-20 13:45:48 +00:00
:delete-item ;; the event id (name)
handle-delete) ;; the handler function for that event
2014-12-17 01:23:53 +00:00
```
2015-01-20 22:40:49 +00:00
Any arriving event vector which has `:delete-item` as the first element will now be routed to our handler.
2015-02-20 13:45:48 +00:00
### Control Via FSM
2015-01-20 22:40:49 +00:00
Above, I commented that event handlers collectively represent the "control layer" of the
application. They contain
2015-02-20 05:46:50 +00:00
logic which interprets arriving events and they "step" the application "forward"
via mutations to `app-db`.
2014-12-15 11:56:43 +00:00
2015-02-20 05:46:50 +00:00
Our `delete-handler` above is trivial, but as an application grows more features, the logic in many
2015-02-10 13:31:41 +00:00
handlers will become more complicated, and they will have to query BOTH the current state of the app
AND the arriving event vector to determine what action to take.
2014-12-15 12:19:47 +00:00
2015-02-10 13:31:41 +00:00
If the app is in logical State A, and event X arrives, then the handler will move the app to logical state B
(by changing values in `app-db`).
2015-02-04 11:35:11 +00:00
2015-02-20 05:46:50 +00:00
Sound like anything you learned in those [Theory Of Computation](https://www.youtube.com/watch?v=Pt6GBVIifZA)
lectures?
2015-02-04 11:35:11 +00:00
2015-02-10 13:31:41 +00:00
That's right - as an app becomes more complex, the handlers are likely to be collectively implementing a
[Finite State Machine](http://en.wikipedia.org/wiki/Finite-state_machine):
2015-02-20 13:45:48 +00:00
- your app is in a certain logical state (defined by the current values in `app-db`)
- the arriving event vector represents a `trigger`.
- the event handler implements "a transition", subject to BOTH the current logical state and the arriving trigger.
- after the handler has run, the transition may have moved the app into a new logical state.
- Repeat.
2014-12-15 12:19:47 +00:00
2015-02-10 13:31:41 +00:00
Not every app has lots of logical `states`, but many do, and if you are implementing one of them, then formally
recognising it and using a technique like
2015-02-22 13:17:49 +00:00
[state charts](http://www.amazon.com/Constructing-User-Interface-Statecharts-Horrocks/dp/0201342782) will help
2015-02-10 13:31:41 +00:00
greatly in getting a clean design and a nice datamodel.
2015-02-04 11:35:11 +00:00
2015-02-10 13:31:41 +00:00
The beauty of re-frame from a FSM point of view is that all the data is in one place - unlike OO systems where
the data is distributed (and synchronized) across many objects. So implementing your control logic as a FSM is
2015-02-20 05:46:50 +00:00
both possible and natural in re-frame, whereas it is often difficult and contrived to do so in other
kinds of architecture (in my experience).
2015-02-04 11:35:11 +00:00
2015-02-20 05:46:50 +00:00
### As A Reduce
2015-01-20 22:40:49 +00:00
2015-02-18 08:43:13 +00:00
So here's another way of thinking about what's happening with this data flow - another useful mental model.
2015-02-10 13:31:41 +00:00
First, imagine that all the events ever dispatched by a certain running app were stored in a collection.
So, if when the app started, the user clicked on button X then the first item in this collection
2015-02-18 08:43:13 +00:00
would be the event generated
by that button, and then, if next the user moved a slider, the associated event would be the
next item in the collection, and so on and so on. We'd end up with a collection of event vectors.
2015-02-10 13:31:41 +00:00
2015-02-04 11:35:11 +00:00
2015-02-18 08:43:13 +00:00
Second, remind yourself that the `combining function` of a `reduce` takes two parameters:
1. the current state of the reduction and
2. the next collection member to fold in.
2015-02-20 05:46:50 +00:00
Then notice that event handlers take two parameters too:
2015-02-18 08:43:13 +00:00
1. the current state of `app-db`
2. the next item to fold in.
Which is the same as a `combining function` in a `reduce`!!
2015-02-04 11:35:11 +00:00
2015-02-11 01:22:49 +00:00
So now we can introduce the new mental model: at any point in time, the value in `app-db` is the result of
performing a `reduce` over
2015-02-10 13:31:41 +00:00
the entire `collection` of events dispatched in the app up until that time. The combining function
for this reduce is the set of handlers.
2015-02-04 11:35:11 +00:00
2015-02-18 08:43:13 +00:00
It is almost like `app-db` is the temporary place where this imagined `perpetual reduce` stores
its on-going reduction.
2015-02-04 11:35:11 +00:00
2015-02-11 01:22:49 +00:00
### Derived Data, Everywhere, flowing
2015-02-04 11:35:11 +00:00
2015-02-18 08:43:13 +00:00
Have you watched that
[StrangeLoop presentation ](https://www.youtube.com/watch?v=fU9hR3kiOK0) yet?
I hope so. Database as a stream, right?
2015-02-04 11:35:11 +00:00
If you have then, given the explanation above, you might twig to the idea that `app-db` is
2015-02-18 08:43:13 +00:00
really a derived value (of the `perpetual reduce`).
And yet, it acts as the authoritative source of state in the app. And yet, it isn't, it is simply
a piece of derived state. And
2015-02-18 08:43:13 +00:00
yet, it is the source.
2015-02-18 08:43:13 +00:00
Hmm. This is an infinite loop of sorts. **Derived data is flowing around the
2015-03-04 23:42:07 +00:00
loop, reactively, through pure functions.** There is a pause in the loop whenever we wait
2015-02-22 13:17:49 +00:00
for a new event, but the moment we get it, it's another iteration of the "derived data" FRP loop.
2015-02-18 08:43:13 +00:00
Derived values, all the way down, forever.
Good news. If you've read this far,
your insiders T-shirt will be arriving soon - it
will feature turtles
2015-02-18 08:43:13 +00:00
and [xkcd](http://xkcd.com/1416/). We're still working on the hilarious caption bit. Open a
2015-02-20 05:46:50 +00:00
repo issue with a suggestion.
Back to the more pragmatic world ...
### Logging And Debugging
2015-02-18 08:43:13 +00:00
How did that exception happen, you wonder, shaking your head? What did the user do immediately prior
2015-02-20 05:46:50 +00:00
to the exception? What state was the app in that this event was so disastrous?
To debug it, you need to know this information:
2015-05-02 15:41:16 +00:00
1. the state of the app immediately before the exception
2015-02-20 13:45:48 +00:00
2. What final event then caused your app to fall in a screaming mess.
2015-02-11 01:22:49 +00:00
Well, with re-frame you need to record (have available):
2015-02-20 13:45:48 +00:00
1. A recent checkpoint of the app state in `app-db` (perhaps the initial state)
2. all the events `dispatch`ed since the last checkpoint, up to the point where the exception occurred.
2015-02-11 01:22:49 +00:00
2015-02-20 05:46:50 +00:00
Note: that's all just data. **Pure, lovely loggable data.**
2015-02-11 01:22:49 +00:00
If you have that data, then you can reproduce the exception.
2015-02-11 01:22:49 +00:00
2015-02-18 08:43:13 +00:00
re-frame allows you to time travel. Install the "checkpoint" state into `app-db`
and then "play forward" through the collection dispatched events.
2015-02-11 01:22:49 +00:00
2015-02-21 13:17:12 +00:00
The only way the app "moves forwards" is via events. "Replaying events" moves you
step by step towards the exception causing problem.
2015-02-04 11:35:11 +00:00
2015-02-20 05:46:50 +00:00
This is utterly, utterly perfect for debugging assuming, of course, you are in a position to capture
a checkpoint, and the events since then.
2014-12-15 11:56:43 +00:00
2015-01-11 12:09:17 +00:00
### Talking To A Server
2014-12-15 13:02:27 +00:00
2015-02-18 08:43:13 +00:00
Some events handlers will need to initiate an async server connection (e.g. GET or POST something).
2014-12-15 13:02:27 +00:00
2015-02-10 13:31:41 +00:00
The initiating event handlers should organise that the `on-success` or `on-fail` handlers for
2015-04-21 13:42:05 +00:00
these HTTP requests themselves simply dispatch a new event. They should never attempt to
2015-02-20 13:45:48 +00:00
modify `app-db` themselves. That is always done in a handler.
2014-12-10 15:33:57 +00:00
2015-02-18 08:43:13 +00:00
**Notes**:
2015-02-20 13:45:48 +00:00
- all events are handled via a call to `dispatch`. GUI events, async HTTP events, everything.
- `dispatch` will cause a handler function to be called. But the process is async. The call is queued.
- if you (further) dispatch in a handler, then that will be async too. The associated handler is
queued for later processing. Why? Partially because handlers are given a snapshot of
the `app-db` and can't be nested.
- if you kick off an HTTP request in a handler, then organise for the on-success or on-fail handlers
to dispatch their outcome. All events are handled via dispatch. on-success should never ever change
`app-db`.
2015-04-21 13:42:05 +00:00
The [wiki](https://github.com/Day8/re-frame/wiki/Talking-To-Servers) has more on the subject.
2015-02-20 13:45:48 +00:00
2015-02-21 13:17:12 +00:00
## The CPU Hog Problem
2015-02-20 13:45:48 +00:00
2015-02-22 11:28:40 +00:00
Sometimes a handler has a lot of CPU intensive work to do, and getting through it will take a while.
2015-02-20 05:46:50 +00:00
2015-02-21 13:17:12 +00:00
When a handler hogs the CPU, nothing else can happen. Browsers only give us one thread of
execution and that CPU-hogging handler owns it, and it isn't giving it up. The UI will be
frozen and there will be
2015-02-22 13:17:49 +00:00
no processing of any other handlers (e.g. on-success of POSTs), etc., etc. Nothing.
2015-02-20 05:46:50 +00:00
2015-02-21 13:17:12 +00:00
And a frozen UI is a problem. GUI repaints are not happening. And user interactions are not being processed.
2015-02-20 05:46:50 +00:00
2015-02-21 13:17:12 +00:00
How are we to show progress updates like "Hey, X% completed"? Or how can we handle the
user clicking on that "Cancel" button trying to stop this long running process?
2015-02-20 05:46:50 +00:00
2015-02-21 13:17:12 +00:00
We need a means by which long running handlers can hand control
back for "other" processing every so often, while still continuing on with their
computation.
2015-02-20 05:46:50 +00:00
2015-03-13 20:44:03 +00:00
Luckily, [re-frame has a solution](https://github.com/Day8/re-frame/wiki/Solve-the-CPU-hog-problem).
2014-12-10 15:33:57 +00:00
2015-02-18 08:43:13 +00:00
2014-12-15 12:19:47 +00:00
### In Summary
2015-02-20 05:46:50 +00:00
re-frame has two distinct flows, and I claim they are BOTH FRP in nature. The first is clearly FRP.
The second one is conceptually FRP, but you do have to squint.
2015-02-24 21:58:01 +00:00
All the parts are simple. The parts are easy to understand in isolation. The parts are composed so that
derived data flows in a perpetual reactive loop, through pure functions.
2015-02-20 05:46:50 +00:00
2015-01-13 07:21:08 +00:00
To build an app using re-frame, you'll have to:
2015-02-20 13:45:48 +00:00
- design your app's data structure.
- write and register subscription functions (query layer).
- write component functions (view layer).
- write and register event handler functions (control layer and/or state transition layer).
2015-05-02 15:41:16 +00:00
### Where Do I Go Next?
Your next steps with re-frame should be:
- look at the examples: https://github.com/Day8/re-frame/tree/master/examples
2015-05-04 01:16:10 +00:00
- use the lein template: https://github.com/Day8/re-frame-template
2015-05-02 15:41:16 +00:00
- read more in the Wiki: https://github.com/Day8/re-frame/wiki
You might also be interested in James MacAulay's excellent work (not re-frame!):
2015-05-02 15:41:16 +00:00
https://github.com/jamesmacaulay/zelkova
If you want reusable layout and widget components, consider this sister project:
https://github.com/Day8/re-com
Here are some open source re-frame apps you can look at for more inspiration:
- https://github.com/madvas/fractalify/
2015-02-25 21:26:08 +00:00
### Licence
Copyright © 2015 Michael Thompson
Distributed under The MIT License (MIT) - See LICENSE.txt
2014-12-09 18:03:11 +00:00
[SPAs]:http://en.wikipedia.org/wiki/Single-page_application
2015-03-07 10:35:32 +00:00
[SPA]:http://en.wikipedia.org/wiki/Single-page_application
2014-12-17 14:42:16 +00:00
[Reagent]:http://reagent-project.github.io/
2014-12-17 03:02:56 +00:00
[Dan Holmsand]:https://twitter.com/holmsand
[Flux]:http://facebook.github.io/flux/docs/overview.html#content
2014-12-09 18:03:11 +00:00
[Hiccup]:https://github.com/weavejester/hiccup
2014-12-08 21:46:34 +00:00
[FRP]:https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
[Elm]:http://elm-lang.org/
2014-12-09 18:03:11 +00:00
[OM]:https://github.com/swannodette/om
[Prismatic Schema]:https://github.com/Prismatic/schema
2014-12-08 21:46:34 +00:00
[datascript]:https://github.com/tonsky/datascript
[Hoplon]:http://hoplon.io/
2014-12-10 15:33:57 +00:00
[Pedestal App]:https://github.com/pedestal/pedestal-app