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

loop and short-circuiting #166

Open
prepor opened this issue Jan 25, 2019 · 5 comments
Open

loop and short-circuiting #166

prepor opened this issue Jan 25, 2019 · 5 comments

Comments

@prepor
Copy link

prepor commented Jan 25, 2019

Hello. Is this code supposed to stop loop execution?

(require '[manifold.deferred :as deferred]
           '[manifold.stream :as stream]
           '[manifold.time :as mt])
  (->
   (deferred/loop []
     (prn "---TICK!")
     (mt/in 200 #(deferred/recur)))
   (deferred/timeout! 1000)
   (deref))

Currently, it loops forever and for me, it looks as broken API. Maybe loop should check result# before each tick?

@ztellman
Copy link
Collaborator

Sure, that would be consistent with other parts of the API, I just hadn't ever considered someone would try something like this. PR is welcome, otherwise I'll get to it when I can.

@prepor
Copy link
Author

prepor commented Jan 25, 2019

@ztellman I started working on PR. master...prepor:patch-1

For this primitive case it works. Then I put loop into chain and we again have infinity loop:

(->
   (deferred/chain
     (deferred/loop []
       (prn "---TICK!")
       (mt/in 200 #(deferred/recur)))
     (fn [_] :finished))
   (deferred/timeout! 1000)
   (deref))

Then I realized that short-circuiting doesn't work for nested chains too:

(->
   (deferred/chain
     (deferred/chain
       (deferred/future (Thread/sleep 2000))
       (fn [_]
         (prn "----FINISH")
         :finished))
     (fn [_] :finished))
   (deferred/timeout! 1000)
   (deref))
=> Execution error (TimeoutException) at manifold.deferred/timeout!$fn (deferred.clj:1160).
timed out after 1000 milliseconds
(after some time) => "----FINISH"

Is this could be considered as a bug or?

@ztellman
Copy link
Collaborator

You'll want the when-not check to be inside the loop, not outside.

@prepor
Copy link
Author

prepor commented Jan 28, 2019

@ztellman yes, it makes sense, fixed.

But it doesn't fix issues which I described previously.

@ztellman
Copy link
Collaborator

ztellman commented Feb 1, 2019

If we had a reified topology of deferreds (like we do streams), we could potentially walk upstream when a given deferred is cancelled and cancel everything that feeds only into that cancelled deferred. Since we don't, there's no easy, general way to address either of the cases you mention. Barring a major reengineering of Manifold, I think we have to be content with the simple cancellation case working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants