/
api.cljc
206 lines (179 loc) · 4.94 KB
/
api.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
(ns portal.api
(:require #?(:clj
[portal.runtime.jvm.commands])
#?(:clj [portal.runtime.jvm.launcher :as l]
:cljs [portal.runtime.node.launcher :as l]
:cljr [portal.runtime.clr.launcher :as l])
#?(:clj [portal.sync :as a]
:cljs [portal.async :as a]
:cljr [portal.sync :as a])
#?(:clj [clojure.java.io :as io]
:cljs [portal.resources :as io])
[clojure.set :as set]
[portal.runtime :as rt]
[portal.runtime.cson :as cson]))
(defn submit
"Tap target function.
Usage:
```clojure
(add-tap #'portal.api/submit)
(remove-tap #'portal.api/submit)
```"
{:added "0.9.0"}
[value]
(rt/update-value value)
nil)
(defn tap
"Add portal as a `tap>` target."
{:added "0.1.0"
:deprecated "0.9"
:superseded-by "submit"}
[]
(add-tap #'submit)
nil)
(def ^:private long->short
{:portal.colors/theme :theme
:portal.launcher/app :app
:portal.launcher/host :host
:portal.launcher/port :port
:portal.launcher/window-title :window-title})
(defn- rename [options]
(set/rename-keys options long->short))
(defn set-defaults!
"Set default options for `open` and `start`.
Parameters passed directly to either will override defaults."
{:added "0.20.0"
:see-also ["open" "start"]}
[options]
(swap! rt/default-options merge (rename options)))
(defn start
"Start the HTTP server with non-default options. Only use if you need
control over the HTTP server."
{:added "0.6.2"
:see-also ["stop"]}
[options]
(l/start (rename options)))
(defn stop
"Stop the HTTP server."
{:added "0.41.0"
:see-also ["start"]}
[]
(l/stop))
(defn open
"Open a new inspector window. A previous instance can be passed as
parameter to make sure it is open."
{:added "0.1.0"
:see-also ["close"]}
([] (open nil))
([portal-or-options]
(if (:session-id portal-or-options)
(open portal-or-options nil)
(open nil portal-or-options)))
([portal options]
(l/open portal (rename options))))
(defn close
"Close all current inspector windows."
{:added "0.1.0"
:see-also ["open"]}
([]
(l/close :all)
nil)
([portal]
(l/close portal)
nil))
(defn clear
"Clear all values."
{:added "0.1.0"}
([]
(l/clear :all)
nil)
([portal]
(l/clear portal)
nil))
(defn register!
"Register a var with portal. For now, the var should be a 1 arity fn.
Example:
```clojure
(register! #'identity)
```
The function name and doc string will show up in the command palette."
{:added "0.16.0"}
[var]
(rt/register! var)
nil)
(defn- print-err [s]
#?(:clj (binding [*out* *err*]
(print s)
(flush))
:cljs (binding [*print-fn* *print-err-fn*]
(print s)
(flush))
:cljr (binding [*out* *err*]
(print s)
(flush))))
(defn eval-str
"Evalute ClojureScript source given as a string in the UI runtime. The parameters:
- portal: portal instance returned from `portal.api/open` or `:all`
- code (string): the ClojureScript source
- opts (map): evaluation options.
- `:verbose` optional, return a map containing more info that just the value.
- Defaults to false.
- `:await` - optional, await a promise result. Defaults to `false`."
{:added "0.19.0"
:see-also ["open"]}
([code]
(eval-str :all code))
([portal code]
(eval-str portal code nil))
([portal code opts]
(a/let [result (l/eval-str portal (assoc opts :code code))]
(when-not (:verbose opts)
(doseq [{:keys [tag val]} (:stdio result)]
(cond
(= :out tag) (do (print val) (flush))
(= :err tag) (print-err val))))
(cond-> result (not (:verbose opts)) :value))))
(defn sessions
"Get all current portal sessions."
{:added "0.27.0"}
[]
(l/sessions))
(defn url
"Get url for portal session."
{:added "0.33.0"}
[portal]
(l/url portal))
(def ^:no-doc ^:dynamic *nrepl-init* nil)
(defn repl
"Start a repl for the given Portal session."
{:added "0.31.0"}
[portal]
(if *nrepl-init*
(*nrepl-init* portal)
(throw
(ex-info
"Please start nREPL with `portal.nrepl/wrap-repl` middleware to enable the portal subrepl."
{:portal-instance portal
:missing-middleware 'portal.nrepl/wrap-repl}))))
(defn- get-docs []
(cson/read
#?(:clj (slurp (io/resource "portal/docs.json"))
:cljs (io/inline "portal/docs.json"))))
(defn docs
"Open portal docs."
{:added "035.0"
:see-also ["open"]}
([]
(docs nil))
([options]
(open (assoc options :window-title "portal-docs" :value (get-docs)))))
(defn inspect
"Open a new portal window to inspect a particular value."
{:command true
:added "0.38.0"
:see-also ["open"]}
([value]
(inspect value (:options rt/*session*)))
([value options]
(open (assoc options :value value))))
(register! #'inspect)