[#6] make callbacks more user friendly
This commit is contained in:
parent
98d4f81432
commit
990d804199
3 changed files with 13 additions and 13 deletions
16
README.md
16
README.md
|
|
@ -335,11 +335,11 @@ as client side code. An example from the
|
||||||
"pod.babashka.filewatcher"
|
"pod.babashka.filewatcher"
|
||||||
'pod.babashka.filewatcher/watch*
|
'pod.babashka.filewatcher/watch*
|
||||||
[path opts]
|
[path opts]
|
||||||
{:handlers {:success (fn [{:keys [:value]}] (cb value))
|
{:handlers {:success cb
|
||||||
:error (fn [{:keys [:ex-message :ex-data]}]
|
:error (fn [{:keys [:ex-message :ex-data]}]
|
||||||
(binding [*out* *err*]
|
(binding [*out* *err*]
|
||||||
(println "ERROR:" ex-message)))
|
(println "ERROR:" ex-message)))
|
||||||
:done (fn [_])}})
|
:done (fn [])}})
|
||||||
nil))
|
nil))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -358,9 +358,9 @@ The return value of `babashka.pods/invoke` is a map containing `:result`. When
|
||||||
not using callbacks, this is the return value from the pod var invocation. When
|
not using callbacks, this is the return value from the pod var invocation. When
|
||||||
using callbacks, this value is undefined.
|
using callbacks, this value is undefined.
|
||||||
|
|
||||||
The callback `:success` is called with a map containing:
|
The callback `:success` is called with a map containing a return value from the
|
||||||
|
pod invocation. The pod can potentially return multiple values. The callback
|
||||||
- `:value`: a return value from the pod var
|
will be called with every value individually.
|
||||||
|
|
||||||
The callback `:error` is called in case the pod sends an error, a map
|
The callback `:error` is called in case the pod sends an error, a map
|
||||||
containing:
|
containing:
|
||||||
|
|
@ -372,9 +372,9 @@ containing:
|
||||||
If desired, `:ex-message` and `:ex-data` can be reified into a
|
If desired, `:ex-message` and `:ex-data` can be reified into a
|
||||||
`java.lang.Exception` using `ex-info`.
|
`java.lang.Exception` using `ex-info`.
|
||||||
|
|
||||||
The callback `:done` is called with one argument, a map. This callback can be
|
The callback `:done` is a 0-arg function. This callback can be used to determine
|
||||||
used to determine if the pod is done sending values, in case it wants to send
|
if the pod is done sending values, in case it wants to send multiple. The
|
||||||
multiple. The callback is only called if no errors were sent by the pod.
|
callback is only called if no errors were sent by the pod.
|
||||||
|
|
||||||
In the above example the wrapper function calls the pod identified by
|
In the above example the wrapper function calls the pod identified by
|
||||||
`"pod.babashka.filewatcher"`. It calls the var
|
`"pod.babashka.filewatcher"`. It calls the var
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@
|
||||||
(cond promise?
|
(cond promise?
|
||||||
(deliver chan (if error? exception value))
|
(deliver chan (if error? exception value))
|
||||||
(and (not error?) success-handler)
|
(and (not error?) success-handler)
|
||||||
(success-handler {:value value})
|
(success-handler value)
|
||||||
(and error? error-handler)
|
(and error? error-handler)
|
||||||
(error-handler {:ex-message ex-message
|
(error-handler {:ex-message ex-message
|
||||||
:ex-data ex-data})))
|
:ex-data ex-data})))
|
||||||
|
|
@ -89,7 +89,7 @@
|
||||||
(when promise?
|
(when promise?
|
||||||
(deliver chan nil))
|
(deliver chan nil))
|
||||||
(when done-handler
|
(when done-handler
|
||||||
(done-handler {})))
|
(done-handler)))
|
||||||
(when out
|
(when out
|
||||||
(binding [*out* out-stream]
|
(binding [*out* out-stream]
|
||||||
(println out)))
|
(println out)))
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@
|
||||||
(def stream-results (atom []))
|
(def stream-results (atom []))
|
||||||
(def done-prom (promise))
|
(def done-prom (promise))
|
||||||
(pods/invoke "pod.test-pod" 'pod.test-pod/range-stream [1 10]
|
(pods/invoke "pod.test-pod" 'pod.test-pod/range-stream [1 10]
|
||||||
{:handlers {:success (fn [{:keys [:value]}]
|
{:handlers {:success (fn [value]
|
||||||
(swap! stream-results conj value))
|
(swap! stream-results conj value))
|
||||||
:done (fn [_]
|
:done (fn []
|
||||||
(deliver done-prom :ok))}})
|
(deliver done-prom :ok))}})
|
||||||
@done-prom
|
@done-prom
|
||||||
|
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
(def callback-result (promise))
|
(def callback-result (promise))
|
||||||
(pods/invoke "pod.test-pod" 'pod.test-pod/add-sync [1 2]
|
(pods/invoke "pod.test-pod" 'pod.test-pod/add-sync [1 2]
|
||||||
{:handlers {:success
|
{:handlers {:success
|
||||||
(fn [{:keys [:value]}]
|
(fn [value]
|
||||||
(deliver callback-result value))}})
|
(deliver callback-result value))}})
|
||||||
|
|
||||||
(def error-result (promise))
|
(def error-result (promise))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue