Repository: penpot/penpot
File: frontend/src/app/main/data/event.cljs (ClojureScript)

Code before:
                          (update-vals (partial count-library-data (:files state))))

                      lib-sizes
                      (->> libraries
                           (vals)
                           (reduce (fn [acc {:keys [components colors typographies]}]
                                     (-> acc
                                         (update :components + components)
                                         (update :colors + colors)
                                         (update :typographies + typographies)))))]
                  (-> state
                      (assoc-in [:performance-info :file-size] file-size)
                      (assoc-in [:performance-info :library-sizes] lib-sizes)
                      (assoc-in [:performance-info :file-start-time] (js/performance.now)))))))]

    (ptk/reify ::store-performace-info
      ptk/UpdateEvent
      (update [_ state]
        (-> state
            micro-benchmark
            (assoc-in [:performance-info :app-start-time] (js/performance.now))))

      ptk/WatchEvent
      (watch [_ _ stream]
        (->> stream
             (rx/filter #(= (ptk/type %) :app.main.data.workspace/all-libraries-resolved))
             (rx/take 1)
             (rx/map save-file-data))))))

(defn initialize
  []
  (when (contains? cf/flags :audit-log)
    (ptk/reify ::initialize
      ptk/WatchEvent
      (watch [_ _ _]
        (rx/of (store-performace-info)))

      ptk/EffectEvent
      (effect [_ _ stream]
        (let [session (atom nil)
              stopper (rx/filter (ptk/type? ::initialize) stream)
              buffer  (atom #queue [])
              profile (->> (rx/from-atom storage/user {:emit-current-value? true})
                           (rx/map :profile)
                           (rx/map :id)
                           (rx/pipe (rxo/distinct-contiguous)))]

          (l/debug :hint "event instrumentation initialized")

          (->> (rx/merge
                (->> (rx/from-atom buffer)

Review comment (suggestion):
`(ptk/type? :app.main.data.workspace/all-libraries-resolved)` returns a predicate with simplier syntax

Code after:
              lib-sizes
              (->> libraries
                   (reduce-kv
                    (fn [acc _ {:keys [components colors typographies]}]
                      (-> acc
                          (update :components + components)
                          (update :colors + colors)
                          (update :typographies + typographies)))
                    {}))]
          (update state :performance-info
                  (fn [info]
                    (-> info
                        (assoc :file-size file-size)
                        (assoc :library-sizes lib-sizes)
                        (assoc :file-start-time (perf/now))))))))))

(defn store-performace-info
  []
  (letfn [(micro-benchmark [state]
            (let [start (perf/now)]
              (loop [i micro-benchmark-iterations]
                (when-not (zero? i)
                  (* (math/sin i) (math/sqrt i))
                  (recur (dec i))))
              (let [end (perf/now)]
                (update state :performance-info assoc :bench-result (- end start)))))]

    (ptk/reify ::store-performace-info
      ptk/UpdateEvent
      (update [_ state]
        (-> state
            micro-benchmark
            (assoc-in [:performance-info :app-start-time] (perf/now))))

      ptk/WatchEvent
      (watch [_ _ stream]
        (->> stream
             (rx/filter (ptk/type? :app.main.data.workspace/all-libraries-resolved))
             (rx/take 1)
             (rx/map save-performance-info))))))

(defn initialize
  []
  (when (contains? cf/flags :audit-log)
    (ptk/reify ::initialize
      ptk/WatchEvent
      (watch [_ _ _]
        (rx/of (store-performace-info)))

      ptk/EffectEvent
      (effect [_ _ stream]