Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chunk seqs into groups of 100 for serialization #18

Merged
merged 1 commit into from
Nov 10, 2020
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
1 change: 1 addition & 0 deletions dev/cljs/user.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@
(tap> #js [1 2 3 4 5])
(tap> (js/Promise.resolve 123))

(tap> (with-meta (range) {:hello :world}))
(tap> data))
1 change: 1 addition & 0 deletions dev/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
;; should cause an error in portal because of transit
(tap> {(with-meta 'k {:a :b}) 'v})

(tap> (with-meta (range) {:hello :world}))
(tap> (json/parse-stream (io/reader "package-lock.json")))
(tap> (io/file "deps.edn"))
(tap> data))
3 changes: 2 additions & 1 deletion src/examples/data.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
{::regex #"hello-world"
::var #'portal.colors/themes
::with-meta (with-meta 'with-meta {:hello :world})
{:example/settings 'complex-key} :hello-world})
{:example/settings 'complex-key} :hello-world
::range (range)})

(def diff-data
[{::removed "value"
Expand Down
32 changes: 27 additions & 5 deletions src/portal/runtime.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,37 @@
:portal/value (list))
(done nil)))

(defn limit-values [state]
(update state :portal/value #(take 25 %)))
(defn limit-seq [value]
(if-not (seq? value)
value
(let [m (meta value)
limit (get m ::more-limit 100)
[realized remaining] (split-at limit value)]
(with-meta
realized
(merge
m
(when (seq remaining)
{::more #(limit-seq (with-meta remaining m))}))))))

(defn- set-limit [state]
(update state
:portal/value
#(with-meta % {::more-limit 25})))

(defn load-state [request done]
(let [state-value @state
id (:portal/state-id request)
in-sync? (= id (:portal/state-id state-value))]
(if-not in-sync?
(done (limit-values state-value))
(done (set-limit state-value))
(let [watch-key (keyword (gensym))]
(add-watch
state
watch-key
(fn [_ _ _old _new]
(remove-watch state watch-key)
(done (limit-values @state))))
(done (set-limit @state))))
(fn [_status]
(remove-watch state watch-key))))))

Expand All @@ -85,7 +100,14 @@
; wait for any newly returned promise to resolve
(a/let [naved naved] (on-datafy naved done)))))

(defn invoke [{:keys [f args]} done]
(try
(done {:return (apply f args)})
(catch #?(:clj Exception :cljs js/Error) e
(done {:return e}))))

(def ops
{:portal.rpc/clear-values clear-values
:portal.rpc/load-state load-state
:portal.rpc/on-nav on-nav})
:portal.rpc/on-nav on-nav
:portal.rpc/invoke invoke})
2 changes: 1 addition & 1 deletion src/portal/runtime/transit.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
(transit/write-handler "portal.transit/var" var->symbol)
java.net.URL
(transit/write-handler "r" str)}
:transform transit/write-meta
:transform (comp transit/write-meta rt/limit-seq)
:default-handler
(transit/write-handler
"portal.transit/object"
Expand Down
2 changes: 1 addition & 1 deletion src/portal/runtime/transit.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
(t/write
(t/writer
:json
{:transform write-meta
{:transform (comp write-meta rt/limit-seq)
:handlers
{js/URL
(t/write-handler (constantly "r") str)
Expand Down
13 changes: 11 additions & 2 deletions src/portal/ui/inspector.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@

(defn preview-coll [open close]
(fn [_settings value]
[s/div {:style {:color "#bf616a"}} open (count value) close]))
[s/div {:style {:color "#bf616a"}}
open
(count value)
(when (-> value meta :portal.runtime/more) "+")
close]))

(def preview-map (preview-coll "{" "}"))
(def preview-vector (preview-coll "[" "]"))
Expand Down Expand Up @@ -147,6 +151,7 @@
[container-map
settings
[l/lazy-seq
settings
(for [[k v] (try-sort-map values)]
[:<>
{:key (hash k)}
Expand Down Expand Up @@ -176,6 +181,7 @@
[container-coll
settings
[l/lazy-seq
settings
(map-indexed
(fn [idx itm]
^{:key idx}
Expand Down Expand Up @@ -346,7 +352,10 @@
(if preview?
(get-preview-component type)
(get-inspect-component type))))
settings (-> settings (update :depth inc) (dissoc :component))
settings (-> settings
(update :depth inc)
(dissoc :component)
(assoc :value value))
nav-target? (= 2 (:depth settings))
on-nav #((:portal/on-nav settings) (assoc settings :value value))]
[s/div
Expand Down
9 changes: 6 additions & 3 deletions src/portal/ui/lazy.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
[react-visibility-sensor :default VisibilitySensor]
[reagent.core :as r]))

(defn lazy-seq [_ opts]
(defn lazy-seq [_settings _ opts]
(let [{:keys [default-take step]
:or {default-take 0 step 10}} opts
n (r/atom default-take)]
(fn [seqable]
(fn [settings seqable]
[:<>
(take @n seqable)
(when (seq (drop @n seqable))
(if-not (seq (drop @n seqable))
(when (= (:depth settings) 1)
((:portal/on-more settings) (:value settings))
nil)
[:> VisibilitySensor
{:key @n
:on-change
Expand Down
22 changes: 21 additions & 1 deletion src/portal/ui/state.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,31 @@
(.then merge-state)
(.then #(:portal/complete? %))))

(defn invoke [send! f & args]
(-> (send!
{:op :portal.rpc/invoke :f f :args args})
(.then #(:return %))))

(defn more [send! value]
(when-let [f (-> value meta :portal.runtime/more)]
(-> (invoke send! f)
(.then
(fn [more]
(swap! (if (contains? @state :portal/value)
state
tap-state)
update :portal/value
(fn [current]
(with-meta
(concat current more)
(meta more)))))))))

(defn get-actions [send!]
{:portal/on-clear (partial on-clear send!)
:portal/on-first on-first
:portal/on-last on-last
:portal/on-nav (partial on-nav send!)
:portal/on-back on-back
:portal/on-forward on-forward
:portal/on-load (partial load-state send!)})
:portal/on-load (partial load-state send!)
:portal/on-more (partial more send!)})
1 change: 1 addition & 0 deletions src/portal/ui/viewer/table.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
:border-radius (:border-radius settings)}}
[s/tbody
[l/lazy-seq
settings
(map-indexed
(fn [row-index row]
[s/tr
Expand Down
1 change: 1 addition & 0 deletions src/portal/ui/viewer/text.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
[s/table
[s/tbody
[l/lazy-seq
settings
(->>
(str/split value #"\n")
(map-indexed
Expand Down
3 changes: 2 additions & 1 deletion src/portal/ui/viewer/tree.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
:flex-direction :column
:flex-wrap :wrap}}
[l/lazy-seq
settings
(for [[k v] (ins/try-sort-map value)]
^{:key (hash k)}
[inspect-tree-item settings
Expand All @@ -112,14 +113,14 @@
:flex-direction :column
:flex-wrap :wrap}}
[l/lazy-seq
settings
(map-indexed
(fn [idx item]
^{:key idx}
[s/div
[inspect-tree-item settings
{:value item
:value-child [inspect-tree settings item]}]])

value)]])

(defn inspect-tree [settings value]
Expand Down