Skip to content

Commit

Permalink
[2262] Leverage the layout data from the frontend to synchronize diag…
Browse files Browse the repository at this point in the history
…rams

Bug: #2262
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
  • Loading branch information
sbegaudeau committed Nov 22, 2023
1 parent 7a6a17e commit 1c664a0
Show file tree
Hide file tree
Showing 27 changed files with 739 additions and 36 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [ADR-115] Split the Sirius Web frontend
- [ADR-116] Add support for changing the content of the explorer
- [ADR-117] Add support for changing the content of the details view
- [ADR-118] Share layout data between subscribers

=== Breaking changes

Expand Down Expand Up @@ -161,7 +162,8 @@ The new implementation of `IEditService`, named `ComposedEditService`, tries fir
- https://github.com/eclipse-sirius/sirius-web/issues/2235[#2235] [diagram] Reduce the amount of code in DiagramRenderer
- https://github.com/eclipse-sirius/sirius-web/issues/2572[#2572] [diagram] Each edges now have a dedicated handle.
- https://github.com/eclipse-sirius/sirius-web/issues/2584[#2584] [diagram] Add feedback during edge creation.
- https://github.com/eclipse-sirius/sirius-web/issues/2608[#2608] [diagram] Improve the placement of handles on the left and right side of nodes so that they are properly centered.
- https://github.com/eclipse-sirius/sirius-web/issues/2608[#2608] [diagram] Improve the placement of handles on the left and right side of nodes so that they are properly centered
- https://github.com/eclipse-sirius/sirius-web/issues/2262[#2262] [diagram] Share diagram layout data between the subscribers of the representation

== v2023.10.0

Expand Down
90 changes: 90 additions & 0 deletions doc/adrs/118_share_layout_data_between_subscribers.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
= ADR-118 - Share layout data between subscribers

== Context

With the switch away from Sprotty to ReactFlow, we are now computing layout data on the frontend.
This switch has thus created a new problem since we now need to share those layout data between the frontend and the backend.

We want to share the layout data computed on the frontend with the backend to keep the layout stable over time.
One should be able to close a diagram with a specific layout and reopen it with the same layout.
As such, the backend should persist the layout computed on the frontend.

We also want the server to propagate newly received layout data to other subscribers in order to synchronize multiple subscribers.
As a result, if one were to open a diagram in two different tabs, changes to the layout in one tab should be visible in real time in the other.

== Decision

=== Persist layout data computed in the frontend

The frontend needs to send layout data to the server after the execution of the layout algorithm.

After the execution of the layout on the frontend, the layout data should be sent to the server to be persisted.
We will introduce a new mutation for that.

```
type Mutation {
layoutDiagram(input: LayoutDiagramInput!): LayoutDiagramPayload!
}
```

The `DiagramEventProcessor` will only consider the input of this mutation if its `id` matches the `id` used to create the refresh in memory.
We don't want to update a diagram with the layout data of a previous diagram instance.
For that, the `DiagramEventProcessor` will be updated to store the `id` of the input responsible for the latest diagram refresh.
It will also create an unique `id` for the retrieval of the initial diagram, when it is first opened.

```
public class DiagramEventProcessor {
private UUID currentRevisionId = UUID.randomUUID();
}
```


=== Share layout data with subscribers

The diagram returned to the frontend by our subscription will now carry a new layout data field.

```

type Diagram implements Representation {
layoutData: DiagramLayoutData!
}

type DiagramLayoutData {
nodeLayoutData: [NodeLayoutData!]!
}

type NodeLayoutData {
id: ID!
position: Position!
size: Size!
}
```

This will allow all subscribers to see the layout data provided by others.
In order to prevent infinite layout between subscribers, we will need to be able to distinguish the reception of a `DiagramRefreshedEventPayload` created for a regular refresh from one created by the update of the layout data.
In the first case, the frontend will have to perform a layout but not in the second case.
For that, a new field will be introduced on `DiagramRefreshedEventPayload`.

```
type DiagramRefreshedEventPayload {
cause: RefreshCause!
}

enum RefreshCause {
refresh
layout
}
```


== Status

Accepted

== Consequences

Now that the layout will be shared between users while being computed on the frontend, we will see a significant increase in the number of HTTP requests to the backend to leverage the newly introduced mutation and new `DiagramRefreshedEventPayload` sent over our subscription to propagate layout data to all subscribers.
The performance impact of those additional payloads will have to be evaluated.

