mirror of
https://github.com/status-im/re-frame.git
synced 2025-02-23 07:18:22 +00:00
Back to compiling (removed namespace cycle)
This commit is contained in:
parent
345bb6ecd7
commit
947de41d02
11
project.clj
11
project.clj
@ -1,11 +1,12 @@
|
||||
(defproject re-frame "0.1.4"
|
||||
(defproject re-frame "0.1.5"
|
||||
:description "A reagent framework"
|
||||
:url "https://github.com/Day8/re-frame.git"
|
||||
|
||||
:dependencies [[org.clojure/clojure "1.6.0"]
|
||||
[org.clojure/clojurescript "0.0-2760" :scope "provided"]
|
||||
[org.clojure/core.async "0.1.346.0-17112a-alpha" :scope "provided"]
|
||||
[reagent "0.5.0-alpha3" :scope "provided"]]
|
||||
|
||||
[org.clojure/clojurescript "0.0-2760"]
|
||||
[org.clojure/core.async "0.1.346.0-17112a-alpha"]
|
||||
[reagent "0.5.0-alpha3"]]
|
||||
|
||||
:profiles {:debug {:debug true}
|
||||
:dev {:dependencies [[spellhouse/clairvoyant "0.0-48-gf5e59d3"]]
|
||||
@ -16,7 +17,7 @@
|
||||
"run/compiled/demo"]
|
||||
|
||||
:resource-paths ["run/resources"]
|
||||
:jvm-opts ["-Xmx1g" "-XX:+UseConcMarkSweepGC"] ;;
|
||||
:jvm-opts ["-Xmx1g" "-XX:+UseConcMarkSweepGC"] ;;
|
||||
:source-paths ["src"]
|
||||
:test-paths ["test"]
|
||||
|
||||
|
@ -8,5 +8,3 @@
|
||||
;; Access is mediated via handlers and subscriptions
|
||||
(def app-db (reagent/atom {}))
|
||||
|
||||
|
||||
|
||||
|
@ -2,9 +2,9 @@
|
||||
(:refer-clojure :exclude [flush])
|
||||
(:require-macros [cljs.core.async.macros :refer [go-loop go]])
|
||||
(:require [reagent.core :refer [flush]]
|
||||
[reagent.ratom :refer [IReactiveAtom]]
|
||||
;[reagent.ratom :refer [IReactiveAtom]]
|
||||
[re-frame.db :refer [app-db]]
|
||||
[re-frame.history :refer [store-now!]]
|
||||
; [re-frame.history :refer [store-now!]]
|
||||
[re-frame.utils :refer [first-in-vector warn]]
|
||||
[cljs.core.async :refer [chan put! <! timeout]]))
|
||||
|
||||
@ -84,83 +84,3 @@
|
||||
(handle event-v))
|
||||
|
||||
|
||||
|
||||
;; -- Middleware Factories -------------------------------------------------------------------------
|
||||
;;
|
||||
;; Middleware wraps handlers, providing a composable pipeline.
|
||||
;;
|
||||
;; Read this (go to "Handlers and Middleware"):
|
||||
;; http://www.flyingmachinestudios.com/programming/boot-clj/
|
||||
;;
|
||||
;; Use "comp" to compose middelware, like this:
|
||||
;;
|
||||
;; (def midware (comp undoable make-pure (validate some-fn))) ;; midware is a function
|
||||
;;
|
||||
;; then imagine that we have a pure handler:
|
||||
;;
|
||||
;; (defn my-handler
|
||||
;; [db v]
|
||||
;; (assoc db :some-key 42))
|
||||
;;
|
||||
;; then apply the composed middleare to my-handler:
|
||||
;;
|
||||
;; (def h (midware my-handler)) ;; h is "my-handler" wrapped in middleware
|
||||
;;
|
||||
;; now call h:
|
||||
;; (h app-db [:delete-it]) <---- h is a handler, just pass in 'db' and 'v'
|
||||
;;
|
||||
;; Which means, you could just register 'h'
|
||||
;;
|
||||
;; (register
|
||||
;; :some-id
|
||||
;; h)
|
||||
;;
|
||||
|
||||
|
||||
(defn undoable
|
||||
"Middleware which stores an undo checkpoint"
|
||||
[next-handler]
|
||||
(fn handler
|
||||
[app-db event-vec]
|
||||
(store-now!)
|
||||
(next-handler app-db event-vec)))
|
||||
|
||||
|
||||
(defn make-pure
|
||||
"Middleware for wrapping a pure handler.
|
||||
1. on the way through it extracts the value in the atom
|
||||
2. resets the atom with the returned value after calling the handler"
|
||||
[next-handler]
|
||||
(fn handler
|
||||
[app-db event-vec]
|
||||
(assert (satisfies? IReactiveAtom app-db) "re-frame: make-pure not given a Ratom")
|
||||
(reset! app-db (next-handler @app-db event-vec))))
|
||||
|
||||
|
||||
;; example of applying
|
||||
#_(defn check-schema
|
||||
"Middleware for checking that a handlers mutations leave the state in a schema-matching way"
|
||||
[a-prismatic-schema]
|
||||
(fn middlewear
|
||||
[next-handler]
|
||||
(fn handler
|
||||
[db v]
|
||||
(let [val (next-handler db v)
|
||||
valid? true] ;; XXXXX replace true by code which checks the schema using original parameter
|
||||
(if (not valid?)
|
||||
(warn "re-frame: schema not valid after:" ))
|
||||
val))))
|
||||
|
||||
|
||||
|
||||
(defn validate
|
||||
"Middleware that applies a validation function to the db after the handler is finished.
|
||||
The validation function f, might assoc warnings and errors to the new state, created by the handler.
|
||||
By validation, I mean validation of what the user has entered, or the state they have taken the app too"
|
||||
[f]
|
||||
(fn middlewear
|
||||
[next-handler]
|
||||
(fn handler
|
||||
[db v]
|
||||
(f (next-handler db v)))))
|
||||
|
||||
|
@ -39,16 +39,16 @@
|
||||
|
||||
(subs/register
|
||||
:undos?
|
||||
(fn
|
||||
(fn handler
|
||||
; "return true is anything is stored in the undo list, otherwise false"
|
||||
[_ _]
|
||||
"return true is anything is stored in the undo list, otherwise false"
|
||||
(reaction (> (count @undo-list) 1))))
|
||||
|
||||
(subs/register
|
||||
:redos?
|
||||
(fn
|
||||
(fn handler
|
||||
; "return true is anything is stored in the redo list, otherwise false"
|
||||
[_ _]
|
||||
"return true is anything is stored in the redo list, otherwise false"
|
||||
(reaction (> (count @redo-list) 0))))
|
||||
|
||||
|
||||
|
87
src/re_frame/middleware.cljs
Normal file
87
src/re_frame/middleware.cljs
Normal file
@ -0,0 +1,87 @@
|
||||
(ns re-frame.middleware
|
||||
(:require [reagent.ratom :refer [IReactiveAtom]]
|
||||
[re-frame.history :refer [store-now!]]))
|
||||
|
||||
;; -- Middleware Factories -------------------------------------------------------------------------
|
||||
;;
|
||||
;; Middleware wraps handlers, providing a composable pipeline. We use middleware so the handlers
|
||||
;; themselves are kept as simple as possible. In particualr, the handlers can be kept as pure functions.
|
||||
;;
|
||||
;; My attempt to explain, by skirting around the hard bits is as follows ...
|
||||
;;
|
||||
;; Use "comp" to compose middelware, like this:
|
||||
;;
|
||||
;; (def midware (comp undoable make-pure (validate some-fn))) ;; midware is a function
|
||||
;;
|
||||
;; then imagine that we have a pure handler:
|
||||
;;
|
||||
;; (defn my-handler
|
||||
;; [db v]
|
||||
;; (assoc db :some-key 42))
|
||||
;;
|
||||
;; then apply the composed middleare to my-handler:
|
||||
;;
|
||||
;; (def h (midware my-handler)) ;; h is "my-handler" wrapped in middleware
|
||||
;;
|
||||
;; now call h:
|
||||
;; (h app-db [:some-key 23]) <---- h is a handler, just pass in 'db' and 'v'
|
||||
;;
|
||||
;; Which means, you could just register 'h'
|
||||
;;
|
||||
;; (register
|
||||
;; :some-id
|
||||
;; h)
|
||||
;;
|
||||
;; Middleware factories do your head in initially, because they involve a function, returning a function,
|
||||
;; returning a function. So I'd suggest you might want to read this explanation
|
||||
;; (go to "Handlers and Middleware"):
|
||||
;; http://www.flyingmachinestudios.com/programming/boot-clj/
|
||||
;;
|
||||
|
||||
(defn undoable
|
||||
"Middleware which stores an undo checkpoint"
|
||||
[next-handler]
|
||||
(fn handler
|
||||
[app-db event-vec]
|
||||
(store-now!)
|
||||
(next-handler app-db event-vec)))
|
||||
|
||||
|
||||
(defn make-pure
|
||||
"Middleware which allows you to write a pure handler.
|
||||
1. on the way through it extracts the value in the atom
|
||||
2. resets the atom with the returned value after calling the handler"
|
||||
[next-handler]
|
||||
(fn handler
|
||||
[app-db event-vec]
|
||||
(assert (satisfies? IReactiveAtom app-db) "re-frame: make-pure not given a Ratom")
|
||||
(reset! app-db (next-handler @app-db event-vec))))
|
||||
|
||||
|
||||
;; example of applying
|
||||
#_(defn check-schema
|
||||
"Middleware for checking that a handlers mutations leave the state in a schema-matching way"
|
||||
[a-prismatic-schema]
|
||||
(fn middlewear
|
||||
[next-handler]
|
||||
(fn handler
|
||||
[db v]
|
||||
(let [val (next-handler db v)
|
||||
valid? true] ;; XXXXX replace true by code which checks the schema using original parameter
|
||||
(if (not valid?)
|
||||
(warn "re-frame: schema not valid after:" ))
|
||||
val))))
|
||||
|
||||
|
||||
(defn validate
|
||||
"Middleware that applies a validation function to the db after the handler is finished.
|
||||
The validation function f, might assoc warnings and errors to the new state, created by the handler.
|
||||
By validation, I mean validation of what the user has entered, or the state they have taken the app too"
|
||||
[f]
|
||||
(fn middlewear
|
||||
[next-handler]
|
||||
(fn handler
|
||||
[db v]
|
||||
(f (next-handler db v)))))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user