Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

continuous switches #26

Closed
leonoel opened this issue Dec 21, 2020 · 1 comment
Closed

continuous switches #26

leonoel opened this issue Dec 21, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@leonoel
Copy link
Owner

leonoel commented Dec 21, 2020

Problem : switching flows is currently possible with ap/?! but the behavior is discrete and eager, which is not always what we want.

Solution : provide a new macro cp (continuous process) returning a continuous flow, with ?! available as a forking operator. Like in ap, ?! runs provided flow, forks current computation and cancels previous branch for each successive value, but each branch is run lazily.

Example :

(def numbers-channel (atom 0))
(def strings-network (atom "hello"))
(def remote-control (atom :strings))

(def tv
  (m/cp
    (case (m/?! (m/watch remote-control))
      :numbers (m/?! (m/watch numbers-channel))
      :strings (m/?! (m/watch strings-network))
      :both (m/?! (m/latest vector (m/watch numbers-channel) (m/watch strings-network)))
      :static-noise)))

(def it (tv #(prn :ready) #(prn :done)))                  ;; :ready
@it #_=> "hello"
(reset! remote-control :numbers)                          ;; :ready
@it #_=> 0
(swap! numbers-channel inc)                               ;; :ready
(swap! numbers-channel inc)
@it #_=> 2
(swap! numbers-channel inc)                               ;; :ready
(reset! remote-control :both)
(reset! strings-network "world")
@it #_=> [3 "world"]
(reset! remote-control nil)                               ;; :ready
@it #_=> :static-noise
@leonoel
Copy link
Owner Author

leonoel commented Feb 13, 2023

Released in b.27

@leonoel leonoel closed this as completed Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant