Browse Source

enhance(rtc): update frontend.db.rtc.op fns return value type

rcmerci 2 years ago
parent
commit
1ed2a9151a

+ 5 - 6
src/main/frontend/db/rtc/core.cljs

@@ -5,7 +5,6 @@
   (:require [cljs-time.coerce :as tc]
             [cljs-time.core :as t]
             [cljs.core.async :as async :refer [<! >! chan go go-loop]]
-            [cljs.core.async.interop :refer [p->c]]
             [clojure.set :as set]
             [cognitect.transit :as transit]
             [frontend.db :as db]
@@ -329,7 +328,7 @@
   (go
     (let [remote-t (:t data-from-ws)
           remote-t-before (:t-before data-from-ws)
-          {:keys [local-tx ops]} (<! (p->c (op/<get-ops&local-tx repo)))]
+          {:keys [local-tx ops]} (<! (op/<get-ops&local-tx repo))]
       (cond
         (not (and (pos? remote-t)
                   (pos? remote-t-before)))
@@ -365,7 +364,7 @@
           (util/profile :apply-remote-move-ops (apply-remote-move-ops repo sorted-move-ops))
           (util/profile :apply-remote-update-ops (apply-remote-update-ops repo update-ops))
           (util/profile :apply-remote-remove-page-ops (apply-remote-remove-page-ops repo remove-page-ops))
-          (<! (p->c (op/<update-local-tx! repo remote-t))))
+          (<! (op/<update-local-tx! repo remote-t)))
         :else (throw (ex-info "unreachable" {:remote-t remote-t
                                              :remote-t-before remote-t-before
                                              :local-t local-tx}))))))
@@ -551,7 +550,7 @@
 (defn- <get-N-ops
   [repo n]
   (go
-    (let [{:keys [ops local-tx]} (<! (p->c (op/<get-ops&local-tx repo)))
+    (let [{:keys [ops local-tx]} (<! (op/<get-ops&local-tx repo))
           ops (take n ops)
           op-keys (map first ops)
           ops (map second ops)
@@ -609,7 +608,7 @@
           ;; else
           (throw (ex-info "Unavailable" {:remote-ex remote-ex})))
         (do (assert (pos? (:t r)) r)
-            (<! (p->c (op/<clean-ops repo op-keys)))
+            (<! (op/<clean-ops repo op-keys))
             (<! (<apply-remote-data repo (rtc-const/data-from-ws-decoder r)
                                     :max-pushed-op-key max-op-key))
             (prn :<client-op-update-handler :t (:t r)))))))
@@ -620,7 +619,7 @@
     (chan)
     (go
       (<! (async/timeout 2000))
-      (when (seq (:ops (<! (p->c (op/<get-ops&local-tx repo)))))
+      (when (seq (:ops (<! (op/<get-ops&local-tx repo))))
         true))))
 
 (defn <loop-for-rtc

+ 3 - 4
src/main/frontend/db/rtc/debug_ui.cljs

@@ -3,7 +3,6 @@
   (:require-macros
    [frontend.db.rtc.macro :refer [with-sub-data-from-ws get-req-id get-result-ch]])
   (:require [cljs.core.async :as async :refer [<! go]]
-            [cljs.core.async.interop :refer [p->c]]
             [fipp.edn :as fipp]
             [frontend.db.rtc.core :as rtc-core]
             [frontend.db.rtc.full-upload-download-graph :as full-upload-download-graph]
@@ -31,7 +30,7 @@
          (<! (<start-rtc state repo))))))
   ([state repo]
    (go
-     (if-let [graph-uuid (<! (p->c (op/<get-graph-uuid repo)))]
+     (if-let [graph-uuid (<! (op/<get-graph-uuid repo))]
        (do (reset! debug-state state)
            (<! (rtc-core/<loop-for-rtc state graph-uuid repo))
            state)
@@ -82,8 +81,8 @@
                  :on-click (fn [_]
                              (go
                                (let [repo (state/get-current-repo)
-                                     {:keys [local-tx ops]} (<! (p->c (op/<get-ops&local-tx repo)))
-                                     graph-uuid (<! (p->c (op/<get-graph-uuid repo)))]
+                                     {:keys [local-tx ops]} (<! (op/<get-ops&local-tx repo))
+                                     graph-uuid (<! (op/<get-graph-uuid repo))]
                                  (reset! (::local-tx state) local-tx)
                                  (reset! (::ops state) (count ops))
                                  (reset! (::graph-uuid state) graph-uuid)

+ 4 - 4
src/main/frontend/db/rtc/full_upload_download_graph.cljs

@@ -51,8 +51,8 @@
         (let [r (<! (get-result-ch))]
           (if-not (:graph-uuid r)
             (ex-info "upload graph failed" r)
-            (do (<! (p->c (op/<update-graph-uuid! repo (:graph-uuid r))))
-                (<! (p->c (op/<update-local-tx! repo (:t r))))
+            (do (<! (op/<update-graph-uuid! repo (:graph-uuid r)))
+                (<! (op/<update-local-tx! repo (:t r)))
                 r)))))))
 
 (def block-type-ident->str
@@ -126,7 +126,7 @@
             blocks** (outliner-pipeline/build-upsert-blocks blocks* nil db)]
         (<! (p->c (persist-db/<new repo)))
         (<! (persist-db/<transact-data repo blocks** nil))
-        (<! (p->c (op/<update-local-tx! repo t)))))))
+        (<! (op/<update-local-tx! repo t))))))
 
 
 (defn <download-graph
@@ -142,7 +142,7 @@
         (ex-info "<download-graph failed" r)
         (let [all-blocks (transit/read transit-r body)]
           (<! (<transact-remote-all-blocks-to-sqlite all-blocks repo))
-          (<! (p->c (op/<update-graph-uuid! repo graph-uuid))))))))
+          (<! (op/<update-graph-uuid! repo graph-uuid)))))))
 
 (comment
   )

