AI-native design editor. Open-source Figma alternative.
Fix scrub lag: simplify useSceneComputed to synchronous sceneVersion
Danila Poyarkov920af0a14e85
diff --git a/packages/vue/src/composables/use-scene-reactive.ts b/packages/vue/src/composables/use-scene-reactive.tsindex 7ac11cfaa..fbfe462b9 100644--- a/packages/vue/src/composables/use-scene-reactive.ts+++ b/packages/vue/src/composables/use-scene-reactive.ts@@ -1,76 +1,19 @@-import { computed, onScopeDispose, shallowRef, triggerRef, watchEffect, type ComputedRef } from 'vue'--import type { Editor } from '@open-pencil/core/editor'+import { computed, type ComputedRef } from 'vue'import { useEditor } from '../context'-interface SceneBridge {- ref: ReturnType<typeof shallowRef<number>>- count: number- subscribe: () => void- unsubscribe: () => void-}--const bridges = new WeakMap<Editor, SceneBridge>()--function getBridge(editor: Editor): SceneBridge {- let bridge = bridges.get(editor)- if (bridge) return bridge-- const ref = shallowRef(0)- const unbinds: Array<() => void> = []-- const trigger = () => {- ref.value++- triggerRef(ref)- }-- bridge = {- ref,- count: 0,- subscribe() {- if (this.count++ > 0) return- unbinds.push(- editor.graph.emitter.on('node:updated', trigger),- editor.graph.emitter.on('node:created', trigger),- editor.graph.emitter.on('node:deleted', trigger),- editor.graph.emitter.on('node:reparented', trigger),- editor.graph.emitter.on('node:reordered', trigger)- )- const stop = watchEffect(() => {- void editor.state.sceneVersion- trigger()- })- unbinds.push(stop)- },- unsubscribe() {- if (--this.count > 0) return- for (const unbind of unbinds) unbind()- unbinds.length = 0- bridges.delete(editor)- }- }-- bridges.set(editor, bridge)- return bridge-}-/*** Creates a computed ref that re-evaluates when the scene graph changes.*- * Bridges the core editor's nanoevents emitter into Vue's reactivity- * system via shallowRef + triggerRef. Listeners are ref-counted and- * cleaned up when the last subscriber's scope is disposed.+ * Tracks `state.sceneVersion` — a counter on a shallowReactive object+ * that the core editor increments synchronously on every mutation via+ * requestRender(). Vue tracks the read automatically, so the computed+ * re-evaluates in the same tick as the change. Zero latency.*/export function useSceneComputed<T>(fn: () => T): ComputedRef<T> {const editor = useEditor()- const bridge = getBridge(editor)-- bridge.subscribe()- onScopeDispose(() => bridge.unsubscribe())-return computed(() => {- void bridge.ref.value+ void editor.state.sceneVersionreturn fn()})}