Last active
July 17, 2016 23:23
-
-
Save mike-thompson-day8/077e5bd41ff6cd167f96170604380026 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> This Gist is out of date. It has been superseded by | |
> https://gist.github.com/mike-thompson-day8/d61692451166e206a9c43771c8b389fc | |
;; WARNING: this code is untested. In fact not even compiled. | |
;; It is just meant to be indicative of how `regsub` might work/look. | |
(ns todomvc.subs | |
(:require-macros [reagent.ratom :refer [reaction]]) | |
(:require [re-frame.core :refer [register-sub]])) | |
(regsub :showing | |
(fn [db _] ;; by default, the first parameter, db, is the value in app-db (not app-db itself which is a ratom) | |
(:showing db))) ;; I repeat: db is a value. Not a ratom. And this fn doesnot return a reaction | |
(regsub :sorted-todos | |
(fn [[db] _] | |
(:todos db))) | |
;; A subscription is effectively a handler functions which is re-run | |
;; whenever its inputs change. | |
;; In the two examples above, the handler will rerun whenever `app-db` changes, and | |
;; each time it re-runs the new value in `app-db` ius supplied as the first parameter. | |
;; But many subscriptions are not directly dependent on app-db, and instead, dedpend on | |
;; some value derived from app-db. These handler represent "intermediate nodes" in the | |
;; signal graph. Values flow this signal graph. | |
;; When writting the handler for such an intermediate node, you must nominate the | |
;; input signal. | |
;; It recomoputes a new value. Which is then reactively passed on to | |
;; further downstream nodes in the signal graph. | |
;; The root of the XXXXX | |
(regsub :todos | |
:<- [:sorted-todo] ;; this subscription itself depends on an input value (subscribe [:sorted-todo]) | |
(fn [sorted-todos _] ;; this signal is created by | |
(vals sorted-todos))) | |
(regsub :visible-todos | |
:<- [:todos] | |
:<- [:showing] | |
(fn [[todos showing] _] | |
(let [filter-fn (case showing | |
:active (complement :done) | |
:done :done | |
:all identity)] | |
(filter filter-fn todos)))) | |
(regsub :completed-count | |
:<- [:todos] | |
(fn [todos _] | |
(count (filter :done todos)))) | |
(regsub :footer-stats ;; different from original. Now does not return showing | |
:<- [:todos] | |
:<- [:completed-count] | |
(fn [[todos completed] _] | |
[(- (count todos) completed) completed])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment