Use promise instead of callback

This commit is contained in:
René Pfeuffer
2020-06-09 16:53:36 +02:00
parent 5b35328e42
commit 850df6a641
3 changed files with 36 additions and 37 deletions

View File

@@ -282,10 +282,10 @@ describe("with hunks the diff expander", () => {
const subsequentHunk = diffExpander.getHunk(2).hunk; const subsequentHunk = diffExpander.getHunk(2).hunk;
fetchMock.get("http://localhost:8081/scm/api/v2/content/abc/CommitMessage.js?start=20&end=21", "new line 1"); fetchMock.get("http://localhost:8081/scm/api/v2/content/abc/CommitMessage.js?start=20&end=21", "new line 1");
let newFile: File; let newFile: File;
diffExpander.getHunk(1).expandBottom(1, file => { await diffExpander
newFile = file; .getHunk(1)
}); .expandBottom(1)
await fetchMock.flush(true); .then(file => (newFile = file));
expect(fetchMock.done()).toBe(true); expect(fetchMock.done()).toBe(true);
expect(newFile!.hunks!.length).toBe(oldHunkCount + 1); expect(newFile!.hunks!.length).toBe(oldHunkCount + 1);
expect(newFile!.hunks![1]).toBe(expandedHunk); expect(newFile!.hunks![1]).toBe(expandedHunk);
@@ -307,10 +307,10 @@ describe("with hunks the diff expander", () => {
"new line 9\nnew line 10\nnew line 11\nnew line 12\nnew line 13" "new line 9\nnew line 10\nnew line 11\nnew line 12\nnew line 13"
); );
let newFile: File; let newFile: File;
diffExpander.getHunk(1).expandHead(5, file => { await diffExpander
newFile = file; .getHunk(1)
}); .expandHead(5)
await fetchMock.flush(true); .then(file => (newFile = file));
expect(fetchMock.done()).toBe(true); expect(fetchMock.done()).toBe(true);
expect(newFile!.hunks!.length).toBe(oldHunkCount + 1); expect(newFile!.hunks!.length).toBe(oldHunkCount + 1);
expect(newFile!.hunks![0]).toBe(preceedingHunk); expect(newFile!.hunks![0]).toBe(preceedingHunk);
@@ -336,10 +336,10 @@ describe("with hunks the diff expander", () => {
"new line 40\nnew line 41\nnew line 42" "new line 40\nnew line 41\nnew line 42"
); );
let newFile: File; let newFile: File;
diffExpander.getHunk(3).expandBottom(10, file => { await diffExpander
newFile = file; .getHunk(3)
}); .expandBottom(10)
await fetchMock.flush(true); .then(file => (newFile = file));
expect(newFile!.hunks!.length).toBe(oldHunkCount + 1); expect(newFile!.hunks!.length).toBe(oldHunkCount + 1);
expect(newFile!.hunks![4].fullyExpanded).toBe(true); expect(newFile!.hunks![4].fullyExpanded).toBe(true);
}); });
@@ -349,9 +349,10 @@ describe("with hunks the diff expander", () => {
"new line 40\nnew line 41\nnew line 42" "new line 40\nnew line 41\nnew line 42"
); );
let newFile: File; let newFile: File;
diffExpander.getHunk(3).expandBottom(-1, file => { await diffExpander
newFile = file; .getHunk(3)
}); .expandBottom(-1)
.then(file => (newFile = file));
await fetchMock.flush(true); await fetchMock.flush(true);
expect(newFile!.hunks![4].fullyExpanded).toBe(true); expect(newFile!.hunks![4].fullyExpanded).toBe(true);
}); });

View File

@@ -73,10 +73,10 @@ class DiffExpander {
} }
}; };
expandHead = (n: number, count: number, callback: (newFile: File) => void) => { expandHead: (n: number, count: number) => Promise<File> = (n, count) => {
const start = this.minLineNumber(n) - Math.min(count, this.computeMaxExpandHeadRange(n)) - 1; const start = this.minLineNumber(n) - Math.min(count, this.computeMaxExpandHeadRange(n)) - 1;
const end = this.minLineNumber(n) - 1; const end = this.minLineNumber(n) - 1;
this.loadLines(start, end).then(lines => { return this.loadLines(start, end).then(lines => {
const hunk = this.file.hunks![n]; const hunk = this.file.hunks![n];
const newHunk = this.createNewHunk( const newHunk = this.createNewHunk(
@@ -86,19 +86,18 @@ class DiffExpander {
lines.length lines.length
); );
const newFile = this.addHunkToFile(newHunk, n); return this.addHunkToFile(newHunk, n);
callback(newFile);
}); });
}; };
expandBottom = (n: number, count: number, callback: (newFile: File) => void) => { expandBottom: (n: number, count: number) => Promise<File> = (n, count) => {
const maxExpandBottomRange = this.computeMaxExpandBottomRange(n); const maxExpandBottomRange = this.computeMaxExpandBottomRange(n);
const start = this.maxLineNumber(n); const start = this.maxLineNumber(n);
const end = const end =
count > 0 count > 0
? start + Math.min(count, maxExpandBottomRange > 0 ? maxExpandBottomRange : Number.MAX_SAFE_INTEGER) ? start + Math.min(count, maxExpandBottomRange > 0 ? maxExpandBottomRange : Number.MAX_SAFE_INTEGER)
: -1; : -1;
this.loadLines(start, end).then(lines => { return this.loadLines(start, end).then(lines => {
const hunk = this.file.hunks![n]; const hunk = this.file.hunks![n];
const newHunk: Hunk = this.createNewHunk( const newHunk: Hunk = this.createNewHunk(
@@ -108,8 +107,7 @@ class DiffExpander {
count count
); );
const newFile = this.addHunkToFile(newHunk, n + 1); return this.addHunkToFile(newHunk, n + 1);
callback(newFile);
}); });
}; };
@@ -178,12 +176,12 @@ class DiffExpander {
return lastChange.newLineNumber || lastChange.lineNumber!; return lastChange.newLineNumber || lastChange.lineNumber!;
}; };
getHunk: (n: number) => ExpandableHunk = (n: number) => { getHunk: (n: number) => ExpandableHunk = n => {
return { return {
maxExpandHeadRange: this.computeMaxExpandHeadRange(n), maxExpandHeadRange: this.computeMaxExpandHeadRange(n),
maxExpandBottomRange: this.computeMaxExpandBottomRange(n), maxExpandBottomRange: this.computeMaxExpandBottomRange(n),
expandHead: (count: number, callback: (newFile: File) => void) => this.expandHead(n, count, callback), expandHead: (count: number) => this.expandHead(n, count),
expandBottom: (count: number, callback: (newFile: File) => void) => this.expandBottom(n, count, callback), expandBottom: (count: number) => this.expandBottom(n, count),
hunk: this.file?.hunks![n] hunk: this.file?.hunks![n]
}; };
}; };
@@ -193,8 +191,8 @@ export type ExpandableHunk = {
hunk: Hunk; hunk: Hunk;
maxExpandHeadRange: number; maxExpandHeadRange: number;
maxExpandBottomRange: number; maxExpandBottomRange: number;
expandHead: (count: number, callback: (newFile: File) => void) => void; expandHead: (count: number) => Promise<File>;
expandBottom: (count: number, callback: (newFile: File) => void) => void; expandBottom: (count: number) => Promise<File>;
}; };
export default DiffExpander; export default DiffExpander;

View File

@@ -78,7 +78,7 @@ const ButtonWrapper = styled.div`
`; `;
const HunkDivider = styled.div` const HunkDivider = styled.div`
background: #33b2e8; background: #98d8f3;
font-size: 0.7rem; font-size: 0.7rem;
`; `;
@@ -155,7 +155,7 @@ class DiffFile extends React.Component<Props, State> {
return ( return (
<Decoration> <Decoration>
<HunkDivider> <HunkDivider>
<span onClick={() => expandableHunk.expandHead(expandableHunk.maxExpandHeadRange, this.diffExpanded)}> <span onClick={() => expandableHunk.expandHead(expandableHunk.maxExpandHeadRange).then(this.diffExpanded)}>
{this.props.t("diff.expandHeadComplete", { count: expandableHunk.maxExpandHeadRange })} {this.props.t("diff.expandHeadComplete", { count: expandableHunk.maxExpandHeadRange })}
</span> </span>
</HunkDivider> </HunkDivider>
@@ -165,10 +165,10 @@ class DiffFile extends React.Component<Props, State> {
return ( return (
<Decoration> <Decoration>
<HunkDivider> <HunkDivider>
<span onClick={() => expandableHunk.expandHead(10, this.diffExpanded)}> <span onClick={() => expandableHunk.expandHead(10).then(this.diffExpanded)}>
{this.props.t("diff.expandHeadByLines", { count: 10 })} {this.props.t("diff.expandHeadByLines", { count: 10 })}
</span>{" "} </span>{" "}
<span onClick={() => expandableHunk.expandHead(expandableHunk.maxExpandHeadRange, this.diffExpanded)}> <span onClick={() => expandableHunk.expandHead(expandableHunk.maxExpandHeadRange).then(this.diffExpanded)}>
{this.props.t("diff.expandHeadComplete", { count: expandableHunk.maxExpandHeadRange })} {this.props.t("diff.expandHeadComplete", { count: expandableHunk.maxExpandHeadRange })}
</span> </span>
</HunkDivider> </HunkDivider>
@@ -186,7 +186,7 @@ class DiffFile extends React.Component<Props, State> {
return ( return (
<Decoration> <Decoration>
<HunkDivider> <HunkDivider>
<span onClick={() => expandableHunk.expandBottom(expandableHunk.maxExpandBottomRange, this.diffExpanded)}> <span onClick={() => expandableHunk.expandBottom(expandableHunk.maxExpandBottomRange).then(this.diffExpanded)}>
{this.props.t("diff.expandBottomComplete", { count: expandableHunk.maxExpandBottomRange })} {this.props.t("diff.expandBottomComplete", { count: expandableHunk.maxExpandBottomRange })}
</span> </span>
</HunkDivider> </HunkDivider>
@@ -196,10 +196,10 @@ class DiffFile extends React.Component<Props, State> {
return ( return (
<Decoration> <Decoration>
<HunkDivider> <HunkDivider>
<span onClick={() => expandableHunk.expandBottom(10, this.diffExpanded)}> <span onClick={() => expandableHunk.expandBottom(10).then(this.diffExpanded)}>
{this.props.t("diff.expandBottomByLines", { count: 10 })} {this.props.t("diff.expandBottomByLines", { count: 10 })}
</span>{" "} </span>{" "}
<span onClick={() => expandableHunk.expandBottom(expandableHunk.maxExpandBottomRange, this.diffExpanded)}> <span onClick={() => expandableHunk.expandBottom(expandableHunk.maxExpandBottomRange).then(this.diffExpanded)}>
{this.props.t("diff.expandBottomComplete", { count: expandableHunk.maxExpandBottomRange })} {this.props.t("diff.expandBottomComplete", { count: expandableHunk.maxExpandBottomRange })}
</span> </span>
</HunkDivider> </HunkDivider>
@@ -216,10 +216,10 @@ class DiffFile extends React.Component<Props, State> {
return ( return (
<Decoration> <Decoration>
<HunkDivider> <HunkDivider>
<span onClick={() => expandableHunk.expandBottom(10, this.diffExpanded)}> <span onClick={() => expandableHunk.expandBottom(10).then(this.diffExpanded)}>
{this.props.t("diff.expandLastBottomByLines")} {this.props.t("diff.expandLastBottomByLines")}
</span>{" "} </span>{" "}
<span onClick={() => expandableHunk.expandBottom(expandableHunk.maxExpandBottomRange, this.diffExpanded)}> <span onClick={() => expandableHunk.expandBottom(expandableHunk.maxExpandBottomRange).then(this.diffExpanded)}>
{this.props.t("diff.expandLastBottomComplete")} {this.props.t("diff.expandLastBottomComplete")}
</span> </span>
</HunkDivider> </HunkDivider>