rename from measure

This commit is contained in:
Benjamin Lu
2025-09-11 17:55:26 -07:00
parent 4de2c2fdb9
commit 57a1359201
2 changed files with 26 additions and 21 deletions

View File

@@ -35,17 +35,17 @@ const nodeRegistry = new Map<string, NodeEntry>()
const pendingNodes = new Set<string>()
let rafId: number | null = null
function scheduleNodeMeasure(nodeId: string) {
function scheduleSlotLayoutSync(nodeId: string) {
pendingNodes.add(nodeId)
if (rafId == null) {
rafId = requestAnimationFrame(() => {
rafId = null
runBatchedMeasure()
flushScheduledSlotLayoutSync()
})
}
}
function runBatchedMeasure() {
function flushScheduledSlotLayoutSync() {
if (pendingNodes.size === 0) return
// Read container origin once from cache
@@ -53,11 +53,11 @@ function runBatchedMeasure() {
for (const nodeId of Array.from(pendingNodes)) {
pendingNodes.delete(nodeId)
measureNodeSlotsNow(nodeId, originLeft, originTop)
syncNodeSlotLayoutsFromDOM(nodeId, originLeft, originTop)
}
}
function measureNodeSlotsNow(
function syncNodeSlotLayoutsFromDOM(
nodeId: string,
originLeft?: number,
originTop?: number
@@ -125,8 +125,8 @@ function updateNodeSlotsFromCache(nodeId: string) {
for (const [slotKey, entry] of node.slots) {
if (!entry.cachedOffset) {
// schedule a remeasure to seed offset
scheduleNodeMeasure(nodeId)
// schedule a sync to seed offset
scheduleSlotLayoutSync(nodeId)
continue
}
@@ -196,7 +196,7 @@ export function useSlotElementTracking(options: {
newLayout.size.height !== oldLayout.size.height
// Only update from cache on move-only changes.
// On resizes (or move+resize), let ResizeObserver remeasure slots accurately.
// On resizes (or move+resize), let ResizeObserver resync slots from DOM accurately.
if (moved && !resized) {
updateNodeSlotsFromCache(nodeId)
}
@@ -211,8 +211,8 @@ export function useSlotElementTracking(options: {
const slotKey = getSlotKey(nodeId, index, isInput)
node.slots.set(slotKey, { el, index, isInput })
// Seed measurement
scheduleNodeMeasure(nodeId)
// Seed initial sync from DOM
scheduleSlotLayoutSync(nodeId)
})
onUnmounted(() => {
@@ -233,13 +233,18 @@ export function useSlotElementTracking(options: {
})
return {
remeasure: () => scheduleNodeMeasure(nodeId)
requestSlotLayoutSync: () => scheduleSlotLayoutSync(nodeId)
}
}
export function remeasureNodeSlotsNow(
export function syncNodeSlotLayoutsNow(
nodeId: string,
origin?: { left: number; top: number }
) {
measureNodeSlotsNow(nodeId, origin?.left, origin?.top)
syncNodeSlotLayoutsFromDOM(nodeId, origin?.left, origin?.top)
}
// Optional helper for callers that are not using the composable
export function requestSlotLayoutSync(nodeId: string) {
scheduleSlotLayoutSync(nodeId)
}

View File

@@ -17,7 +17,7 @@ import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
import type { Point } from '@/renderer/core/layout/types'
import type { Bounds, NodeId } from '@/renderer/core/layout/types'
import { remeasureNodeSlotsNow } from './useSlotElementTracking'
import { syncNodeSlotLayoutsNow } from './useSlotElementTracking'
// Per-element conversion context
const elementConversion = new WeakMap<
@@ -68,8 +68,8 @@ const trackingConfigs: Map<string, ElementTrackingConfig> = new Map([
const resizeObserver = new ResizeObserver((entries) => {
// Group updates by type, then flush via each config's handler
const updatesByType = new Map<string, ElementBoundsUpdate[]>()
// Track nodes whose slots should be remeasured after node size changes
const nodesNeedingSlotRemeasure = new Set<string>()
// Track nodes whose slots should be resynced after node size changes
const nodesNeedingSlotResync = new Set<string>()
// Read container origin once per batch via cache
const { left: originLeft, top: originTop } = getCanvasClientOrigin()
@@ -130,9 +130,9 @@ const resizeObserver = new ResizeObserver((entries) => {
}
updates.push({ id: elementId, bounds })
// If this entry is a node, mark it for slot remeasure
// If this entry is a node, mark it for slot layout resync
if (elementType === 'node' && elementId) {
nodesNeedingSlotRemeasure.add(elementId)
nodesNeedingSlotResync.add(elementId)
}
}
@@ -143,9 +143,9 @@ const resizeObserver = new ResizeObserver((entries) => {
}
// After node bounds are updated, refresh slot cached offsets and layouts
if (nodesNeedingSlotRemeasure.size > 0) {
for (const nodeId of nodesNeedingSlotRemeasure) {
remeasureNodeSlotsNow(nodeId, { left: originLeft, top: originTop })
if (nodesNeedingSlotResync.size > 0) {
for (const nodeId of nodesNeedingSlotResync) {
syncNodeSlotLayoutsNow(nodeId, { left: originLeft, top: originTop })
}
}
})