-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
react-native: reduce breadcrumb size (#320)
* react-native: add factory to FileBreadcrumbsStorage and use it * react-native: add lengthChunkSplitter * react-native: add combinedChunkSplitter, use both splitters in FileBreadcrumbsStorage * react-native: add FileBreadcrumbsStorage tests * react-native: fix maximumTotalBreadcrumbsSize allowing 2x size * react-native: add tests for breadcrumbs with lines in them (NFC) * react-native: update test case in FileBreadcrumbsStorage.spec (NFC) --------- Co-authored-by: Sebastian Alex <[email protected]>
- Loading branch information
Showing
8 changed files
with
641 additions
and
22 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
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
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
33 changes: 33 additions & 0 deletions
33
packages/react-native/src/storage/combinedChunkSplitter.ts
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,33 @@ | ||
import type { Chunk, ChunkSplitter } from './Chunkifier'; | ||
|
||
/** | ||
* Combines several splitters into one. | ||
* | ||
* Each splitter is checked, in order that they are passed. | ||
* Splitters receive always the first chunk. | ||
* | ||
* If more than one splitter returns splitted chunks, the second | ||
* chunks are concatenated and treated as one chunk. | ||
* @param splitters | ||
* @returns | ||
*/ | ||
export function combinedChunkSplitter<W extends Chunk>( | ||
join: (chunks: W[]) => W, | ||
...splitters: ChunkSplitter<W>[] | ||
): ChunkSplitter<W> { | ||
return (chunk) => { | ||
const rest: W[] = []; | ||
|
||
for (const splitter of splitters) { | ||
const [c1, c2] = splitter(chunk); | ||
chunk = c1; | ||
if (c2) { | ||
// Prepend second chunk to the rest | ||
rest.unshift(c2); | ||
} | ||
} | ||
|
||
// If any chunks are in rest, concatenate them and pass as the second chunk | ||
return [chunk, rest.length ? join(rest) : undefined]; | ||
}; | ||
} |
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,57 @@ | ||
import type { ChunkSplitter } from './Chunkifier'; | ||
|
||
/** | ||
* Splits data into chunks with maximum length. | ||
* @param maxLength Maximum length of one chunk. | ||
* @param wholeLines Can be one of: | ||
* * `"skip"` - if last line does not fit in the chunk, it will be skipped entirely | ||
* * `"break"` - if last line does not fit in the chunk, it will be broken into two new chunks | ||
* * `false` - last line will be always broken into old and new chunk | ||
*/ | ||
export function lengthChunkSplitter( | ||
maxLength: number, | ||
wholeLines: 'skip' | 'break' | false = false, | ||
): ChunkSplitter<string> { | ||
let seen = 0; | ||
|
||
const emptyBuffer = ''; | ||
|
||
return function lengthChunkSplitter(data) { | ||
const remainingLength = maxLength - seen; | ||
if (data.length <= remainingLength) { | ||
seen += data.length; | ||
return [data]; | ||
} | ||
|
||
seen = 0; | ||
if (!wholeLines) { | ||
return [data.substring(0, remainingLength), data.substring(remainingLength)]; | ||
} | ||
|
||
// Check last newline before first chunk end | ||
const lastLineIndex = data.substring(0, remainingLength).lastIndexOf('\n'); | ||
|
||
// If there is no newline, pass empty buffer as the first chunk | ||
// and write all data into the second | ||
if (lastLineIndex === -1) { | ||
if (remainingLength !== maxLength) { | ||
return [emptyBuffer, data]; | ||
} | ||
|
||
if (wholeLines === 'break') { | ||
// Break the line into two chunks | ||
return [data.substring(0, remainingLength), data.substring(remainingLength)]; | ||
} else { | ||
const firstNewLine = data.indexOf('\n', remainingLength); | ||
if (firstNewLine === -1) { | ||
return [emptyBuffer]; | ||
} | ||
|
||
return [emptyBuffer, data.substring(firstNewLine + 1)]; | ||
} | ||
} | ||
|
||
// +1 - include trailing newline in first chunk, skip in second | ||
return [data.substring(0, lastLineIndex + 1), data.substring(lastLineIndex + 1)]; | ||
}; | ||
} |
Oops, something went wrong.