Skip to content

Instantly share code, notes, and snippets.

@clojj
Created January 9, 2015 10:37
Show Gist options
  • Save clojj/ee4c779df57da9fde55f to your computer and use it in GitHub Desktop.
Save clojj/ee4c779df57da9fde55f to your computer and use it in GitHub Desktop.
(ns clojure.core.suspendable-test
(:require [clojure.core.async :as async]))
(def c (async/chan))
(defn fn1 [ctrl-chan]
(async/go-loop [cmd-old :run cmd-new :run]
(condp = cmd-new
:run (do
(when (not (= cmd-old cmd-new))
(println "SWITCH " cmd-old cmd-new))
(println "running...")
(let [[cmd _] (async/alts! [ctrl-chan (async/timeout 1000)])]
(recur :run (if (nil? cmd) :run cmd))))
:suspend (do
(when (not (= cmd-old cmd-new))
(println "SWITCH " cmd-old cmd-new))
(println "suspended...")
(let [[cmd _] (async/alts! [ctrl-chan (async/timeout 3000)])]
(recur :suspend (if (nil? cmd) :suspend cmd))))
:stop (println "stopped")
(do
(println "invalid command")
(recur cmd-old cmd-old)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment