-
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathConnection.js
More file actions
134 lines (110 loc) · 4.17 KB
/
Copy pathConnection.js
File metadata and controls
134 lines (110 loc) · 4.17 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright (c) 2019 - present Adobe. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*global exports */
/*eslint no-console: 0*/
/* eslint no-empty: ["error", { "allowEmptyCatch": true }] */
(function () {
var protocol = require("vscode-languageserver-protocol");
var Actions = {
OnClose: {
Stop: 0,
Restart: 1
},
OnError: {
Ignore: 0,
Stop: 1
}
};
function ActionController() {
this.restartsTimes = [];
}
ActionController.prototype.getOnErrorAction = function (errorData) {
var errorCount = errorData[2];
if (errorCount <= 3) {
return Actions.OnError.Ignore;
}
return Actions.OnError.Restart;
};
ActionController.prototype.getOnCloseAction = function () {
var currentTime = Date.now();
this.restartsTimes.push(currentTime);
var numRestarts = this.restartsTimes.length;
if (numRestarts < 5) {
return Actions.OnClose.Restart;
}
var timeBetweenFiveRestarts = this.restartsTimes[numRestarts - 1] - this.restartsTimes[0];
if (timeBetweenFiveRestarts <= 3 * 60 * 1000) { //3 minutes
return Actions.OnClose.Stop;
}
this.restartsTimes.shift();
return Actions.OnClose.Restart;
};
function _getOnCloseHandler(connection, actionController, restartLanguageClient) {
return function () {
try {
if (connection) {
connection.dispose();
}
} catch (error) {}
var action = Actions.OnClose.Stop;
try {
action = actionController.getOnCloseAction();
} catch (error) {}
if (action === Actions.OnClose.Restart) {
restartLanguageClient();
}
};
}
function _getOnErrorHandler(actionController, stopLanguageClient) {
return function (errorData) {
var action = actionController.getOnErrorAction(errorData);
if (action === Actions.OnError.Stop) {
stopLanguageClient();
}
};
}
function Logger() {}
Logger.prototype.error = function (message) {
console.error(message);
};
Logger.prototype.warn = function (message) {
console.warn(message);
};
Logger.prototype.info = function (message) {
console.info(message);
};
Logger.prototype.log = function (message) {
console.log(message);
};
function createConnection(reader, writer, restartLanguageClient, stopLanguageClient) {
var logger = new Logger(),
actionController = new ActionController(),
connection = protocol.createProtocolConnection(reader, writer, logger),
errorHandler = _getOnErrorHandler(actionController, stopLanguageClient),
closeHandler = _getOnCloseHandler(connection, actionController, restartLanguageClient);
connection.onError(errorHandler);
connection.onClose(closeHandler);
return connection;
}
exports.createConnection = createConnection;
}());