No more registry functions in pluto

Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
Aleksandr Pantiukhov 2018-11-06 10:51:16 +01:00 committed by Julien Eluard
parent f1c904d309
commit 5fe6ac05c1
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
5 changed files with 1 additions and 158 deletions

View File

@ -1,4 +1,4 @@
(defproject status-im/pluto "iteration-3"
(defproject status-im/pluto "iteration-4-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.9.0"]
[org.clojure/clojurescript "1.10.238"]
[org.clojure/tools.reader "1.2.1"]

View File

@ -1,59 +0,0 @@
(ns pluto.registry
(:refer-clojure :exclude [remove])
(:require [clojure.spec.alpha :as spec]
[clojure.set :as set]
[clojure.string :as string]
[pluto.reader.hooks :as hooks]
[pluto.utils :as utils]))
(spec/def :registry/registry (spec/map-of string? :registry/extension))
(spec/def :registry/state #{:active :inactive})
(spec/def :registry/data any?)
(spec/def :registry/extension (spec/keys :req-un [:registry/state :registry/data]))
;; TODO move to a protocol and provide default re-frame implementation
(defn add
"Takes parsed data and coeffects map, adds extension to registry with `:inactive` initial state."
[parsed-data {:keys [db]}]
(let [extension-key (get-in parsed-data ['meta :name])]
{:db (assoc-in db [:registry extension-key] {:state :inactive
:data parsed-data})}))
(def ^:private state->new-state {:active :inactive
:inactive :active})
(defn- switch [new-state extension-key {:keys [db] :as cofx}]
(when-let [{:keys [state data]} (get-in db [:registry extension-key])]
(when-not (= state new-state)
(apply utils/merge-fx cofx
(constantly {:db (assoc-in db [:registry extension-key :state] (get state->new-state state))})
(mapcat (fn [[app-hook extension-hooks]]
(map (fn [[hook-id {:keys [hook-ref parsed]}]]
(if (= :active new-state)
(partial hooks/hook-in (:hook hook-ref) hook-id parsed)
(partial hooks/unhook (:hook hook-ref) hook-id parsed)))
extension-hooks))
(:hooks data))))))
(def activate
"Takes extension key and activates it by turning on all hooks. Extension state is switched to active."
(partial switch :active))
(def deactivate
"Takes extension key and de-activates it by turning off all hooks. Extension state is switched to inactive."
(partial switch :inactive))
(defn remove
"Removes extension from extension map altogether, if the extension is in active state, deactives it first."
[extension-key {:keys [db] :as cofx}]
(when-let [{:keys [state]} (get-in db [:registry extension-key])]
(if (= :inactive state)
{:db (update db :registry dissoc extension-key)}
(utils/merge-fx cofx
(partial deactivate extension-key)
(fn [{:keys [db]}]
{:db (update db :registry dissoc extension-key)})))))

View File

@ -28,17 +28,3 @@
(defn interpolate [values s]
(reduce-kv #(string/replace %1 (str "${" (str %2) "}") (str %3))
s values))
(defn- update-db [cofx {:keys [db] :as fx}]
(if db (assoc cofx :db db) cofx))
;; TODO(janherich): we have similar function in `status-react` - it's probably worth to push such things
;; into some `re-frame-helpers` library, together with collection of generic, app agnostic co-effects/effects
;; like `now`/`random-id` co-effects or `http` call effect
(defn merge-fx
([cofx & fx-fns]
(first (reduce (fn [[fx cofx] fx-fn]
(let [new-fx (fx-fn cofx)]
[(merge fx new-fx) (update-db cofx new-fx)]))
[{} cofx]
fx-fns))))

View File

@ -1,69 +0,0 @@
(ns pluto.registry-test
(:refer-clojure :exclude [read])
(:require [clojure.test :refer [is deftest testing]]
[pluto.reader.hooks :as hooks]
[pluto.registry :as registry]
[pluto.utils :as utils]))
(def parsed-data {'meta {:name "test"}
:hooks {:main {:a {:parsed {:name "tester"}
:hook-ref {:properties {:name :string}
:hook
(reify hooks/Hook
(hook-in [_ id properties {:keys [db]}]
{:db (assoc-in db [:main id] properties)})
(unhook [_ id _ {:keys [db]}]
{:db (update db :main dissoc id)}))}}}}})
(deftest add-test
(testing "Correctly adds extension"
(is (= {:db {:registry {"test" {:state :inactive
:data parsed-data}}}}
(registry/add parsed-data {:db {}}))))
(testing "Correctly adds new extension without touching existing one"
(is (= {:db {:registry {"test" {:state :inactive
:data parsed-data}
"old" {:state :active}}}}
(registry/add parsed-data {:db {:registry {"old" {:state :active}}}})))))
(deftest activate-test
(testing "When extension is not present, do nothing"
(is (= nil (registry/activate "test" {:db {}}))))
(testing "When extension is present and activated, state is correctly switched + all app hooks are hooked-in"
(is (= {:db {:registry {"test" {:state :active
:data parsed-data}}
:main {:a {:name "tester"}}}}
(utils/merge-fx {:db {}}
(partial registry/add parsed-data)
(partial registry/activate "test")))))
(testing "When extension is already activated, do nothing"
(is (= nil (registry/activate "test" {:db {:registry {"test" {:state :active}}}})))))
(deftest deactivate-test
(testing "When extension is not present, do nothing"
(is (= nil (registry/deactivate "test" {:db {}}))))
(testing "When extension is present and deactivated, state is correctly switched + all app hooks are unhooked"
(is (= {:db {:registry {"test" {:state :inactive
:data parsed-data}}
:main {}}}
(utils/merge-fx {:db {}}
(partial registry/add parsed-data)
(partial registry/activate "test")
(partial registry/deactivate "test")))))
(testing "When extension is already deactivated, do nothing"
(is (= nil (registry/deactivate "test" {:db {:registry {"test" {:state :inactive}}}})))))
(deftest remove-test
(testing "When extension is not present, do nothing"
(is (= nil (registry/remove "test" {:db {}}))))
(testing "When extension is present and inactive, remove it"
(is (= {:db {:registry {}}}
(registry/remove "test"
{:db {:registry {"test" {:state :inactive}}}}))))
(testing "When extension is present and active, remove it + produce unhook effects"
(is (= {:db {:registry {}
:main {}}}
(utils/merge-fx {:db {}}
(partial registry/add parsed-data)
(partial registry/activate "test")
(partial registry/remove "test"))))))

View File

@ -3,21 +3,6 @@
(:require [clojure.test :refer [is deftest testing]]
[pluto.utils :as utils]))
(deftest merge-fx-test
(testing "Correctly merges results of multiple effect producing functions"
(is (= {:db {:a 2
:b 1}
:fire-missile true}
(utils/merge-fx {:db {:a 0}
:now "current-time"}
(fn [{:keys [db]}]
{:db (update db :a inc)})
(fn [{:keys [db]}]
{:db (-> db
(update :a inc)
(assoc :b 1))})
(constantly {:fire-missile true}))))))
#?(:clj
(defmacro slurp [file]
(clojure.core/slurp file)))