-
Notifications
You must be signed in to change notification settings - Fork 2
/
DocumentWatcher.js
77 lines (59 loc) · 2.49 KB
/
DocumentWatcher.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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, setTimeout, clearTimeout */
define(function (require, exports, module) {
"use strict";
var DELAY_FOR_THROTTLING_ERROR_CHECKING = 500;
var ContinuousCompilationController = require("ContinuousCompilationController"),
documentToWatch,
timer = null;
function _cancelCompilation() {
clearTimeout(timer);
}
function _runDelayedCompilationForDocument(documentToWatch) {
//
// Run this in a timer so that we modify a document without getting
// all kinds of noise in the document in the middle of typing, which
// can be rather distracting.
//
if (timer) {
clearTimeout(timer);
timer = null;
}
timer = setTimeout(function () {
timer = null;
documentToWatch._ensureMasterEditor();
documentToWatch._masterEditor._codeMirror.operation(function () {
ContinuousCompilationController.compileCodeAndDisplayErrors(documentToWatch.getText());
});
}, DELAY_FOR_THROTTLING_ERROR_CHECKING);
}
function setDocumentToWatch(newDocumentToWatch) {
_cancelCompilation();
if (documentToWatch) {
$(documentToWatch).off("change.ContinuousCompilationController");
documentToWatch.releaseRef();
}
if (!newDocumentToWatch) {
documentToWatch = null;
} else {
if (newDocumentToWatch.getLanguage()._name !== "JavaScript") {
documentToWatch = null;
} else {
documentToWatch = newDocumentToWatch;
}
}
if (!documentToWatch) {
ContinuousCompilationController.setCodeMirrorToAddHighlightsTo(null);
ContinuousCompilationController.compileCodeAndDisplayErrors(null);
} else {
documentToWatch.addRef();
$(documentToWatch).on("change.ContinuousCompilationController", function () {
_runDelayedCompilationForDocument(documentToWatch);
});
documentToWatch._ensureMasterEditor();
ContinuousCompilationController.setCodeMirrorToAddHighlightsTo(documentToWatch._masterEditor._codeMirror);
_runDelayedCompilationForDocument(documentToWatch);
}
}
exports.setDocumentToWatch = setDocumentToWatch;
});