+ 19 - 17
src/main/frontend/db/rtc/op.cljs

@@ -1,9 +1,10 @@
 (ns frontend.db.rtc.op
-  "TODO"                                ; @zhiyuan
-  (:require [malli.core :as m]
+  "Wrappers for `frontend.db.rtc.ops-idb-store`"
+  (:require [cljs.core.async.interop :refer [p->c]]
             [frontend.db.rtc.ops-idb-store :as op-store]
-            [promesa.core :as p]
-            [malli.transform :as mt]))
+            [malli.core :as m]
+            [malli.transform :as mt]
+            [promesa.core :as p]))
 
 (def op-schema
   [:multi {:dispatch first}
@@ -62,34 +63,35 @@
 
 (defn <get-ops&local-tx
   [repo]
-  (p/let [all-data (op-store/<get-all-ops repo)]
-    (let [all-data-m (into {} all-data)
-          local-tx (get all-data-m "local-tx")
-          ops (->> all-data
-                   (filter (comp number? first))
-                   (sort-by first <)
-                   ops-from-store-decoder)]
-      (assert (ops-from-store-validator ops) ops)
-      {:ops ops :local-tx local-tx})))
+  (p->c
+   (p/let [all-data (op-store/<get-all-ops repo)]
+     (let [all-data-m (into {} all-data)
+           local-tx (get all-data-m "local-tx")
+           ops (->> all-data
+                    (filter (comp number? first))
+                    (sort-by first <)
+                    ops-from-store-decoder)]
+       (assert (ops-from-store-validator ops) ops)
+       {:ops ops :local-tx local-tx}))))
 
 (defn <clean-ops
   [repo keys]
-  (op-store/<clear-ops! repo keys))
+  (p->c (op-store/<clear-ops! repo keys)))
 
 
 (defn <update-local-tx!
   [repo t]
   {:pre [(pos-int? t)]}
-  (op-store/<update-local-tx! repo t))
+  (p->c (op-store/<update-local-tx! repo t)))
 
 (defn <update-graph-uuid!
   [repo graph-uuid]
   {:pre [(some? graph-uuid)]}
-  (op-store/<update-graph-uuid! repo graph-uuid))
+  (p->c (op-store/<update-graph-uuid! repo graph-uuid)))
 
 (defn <get-graph-uuid
   [repo]
-  (op-store/<get-graph-uuid repo))
+  (p->c (op-store/<get-graph-uuid repo)))
 
 (comment
   (defn <get-local-tx

+ 16 - 1
src/test/frontend/db/rtc/mock.cljs

@@ -2,7 +2,7 @@
   (:require [clojure.core.async :as async]
             [frontend.db.rtc.const :as rtc-const]))
 
-
+;;; websocket
 (defrecord Mock-WebSocket [onopen onmessage onclose onerror readyState push-data-chan ^:mutable push-data-fn]
   Object
   (close [_]
@@ -52,3 +52,18 @@
 ;; (defn mock-ws-push-data-fn
 ;;   [ws f]
 ;;   (.set-push-data-fn ws f))
+
+;;; websocket ends ;;;;
+
+
+;;; frontend.db.rtc.op mock
+;; (def *ops-store (atom {}))
+
+;; (defn <get-ops&local-tx
+;;   [repo]
+
+;;   )
+
+
+
+;;; frontend.db.rtc.op mock ends