-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add experimental flow protocol enforcer
- Loading branch information
Showing
2 changed files
with
40 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
(ns hyperfiddle.incseq.flow-protocol-enforcer | ||
(:require [hyperfiddle.electric.impl.array-fields :as a]) | ||
#?(:clj (:import [clojure.lang IDeref IFn])) | ||
#?(:cljs (:require-macros [hyperfiddle.incseq.flow-protocol-enforcer :refer [cannot-throw]]))) | ||
|
||
(defn violated | ||
([nm msg] (println nm "flow protocol violation:" msg) #?(:cljs (.error js/console) :clj (prn (Throwable.)))) | ||
([nm msg e] | ||
(println nm "flow protocol violation:" msg) | ||
(#?(:clj prn :cljs js/console.error) e))) | ||
|
||
(defmacro cannot-throw [nm f] `(try (~f) (catch ~(if (:js-globals &env) :default 'Throwable) e# | ||
(violated ~nm ~(str f " cannot throw") e#)))) | ||
|
||
(def field-count (a/deffields -should-step -is-done)) | ||
(defn flow | ||
([input-flow] (flow "" input-flow)) | ||
([nm input-flow] | ||
(fn [step done] | ||
(let [o (object-array field-count) | ||
_ (a/set o -should-step ::init, -is-done false) | ||
step (fn [] | ||
(when (a/get o -is-done) (violated nm "step after done")) | ||
(if (a/getswap o -should-step not) (cannot-throw nm step) (violated nm "double step"))) | ||
done (fn [] (if (a/getset o -is-done true) (violated nm "done called twice") (cannot-throw nm done))) | ||
cancel (try (input-flow step done) | ||
(catch #?(:clj Throwable :cljs :default) e (violated "flow process creation threw" e)))] | ||
(reify | ||
IFn (#?(:clj invoke :cljs -invoke) [_] (cannot-throw nm cancel)) | ||
IDeref (#?(:clj deref :cljs -deref) [_] | ||
(if-let [should-step (a/getswap o -should-step not)] | ||
(violated nm (if (= ::init should-step) "transfer without initial step" "double transfer")) | ||
@cancel))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters