Skip to content

Commit

Permalink
Only sort columns when the necessary props change
Browse files Browse the repository at this point in the history
Before, rows got sorted on every render. This hurt performance,
since v-table renders every time you scroll. Now, we check if the
props necessary for sorting have actually changed, and only then do we
call the sort function.
  • Loading branch information
kimo-k committed Feb 17, 2024
1 parent 96c9b65 commit a1eb65e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

> Committed but unreleased changes are put here, at the top. Older releases are detailed chronologically below.
## 2.18.1 (2024-02-16)

#### Added

- `v-table`, `simple-v-table` fixed performance drop when scrolling through sorted rows.


## 2.18.0 (2024-02-02)

#### Added
Expand Down
15 changes: 11 additions & 4 deletions src/re_com/v_table.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,17 @@
(reset! row-viewport-element (.getElementById js/document row-viewport-id)) ;; TODO: [MT] Use refs?
(.addResizeListener js/window @row-viewport-element on-viewport-resize))

:component-did-update
(fn v-table-component-did-update
[this [_ & {old-sort-keyfn :sort-keyfn old-sort-comp :sort-comp :as old-args}]]
(println "Component!")
(let [[_ & {new-sort-keyfn :sort-keyfn new-sort-comp :sort-comp :as new-args}] (reagent/argv this)]
(when (or (not= old-sort-keyfn new-sort-keyfn) (not= new-sort-comp old-sort-comp))
(cond
(and new-sort-keyfn new-sort-comp) (reset! row-sort-fn (partial new-sort-keyfn new-sort-comp))
new-sort-keyfn (reset! row-sort-fn (partial sort-by new-sort-keyfn))
new-sort-comp (reset! row-sort-fn (partial sort new-sort-comp))))))

:component-will-unmount
(fn v-table-component-will-unmount
[]
Expand Down Expand Up @@ -1074,10 +1085,6 @@
(do
(reset! content-rows-width row-content-width)
(reset! content-rows-height (* @m-size row-height))
(cond
(and sort-keyfn sort-comp) (reset! row-sort-fn (partial sort-by sort-keyfn sort-comp))
sort-keyfn (reset! row-sort-fn (partial sort-by sort-keyfn))
sort-comp (reset! row-sort-fn (partial sort sort-comp)))
;; Scroll rows into view handling
(when (not= (deref-or-value scroll-rows-into-view) @internal-scroll-rows-into-view)
;; TODO: Ideally allow non-atom nil but exception if it's not an atom when there's a value
Expand Down

0 comments on commit a1eb65e

Please sign in to comment.