Prefetch diff for next file

If there is another file remaining in the change, fetch it in the
background while viewing the current file.

The /diff URL is cacheable by the browser for up to 1 week, allowing
the browser to skip loading the data when the next file is opened.

Change-Id: I236b11628bfacae622dc32f7b7621df7da157bf2
This commit is contained in:
Shawn Pearce
2013-08-31 17:11:17 -07:00
parent 8b97cd57a8
commit 894b0a15e5
2 changed files with 36 additions and 2 deletions

View File

@@ -64,6 +64,7 @@ class Header extends Composite {
private final String path; private final String path;
private boolean hasPrev; private boolean hasPrev;
private boolean hasNext; private boolean hasNext;
private String nextPath;
Header(KeyCommandSet keys, PatchSet.Id patchSetId, String path) { Header(KeyCommandSet keys, PatchSet.Id patchSetId, String path) {
initWidget(uiBinder.createAndBindUi(this)); initWidget(uiBinder.createAndBindUi(this));
@@ -107,10 +108,13 @@ class Header extends Composite {
break; break;
} }
} }
FileInfo nextInfo = index == files.length() - 1
? null
: files.get(index + 1);
setupNav(prev, '[', PatchUtil.C.previousFileHelp(), setupNav(prev, '[', PatchUtil.C.previousFileHelp(),
index == 0 ? null : files.get(index - 1)); index == 0 ? null : files.get(index - 1));
setupNav(next, ']', PatchUtil.C.nextFileHelp(), setupNav(next, ']', PatchUtil.C.nextFileHelp(), nextInfo);
index == files.length() - 1 ? null : files.get(index + 1)); nextPath = nextInfo != null ? nextInfo.path() : null;
} }
}); });
@@ -194,4 +198,8 @@ class Header extends Composite {
boolean hasNext() { boolean hasNext() {
return hasNext; return hasNext;
} }
String getNextPath() {
return nextPath;
}
} }

View File

@@ -257,6 +257,8 @@ public class SideBySide2 extends Screen {
cmB.setOption("viewportMargin", 10); cmB.setOption("viewportMargin", 10);
cmB.setCursor(LineCharacter.create(0)); cmB.setCursor(LineCharacter.create(0));
cmB.focus(); cmB.focus();
prefetchNextFile();
} }
@Override @Override
@@ -1340,4 +1342,28 @@ public class SideBySide2 extends Screen {
CodeMirror getCmB() { CodeMirror getCmB() {
return cmB; return cmB;
} }
private void prefetchNextFile() {
String nextPath = header.getNextPath();
if (nextPath != null) {
DiffApi.diff(revision, nextPath)
.base(base)
.wholeFile()
.intraline(pref.isIntralineDifference())
.ignoreWhitespace(pref.getIgnoreWhitespace())
.get(new AsyncCallback<DiffInfo>() {
@Override
public void onSuccess(DiffInfo info) {
new ModeInjector()
.add(getContentType(info.meta_a()))
.add(getContentType(info.meta_b()))
.inject(CallbackGroup.<Void> emptyCallback());
}
@Override
public void onFailure(Throwable caught) {
}
});
}
}
} }