Add Session class and Session options decoration by plugins (#3464)

* Add Session class and options plugin decoration

* add init() method

* Add warning for non-overrided session methods
This commit is contained in:
CHaBou 2019-02-10 22:31:52 +01:00 committed by Juan Campa
parent 3144e6b111
commit 663b16bc12
4 changed files with 60 additions and 17 deletions

View file

@ -332,9 +332,9 @@ exports.onWindow = win => {
}); });
}; };
// decorates the base object by calling plugin[key] // decorates the base entity by calling plugin[key]
// for all the available plugins // for all the available plugins
function decorateObject(base, key) { function decorateEntity(base, key, type) {
let decorated = base; let decorated = base;
modules.forEach(plugin => { modules.forEach(plugin => {
if (plugin[key]) { if (plugin[key]) {
@ -345,7 +345,7 @@ function decorateObject(base, key) {
notify('Plugin error!', `"${plugin._name}" when decorating ${key}`, {error: e}); notify('Plugin error!', `"${plugin._name}" when decorating ${key}`, {error: e});
return; return;
} }
if (res && typeof res === 'object') { if (res && (!type || typeof res === type)) {
decorated = res; decorated = res;
} else { } else {
notify('Plugin error!', `"${plugin._name}": invalid return type for \`${key}\``); notify('Plugin error!', `"${plugin._name}": invalid return type for \`${key}\``);
@ -356,6 +356,14 @@ function decorateObject(base, key) {
return decorated; return decorated;
} }
function decorateObject(base, key) {
return decorateEntity(base, key, 'object');
}
function decorateClass(base, key) {
return decorateEntity(base, key, 'function');
}
exports.getDeprecatedConfig = () => { exports.getDeprecatedConfig = () => {
const deprecated = {}; const deprecated = {};
const baseConfig = config.getConfig(); const baseConfig = config.getConfig();
@ -409,4 +417,12 @@ exports.getDecoratedBrowserOptions = defaults => {
return decorateObject(defaults, 'decorateBrowserOptions'); return decorateObject(defaults, 'decorateBrowserOptions');
}; };
exports.decorateSessionOptions = defaults => {
return decorateObject(defaults, 'decorateSessionOptions');
};
exports.decorateSessionClass = Session => {
return decorateClass(Session, 'decorateSessionClass');
};
exports._toDependencies = toDependencies; exports._toDependencies = toDependencies;

View file

@ -4,6 +4,8 @@ module.exports = {
'onWindow', 'onWindow',
'onRendererWindow', 'onRendererWindow',
'onUnload', 'onUnload',
'decorateSessionClass',
'decorateSessionOptions',
'middleware', 'middleware',
'reduceUI', 'reduceUI',
'reduceSessions', 'reduceSessions',

View file

@ -74,9 +74,17 @@ class DataBatcher extends EventEmitter {
} }
module.exports = class Session extends EventEmitter { module.exports = class Session extends EventEmitter {
constructor({uid, rows, cols: columns, cwd, shell, shellArgs}) { constructor(options) {
const osLocale = require('os-locale');
super(); super();
this.pty = null;
this.batcher = null;
this.shell = null;
this.ended = false;
this.init(options);
}
init({uid, rows, cols: columns, cwd, shell, shellArgs}) {
const osLocale = require('os-locale');
const baseEnv = Object.assign( const baseEnv = Object.assign(
{}, {},
process.env, process.env,
@ -141,24 +149,39 @@ module.exports = class Session extends EventEmitter {
} }
write(data) { write(data) {
this.pty.write(data); if (this.pty) {
this.pty.write(data);
} else {
//eslint-disable-next-line no-console
console.warn('Warning: Attempted to write to a session with no pty');
}
} }
resize({cols, rows}) { resize({cols, rows}) {
try { if (this.pty) {
this.pty.resize(cols, rows); try {
} catch (err) { this.pty.resize(cols, rows);
} catch (err) {
//eslint-disable-next-line no-console
console.error(err.stack);
}
} else {
//eslint-disable-next-line no-console //eslint-disable-next-line no-console
console.error(err.stack); console.warn('Warning: Attempted to resize a session with no pty');
} }
} }
destroy() { destroy() {
try { if (this.pty) {
this.pty.kill(); try {
} catch (err) { this.pty.kill();
} catch (err) {
//eslint-disable-next-line no-console
console.error('exit error', err.stack);
}
} else {
//eslint-disable-next-line no-console //eslint-disable-next-line no-console
console.error('exit error', err.stack); console.warn('Warning: Attempted to destroy a session with no pty');
} }
this.emit('exit'); this.emit('exit');
this.ended = true; this.ended = true;

View file

@ -14,6 +14,7 @@ const Session = require('../session');
const contextMenuTemplate = require('./contextmenu'); const contextMenuTemplate = require('./contextmenu');
const {execCommand} = require('../commands'); const {execCommand} = require('../commands');
const {setRendererType, unsetRendererType} = require('../utils/renderer-utils'); const {setRendererType, unsetRendererType} = require('../utils/renderer-utils');
const {decorateSessionOptions, decorateSessionClass} = require('../plugins');
module.exports = class Window { module.exports = class Window {
constructor(options_, cfg, fn) { constructor(options_, cfg, fn) {
@ -89,7 +90,7 @@ module.exports = class Window {
function createSession(extraOptions = {}) { function createSession(extraOptions = {}) {
const uid = uuid.v4(); const uid = uuid.v4();
const options = Object.assign( const defaultOptions = Object.assign(
{ {
rows: 40, rows: 40,
cols: 100, cols: 100,
@ -101,8 +102,9 @@ module.exports = class Window {
extraOptions, extraOptions,
{uid} {uid}
); );
const options = decorateSessionOptions(defaultOptions);
const session = new Session(options); const DecoratedSession = decorateSessionClass(Session);
const session = new DecoratedSession(options);
sessions.set(uid, session); sessions.set(uid, session);
return {session, options}; return {session, options};
} }