Skip to content

Commit

Permalink
adding tab check in TabbedPane.setTabIcon to avoid unhandled promise …
Browse files Browse the repository at this point in the history
…rejection after implementing appendTab
  • Loading branch information
Michael Liao committed Apr 23, 2020
1 parent cb01962 commit 10912cf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions postBuildStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
applyInspectorCommonNetworkPatch,
applyMainViewPatch,
applyPersistRequestBlockingTab,
applySetTabIconPatch,
applyShowElementsTab,
applyShowRequestBlockingTab,
} from "./src/host/polyfills/simpleView";
Expand Down Expand Up @@ -107,6 +108,7 @@ async function patchFilesForWebView(toolsOutDir: string, debugMode: boolean) {
await patchFileForWebView("ui/TabbedPane.js", toolsOutDir, true, [
applyAppendTabPatch,
applyPersistRequestBlockingTab,
applySetTabIconPatch,
]);
await patchFileForWebView("ui/ViewManager.js", toolsOutDir, true, [
applyShowElementsTab,
Expand All @@ -126,6 +128,7 @@ async function patchFilesForWebView(toolsOutDir: string, debugMode: boolean) {
await patchFileForWebView("ui/TabbedPane.js", toolsOutDir, false, [
applyAppendTabPatch,
applyPersistRequestBlockingTab,
applySetTabIconPatch,
]);
await patchFileForWebView("ui/ViewManager.js", toolsOutDir, true, [
applyShowElementsTab,
Expand Down
10 changes: 10 additions & 0 deletions src/host/polyfills/simpleView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ describe("simpleView", () => {
"this._showDrawer.bind(this, false), 'drawer-view', true, true, 'network.blocked-urls'"));
});

it("applySetTabIconPatch correctly changes text", async () => {
const apply = await import("./simpleView");
const comparableText = " setTabIcon(id, icon) {const tab = this._tabsById.get(id); tab._setIcon(icon);this._updateTabElements();}";
let fileContents = getTextFromFile("ui/TabbedPane.js");
fileContents = fileContents ? fileContents : comparableText;
const result = apply.applySetTabIconPatch(fileContents);
expect(result).not.toEqual(null);
expect(result).toEqual(expect.stringContaining("if(!tab){return;}"));
});

it("applyAppendTabPatch correctly changes text", async () => {
const apply = await import("./simpleView");
const comparableText = "appendTab(id, tabTitle, view, tabTooltip, userGesture, isCloseable, index) {";
Expand Down
12 changes: 12 additions & 0 deletions src/host/polyfills/simpleView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ export function applyPersistRequestBlockingTab(content: string) {
}
}

export function applySetTabIconPatch(content: string) {
// Patching setTabIcon so it does not throw an unhandled promise rejection when hard coded tab names are not in the tablist.
// This is needed due to applyAppendTabPatch which removes unused tabs from the tablist.
const pattern = /setTabIcon\(id,\s*icon\)\s*{\s*const tab\s*=\s*this\._tabsById\.get\(id\);/;

if (content.match(pattern)) {
return content.replace(pattern, "setTabIcon(id,icon){const tab=this._tabsById.get(id); if(!tab){return;}");
} else {
return null;
}
}

export function applyAppendTabPatch(content: string) {
// The appendTab function chooses which tabs to put in the tabbed pane header section
// showTabElement and selectTab are only called by tabs that have already been appended via appendTab.
Expand Down

0 comments on commit 10912cf

Please sign in to comment.