Back
assoc! (clj)
(source)function
(assoc! coll key val)
(assoc! coll key val & kvs)
When applied to a transient map, adds mapping of key(s) to
val(s). When applied to a transient vector, sets the val at index.
Note - index must be <= (count vector). Returns coll.
Examples
thheller/shadow-cljs
(ns shadow.remote.runtime.cljs.js-builtins
(:require
[goog.object :as gobj]
[clojure.core.protocols :as p]))
(extend-protocol p/Datafiable
;; FIXME: this is kind of a bad idea
;; can't do this for all objects, since none of the CLJS types implement this
;; protocol either. the protocol dispatch will end up using object
;; FIXME: this could detect CLJS types to some extent
;; or should it just implement the protocols for the types?
object
(datafy [o]
(if-not (identical? (.-__proto__ o) js/Object.prototype)
o
(with-meta
(->> (gobj/getKeys o)
(reduce
(fn [m key]
(assoc! m key (gobj/get o key)))
(transient {}))
(persistent!))
mhuebert/maria
(ns cells.cell
(:refer-clojure :exclude [bound-fn get])
(:require [clojure.core :as core]
[cells.util :as util]
[applied-science.js-interop :as j]))
(defmacro memoized-on [o k & body]
`(let [o# ~o]
(~'applied-science.js-interop/get o# ~k
(let [v# (do ~@body)]
(~'applied-science.js-interop/assoc! o# ~k)
v#))))
wilkerlucio/pathom3
(ns com.wsscode.pathom3.interface.smart-map-test
(:require
[clojure.core.protocols :as d]
[clojure.test :refer [deftest is are run-tests testing]]
[com.wsscode.pathom3.connect.built-in.resolvers :as pbir]
[com.wsscode.pathom3.connect.indexes :as pci]
[com.wsscode.pathom3.connect.operation :as pco]
[com.wsscode.pathom3.entity-tree :as p.ent]
[com.wsscode.pathom3.interface.smart-map :as psm]
[com.wsscode.pathom3.test.geometry-resolvers :as geo]
[com.wsscode.pathom3.test.helpers :as th]
[matcher-combinators.test])
#?(:clj
(:import
(clojure.lang
ExceptionInfo))))
(deftest sm-assoc!-test
(testing "uses source context on the new smart map"
(let [sm (psm/smart-map (pci/register registry)
{:x 3 :width 5})]
(is (= (:right sm) 8))
(is (= (:right (psm/sm-assoc! sm :width 10)) 8))
(is (= (:width sm) 10)))))
replikativ/replikativ
(ns replikativ.ormap-test
(:require [clojure.test :refer :all]
[replikativ.environ :refer [*date-fn*]]
[superv.async :refer [<?? S]]
[clojure.core.async :refer [timeout]]
[kabel.peer :refer [start stop]]
[konserve
[filestore :refer [new-fs-store]]
[memory :refer [new-mem-store]]]
[replikativ
[peer :refer [client-peer]]
[stage :refer [connect! create-stage!]]]
[replikativ.crdt.ormap.stage :as ors]
[replikativ.crdt.ormap.realize :as real]))
(deftest ormap-stage-test
(testing "ormap operations"
(let [user "mail:prototype@your-domain.com"
ormap-id #uuid "12345678-be95-4700-8150-66e4651b8e46"
store (<?? S (new-mem-store))
peer (<?? S (client-peer S store))
stage (<?? S (create-stage! user peer))
_ (<?? S (ors/create-ormap! stage
:id ormap-id
:description "some or map"
:public false))]
(is (= (get-in @stage [user ormap-id :downstream :crdt]) :ormap))
(binding [*date-fn* (constantly 0)]
(<?? S (ors/assoc! stage [user ormap-id] :me [['set-person {:name "Hal"}]])))
(is (= (map #(dissoc % :uid) (<?? S (ors/get stage [user ormap-id] :me)))
[{:transactions [['set-person {:name "Hal"}]],
:ts 0,
:author "mail:prototype@your-domain.com",
:version 1,
:crdt :ormap}]))
(<?? S (ors/dissoc! stage [user ormap-id] :me [['remove-person {:name "Hal"}]]))
(is (= (<?? S (ors/get stage [user ormap-id] :me)) nil))
(stop peer))))
(deftest ormap-streaming
(testing "ormap stream into identity"
(let [user "mail:prototype@your-domain.com"
ormap-id #uuid "12345678-be95-4700-8150-66e4651b8e46"
store (<?? S (new-mem-store))
peer (<?? S (client-peer S store))
stage (<?? S (create-stage! user peer))
val-atom (atom {})
eval-fn {'set-person (fn [S old [k v]]
(swap! old assoc k v)
old)
'remove-person (fn [S old k]
(swap! old dissoc k)
old)}
close-stream (real/stream-into-identity! stage [user ormap-id] eval-fn val-atom
:conflict-cb
(fn [cs] (is (= cs
{"Hal"
#{#uuid "275b3aed-818c-5f1b-9667-a51f5fc7f043"
#uuid "1f8c5b27-21d2-5b82-aab8-087784ee8903"}}))))
_ (<?? S (ors/create-ormap! stage
:id ormap-id
:description "some or map"
:public false))]
(is (= (get-in @stage [user ormap-id :downstream :crdt]) :ormap))
(binding [*date-fn* (constantly 0)]
(<?? S (ors/assoc! stage [user ormap-id] "Hal" [['set-person ["Hal" {:name "Hal"}]]])))
(is (= (map #(dissoc % :uid) (<?? S (ors/get stage [user ormap-id] "Hal")))
[{:transactions [['set-person ["Hal" {:name "Hal"}]]],
:ts 0,
:author "mail:prototype@your-domain.com",
:version 1,
:crdt :ormap}]))
(<?? S (timeout 100))
(is (= (get @val-atom "Hal") {:name "Hal"}))
(<?? S (ors/dissoc! stage [user ormap-id] "Hal" [['remove-person "Hal"]]))
(<?? S (timeout 100))
(is (= (<?? S (ors/get stage [user ormap-id] "Hal")) nil))
(binding [*date-fn* (constantly 0)]
(<?? S (ors/assoc! stage [user ormap-id] "Hal"
[['set-person ["Hal" {:name "Hal"}]]]))
(<?? S (ors/assoc! stage [user ormap-id] "Hal"
[['set-person ["Hal" {:name "Lah"}]]])))
(<?? S (timeout 100))
(is (= (get @val-atom "Hal") {:name "Lah"}))
(stop peer))))
(<?? (ors/or-assoc! stage-a [user-a ormap-id] 12 [['+ 42]]))
replikativ/replikativ
(ns replikativ.merging-ormap-test
(:require [clojure.test :refer :all]
[replikativ.crdt.merging-ormap.core :refer :all]
[replikativ.environ :refer [*date-fn*]]
[superv.async :refer [<?? S]]
[clojure.core.async :refer [timeout]]
[kabel.peer :refer [start stop]]
[konserve
[filestore :refer [new-fs-store]]
[memory :refer [new-mem-store]]]
[replikativ
[peer :refer [client-peer server-peer]]
[stage :refer [connect! create-stage!]]]
[replikativ.crdt.merging-ormap.stage :as mors]
#_[replikativ.crdt.ormap.realize :as real]))
(deftest merging-ormap-stage-test
(testing "merging ormap operations"
(let [user "mail:prototype@your-domain.com"
mormap-id #uuid "12345678-be95-4700-8150-66e4651b8e46"
store (<?? S (new-mem-store))
peer (<?? S (client-peer S store))
stage (<?? S (create-stage! user peer))
_ (<?? S (mors/create-merging-ormap! stage 'max max
:id mormap-id
:description "some or merging map"
:public false))]
(is (= (get-in @stage [user mormap-id :downstream :crdt]) :merging-ormap))
(<?? S (mors/assoc! stage [user mormap-id] :me 42))
(<?? S (mors/assoc! stage [user mormap-id] :me 43))
(is (= (<?? S (mors/get stage [user mormap-id] :me)) 43))
(<?? S (timeout 1000))
(is (= (<?? S (mors/get stage [user mormap-id] :me)) 43))
(<?? S (mors/dissoc! stage [user mormap-id] :me))
(is (= (<?? S (mors/get stage [user mormap-id] :me)) nil))
(stop peer))))
_ (<?? S (mors/create-merging-ormap! stage-a 'max max
:id mormap-id
:description "some or merging map"
:public false))
_ (<?? S (mors/create-merging-ormap! stage-b 'max max
:id mormap-id
:description "some or merging map"
:public false))]
(<?? S (start peer-a))
(<?? S (timeout 1000))
(<?? S (connect! stage-b uri))
(is (= (get-in @stage-a [user mormap-id :downstream :crdt]) :merging-ormap))
(<?? S (mors/assoc! stage-a [user mormap-id] :me 42))
(<?? S (mors/assoc! stage-b [user mormap-id] :me 43))
(<?? S (timeout 1000))
(is (= (<?? S (mors/get stage-a [user mormap-id] :me)) 43))
(is (= (<?? S (mors/get stage-b [user mormap-id] :me)) 43))
(<?? S (mors/dissoc! stage-a [user mormap-id] :me))
(<?? S (timeout 1000))
(is (= (<?? S (mors/get stage-a [user mormap-id] :me)) nil))
(is (= (<?? S (mors/get stage-b [user mormap-id] :me)) nil))
(stop peer-a)
(stop peer-b))))