Implemented extension context

Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
Andrey Shovkoplyas 2018-11-18 13:33:05 +01:00 committed by Julien Eluard
parent afaeb63e63
commit 7778e2d2e7
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
3 changed files with 17 additions and 13 deletions

View File

@ -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]

View File

@ -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

View File

@ -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)}))))