🎉 Add malli based validation and coersion subsystem

This commit is contained in:
Andrey Antukh 2023-03-18 10:32:26 +01:00
parent dbc08ba80f
commit 5ca3d01ea1
125 changed files with 4984 additions and 2762 deletions

View file

@ -6,8 +6,9 @@
(ns app.worker
(:require
[app.common.data.macros :as dm]
[app.common.logging :as log]
[app.common.spec :as us]
[app.common.schema :as sm]
[app.worker.export]
[app.worker.impl :as impl]
[app.worker.import]
@ -16,33 +17,29 @@
[app.worker.snaps]
[app.worker.thumbnails]
[beicon.core :as rx]
[cljs.spec.alpha :as s]
[promesa.core :as p]))
(log/setup! {:app :info})
;; --- Messages Handling
(s/def ::cmd keyword?)
(def schema:message
[:map {:title "WorkerMessage"}
[:sender-id ::sm/uuid]
[:payload
[:map
[:cmd :keyword]]]
[:buffer? {:optional true} :boolean]])
(s/def ::payload
(s/keys :req-un [::cmd]))
(s/def ::sender-id uuid?)
(s/def ::buffer? boolean?)
(s/def ::message
(s/keys
:opt-un [::buffer?]
:req-un [::payload ::sender-id]))
(def message?
(sm/pred-fn schema:message))
(def buffer (rx/subject))
(defn- handle-message
"Process the message and returns to the client"
[{:keys [sender-id payload] :as message}]
(us/assert ::message message)
(dm/assert! (message? message))
(letfn [(post [msg]
(let [msg (-> msg (assoc :reply-to sender-id) (wm/encode))]
(.postMessage js/self msg)))
@ -88,7 +85,7 @@
(defn- drop-message
"Sends to the client a notification that its messages have been dropped"
[{:keys [sender-id] :as message}]
(us/assert ::message message)
(dm/assert! (message? message))
(.postMessage js/self (wm/encode {:reply-to sender-id
:dropped true})))