🎉 Refactor caret blinking to reduce CPU usage

This commit is contained in:
Alejandro Alonso 2026-02-24 13:38:07 +01:00
parent 12a1cb1d32
commit f5109c7df2

View File

@ -19,6 +19,8 @@
[rumext.v2 :as mf]) [rumext.v2 :as mf])
(:import goog.events.EventType)) (:import goog.events.EventType))
(def caret-blink-interval-ms 250)
(defn- sync-wasm-text-editor-content! (defn- sync-wasm-text-editor-content!
"Sync WASM text editor content back to the shape via the standard "Sync WASM text editor content back to the shape via the standard
commit pipeline. Called after every text-modifying input." commit pipeline. Called after every text-modifying input."
@ -54,18 +56,17 @@
(.focus node)) (.focus node))
js/undefined)) js/undefined))
;; Animation loop for cursor blink
(mf/use-effect (mf/use-effect
(fn [] (fn []
(let [raf-id (atom nil) (let [timeout-id (atom nil)
animate (fn animate [] schedule-blink (fn schedule-blink []
(when (text-editor/text-editor-is-active?) (when (text-editor/text-editor-is-active?)
(wasm.api/request-render "cursor-blink") (wasm.api/request-render "cursor-blink"))
(reset! raf-id (js/requestAnimationFrame animate))))] (reset! timeout-id (js/setTimeout schedule-blink caret-blink-interval-ms)))]
(animate) (schedule-blink)
(fn [] (fn []
(when @raf-id (when @timeout-id
(js/cancelAnimationFrame @raf-id)))))) (js/clearTimeout @timeout-id))))))
;; Document-level keydown handler for control keys ;; Document-level keydown handler for control keys
(mf/use-effect (mf/use-effect