()
| 58 | } |
| 59 | |
| 60 | async function closeWindowsInternal() { |
| 61 | // If there are no editors, we can skip. This seems to time out if no editors visible. |
| 62 | if (!vscode.window.visibleTextEditors || !isANotebookOpen()) { |
| 63 | // Instead just post the command |
| 64 | vscode.commands.executeCommand('workbench.action.closeAllEditors').then(noop, noop); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | class CloseEditorsTimeoutError extends Error { |
| 69 | constructor() { |
| 70 | super("Command 'workbench.action.closeAllEditors' timed out"); |
| 71 | } |
| 72 | } |
| 73 | const closeWindowsImplementation = (timeout = 1_000) => { |
| 74 | return new Promise<void>((resolve, reject) => { |
| 75 | // Attempt to fix #1301. |
| 76 | // Lets not waste too much time. |
| 77 | const timer = setTimeout(() => reject(new CloseEditorsTimeoutError()), timeout); |
| 78 | vscode.commands.executeCommand('workbench.action.closeAllEditors').then( |
| 79 | () => { |
| 80 | clearTimeout(timer); |
| 81 | resolve(); |
| 82 | }, |
| 83 | (ex) => { |
| 84 | clearTimeout(timer); |
| 85 | reject(ex); |
| 86 | } |
| 87 | ); |
| 88 | }); |
| 89 | }; |
| 90 | |
| 91 | // For some reason some times the command times out. |
| 92 | // If this happens, just wait & retry, no idea why VS Code is flaky. |
| 93 | // Lets wait & retry executing the command again, hopefully it'll work second time. |
| 94 | try { |
| 95 | await closeWindowsImplementation(); |
| 96 | } catch (ex) { |
| 97 | if (ex instanceof CloseEditorsTimeoutError) { |
| 98 | // Do nothing. Just stop waiting. |
| 99 | } else { |
| 100 | throw ex; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | function isANotebookOpen() { |
| 106 | if (!isInsiders()) { |
no test coverage detected