Skip to content

Commit

Permalink
File watcher added. Nex file viewer will update automatically if file…
Browse files Browse the repository at this point in the history
… changes in background.
  • Loading branch information
maziac committed Jan 3, 2021
1 parent 07f2a1c commit 331251e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Changelog

# 1.1.0
- Nex file viewer will update automatically if file changes in background.
## 1.0.0
- First released version.
- First beta test version.
- Register view.
- Memory dumps.
- Screen image.
Expand Down
35 changes: 28 additions & 7 deletions src/editorprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ export class EditorProvider implements vscode.CustomReadonlyEditorProvider {
webviewPanel.webview.onDidReceiveMessage(message => {
switch (message.command) {
case 'ready':
// Send file data to webview
const snaData = readFileSync(filePath);
const message = {
command: 'setData',
snaData: [...snaData]
};
webviewPanel.webview.postMessage(message);
// Send data
this.sendDataToWebView(filePath, webviewPanel);
// Establish file watcher to check for changes
const fsWatcher = vscode.workspace.createFileSystemWatcher(filePath, false, false, true);
fsWatcher.onDidChange(() => {
// Re-read data
this.sendDataToWebView(filePath, webviewPanel);
});
// Cleanup
webviewPanel.onDidDispose(() => {
fsWatcher.dispose();
});
break;
}
});
Expand All @@ -54,6 +59,22 @@ export class EditorProvider implements vscode.CustomReadonlyEditorProvider {
}


/**
* Reads the file and sends the data to the webview.
* @param filePath The file name.
* @param webviewPanel The webview to send the data to.
*/
protected sendDataToWebView(filePath: string, webviewPanel: vscode.WebviewPanel) {
// Send file data to webview
const snaData = readFileSync(filePath);
const message = {
command: 'setData',
snaData: [...snaData]
};
webviewPanel.webview.postMessage(message);
}


/**
* Returns the html code to display the text.
*/
Expand Down

0 comments on commit 331251e

Please sign in to comment.