Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: vim switching perf (#3906)

Myles Scolnickf14a9d522cda
diff --git a/frontend/src/core/codemirror/keymaps/vim.ts b/frontend/src/core/codemirror/keymaps/vim.ts
index 2082466fc8..6411f67dd6 100644
--- a/frontend/src/core/codemirror/keymaps/vim.ts
+++ b/frontend/src/core/codemirror/keymaps/vim.ts
@@ -12,6 +12,7 @@ import type { Extension } from "@codemirror/state";
import { Logger } from "@/utils/Logger";
import { goToDefinitionAtCursorPosition } from "../go-to-definition/utils";
import { once } from "@/utils/once";
+import { onIdle } from "@/utils/idle";
export function vimKeymapExtension(callbacks: {
focusUp: () => void;
@@ -45,7 +46,10 @@ export function vimKeymapExtension(callbacks: {
},
]),
ViewPlugin.define((view) => {
- CodeMirrorVimSync.INSTANCES.addInstance(view);
+ // Wait for the next animation frame so the CodeMirror instance is ready
+ requestAnimationFrame(() => {
+ CodeMirrorVimSync.INSTANCES.addInstance(view);
+ });
return {
destroy() {
CodeMirrorVimSync.INSTANCES.removeInstance(view);
@@ -93,8 +97,11 @@ class CodeMirrorVimSync {
}
invariant("mode" in e, 'Expected event to have a "mode" property');
this.isBroadcasting = true;
- this.broadcastModeChange(instance, e.mode, e.subMode);
- this.isBroadcasting = false;
+ // We use onIdle to keep the focused editor snappy
+ onIdle(() => {
+ this.broadcastModeChange(instance, e.mode, e.subMode);
+ this.isBroadcasting = false;
+ });
});
}
diff --git a/frontend/src/utils/idle.ts b/frontend/src/utils/idle.ts
new file mode 100644
index 0000000000..c1b1371ed8
--- /dev/null
+++ b/frontend/src/utils/idle.ts
@@ -0,0 +1,19 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+/**
+ * Schedule a callback to run when the browser is idle.
+ *
+ * @param callback - The callback to run when the browser is idle.
+ */
+export function onIdle(callback: () => void) {
+ if ("scheduler" in window) {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ (window as any).scheduler.postTask(callback, {
+ priority: "background",
+ });
+ } else if ("requestIdleCallback" in window) {
+ requestIdleCallback(callback);
+ } else {
+ setTimeout(callback, 0);
+ }
+}