Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Move to utilizing the Task API
Browse files Browse the repository at this point in the history
Move to using Atom's Task API to manage the worker process.
  • Loading branch information
Arcanemagus committed Apr 27, 2017
1 parent 959b4a0 commit bf275c7
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 393 deletions.
150 changes: 91 additions & 59 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,56 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.processESLintMessages = exports.generateDebugString = exports.getDebugInfo = undefined;
exports.processESLintMessages = exports.generateDebugString = exports.getDebugInfo = exports.sendJob = undefined;

/**
* Send a job to the worker and return the results
* @param {Task} worker The worker Task to use
* @param {Object} config Configuration for the job to send to the worker
* @return {Object|String|Error} The data returned from the worker
*/
let sendJob = exports.sendJob = (() => {
var _ref = _asyncToGenerator(function* (worker, config) {
// Ensure the worker is started
try {
startWorker(worker);
} catch (e) {
throw e;
}
// Expand the config with a unique ID to emit on
// NOTE: Jobs _must_ have a unique ID as they are completely async and results
// can arrive back in any order.
config.emitKey = (0, _cryptoRandomString2.default)(10);

return new Promise(function (resolve, reject) {
const errSub = worker.on('task:error', function () {
// Re-throw errors from the task
const error = new Error(arguments.length <= 0 ? undefined : arguments[0]);
// Set the stack to the one given to us by the worker
error.stack = arguments.length <= 1 ? undefined : arguments[1];
reject(error);
});
const responseSub = worker.on(config.emitKey, function (data) {
errSub.dispose();
responseSub.dispose();
resolve(data);
});
// Send the job on to the worker
try {
worker.send(config);
} catch (e) {
console.error(e);
}
});
});

return function sendJob(_x, _x2) {
return _ref.apply(this, arguments);
};
})();

