freactive/README.md

67 lines
3 KiB
Markdown
Raw Normal View History

2014-07-10 19:57:02 +00:00
# freactive
2014-11-13 05:20:19 +00:00
The goal of this library is to provide some idioms for functional reactive programming based on Clojure's existing `deref`, `swap!` and `reset!` functions. Currently freactive has reactive `atom`, `rx` (reactive expressions or computations), and lens-based `cursor` types.
2014-11-13 05:21:03 +00:00
This idea for this came when I needed to write a fairly complex, real-time JavaFX app (using my own [fx-clj](https://github.com/aaronc/fx-clj/)). I knew of [Om](https://github.com/swannodette/om) and [Reagent](https://github.com/reagent-project/reagent) and wanted something for JavaFX that used Reagent-like reactive atoms. While doing this, I realized that with a common library of reactive "ref"'s, different UI libaries could share the same data structures and state management idioms. That is the vision for this library.
2014-11-13 05:20:19 +00:00
*I have drafted a brief [spec](https://github.com/aaronc/freactive/wiki/User-Interface-Spec) for user interface libraries that could be based upon freactive using a similar Hiccup-like API. The goal is for fx-clj and an freactive-based DOM library to support this spec.*
2014-11-12 18:17:35 +00:00
Example:
2014-11-13 04:46:59 +00:00
```clojure
2014-11-13 04:59:59 +00:00
;; ClojureScript:
2014-11-12 18:17:35 +00:00
(ns test-freactive
(:refer-clojure :exclude [atom])
2014-11-13 04:59:59 +00:00
(:require [freactive.core :refer [atom cursor]]
[cljs.reader :refer [read-string]])
(:require-macros [freactive.macros :refer [rx]]))
2014-11-12 18:17:35 +00:00
2014-11-13 04:46:44 +00:00
(def my-atom (atom {:a 1}))
2014-11-13 04:59:59 +00:00
(def my-rx (rx (str @my-atom)))
2014-11-13 04:46:44 +00:00
(def cursor-a (cursor my-atom :a))
(def cursor-a-str (cursor cursor-a pr-str (fn [_ new-value] (read-string new-value))))
2014-11-13 04:46:44 +00:00
(println @cursor-a-str)
;; "1"
2014-11-13 04:46:44 +00:00
(reset! cursor-a-str "4")
2014-11-12 18:17:35 +00:00
(println @my-rx)
2014-11-13 04:46:44 +00:00
;; "{:a 4}"
2014-11-12 18:17:35 +00:00
```
2014-11-13 05:23:53 +00:00
For maximum efficiency, pains were taken to keep the propogation of state changes to the minimum necessary and to allow for configurable laziness where applicable.
All of the data types in this library implement the `IDeref` interface and will be registered as dependents when they are `deref`'ed from another "reactive expression" .
*The ClojureScript version of the API is described here - as the Java-version was designed first, the ClojureScript version had the benefit of further refinement. The Java-version will be updated to match this document as soon as I have a chance.*
2014-11-12 18:17:35 +00:00
## Reactive Atoms
2014-11-12 18:17:35 +00:00
Reactive atoms are the same as standard Clojure atoms, except for two differences:
2014-11-12 18:17:35 +00:00
* When `deref` is called on a reactive atom, it calls a `register-dep` function
with itself as an argument so that it can be registered as a dependency to
a computation that has bound the `*register-dep*` var in the current scope.
* Reactive atoms will not notify their watches unless they have actually changed
(i.e. they will do an equality test between the old value and new value before
notifying of changes).
2014-11-12 18:17:35 +00:00
## Reactive Expressions
2014-11-12 18:17:35 +00:00
A reactive expression is an `IDeref` instance whose value is the result of
a computation that can be updated reactively when it's dependencies are
invalidatted.
## Reactive Cursors
2014-07-10 19:57:02 +00:00
## License
Copyright © 2014 Aaron Craelius
Some content Copyright © Rich Hickey
2014-07-10 19:57:02 +00:00
Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version.