-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} | ||
} | ||
} |