We may also witness some flickering due to temporary layout computation made by subscribers which do not have all the relevant data to perform the layout computation.
We may end up propagating the `referencePosition` and remove it as a frontend stored variable in order to minimize this effect.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
package org.eclipse.sirius.components.collaborative.diagrams;

import java.util.Objects;
import java.util.UUID;

import org.eclipse.sirius.components.collaborative.diagrams.dto.DiagramRefreshedEventPayload;
import org.eclipse.sirius.components.core.api.IInput;
import org.eclipse.sirius.components.core.api.IPayload;
import org.eclipse.sirius.components.diagrams.Diagram;
import org.slf4j.Logger;
Expand Down Expand Up @@ -44,19 +44,19 @@ public DiagramEventFlux(Diagram currentDiagram) {
this.currentDiagram = Objects.requireNonNull(currentDiagram);
}

public void diagramRefreshed(IInput input, Diagram newDiagram) {
public void diagramRefreshed(UUID id, Diagram newDiagram, String cause) {
this.currentDiagram = newDiagram;
if (this.sink.currentSubscriberCount() > 0) {
EmitResult emitResult = this.sink.tryEmitNext(new DiagramRefreshedEventPayload(input.id(), this.currentDiagram));
EmitResult emitResult = this.sink.tryEmitNext(new DiagramRefreshedEventPayload(id, this.currentDiagram, cause));
if (emitResult.isFailure()) {
String pattern = "An error has occurred while emitting a DiagramRefreshedEventPayload: {}";
this.logger.warn(pattern, emitResult);
}
}
}

public Flux<IPayload> getFlux(IInput input) {
var initialRefresh = Mono.fromCallable(() -> new DiagramRefreshedEventPayload(input.id(), this.currentDiagram));
public Flux<IPayload> getFlux(UUID id) {
var initialRefresh = Mono.fromCallable(() -> new DiagramRefreshedEventPayload(id, this.currentDiagram, DiagramRefreshedEventPayload.CAUSE_REFRESH));
return Flux.concat(initialRefresh, this.sink.asFlux());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
package org.eclipse.sirius.components.collaborative.diagrams;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

import org.eclipse.sirius.components.collaborative.api.ChangeDescription;
import org.eclipse.sirius.components.collaborative.api.ChangeKind;
Expand All @@ -26,15 +29,21 @@
import org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramEventHandler;
import org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramEventProcessor;
import org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramInput;
import org.eclipse.sirius.components.collaborative.diagrams.dto.DiagramRefreshedEventPayload;
import org.eclipse.sirius.components.collaborative.diagrams.dto.LayoutDiagramInput;
import org.eclipse.sirius.components.collaborative.diagrams.dto.NodeLayoutDataInput;
import org.eclipse.sirius.components.collaborative.diagrams.dto.RenameDiagramInput;
import org.eclipse.sirius.components.collaborative.dto.RenameRepresentationInput;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IInput;
import org.eclipse.sirius.components.core.api.IPayload;
import org.eclipse.sirius.components.core.api.IRepresentationDescriptionSearchService;
import org.eclipse.sirius.components.core.api.IRepresentationInput;
import org.eclipse.sirius.components.core.api.SuccessPayload;
import org.eclipse.sirius.components.diagrams.Diagram;
import org.eclipse.sirius.components.diagrams.description.DiagramDescription;
import org.eclipse.sirius.components.diagrams.layoutdata.DiagramLayoutData;
import org.eclipse.sirius.components.diagrams.layoutdata.NodeLayoutData;
import org.eclipse.sirius.components.representations.IRepresentation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -44,7 +53,7 @@
import reactor.core.publisher.Sinks.One;

/**
* Reacts to input that target a specific diagram, and {@link #getDiagramUpdates() publishes} updated versions of the
* Reacts to input that target a specific diagram, and {@link #getOutputEvents(IInput)} publishes} updated versions of the
* diagram to interested subscribers.
*
* @author sbegaudeau
Expand Down Expand Up @@ -72,6 +81,8 @@ public class DiagramEventProcessor implements IDiagramEventProcessor {

private final DiagramEventFlux diagramEventFlux;

private UUID currentRevisionId = UUID.randomUUID();

public DiagramEventProcessor(DiagramEventProcessorParameters parameters) {
this.logger.trace("Creating the diagram event processor {}", parameters.diagramContext().getDiagram().getId());

Expand Down Expand Up @@ -108,6 +119,31 @@ public ISubscriptionManager getSubscriptionManager() {

@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IRepresentationInput representationInput) {
if (representationInput instanceof LayoutDiagramInput layoutDiagramInput) {
if (layoutDiagramInput.id().equals(this.currentRevisionId)) {
var diagram = this.diagramContext.getDiagram();
var nodeLayoutData = layoutDiagramInput.diagramLayoutData().nodeLayoutData().stream()
.collect(Collectors.toMap(
NodeLayoutDataInput::id,
nodeLayoutDataInput -> new NodeLayoutData(nodeLayoutDataInput.id(), nodeLayoutDataInput.position(), nodeLayoutDataInput.size())
));

var layoutData = new DiagramLayoutData(nodeLayoutData, Map.of(), Map.of());
var laidOutDiagram = Diagram.newDiagram(diagram)
.layoutData(layoutData)
.build();

this.representationPersistenceService.save(this.editingContext, laidOutDiagram);
this.diagramContext.reset();
this.diagramContext.update(laidOutDiagram);
this.diagramEventFlux.diagramRefreshed(layoutDiagramInput.id(), laidOutDiagram, DiagramRefreshedEventPayload.CAUSE_LAYOUT);

payloadSink.tryEmitValue(new SuccessPayload(layoutDiagramInput.id()));
} else {
payloadSink.tryEmitValue(new SuccessPayload(layoutDiagramInput.id()));
}
}

IRepresentationInput effectiveInput = representationInput;
if (representationInput instanceof RenameRepresentationInput renameRepresentationInput) {
effectiveInput = new RenameDiagramInput(renameRepresentationInput.id(), renameRepresentationInput.editingContextId(), renameRepresentationInput.representationId(),
Expand Down Expand Up @@ -137,7 +173,9 @@ public void refresh(ChangeDescription changeDescription) {

this.diagramContext.reset();
this.diagramContext.update(refreshedDiagram);
this.diagramEventFlux.diagramRefreshed(changeDescription.getInput(), refreshedDiagram);

this.currentRevisionId = changeDescription.getInput().id();
this.diagramEventFlux.diagramRefreshed(changeDescription.getInput().id(), refreshedDiagram, DiagramRefreshedEventPayload.CAUSE_REFRESH);
}
}

Expand Down Expand Up @@ -181,7 +219,7 @@ private IRepresentationRefreshPolicy getDefaultRefreshPolicy() {
public Flux<IPayload> getOutputEvents(IInput input) {
// @formatter:off
return Flux.merge(
this.diagramEventFlux.getFlux(input),
this.diagramEventFlux.getFlux(this.currentRevisionId),
this.subscriptionManager.getFlux(input)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*******************************************************************************
* Copyright (c) 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.diagrams.dto;

import java.util.List;

/**
* Input used to receive diagram layout data.
*
* @author sbegaudeau
*/
public record DiagramLayoutDataInput(List<NodeLayoutDataInput> nodeLayoutData) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*******************************************************************************
* Copyright (c) 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.diagrams.dto;

import java.util.List;

import org.eclipse.sirius.components.diagrams.layoutdata.NodeLayoutData;

/**
* Holds the layout data of the diagram.
*
* @author sbegaudeau
*/
public record DiagramLayoutDataPayload(List<NodeLayoutData> nodeLayoutData) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
*
* @author sbegaudeau
*/
public record DiagramRefreshedEventPayload(UUID id, Diagram diagram) implements IPayload {
public record DiagramRefreshedEventPayload(UUID id, Diagram diagram, String cause) implements IPayload {

public static final String CAUSE_REFRESH = "refresh";

public static final String CAUSE_LAYOUT = "layout";

public DiagramRefreshedEventPayload {
Objects.requireNonNull(id);
Objects.requireNonNull(diagram);
Objects.requireNonNull(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
*
* @author sbegaudeau
*/
public record LayoutDiagramInput(UUID id, String editingContextId, String representationId) implements IDiagramInput {
public record LayoutDiagramInput(UUID id, String editingContextId, String representationId, DiagramLayoutDataInput diagramLayoutData) implements IDiagramInput {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright (c) 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.diagrams.dto;

import org.eclipse.sirius.components.diagrams.layoutdata.Position;
import org.eclipse.sirius.components.diagrams.layoutdata.Size;

/**
* Input used to receive node layout data.
*
* @author sbegaudeau
*/
public record NodeLayoutDataInput(String id, Position position, Size size) {
}
Loading

0 comments on commit 1c664a0

Please sign in to comment.