forked from maty21/mocha-sidebar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changesNotification.js
110 lines (102 loc) · 4.51 KB
/
changesNotification.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const vscode = require('vscode');
const Glob = require('glob').Glob;
const path = require('path');
const { runTestsOnSave, files, sideBarOptions, coverage } = require('./config');
const codeCoverage = require('./lib/coverage/code-coverage');
class changesNotification {
constructor(mochaProvider, lensProvider) {
this._autoPlayOnSave = (runTestsOnSave() == 'true');
this._mochaProvider = mochaProvider;
this._activeEditor = vscode.window.activeTextEditor;
this._lensProvider = lensProvider;
this.onChangeTimeOutActive = false;
this.ON_CHANGE_TIME_OUT = 2000;
vscode.window.onDidChangeActiveTextEditor(editor => {
console.log(`onDidChangeActiveTextEditor: ${editor}`)
this._updatePathOnTestChange(editor, 1000)
if (coverage().enable) {
codeCoverage.updateDecorationByFile();
}
// this._mochaProvider.updateDecorations(editor.document.fileName);
})
vscode.workspace.onDidSaveTextDocument(editor => {
if (this._autoPlayOnSave) {
this._mochaProvider.runAllTests(this._mochaProvider.item);
}
if (coverage().enable && coverage().runCoverageAfterFileSave) {
codeCoverage.runViaRequest();
}
console.log(`onDidSaveTextDocument: ${editor}`)
})
vscode.debug.onDidChangeBreakpoints(breakpoints => {
// console.log(editor);
/* Object.values(breakpoints).forEach(v => {
if (v.length > 0) {
codeCoverage.updateDecorationAfterNotification(v[0].location.uri.path, v[0].location.range._start._line + 1);
}
}) */
codeCoverage.updateDecorationByFile();
})
vscode.workspace.onDidChangeTextDocument(editor => {
//just a simple semaphore for avoiding multiple calls
if (!this.onChangeTimeOutActive) {
this.onChangeTimeOutActive = true;
setTimeout(() => {
console.log(`onDidChangeTextDocument: ${editor}`)
this._updatePathOnTestChange(editor, sideBarOptions().autoUpdateTime)
this.onChangeTimeOutActive = false;
}, this.ON_CHANGE_TIME_OUT);
}
})
}
//avoiding extra time out
_calcAutoUpdateTime() {
if (this.ON_CHANGE_TIME_OUT > sideBarOptions().autoUpdateTime) {
return 100;
}
return sideBarOptions.autoUpdateTime - this.ON_CHANGE_TIME_OUT;
}
start() {
this._autoPlayOnSave = true;
}
pause() {
this._autoPlayOnSave = false;
}
_updatePathOnTestChange(editor, timeOut) {
let { ignore, glob } = files();
this.isTimeOutActive = false;
new Glob(files().glob, { cwd: vscode.workspace.rootPath, ignore },
(err, files) => {
if (err) {
return err;
}
else if (!editor) {
console.warn(`notification:editor is not defined therefore tests will not updates
please refresh it manually via side bar `);
return;
}
else {
files.forEach(f => {
if (path.normalize(`${vscode.workspace.rootPath}/${f}`) == editor.document.fileName) {
console.log(`true`);
if (!this.isTimeOutActive) {
this.isTimeOutActive = true;
console.log(`notification: changes detected update will start in ${timeOut} seconds`);
setTimeout(() => {
console.log(`notification: timeout reached activating and set time out to active again`);
this._mochaProvider.refreshExplorer();
this._mochaProvider.updateDecorations(vscode.workspace.rootPath);
this._lensProvider.raiseEventOnUpdate();
this.isTimeOutActive = false;
}, this._calcAutoUpdateTime());
}
else {
console.log(`notification: ignore changes timeout active`)
}
}
})
}
})
}
}
module.exports = changesNotification;