Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(ns app.a-trackers
(:require [reagent.core :as r]))
(defonce reg-trackers (atom {}))
(defn reg-tracker
([k signal]
(reg-tracker k signal #(rf/dispatch [k %])))
([k signal effect]
(when-let [{:keys [tracker]} (get @reg-trackers k)]
(r/dispose! tracker))
(let [signal (r/track #(signal @re-frame.db/app-db))
tracker (r/track! #(when-let [data @signal] (effect data)))]
(swap! reg-trackers assoc k {:signal signal :tracker tracker}))))
(defn clear-trackers! []
(doseq [[k {:keys [tracker]}] @reg-trackers]
(r/dispose! tracker))
(reset! reg-trackers {}))
(defn sample-tracker [k]
(let [{:keys [signal]} (get @reg-trackers k)]
@signal))
(ns b-example)
(defn load-species-args [db]
(let [region_id (get-in db [:app/state :region :pk])
category_id (get-in db [:app/state :category :id])]
(when (and region_id category_id)
[region_id category_id])))
(reg-event-fx
:app/load-species
(fn [_ [_ args]]
{:sqlite/execute-sql {:query (str " SELECT * FROM SPECIES "
" WHERE ID IN (SELECT SPECIES_ID FROM SPECIES_REGION WHERE REGION_ID=?) "
" AND ID IN (SELECT SPECIES_ID FROM SPECIES_CATEGORY WHERE CATEGORY_ID=?) "
" ORDER BY COMMON_NAME")
:args args
:resp-v [:app/load-species.resp]}}))
(reg-tracker :app/load-species load-species-args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment