Skip to content

Commit

Permalink
fix: only clear spatial edges after update
Browse files Browse the repository at this point in the history
Do not clear spatial edges if the added data did not actually
upade a cell.
  • Loading branch information
oscarlorentzon committed Nov 15, 2023
1 parent 0dd4386 commit c2552fb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/graph/GraphService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
startWith,
tap,
takeLast,
withLatestFrom,
} from "rxjs/operators";

import { FilterFunction } from "./FilterCreator";
Expand Down Expand Up @@ -656,11 +657,15 @@ export class GraphService {
first(),
mergeMap(
graph => {
return graph.updateCells$(event.cellIds);
}),
withLatestFrom(this._graph$.pipe(first())),
tap(
([_, graph]) => {
graph.resetSpatialArea();
graph.resetSpatialEdges();
return graph.updateCells$(event.cellIds);
}))
.subscribe(cellId => { this._dataAdded$.next(cellId); });
.subscribe(([cellId]) => { this._dataAdded$.next(cellId); });
};

private _onDataDeleted = (event: ProviderClusterEvent): void => {
Expand Down
83 changes: 81 additions & 2 deletions test/graph/GraphService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ describe("GraphService.dataAdded$", () => {
const updateCellsSpy = spyOn(graph, "updateCells$");
updateCellsSpy.and.returnValue(observableOf("cellId"));

const resetSpatialAreaSpy =
spyOn(graph, "resetSpatialArea").and.stub();
const resetSpatialEdgesSpy =
spyOn(graph, "resetSpatialEdges").and.stub();

Expand All @@ -108,6 +110,7 @@ describe("GraphService.dataAdded$", () => {
expect(cellId).toBe("cellId");

expect(updateCellsSpy.calls.count()).toBe(1);
expect(resetSpatialAreaSpy.calls.count()).toBe(1);
expect(resetSpatialEdgesSpy.calls.count()).toBe(1);

done();
Expand All @@ -120,7 +123,7 @@ describe("GraphService.dataAdded$", () => {
});
});

it("should reset spatial edges one time for multiple updated cells", (done: Function) => {
it("should reset spatial edges for each updated cell", (done: Function) => {
const dataProvider = new DataProvider();
const api = new APIWrapper(dataProvider);
const graph = new Graph(api);
Expand All @@ -129,6 +132,8 @@ describe("GraphService.dataAdded$", () => {
const cellIds = ["cellId1", "cellId2"];
updateCellsSpy.and.returnValue(observableFrom(cellIds.slice()));

const resetSpatialAreaSpy =
spyOn(graph, "resetSpatialArea").and.stub();
const resetSpatialEdgesSpy =
spyOn(graph, "resetSpatialEdges").and.stub();

Expand All @@ -142,7 +147,9 @@ describe("GraphService.dataAdded$", () => {
expect(cellIds.includes(cellId)).toBe(true);

expect(updateCellsSpy.calls.count()).toBe(1);
expect(resetSpatialEdgesSpy.calls.count()).toBe(1);

expect(resetSpatialEdgesSpy.calls.count()).toBe(count);
expect(resetSpatialAreaSpy.calls.count()).toBe(count);

if (count === 2) { done(); }
});
Expand All @@ -154,6 +161,78 @@ describe("GraphService.dataAdded$", () => {
cellIds: cellIds.slice(),
});
});

it("should not reset spatial edges when no updated cells", () => {
const dataProvider = new DataProvider();
const api = new APIWrapper(dataProvider);
const graph = new Graph(api);

const updateCellsSpy = spyOn(graph, "updateCells$");
const cellIds = ["cellId1", "cellId2"];
updateCellsSpy.and.returnValue(observableFrom([]));

const resetSpatialAreaSpy =
spyOn(graph, "resetSpatialArea").and.stub();
const resetSpatialEdgesSpy =
spyOn(graph, "resetSpatialEdges").and.stub();

const graphService = new GraphService(graph);

graphService.dataAdded$
.subscribe(
(): void => {
fail();
});

const type: ProviderEventType = "datacreate";
dataProvider.fire(type, {
type,
target: dataProvider,
cellIds: cellIds.slice(),
});

expect(updateCellsSpy.calls.count()).toBe(1);

expect(resetSpatialEdgesSpy.calls.count()).toBe(0);
expect(resetSpatialAreaSpy.calls.count()).toBe(0);
});

it("should only reset spatial edges each updated cells", (done: Function) => {
const dataProvider = new DataProvider();
const api = new APIWrapper(dataProvider);
const graph = new Graph(api);

const updateCellsSpy = spyOn(graph, "updateCells$");
const cellIds = ["cellId1", "cellId2"];
updateCellsSpy.and.returnValue(observableFrom([cellIds[1]]));

const resetSpatialAreaSpy =
spyOn(graph, "resetSpatialArea").and.stub();
const resetSpatialEdgesSpy =
spyOn(graph, "resetSpatialEdges").and.stub();

const graphService = new GraphService(graph);

graphService.dataAdded$
.subscribe(
(cellId): void => {
expect(cellId).toBe(cellIds[1]);

expect(updateCellsSpy.calls.count()).toBe(1);

expect(resetSpatialEdgesSpy.calls.count()).toBe(1);
expect(resetSpatialAreaSpy.calls.count()).toBe(1);

done();
});

const type: ProviderEventType = "datacreate";
dataProvider.fire(type, {
type,
target: dataProvider,
cellIds: cellIds.slice(),
});
});
});

describe("GraphService.dataDeleted$", () => {
Expand Down

0 comments on commit c2552fb

Please sign in to comment.