Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
refactored createAesop function to reference global variable currentP…
Browse files Browse the repository at this point in the history
…anel, stopping unwanted multi instance bug
  • Loading branch information
ArloGui committed Apr 9, 2020
1 parent 8342adf commit 8d74399
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"out": true // set this to false to include "out" folder in search results
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off"
"typescript.tsc.autoDetect": "off",
"git.ignoreLimitWarning": true
}
42 changes: 12 additions & 30 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function activate(context: vscode.ExtensionContext) {
};

const command = commands[platform];
let instances = 0;
let currentPanel: vscode.WebviewPanel | undefined = undefined;
//@TODO: if aesop already opened sb in webview - subsequent calls to aesop should not open a new webview

//set context "aesop-awake" to true; enabling views
Expand Down Expand Up @@ -234,34 +234,19 @@ export function activate(context: vscode.ExtensionContext) {
}) //close fs access

aesopEmitter.on('sb_on', () => {
createAesopOnce(PORT, host);
createAesop(PORT, host);
});

const createAesopOnce = once(createAesop);

function once(func) {

return function addedOnce(...args) {

if (instances < 1) {
instances += 1;
const panel = func(...args);
panel.onDidDispose(() => {
vscode.window.showInformationMessage(`We got a disposed`);
instances = 0
}, null, context.subscriptions);
return;
function createAesop(PORT: number, host: string): void {
//currentPanel stores our webview. If createAesop is called with an active webview open, display an error message. If not, create a new panel and reassign global variable.
if (currentPanel) {
vscode.window.showErrorMessage(`Aesop has already been run.`);
return;
}
vscode.window.showInformationMessage(`Aesop has already been run`)
throw new Error()

}
}

function createAesop(PORT, host) {

statusText.hide();
vscode.window.showInformationMessage(`Welcome to Aesop Storybook`);
const panel = vscode.window.createWebviewPanel(
currentPanel = vscode.window.createWebviewPanel(
'aesop-sb',
'Aesop',
vscode.ViewColumn.Beside,
Expand Down Expand Up @@ -293,12 +278,9 @@ export function activate(context: vscode.ExtensionContext) {
</body>
</html>`


// panel.onDidDispose(() => {
// vscode.window.showInformationMessage(`We got a disposed`);
// },
// null,
// context.subscriptions)
currentPanel.onDidDispose(() => {
currentPanel = undefined;
}, null, context.subscriptions);

return panel;
} // close createAesop helper function
Expand Down

0 comments on commit 8d74399

Please sign in to comment.