Skip to content

Commit

Permalink
feat: Ensure superceded matches are removed from the document correctly
Browse files Browse the repository at this point in the history
BREAKING CHANGE
  • Loading branch information
jonathonherbert committed Dec 11, 2023
1 parent 0b954b3 commit 6790741
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/ts/state/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,15 +613,17 @@ const handleMatchesRequestSuccess = (ignoreMatch: IIgnoreMatchPredicate) => (
}

// Remove matches superceded by the incoming matches.
const blockRanges = blocksInFlight.map(_ => _.block);
let currentMatches = state.currentMatches.filter(match => {
const incomingMatchesAreOfTheSameCategory = !response.categoryIds.includes(match.category.id);
const matchIsInSameBlock = removeOverlappingRanges(
match.ranges,
blocksInFlight.map(_ => _.block)
).length > 0

return !(incomingMatchesAreOfTheSameCategory && matchIsInSameBlock);
});
const incomingMatchesAreOfTheSameCategory = response.categoryIds.includes(
match.category.id
);
const matchIsInSameBlock = match.ranges.some(
range => findOverlappingRangeIndex(range, blockRanges) !== -1
);

return !(incomingMatchesAreOfTheSameCategory && matchIsInSameBlock);
});

// Remove decorations superceded by the incoming matches.
const decsToRemove = blocksInFlight.reduce(
Expand Down
30 changes: 30 additions & 0 deletions src/ts/state/test/reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,21 @@ describe("Action handlers", () => {
]
);

it("should remove previous matches that match block and category of the incoming match", () => {
const newState = reducer(
tr,
state,
requestMatchesSuccess({
blocks: [firstBlock, secondBlock],
categoryIds: ["1", category.id],
matches: [],
requestId: exampleRequestId
})
);

expect(newState.currentMatches).toEqual([]);
});

it("should remove previous decorations that match block and category of the incoming match", () => {
const newState = reducer(
tr,
Expand Down Expand Up @@ -369,6 +384,21 @@ describe("Action handlers", () => {
expect(newState.decorations).toBe(state.decorations);
});

it("should remove superceded matches remain", () => {
const newState = reducer(
tr,
state,
requestMatchesSuccess({
blocks: [firstBlock],
categoryIds: ["another-category"],
matches: [],
requestId: exampleRequestId
})
);

expect(newState.decorations).toBe(state.decorations);
});

describe("Dirtied ranges – realtime checks (requestMatchesOnDocModified = true)", () => {
it("should not apply matches if the ranges they apply to have since been dirtied and requestMatchesOnDocModified = true", () => {
const { state, tr } = createInitialData(defaultDoc, 1337);
Expand Down

0 comments on commit 6790741

Please sign in to comment.