Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Code cleanup #2: Removing timeout and adding comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhsnov committed Apr 24, 2019
1 parent 9614529 commit b28aff0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
46 changes: 42 additions & 4 deletions src/languageTools/ClientLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,27 @@ define(function (require, exports, module) {
return result;
}

/**
* This function passes Brackets's native directory path as well as the tooling commands
* required by the LanguageClient node module. This information is then maintained in memory
* in the node process server for succesfully loading and functioning of all language clients
* since it is a direct dependency.
*/
function sendLanguageClientInfo() {
//Init node with Information required by Language Client
clientInfoLoadedPromise = clientInfoDomain.exec("initialize", _bracketsPath, ToolingInfo);

function logInitializationError() {
console.error("Failed to Initialize LanguageClient Module Information.");
}

//Attach success and failure function for the clientInfoLoadedPromise
clientInfoLoadedPromise.then(function () {
clientInfoLoadedPromise.then(function (success) {
if (!success) {
logInitializationError();
return;
}

if (Array.isArray(pendingClientsToBeLoaded)) {
pendingClientsToBeLoaded.forEach(function (pendingClient) {
pendingClient.load();
Expand All @@ -130,11 +145,34 @@ define(function (require, exports, module) {
}
pendingClientsToBeLoaded = null;
}, function () {
console.error("Failed to Initialize LanguageClient Module Information.");
logInitializationError();
});
}
clientInfoDomain = new NodeDomain("LanguageClientInfo", _domainPath);
clientInfoDomain.on("requestLanguageClientInfo", sendLanguageClientInfo);

/**
* This function starts a domain which initializes the LanguageClient node module
* required by the Language Server Protocol framework in Brackets. All the LSP clients
* can only be successfully initiated once this domain has been successfully loaded and
* the LanguageClient info initialized. Refer to sendLanguageClientInfo for more.
*/
function initDomainAndHandleNodeCrash() {
clientInfoDomain = new NodeDomain("LanguageClientInfo", _domainPath);
//Initialize LanguageClientInfo once the domain has successfully loaded.
clientInfoDomain.promise().done(function () {
sendLanguageClientInfo();
//This is to handle the node failure. If the node process dies, we get an on close
//event on the websocket connection object. Brackets then spawns another process and
//restablishes the connection. Once the connection is restablished we send reinitialize
//the LanguageClient info.
clientInfoDomain.connection.on("close", function (event, reconnectedPromise) {
reconnectedPromise.done(sendLanguageClientInfo);
});
}).fail(function (err) {
console.error("ClientInfo domain could not be loaded: ", err);
});
}
initDomainAndHandleNodeCrash();


exports.initiateLanguageClient = initiateLanguageClient;
exports.syncPrefsWithDomain = syncPrefsWithDomain;
Expand Down
22 changes: 8 additions & 14 deletions src/languageTools/node/RegisterLanguageClientInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ function syncPreferences(prefs) {
global.LanguageClientInfo.preferences = prefs || global.LanguageClientInfo.preferences || {};
}

function initialize(bracketsSourcePath, toolingInfo) {
function initialize(bracketsSourcePath, toolingInfo, resolve) {
if (!bracketsSourcePath || !toolingInfo) {
resolve(true, null); //resolve with err param
}

var normalizedBracketsSourcePath = bracketsSourcePath.split(BACKWARD_SLASH).join(FORWARD_SLASH),
bracketsSourcePathArray = normalizedBracketsSourcePath.split(FORWARD_SLASH),
languageClientAbsolutePath = bracketsSourcePathArray.concat(LANGUAGE_CLIENT_RELATIVE_PATH_ARRAY).join(FORWARD_SLASH);
Expand All @@ -239,6 +243,8 @@ function initialize(bracketsSourcePath, toolingInfo) {
global.LanguageClientInfo.defaultBracketsCapabilities = defaultBracketsCapabilities;
global.LanguageClientInfo.toolingInfo = toolingInfo;
global.LanguageClientInfo.preferences = {};

resolve(null, true); //resolve with boolean denoting success
}

function init(domainManager) {
Expand All @@ -253,7 +259,7 @@ function init(domainManager) {
domainName,
"initialize",
initialize,
false,
true,
"Initialize node environment for Language Client Module",
[
{
Expand Down Expand Up @@ -285,18 +291,6 @@ function init(domainManager) {
],
[]
);

domainManager.registerEvent(
domainName,
"requestLanguageClientInfo",
[] //no parameters
);

function requestInfo() {
domainManager.emitEvent(domainName, "requestLanguageClientInfo", []);
}
//Allow the handler enough time to get registered on Brackets side.
setTimeout(requestInfo, 500);
}

exports.init = init;

0 comments on commit b28aff0

Please sign in to comment.