From 7778e2d2e725cebed169b678a5f5e79e62575e18 Mon Sep 17 00:00:00 2001 From: Andrey Shovkoplyas Date: Sun, 18 Nov 2018 13:33:05 +0100 Subject: [PATCH] Implemented extension context Signed-off-by: Julien Eluard --- examples/src/pluto/examples.cljs | 14 ++++++++------ src/pluto/reader.cljc | 2 ++ src/pluto/reader/types.cljc | 14 +++++++------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/examples/src/pluto/examples.cljs b/examples/src/pluto/examples.cljs index 1a0d2ab..7114a37 100644 --- a/examples/src/pluto/examples.cljs +++ b/examples/src/pluto/examples.cljs @@ -30,15 +30,15 @@ (re-frame/reg-event-fx :alert - (fn [cofx [_ {:keys [value]}]] - {::alert value})) + (fn [cofx [_ env {:keys [value]}]] + {::alert (str (:id env) value)})) (re-frame/reg-sub :random-boolean :random) (re-frame/reg-sub :extensions/identity - (fn [_ [_ {:keys [value]}]] value)) + (fn [_ [_ _ {:keys [value]}]] value)) (defn render [h el] (reagent/render (h {:name "Test Extension" @@ -60,17 +60,19 @@ (unhook [_ id {:keys [scope]} {:keys [db] :as cofx}]))) (defn parse [m] - (reader/parse {:capacities {:components html/components + (reader/parse {:env {:id "Extension ID"} + :capacities {:components html/components :queries {'random-boolean {:value :random-boolean} - 'identity {:value :extensions/identity :arguments {:value :map}}} + 'identity + {:value :extensions/identity :arguments {:value :map}}} :hooks {:main {:hook hook :properties {:view :view}}} :events {'alert {:permissions [:read] :value :alert - :arguments {:value :string}}}}} + :arguments {:value :string}}}}} m)) (defn render-extension [m el el-errors] diff --git a/src/pluto/reader.cljc b/src/pluto/reader.cljc index 0a80973..230558a 100644 --- a/src/pluto/reader.cljc +++ b/src/pluto/reader.cljc @@ -86,6 +86,8 @@ "Parse an extension definition map as encapsulated in :data key of the map returned by read. `ctx` is a map defining: * `capacities` a map of valid supported capacities (hooks, queries, events) + * `env` [optional] a map of extension environment, may contain for example id of extension {:id 'id'}, will be + * provided as second parameter into event and query handlers Returns a map defining: * :data a map of meta and parsed hooks diff --git a/src/pluto/reader/types.cljc b/src/pluto/reader/types.cljc index 93ac79a..7d2312b 100644 --- a/src/pluto/reader/types.cljc +++ b/src/pluto/reader/types.cljc @@ -116,19 +116,19 @@ (string? o) (utils/interpolate env o) :else (walk/postwalk-replace env o))) -(defn event-after-env [ref data args bindings] +(defn event-after-env [ctx ref data args bindings] (with-meta (fn [o env] (let [env (merge env (reduce-kv #(assoc %1 (symbol (name %2)) %3) {} o) (:data (destructuring/destructure bindings (merge o args (reduce-kv #(assoc %1 (keyword (name %2)) %3) {} env))))) dic (reduce-kv #(assoc %1 %2 (if (contains? env %3) (get env %3) %3)) {} env)] - [ref (merge o (reduce-kv #(assoc %1 %2 (replace-atom dic %3)) {} data))])) + [ref (:env ctx) (merge o (reduce-kv #(assoc %1 %2 (replace-atom dic %3)) {} data))])) {:event true})) -(defn- reference-with-arguments [ctx ext ref event arguments args bindings] +(defn- event-reference-with-arguments [ctx ext ref event arguments args bindings] (if arguments (let [{:keys [data errors]} (resolve-arguments ctx ext event arguments)] - (errors/merge-errors {:data (event-after-env ref data args bindings)} errors)) + (errors/merge-errors {:data (event-after-env ctx ref data args bindings)} errors)) {:data (fn [o v] [ref o])})) (defn- reference-symbol [value] @@ -168,16 +168,16 @@ (let [{:keys [data errors]} (resolve-local-reference ctx ext type value) {event :event ref :ref args :args bindings :bindings} data] ;; TODO better separate local event handling - (merge (when data (reference-with-arguments ctx ext ref event (or args arguments) arguments bindings)) + (merge (when data (event-reference-with-arguments ctx ext ref event (or args arguments) arguments bindings)) (when errors {:errors (apply conj [(errors/error ::errors/unknown-event symbol)] errors)})))) (defmethod resolve :event [ctx ext type [name arguments :as value]] (resolve-event ctx ext type value)) -(defmethod resolve :query [ctx ext type [name arguments :as value]] +(defmethod resolve :query [{:keys [env] :as ctx} ext type [name arguments :as value]] (let [{:keys [data errors]} (reference/resolve ctx ext type value)] - (merge (when data {:data (if arguments [data arguments] [data])}) + (merge (when data {:data (if arguments [data env arguments] [data env])}) (when errors {:errors (apply conj [(errors/error ::errors/unknown-query name)] errors)}))))