Skip to content

Commit

Permalink
track old value ourselves
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Dec 13, 2023
1 parent 414045a commit 6e3451a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
12 changes: 0 additions & 12 deletions client/src/issue/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,3 @@ export async function getIssueComments(
return commentSchema.parse(val);
});
}

const REVERSE_TIMESTAMP_LENGTH = Number.MAX_SAFE_INTEGER.toString().length;

export function reverseTimestampSortKey(timestamp: number, id: string): string {
return (
Math.floor(Number.MAX_SAFE_INTEGER - timestamp)
.toString()
.padStart(REVERSE_TIMESTAMP_LENGTH, '0') +
'-' +
id
);
}
15 changes: 8 additions & 7 deletions client/src/materialite/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,22 @@ const issueComparators = Object.fromEntries(
}),
) as Record<Order, (l: Issue, r: Issue) => number>;

// We could build the indices on the fly when the user selects a given ordering.
// Creating all the orderings up front for now.
/**
* Maintains base collection and indices for issues
*/
class IssueCollection {
readonly #orderedIndices = new Map<Order, MutableSetSource<Issue>>();
readonly #base = m.newUnorderedSet<string, Issue>((issue: Issue) => issue.id);

add(issue: Issue) {
this.#base.add(issue);
for (const index of this.#orderedIndices.values()) {
index.add(issue);
}
}

delete(issue: Issue) {
this.#base.delete(issue);
for (const index of this.#orderedIndices.values()) {
index.delete(issue);
}
Expand All @@ -70,11 +74,8 @@ class IssueCollection {
index = m.newSortedSet<Issue>(issueComparators[order]);
const newIndex = index;
m.tx(() => {
const existingIndex = [...this.#orderedIndices.values()][0];
if (existingIndex) {
for (const issue of existingIndex.value) {
newIndex.add(issue);
}
for (const issue of this.#base.value.values()) {
newIndex.add(issue);
}
});
this.#orderedIndices.set(order, index);
Expand Down

0 comments on commit 6e3451a

Please sign in to comment.