-
Notifications
You must be signed in to change notification settings - Fork 4
/
environment.cljc
46 lines (39 loc) · 1.58 KB
/
environment.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(ns com.fulcrologic.statecharts.environment
"Helper functions related to the environment that can be used in a lambda execution environment on
`env`."
(:require
[clojure.spec.alpha :as s]
[com.fulcrologic.guardrails.core :refer [>defn => ?]]
[com.fulcrologic.statecharts :as sc]
[com.fulcrologic.statecharts.data-model.operations :as ops]
[com.fulcrologic.statecharts.protocols :as sp]))
(>defn session-id
"Returns the session ID from an env."
[{::sc/keys [vwmem] :as _env}]
[::sc/processing-env => (? ::sc/session-id)]
(some-> vwmem deref ::sc/session-id))
(>defn is-in-state?
"Returns true if the given `state-id` is active (in the configuration) of the state chart."
[{::sc/keys [vwmem] :as _env} state-id]
[::sc/env ::sc/id => boolean?]
(let [active-states (some-> vwmem deref ::sc/configuration)]
(contains? active-states state-id)))
(def In
"[env state-id]
Alias for `is-in-state?`"
is-in-state?)
(>defn context-element-id
"Returns the ID of the context (state of interest for the current operation) from an env, if set."
[env]
[(s/keys :req [::sc/context-element-id]) => (? ::sc/id)]
(::sc/context-element-id env))
(defn assign!
"Side effect against the data model in `env`, with the given path-value pairs."
[{::sc/keys [data-model] :as env} & {:as path-value-pairs}]
(sp/update! data-model env {:ops (ops/set-map-ops path-value-pairs)})
nil)
(defn delete!
"Side effect against the data model in `env`, with the given keys/paths"
[{::sc/keys [data-model] :as env} & ks]
(sp/update! data-model env {:ops [(ops/delete ks)]})
nil)