Skip to content

Commit

Permalink
chore: stream large lists diff
Browse files Browse the repository at this point in the history
  • Loading branch information
DoneDeal0 committed Oct 4, 2024
1 parent c6a186e commit e56dec4
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/models/stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { LIST_STATUS } from "./list";

export type StreamListsDiff<T extends Record<string, unknown>> = {
currentValue: T | null;
previousValue: T | null;
prevIndex: number | null;
newIndex: number | null;
indexDiff: number | null;
status: LIST_STATUS;
};

export type ReferenceProperty<T extends Record<string, unknown>> = keyof T;

export type StreamReferences<T extends Record<string, unknown>> = Map<
ReferenceProperty<T>,
{ prevIndex: number; nextIndex?: number }
>;

export type ListStreamOptions = {
chunksSize?: number; // 0 by default. If 0, stream will be live
showOnly?: `${LIST_STATUS}`[];
considerMoveAsUpdate?: boolean;
};

export const DEFAULT_LIST_STREAM_OPTIONS: ListStreamOptions = {
chunksSize: 0,
};
136 changes: 136 additions & 0 deletions src/stream-list-diff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import {
DEFAULT_LIST_STREAM_OPTIONS,
ListStreamOptions,
ReferenceProperty,
StreamListsDiff,
StreamReferences,
} from "./models/stream";
import { LIST_STATUS } from "./models/list";
import { isEqual } from "./utils";

function outputDiffChunk<T extends Record<string, unknown>>() {
let chunks: StreamListsDiff<T>[] = [];

return function handleDiffChunk(
chunk: StreamListsDiff<T>,
options: ListStreamOptions,
): StreamListsDiff<T>[] | null {
const showChunk = options?.showOnly
? options?.showOnly.includes(chunk.status)
: true;
if (!showChunk) {
return null;
}
if ((options.chunksSize as number) > 0) {
chunks.push(chunk);
if (chunks.length >= (options.chunksSize as number)) {
const output = chunks;
chunks = [];
return output;
}
}
return [chunk];
};
}

export function streamListsDiff<T extends Record<string, unknown>>(
prevList: T[],
nextList: T[],
referenceProperty: ReferenceProperty<T>,
options: ListStreamOptions = DEFAULT_LIST_STREAM_OPTIONS,
): void {
const listsReferences: StreamReferences<T> = new Map();
const handleDiffChunk = outputDiffChunk<T>();
prevList.forEach((data, i) => {
if (data) {
listsReferences.set(String(data[referenceProperty]), {
prevIndex: i,
nextIndex: undefined,
});
}
});

nextList.forEach((data, i) => {
if (data) {
const listReference = listsReferences.get(
String(data[referenceProperty]),
);
if (listReference) {
listReference.nextIndex = i;
} else {
handleDiffChunk(
{
previousValue: null,
currentValue: data,
prevIndex: null,
newIndex: i,
indexDiff: null,
status: LIST_STATUS.ADDED,
},
options,
);
}
}
});

for (const data of listsReferences.values()) {
if (!data.nextIndex) {
handleDiffChunk(
{
previousValue: prevList[data.prevIndex],
currentValue: null,
prevIndex: data.prevIndex,
newIndex: null,
indexDiff: null,
status: LIST_STATUS.DELETED,
},
options,
);
} else {
const prevData = prevList[data.prevIndex];
const nextData = nextList[data.nextIndex];
const isDataEqual = isEqual(prevData, nextData);
const indexDiff = data.prevIndex - data.nextIndex;
if (isDataEqual) {
if (indexDiff === 0) {
handleDiffChunk(
{
previousValue: prevList[data.prevIndex],
currentValue: nextList[data.nextIndex],
prevIndex: null,
newIndex: data.nextIndex,
indexDiff: null,
status: LIST_STATUS.EQUAL,
},
options,
);
}
handleDiffChunk(
{
previousValue: prevList[data.prevIndex],
currentValue: nextList[data.nextIndex],
prevIndex: data.prevIndex,
newIndex: data.nextIndex,
indexDiff,
status: options.considerMoveAsUpdate
? LIST_STATUS.UPDATED
: LIST_STATUS.MOVED,
},
options,
);
} else {
handleDiffChunk(
{
previousValue: prevList[data.prevIndex],
currentValue: nextList[data.nextIndex],
prevIndex: data.prevIndex,
newIndex: data.nextIndex,
indexDiff,
status: LIST_STATUS.UPDATED,
},
options,
);
}
}
}
}

0 comments on commit e56dec4

Please sign in to comment.