Skip to content

Commit

Permalink
drain - impl + docstring + test
Browse files Browse the repository at this point in the history
  • Loading branch information
leonoel committed Jan 26, 2024
1 parent 9cccd8c commit 511fc0c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/hyperfiddle/electric/impl/runtime_de.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@

(def fixed-signals (comp (partial m/signal i/combine) i/fixed))

(defn drain [incseq]
(m/ap
(m/amb (i/empty-diff 0)
(do (m/?> incseq) (m/amb)))))

(def peer-slot-input 0)
(def peer-slot-store 1)
(def peer-slots 2)
Expand Down
9 changes: 8 additions & 1 deletion src/hyperfiddle/electric_de.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ Syntax :
Stabilizes successive states of collection `xs` with function `kf`. Returns each item as a table.
" [f xs] `(join (i/diff-by ~f (join (i/items (pure ~xs))))))

;; (defmacro drain [expr] `(join (i/drain (pure ~expr))))
(defmacro drain "
Syntax :
```clojure
(drain expr)
```
Samples and discards `expr` synchronously with changes. Returns nothing.
" [expr] `(join (r/drain (pure ~expr))))

(defmacro client [& body] `(::lang/site :client ~@body))
(defmacro server [& body] `(::lang/site :server ~@body))

Expand Down
11 changes: 10 additions & 1 deletion test/hyperfiddle/electric/impl/runtime_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,13 @@
[x y])))
% := {:degree 1, :permutation {}, :grow 1, :shrink 0, :change {0 [[1 0] [2 0] [2 1]]}, :freeze #{}}
(swap! !n inc)
% := {:degree 1, :permutation {}, :grow 0, :shrink 0, :change {0 [[1 0] [2 0] [2 1] [3 0] [3 1] [3 2]]}, :freeze #{}})
% := {:degree 1, :permutation {}, :grow 0, :shrink 0, :change {0 [[1 0] [2 0] [2 1] [3 0] [3 1] [3 2]]}, :freeze #{}})

(tests
(def !x (atom 0))
(on-diff! rcf/tap
(root-frame (e/drain (rcf/tap (e/watch !x)))))
% := 0
% := {:degree 0, :permutation {}, :grow 0, :shrink 0, :change {}, :freeze #{}}
(swap! !x inc)
% := 1)

0 comments on commit 511fc0c

Please sign in to comment.