Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent node name collisions when drag-and-dropping multiple files #10979

Merged
merged 4 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/dashboard/src/components/Devtools/EnsoDevtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export function EnsoDevtools(props: EnsoDevtoolsProps) {
{getText('localStorage')}
</Text>
{unsafeEntries(LocalStorage.keyMetadata).map(([key]) => (
<div className="flex gap-1">
<div className="flex gap-1" key={key}>
<ButtonGroup className="grow-0">
<Button
size="small"
Expand Down
2 changes: 1 addition & 1 deletion app/dashboard/src/pages/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,11 @@ function DashboardInner(props: DashboardProps) {
launchedProjects.map((project) => (
<aria.TabPanel
shouldForceMount
key={project.id}
id={project.id}
className="flex min-h-0 grow [&[data-inert]]:hidden"
>
<Editor
key={project.id}
hidden={page !== project.id}
ydocUrl={ydocUrl}
project={project}
Expand Down
6 changes: 3 additions & 3 deletions app/gui2/src/components/GraphEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ const { place: nodePlacement, collapse: collapsedNodePlacement } = usePlacement(
toRef(graphNavigator, 'viewport'),
)

const { createNode, createNodes, placeNode } = provideNodeCreation(
const { scheduleCreateNode, createNodes, placeNode } = provideNodeCreation(
graphStore,
toRef(graphNavigator, 'viewport'),
toRef(graphNavigator, 'sceneMousePos'),
Expand Down Expand Up @@ -468,7 +468,7 @@ function commitComponentBrowser(
graphStore.setNodeContent(graphStore.editedNodeInfo.id, content, requiredImports)
} else if (content != '') {
// We finish creating a new node.
createNode({
scheduleCreateNode({
placement: { type: 'fixed', position: componentBrowserNodePosition.value },
expression: content,
type,
Expand Down Expand Up @@ -630,7 +630,7 @@ async function handleFileDrop(event: DragEvent) {
)
const uploadResult = await uploader.upload()
if (uploadResult.ok) {
createNode({
scheduleCreateNode({
placement: { type: 'mouseEvent', position: pos },
expression: uploadedExpression(uploadResult.value),
})
Expand Down
19 changes: 15 additions & 4 deletions app/gui2/src/composables/nodeCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Rect } from '@/util/data/rect'
import { Vec2 } from '@/util/data/vec2'
import { qnLastSegment, tryQualifiedName } from '@/util/qualifiedName'
import type { ToValue } from '@/util/reactivity'
import { toValue } from 'vue'
import { nextTick, toValue } from 'vue'
import { assert, assertNever } from 'ydoc-shared/util/assert'
import { mustExtend } from 'ydoc-shared/util/types'

Expand Down Expand Up @@ -173,8 +173,19 @@ export function useNodeCreation(
}
}

function createNode(options: NodeCreationOptions) {
createNodes([options])
let delayedNodesToCreate: NodeCreationOptions[] = []

function scheduleCreateNode(options: NodeCreationOptions) {
delayedNodesToCreate.push(options)
// Delay node creation to next tick, batch multiple synchronous createNode calls together
// to avoid node name collisions.
if (delayedNodesToCreate.length === 1) {
Copy link
Contributor

@vitvakatu vitvakatu Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A strict === 1 comparison seems suspicious, why not >= 1? It kinda contradicts the comment which says batch multiple … calls together

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It schedules a flush operation when the queue transitions from empty to non-empty. If the queue is already non-empty (i.e. after adding an element, the queue's new length is 2 or more), a flush has already been scheduled.

nextTick(() => {
const toCreate = delayedNodesToCreate
delayedNodesToCreate = []
createNodes(toCreate)
})
}
}

function newAssignmentNode(
Expand Down Expand Up @@ -208,7 +219,7 @@ export function useNodeCreation(
return ident
}

return { createNode, createNodes, placeNode }
return { scheduleCreateNode, createNodes, placeNode }
}

const operatorCodeToName: Record<string, string> = {
Expand Down
Loading