pss-langserver
Version:
A Language server for the Portable Stimulus Standard
76 lines (75 loc) • 2.74 kB
JavaScript
;
/*
* Copyright (C) 2025 Darshan(@thisisthedarshan)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.notify = notify;
exports.buildASTForFiles = buildASTForFiles;
exports.spawnProcessor = spawnProcessor;
const helpers_1 = require("./parser/helpers");
const fs_extra_1 = __importDefault(require("fs-extra"));
const worker_threads_1 = require("worker_threads");
function notify(conn, message, err = false) {
if (err) {
conn.window.showErrorMessage(message);
}
else {
conn.window.showInformationMessage(message);
}
}
/**@deprecated */
function buildASTForFiles(files) {
let pssAST = {};
for (const file of files) {
const content = fs_extra_1.default.readFileSync(file, 'utf8');
const fileURI = encodeURI("file://" + file);
// updateAST(fileURI, content).then(vars => {
// globalAST = updateASTMeta(globalAST, vars);
// });
(0, helpers_1.updateASTNew)(fileURI, content).then(vars => {
pssAST[fileURI] = vars;
});
}
return pssAST;
}
/* This is the timeout for the worker */
const MINS = 5; // 5 minutes
const WORKER_TIMEOUT_MS = MINS * 60 * 1000;
function spawnProcessor(content, uri, callback) {
let id = (0, helpers_1.generateUniqueId)();
const worker = new worker_threads_1.Worker(__dirname + '/parser/worker.js', {
workerData: { content: content, uri: uri, id: id }
});
let timeout = setTimeout(() => {
console.warn(`Worker Timed-out. De-spawning thread with id ${id}`);
worker.terminate();
}, WORKER_TIMEOUT_MS);
worker.on('message', (result) => {
clearTimeout(timeout);
callback(result);
});
worker.on('error', (err) => {
console.error('Worker Error - ', err);
});
worker.on('exit', (code) => {
if (code != 0) {
console.error(`Worker stopped with exit code ${code}`);
}
});
}