From a96a86c0b57e850ad876fc5cada955eafa094e08 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Tue, 12 May 2026 09:38:17 +0200 Subject: [PATCH] Slightly optimize vertical jiggering by pre-computing all widgets on page load --- client/client_system.ts | 2 +- client/codemirror/iframe_widget.ts | 13 +++++++++- client/codemirror/lua_widget.ts | 20 ++++++++++++--- client/editor_commands.ts | 1 + client/plugos/syscalls/client_code_widget.ts | 4 ++- client/widget_cache.ts | 27 ++++++++++++++++++++ 6 files changed, 61 insertions(+), 6 deletions(-) diff --git a/client/client_system.ts b/client/client_system.ts index c1fea0f4..10efb7c3 100644 --- a/client/client_system.ts +++ b/client/client_system.ts @@ -160,7 +160,7 @@ export class ClientSystem { markdownSyscalls(client), assetSyscalls(this.system), codeWidgetSyscalls(this.codeWidgetHook), - clientCodeWidgetSyscalls(), + clientCodeWidgetSyscalls(this.client), languageSyscalls(), jsonschemaSyscalls(), indexSyscalls(this.objectIndex, this.client), diff --git a/client/codemirror/iframe_widget.ts b/client/codemirror/iframe_widget.ts index 904bd70c..361a0ecf 100644 --- a/client/codemirror/iframe_widget.ts +++ b/client/codemirror/iframe_widget.ts @@ -15,6 +15,13 @@ export class IFrameWidget extends WidgetType { readonly codeWidgetCallback: CodeWidgetCallback, ) { super(); + // Eagerly kick off the callback so the result is in flight before + // CodeMirror mounts the widget. Idempotent on bodyText. + this.client.widgetCache.prewarmResult(this.bodyText, () => + this.codeWidgetCallback(this.bodyText, this.client.currentName()), + ).catch(() => { + // renderContent / iframe message handler will surface errors. + }); } override get estimatedHeight(): number { @@ -29,7 +36,9 @@ export class IFrameWidget extends WidgetType { const iframe = createWidgetSandboxIFrame( this.client, this.bodyText, - this.codeWidgetCallback(this.bodyText, this.client.currentName()), + this.client.widgetCache.prewarmResult(this.bodyText, () => + this.codeWidgetCallback(this.bodyText, this.client.currentName()), + ), (message) => { switch (message.type) { case "blur": { @@ -42,6 +51,8 @@ export class IFrameWidget extends WidgetType { break; } case "reload": + // Force-refresh: drop any prewarmed result and re-run. + this.client.widgetCache.invalidatePrewarm(this.bodyText); void this.codeWidgetCallback( this.bodyText, this.client.currentName(), diff --git a/client/codemirror/lua_widget.ts b/client/codemirror/lua_widget.ts index f5817970..c8f59d3c 100644 --- a/client/codemirror/lua_widget.ts +++ b/client/codemirror/lua_widget.ts @@ -73,6 +73,20 @@ export class LuaWidget extends WidgetType { openRef: null, ...opts, }; + // Eagerly kick off the widget callback so the query result is ready + // (or close to it) by the time CodeMirror mounts the widget. Idempotent + // on cacheKey — the decorator state field rebuilds widgets on every + // editor update, but only the first construction per cacheKey starts + // the actual fetch. + this.opts.client.widgetCache.prewarmResult(this.opts.cacheKey, () => + this.opts.callback( + this.opts.expressionText, + this.opts.client.currentName(), + ), + ).catch(() => { + // Ignore: renderContent re-awaits the same promise and handles + // errors via its own catch path. + }); } override get estimatedHeight(): number { @@ -136,9 +150,9 @@ export class LuaWidget extends WidgetType { async renderContent(div: HTMLElement) { const currentName = this.opts.client.currentName(); - let widgetContent = await this.opts.callback( - this.opts.expressionText, - currentName, + let widgetContent = await this.opts.client.widgetCache.prewarmResult( + this.opts.cacheKey, + () => this.opts.callback(this.opts.expressionText, currentName), ); activeWidgets.add(this); if (widgetContent === null || widgetContent === undefined) { diff --git a/client/editor_commands.ts b/client/editor_commands.ts index 4031ce6b..eb55b45d 100644 --- a/client/editor_commands.ts +++ b/client/editor_commands.ts @@ -532,6 +532,7 @@ export function registerEditorCommands( name: "Widgets: Refresh All", requireEditor: "page", run: async () => { + client.widgetCache.clearPrewarm(); broadcastReload(); return reloadAllWidgets(); }, diff --git a/client/plugos/syscalls/client_code_widget.ts b/client/plugos/syscalls/client_code_widget.ts index f03c3544..e5f0276c 100644 --- a/client/plugos/syscalls/client_code_widget.ts +++ b/client/plugos/syscalls/client_code_widget.ts @@ -1,10 +1,12 @@ import type { SysCallMapping } from "../../plugos/system.ts"; +import type { Client } from "../../client.ts"; import { reloadAllWidgets } from "../../codemirror/code_widget.ts"; import { broadcastReload } from "../../components/widget_sandbox_iframe.ts"; -export function clientCodeWidgetSyscalls(): SysCallMapping { +export function clientCodeWidgetSyscalls(client: Client): SysCallMapping { return { "codeWidget.refreshAll": () => { + client.widgetCache.clearPrewarm(); broadcastReload(); return reloadAllWidgets(); }, diff --git a/client/widget_cache.ts b/client/widget_cache.ts index b48c9872..15923238 100644 --- a/client/widget_cache.ts +++ b/client/widget_cache.ts @@ -9,6 +9,12 @@ export type WidgetMeta = { export class WidgetCache { private widgetMetaCache = new LimitedMap(1000); + // Session-only cache of in-flight or completed widget callback results. + // Used to kick off widget queries before CodeMirror actually mounts the + // widget (e.g. when scrolling fast past widgets that haven't entered the + // viewport yet), so renderContent can synchronously await a result that's + // already been computed. + private pendingResults = new LimitedMap>(1000); private debouncedWidgetMetaCacheFlush = throttle(() => { this.ds @@ -48,4 +54,25 @@ export class WidgetCache { this.widgetMetaCache.remove(key); this.debouncedWidgetMetaCacheFlush(); } + + // Pre-execute (or return the in-flight/completed result of) a widget + // callback. Subsequent calls with the same key return the same promise, + // so calling this from a widget's constructor is safe even when the + // CodeMirror decoration field re-builds widgets on every state update. + prewarmResult(key: string, fn: () => Promise): Promise { + let p = this.pendingResults.get(key) as Promise | undefined; + if (!p) { + p = fn(); + this.pendingResults.set(key, p); + } + return p; + } + + invalidatePrewarm(key: string) { + this.pendingResults.remove(key); + } + + clearPrewarm() { + this.pendingResults = new LimitedMap(1000); + } }