Browse code

Merge pull request #39 from jbweston/fix/slow-render

Fix slow rendering on large outputs.
This was because we were explicitly calling sub-views, e.g.
'(item-panel item)'. By using a vector instead,
e.g. '[item-panel item]' we delegate to re-agent for calling the view
function whenever the subscription requires it.

Closes #31.

Joseph Weston authored on 06/12/2021 01:05:49 • GitHub committed on 06/12/2021 01:05:49
Showing 2 changed files
... ...
@@ -16,8 +16,3 @@
16 16
  ::cmd-history
17 17
  (fn [db]
18 18
    (get-in db [:cmdline :history])))
19
-
20
-(re-frame/reg-sub
21
- ::spell
22
- (fn [db [_ spell-id]]
23
-   (get-in db [:spells spell-id])))
... ...
@@ -139,9 +139,9 @@
139 139
 
140 140
 (defn spell-panel [spell]
141 141
       [:article {:class (styles/spell-panel)}
142
-       (heading spell)
142
+       [heading spell]
143 143
        [:div [:span.kind (short-spell-description spell)]]
144
-       (property-list
144
+       [property-list
145 145
         ; Refactor these so that we get the "name" from the keyword by replacing
146 146
         ; '-' and '_' and capitalizing
147 147
          :Damage
... ...
@@ -160,25 +160,25 @@
160 160
          :Duration
161 161
            (spaced
162 162
             (-> spell :duration string/lower-case)
163
-            (when (spell :concentration) "(requires concentration)")))
164
-       (description spell)
163
+            (when (spell :concentration) "(requires concentration)"))]
164
+       [description spell]
165 165
        (let [at-higher-levels (some-> spell :higher_level join-lines)]
166 166
         (when at-higher-levels
167 167
            [:section.higher-level
168 168
              [:span "At higher levels"]
169 169
              [:span at-higher-levels]]))
170
-       (license-notice spell)])
170
+       [license-notice spell]])
171 171
 
172 172
 (defn equipment-panel [item]
173 173
     [:article {:class (styles/equipment-panel)}
174
-     (heading item)
174
+     [heading item]
175 175
      [:span.kind
176 176
       (spaced
177 177
        (-> item :equipment_category :name)
178 178
        (cond
179 179
          (item :gear_category) (-> item :gear_category :name string/lower-case parenthesize)
180 180
          (item :vehicle_category) (-> item :vehicle_category string/lower-case parenthesize)))]
181
-     (property-list
181
+     [property-list
182 182
       :Speed
183 183
         (speed item)
184 184
       "Carrying Capacity"
... ...
@@ -188,17 +188,17 @@
188 188
       :Cost
189 189
         (spaced
190 190
          (cost item)
191
-         (some->> item :quantity (str "for ") parenthesize)))
192
-     (description item)
193
-     (license-notice item)])
191
+         (some->> item :quantity (str "for ") parenthesize))]
192
+     [description item]
193
+     [license-notice item]])
194 194
 
195 195
 (defn weapon-panel [weapon]
196 196
  (let [weapon-kind (-> weapon :weapon_range string/lower-case)
197 197
        weapon-category (-> weapon :weapon_category string/lower-case)]
198 198
   [:article {:class (styles/weapon-panel)}
199
-    (heading weapon)
199
+    [heading weapon]
200 200
     [:span.kind (spaced weapon-category weapon-kind "weapon")]
201
-    (property-list
201
+    [property-list
202 202
      ; 'two-handed-damage' is defined for *single-handed* weapons with the "versatile"
203 203
      ; property. Actual two-handed weapons have 'damage' only.
204 204
      :Damage
... ...
@@ -216,12 +216,12 @@
216 216
      :Weight
217 217
        (weight weapon)
218 218
      :Cost
219
-       (cost weapon))
220
-    (license-notice weapon)]))
219
+       (cost weapon)]
220
+    [license-notice weapon]]))
221 221
 
222 222
 (defn spell-list-panel [spells]
223 223
   [:article {:class (styles/spell-list-panel)}
224
-    (heading "Spells")
224
+    [heading "Spells"]
225 225
     [:ul
226 226
      (for [[index spell] (enumerated (sort-by :name spells))]
227 227
         ^{:key index}
... ...
@@ -231,8 +231,8 @@
231 231
 
232 232
 (defn equipment-list-panel [items]
233 233
   [:article {:class (styles/item-list-panel)}
234
-   (heading "Items")
235
-   (property-table items
234
+   [heading "Items"]
235
+   [property-table items
236 236
      :Name
237 237
        {}
238 238
        :name
... ...
@@ -241,7 +241,7 @@
241 241
        cost
242 242
      :Weight
243 243
        {:align :right}
244
-       #(weight % :unit "lb" :no-quantity true))])
244
+       #(weight % :unit "lb" :no-quantity true)]])
245 245
 
246 246
 (defn error-message
247 247
     [{e :err}]
... ...
@@ -262,22 +262,22 @@
262 262
 (defn output [x]
263 263
     [:div.output
264 264
      (cond
265
-      (is-license? x) (license-panel x)
266
-      (is-equipment-list? x) (equipment-list-panel x)
267
-      (is-spell-list? x) (spell-list-panel x)
265
+      (is-license? x) [license-panel x]
266
+      (is-equipment-list? x) [equipment-list-panel x]
267
+      (is-spell-list? x) [spell-list-panel x]
268 268
       (is-spell? x) (spell-panel x)
269 269
       (is-equipment? x) (cond
270
-                         (is-weapon? x) (weapon-panel x)
271
-                         :else (equipment-panel x))
272
-      (is-error? x) (error-message x))])
270
+                         (is-weapon? x) [weapon-panel x]
271
+                         :else [equipment-panel x])
272
+      (is-error? x) [error-message x])])
273 273
 
274 274
 (defn history []
275 275
   (let [history (re-frame/subscribe [::subs/cmd-history])]
276 276
     [:ul {:class (styles/history-style)}
277 277
       (for [[index item] (enumerated @history)]
278
-        ^{:key index} [:li (-> item :input input) (-> item :output output)])]))
278
+        ^{:key index} [:li [input (item :input)] [output (item :output)]])]))
279 279
 
280 280
 (defn main-panel []
281 281
   [:div {:class (styles/screen)}
282 282
    [:div {:class (styles/main-panel)}
283
-    (title) (history) (prompt)]])
283
+    [title] [history] [prompt]]])