let getDebugInfo = exports.getDebugInfo = (() => {
var _ref = _asyncToGenerator(function* (worker) {
var _ref2 = _asyncToGenerator(function* (worker) {
const textEditor = atom.workspace.getActiveTextEditor();
let filePath;
let editorScopes;
Expand All @@ -32,7 +78,7 @@ let getDebugInfo = exports.getDebugInfo = (() => {
const hoursSinceRestart = Math.round(process.uptime() / 3600 * 10) / 10;
let returnVal;
try {
const response = yield worker.request('job', {
const response = yield sendJob(worker, {
type: 'debug',
config,
filePath
Expand All @@ -55,20 +101,20 @@ let getDebugInfo = exports.getDebugInfo = (() => {
return returnVal;
});

return function getDebugInfo(_x2) {
return _ref.apply(this, arguments);
return function getDebugInfo(_x4) {
return _ref2.apply(this, arguments);
};
})();

let generateDebugString = exports.generateDebugString = (() => {
var _ref2 = _asyncToGenerator(function* (worker) {
var _ref3 = _asyncToGenerator(function* (worker) {
const debug = yield getDebugInfo(worker);
const details = [`Atom version: ${debug.atomVersion}`, `linter-eslint version: ${debug.linterEslintVersion}`, `ESLint version: ${debug.eslintVersion}`, `Hours since last Atom restart: ${debug.hoursSinceRestart}`, `Platform: ${debug.platform}`, `Using ${debug.eslintType} ESLint from: ${debug.eslintPath}`, `Current file's scopes: ${JSON.stringify(debug.editorScopes, null, 2)}`, `linter-eslint configuration: ${JSON.stringify(debug.linterEslintConfig, null, 2)}`];
return details.join('\n');
});

return function generateDebugString(_x3) {
return _ref2.apply(this, arguments);
return function generateDebugString(_x5) {
return _ref3.apply(this, arguments);
};
})();

Expand All @@ -78,21 +124,21 @@ let generateDebugString = exports.generateDebugString = (() => {
* @param {Object} response The raw response from ESLint
* @param {TextEditor} textEditor The Atom::TextEditor of the file the messages belong to
* @param {bool} showRule Whether to show the rule in the messages
* @param {Object} worker The current Worker process to send Debug jobs to
* @param {Object} worker The current Worker Task to send Debug jobs to
* @return {Promise} The messages transformed into Linter messages
*/
let processESLintMessages = exports.processESLintMessages = (() => {
var _ref4 = _asyncToGenerator(function* (response, textEditor, showRule, worker) {
var _ref5 = _asyncToGenerator(function* (response, textEditor, showRule, worker) {
return Promise.all(response.map((() => {
var _ref6 = _asyncToGenerator(function* (_ref5) {
let message = _ref5.message,
line = _ref5.line,
severity = _ref5.severity,
ruleId = _ref5.ruleId,
column = _ref5.column,
fix = _ref5.fix,
endLine = _ref5.endLine,
endColumn = _ref5.endColumn;
var _ref7 = _asyncToGenerator(function* (_ref6) {
let message = _ref6.message,
line = _ref6.line,
severity = _ref6.severity,
ruleId = _ref6.ruleId,
column = _ref6.column,
fix = _ref6.fix,
endLine = _ref6.endLine,
endColumn = _ref6.endColumn;

const filePath = textEditor.getPath();
const textBuffer = textEditor.getBuffer();
Expand Down Expand Up @@ -164,26 +210,19 @@ let processESLintMessages = exports.processESLintMessages = (() => {
return ret;
});

return function (_x18) {
return _ref6.apply(this, arguments);
return function (_x20) {
return _ref7.apply(this, arguments);
};
})()));
});

return function processESLintMessages(_x14, _x15, _x16, _x17) {
return _ref4.apply(this, arguments);
return function processESLintMessages(_x16, _x17, _x18, _x19) {
return _ref5.apply(this, arguments);
};
})();

exports.spawnWorker = spawnWorker;
exports.showError = showError;

var _child_process = require('child_process');

var _child_process2 = _interopRequireDefault(_child_process);

var _processCommunication = require('process-communication');

var _path = require('path');

var _escapeHtml = require('escape-html');
Expand All @@ -196,6 +235,10 @@ var _eslintRuleDocumentation2 = _interopRequireDefault(_eslintRuleDocumentation)

var _atomLinter = require('atom-linter');

var _cryptoRandomString = require('crypto-random-string');

var _cryptoRandomString2 = _interopRequireDefault(_cryptoRandomString);

var _atom = require('atom');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
Expand All @@ -205,32 +248,21 @@ function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, a
// eslint-disable-next-line import/no-extraneous-dependencies, import/extensions


function spawnWorker() {
const env = Object.create(process.env);

delete env.NODE_PATH;
delete env.NODE_ENV;
delete env.OS;

const child = _child_process2.default.fork((0, _path.join)(__dirname, 'worker.js'), [], { env, silent: true });
const worker = (0, _processCommunication.createFromProcess)(child);

child.stdout.on('data', chunk => {
console.log('[Linter-ESLint] STDOUT', chunk.toString());
});
child.stderr.on('data', chunk => {
console.log('[Linter-ESLint] STDERR', chunk.toString());
});

return {
worker,
subscription: new _atom.Disposable(() => {
worker.kill();
})
};
}

function showError(givenMessage) {
/**
* Start the worker process if it hasn't already been started
* @param {Task} worker The worker process reference to act on
* @return {undefined}
*/
const startWorker = worker => {
if (worker.started) {
// Worker start request has already been sent
return;
}
// Send empty arguments as we don't use them in the worker
worker.start([]);
// NOTE: Modifies the Task of the worker, but it's the only clean way to track this
worker.started = true;
};function showError(givenMessage) {
let givenDetail = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

let detail;
Expand All @@ -257,7 +289,7 @@ function validatePoint(textEditor, line, col) {
}

const generateInvalidTrace = (() => {
var _ref3 = _asyncToGenerator(function* (msgLine, msgCol, msgEndLine, msgEndCol, eslintFullRange, filePath, textEditor, ruleId, message, worker) {
var _ref4 = _asyncToGenerator(function* (msgLine, msgCol, msgEndLine, msgEndCol, eslintFullRange, filePath, textEditor, ruleId, message, worker) {
let errMsgRange = `${msgLine + 1}:${msgCol}`;
if (eslintFullRange) {
errMsgRange += ` - ${msgEndLine + 1}:${msgEndCol + 1}`;
Expand Down Expand Up @@ -288,7 +320,7 @@ const generateInvalidTrace = (() => {
};
});

return function generateInvalidTrace(_x4, _x5, _x6, _x7, _x8, _x9, _x10, _x11, _x12, _x13) {
return _ref3.apply(this, arguments);
return function generateInvalidTrace(_x6, _x7, _x8, _x9, _x10, _x11, _x12, _x13, _x14, _x15) {
return _ref4.apply(this, arguments);
};
})();
Loading

0 comments on commit bf275c7

Please sign in to comment.