Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(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