freactive/README.md

73 lines
2.3 KiB
Markdown
Raw Normal View History

2014-07-10 19:57:02 +00:00
# freactive
2014-11-13 03:32:35 +00:00
The idea of this library is to provide some idioms for functional reactive
2014-11-12 18:17:35 +00:00
programming based on Clojure's already existing `deref`, `swap!` and `reset!`
2014-11-13 03:32:35 +00:00
functions. Currently there are reactive `atom`, `rx` (reactive expressions or computations), `lens`
and `cursor` types.
2014-11-13 03:34:12 +00:00
This library started when I needed to write a fairly
2014-11-13 03:32:35 +00:00
large Clojure project in JavaFX (using my own [fx-clj](https://github.com/aaronc/fx-clj/)).
I was aware of [Om](https://github.com/swannodette/om) and [Reagent](https://github.com/reagent-project/reagent)
2014-11-13 03:34:12 +00:00
and figured something along those lines could be created with a Reagent-like reactive atom library that
interacted with JavaFX. While doing this, I realized that a library with the same API could be created
in ClojureScript and that also, a DOM library with a similar API to the JavaFX library could be created.
2014-11-13 04:09:45 +00:00
I created this [spec](https://github.com/aaronc/freactive/wiki/User-Interface-Spec) for user interface libraries that could be based upon freactive using a similar API.
2014-11-12 18:17:35 +00:00
Example:
2014-11-13 04:08:31 +00:00
```clojure
2014-11-12 18:17:35 +00:00
(ns test-freactive
(:refer-clojure :exclude [atom])
(:require [freactive.core :refer [atom rx lens cursor]))
(def a1 (atom 0))
(def a2 (atom 0))
2014-11-13 04:09:45 +00:00
(def my-rx (rx (inc @a1))
2014-11-12 18:17:35 +00:00
(println @my-rx)
2014-11-13 04:09:45 +00:00
;; 1
2014-11-12 18:17:35 +00:00
(swap! a1 inc)
2014-11-12 18:17:35 +00:00
(println @my-rx)
2014-11-13 04:09:45 +00:00
;; 2
2014-11-12 18:17:35 +00:00
```
2014-11-12 18:17:35 +00:00
All of the data types in this library implement the `IDeref` interface and
when they are `deref`'ed from another "reactive expression" will be registered
as dependents.
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 Lenses
## 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.