Skip to content

Commit

Permalink
refactor(rtc): support refactored update-op, update local db
Browse files Browse the repository at this point in the history
  • Loading branch information
RCmerci committed May 16, 2024
1 parent 55cec66 commit a12a0d3
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 345 deletions.
10 changes: 6 additions & 4 deletions deps/db/src/logseq/db/sqlite/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@
(assert (some? (:db/id entity)))
(assoc (.-kv entity)
:db/id (:db/id entity)))))
(merge (cljs-bean.transit/writer-handlers)))]
(merge (cljs-bean.transit/writer-handlers)))
writer (transit/writer :json {:handlers write-handlers})]
(fn write-transit-str* [o]
(try (transit/write (transit/writer :json {:handlers write-handlers}) o)
(try (transit/write writer o)
(catch :default e
(prn ::write-transit-str o)
(throw e))))))

(def read-transit-str
(let [read-handlers (assoc dt/read-handlers
"datascript/Entity" identity)]
(fn read-transit-str* [s] (transit/read (transit/reader :json {:handlers read-handlers}) s))))
"datascript/Entity" identity)
reader (transit/reader :json {:handlers read-handlers})]
(fn read-transit-str* [s] (transit/read reader s))))

(defn db-based-graph?
[graph-name]
Expand Down
3 changes: 1 addition & 2 deletions src/main/frontend/worker/rtc/client.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
(swap! *sent assoc ws false))
(when (not (@*sent ws))
(m/? (c.m/backoff
(take 5 c.m/delays) ;retry 5 times (32s) if remote-graph is creating
(take 5 (drop 2 c.m/delays)) ;retry 5 times if remote-graph is creating (4000 8000 16000 32000 64000)
(register-graph-updates get-ws-create-task graph-uuid)))
(swap! *sent assoc ws true))
ws))))
Expand Down Expand Up @@ -293,7 +293,6 @@
(when-let [ops-for-remote (rtc-const/to-ws-ops-decoder
(sort-remote-ops
remote-ops))]
(assert (= (count remote-ops) (count ops-for-remote)))
(op-mem-layer/new-branch! repo)
(let [local-tx (op-mem-layer/get-local-tx repo)
r (m/? (send&recv get-ws-create-task {:action "apply-ops" :graph-uuid graph-uuid
Expand Down
106 changes: 27 additions & 79 deletions src/main/frontend/worker/rtc/const.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -36,60 +36,6 @@
[:target-uuid [:maybe :uuid]]
[:pos [:enum :sibling :child :no-order :no-parent-sibling]]])

(comment
(def to-ws-op-schema-deprecated
"TODO: remove this schema"
[:multi {:dispatch first :decode/string #(update % 0 keyword)}
[:move
[:cat :keyword
[:map
[:block-uuid :uuid]
[:target-uuid :uuid]
[:pos block-pos-type-schema]]]]
[:remove
[:cat :keyword
[:map
[:block-uuids [:sequential :uuid]]]]]

[:update
[:cat :keyword
[:map
[:block-uuid :uuid]
[:target-uuid {:optional true} :uuid]
[:pos {:optional true} block-pos-type-schema]
[:content {:optional true} :string]
[:updated-at {:optional true} :int]
[:created-at {:optional true} :int]
[:created-by {:optional true} :string]
[:tags {:optional true} [:map
[:add {:optional true} [:maybe [:set :uuid]]]
[:retract {:optional true} [:maybe [:set :uuid]]]]]
[:alias {:optional true} [:map
[:add {:optional true} [:maybe [:set :uuid]]]
[:retract {:optional true} [:maybe [:set :uuid]]]]]
[:type {:optional true} [:map
[:add {:optional true} [:maybe [:set block-type-schema]]]
[:retract {:optional true} [:maybe [:set block-type-schema]]]]]
[:schema {:optional true} :string ;transit-string
]
[:properties {:optional true} [:map
[:add {:optional true} [:sequential [:cat :uuid :string ;; transit-string
]]]
[:retract {:optional true} [:set :uuid]]]]
[:link {:optional true} :uuid]
[:journal-day {:optional true} :int]
[:ident {:optional true} :string]]]]
[:update-page
[:cat :keyword
[:map
[:block-uuid :uuid]
[:page-name :string]
[:original-name :string]]]]
[:remove-page
[:cat :keyword
[:map
[:block-uuid :uuid]]]]]))

(def av-schema
[:cat
:keyword
Expand Down Expand Up @@ -132,6 +78,13 @@
(def to-ws-ops-validator (m/validator [:sequential to-ws-op-schema]))
(def to-ws-ops-decoder (m/decoder [:sequential to-ws-op-schema] mt/string-transformer))

(def ^:private extra-attr-map-schema
[:map-of
:keyword
[:or
[:or :uuid :string]
[:sequential [:or :uuid :string]]]])

(def data-from-ws-schema
[:map
[:req-id :string]
Expand All @@ -143,37 +96,32 @@
[:map-of :uuid
[:multi {:dispatch :op :decode/string #(update % :op keyword)}
[:move
(apply conj
[:map
[:op :keyword]
[:self :uuid]
[:parents [:sequential :uuid]]
[:left [:maybe :uuid]] ;nil when it's :no-order block
[:content {:optional true} :string]
[:hash {:optional true} :int]]
general-attrs-schema-coll)]
[:map
[:op :keyword]
[:self :uuid]
[:parents [:sequential :uuid]]
[:left [:maybe :uuid]] ;nil when it's :no-order block
[:hash {:optional true} :int]
[::m/default extra-attr-map-schema]]]
[:remove
[:map
[:op :keyword]
[:block-uuid :uuid]]]
[:update-attrs
(apply conj
[:map
[:op :keyword]
[:self :uuid]
[:parents {:optional true} [:sequential :uuid]]
[:left {:optional true} [:maybe :uuid]] ;nil when it's :no-order block
[:content {:optional true} :string]
[:hash {:optional true} :int]]
general-attrs-schema-coll)]
[:map
[:op :keyword]
[:self :uuid]
[:parents {:optional true} [:sequential :uuid]]
[:left {:optional true} [:maybe :uuid]] ;nil when it's :no-order block
[:hash {:optional true} :int]
[::m/default extra-attr-map-schema]]]
[:update-page
(apply conj
[:map
[:op :keyword]
[:self :uuid]
[:page-name :string]
[:original-name :string]]
general-attrs-schema-coll)]
[:map
[:op :keyword]
[:self :uuid]
[:page-name :string]
[:block/original-name :string]
[::m/default extra-attr-map-schema]]]
[:remove-page
[:map
[:op :keyword]
Expand Down

0 comments on commit a12a0d3

Please sign in to comment.