Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/cljc/schema/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
See the docstrings of defrecord, fn, and defn for more details about how
to use these macros."
;; don't exclude def because it's not a var.
(:refer-clojure :exclude [Keyword Symbol Inst atom defprotocol defrecord defn letfn defmethod fn MapEntry ->MapEntry])
(:refer-clojure :exclude [Keyword Symbol Inst atom defprotocol defrecord defn letfn defmethod fn MapEntry ->MapEntry
volatile volatile? Volatile ->Volatile])
(:require
#?(:clj [clojure.pprint :as pprint])
[clojure.string :as str]
Expand Down Expand Up @@ -699,6 +700,28 @@
[schema]
(->Atomic schema))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Volatile schema

(defn- volatile? [x]
#?(:clj (instance? clojure.lang.Volatile x)
:cljs (satisfies? IVolatile x)))

(macros/defrecord-schema Volatile [schema]
Schema
(spec [this]
(collection/collection-spec
(spec/simple-precondition this volatile?)
clojure.core/volatile!
[(collection/one-element true schema (clojure.core/fn [item-fn coll] (item-fn @coll) nil))]
(clojure.core/fn [_ xs _] (clojure.core/volatile! (first xs)))))
(explain [this] (list 'volatile (explain schema))))

(clojure.core/defn volatile
"A volatile containing a value matching 'schema'."
[schema]
(->Volatile schema))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Map Schemas
Expand Down
9 changes: 9 additions & 0 deletions test/cljc/schema/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,15 @@
(invalid! s (delay "asdf") "(not (atom? a-clojure.lang.Delay))")
(invalid! s (atom 1))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Atom schemas

(deftest volatile-test
(let [s (s/volatile s/Str)]
(is (not (s/check s (volatile! "asdf")))) ;; don't expect identity after walking
(invalid! s (delay "asdf") "(not (volatile? a-clojure.lang.Delay))")
(invalid! s (volatile! 1))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Map Schemas

Expand Down
Loading