mirror of
https://github.com/quine-global/hyper.git
synced 2026-01-13 04:28:41 -09:00
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:
parent
3144e6b111
commit
663b16bc12
4 changed files with 60 additions and 17 deletions
|
|
@ -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
|
||||
function decorateObject(base, key) {
|
||||
function decorateEntity(base, key, type) {
|
||||
let decorated = base;
|
||||
modules.forEach(plugin => {
|
||||
if (plugin[key]) {
|
||||
|
|
@ -345,7 +345,7 @@ function decorateObject(base, key) {
|
|||
notify('Plugin error!', `"${plugin._name}" when decorating ${key}`, {error: e});
|
||||
return;
|
||||
}
|
||||
if (res && typeof res === 'object') {
|
||||
if (res && (!type || typeof res === type)) {
|
||||
decorated = res;
|
||||
} else {
|
||||
notify('Plugin error!', `"${plugin._name}": invalid return type for \`${key}\``);
|
||||
|
|
@ -356,6 +356,14 @@ function decorateObject(base, key) {
|
|||
return decorated;
|
||||
}
|
||||
|
||||
function decorateObject(base, key) {
|
||||
return decorateEntity(base, key, 'object');
|
||||
}
|
||||
|
||||
function decorateClass(base, key) {
|
||||
return decorateEntity(base, key, 'function');
|
||||
}
|
||||
|
||||
exports.getDeprecatedConfig = () => {
|
||||
const deprecated = {};
|
||||
const baseConfig = config.getConfig();
|
||||
|
|
@ -409,4 +417,12 @@ exports.getDecoratedBrowserOptions = defaults => {
|
|||
return decorateObject(defaults, 'decorateBrowserOptions');
|
||||
};
|
||||
|
||||
exports.decorateSessionOptions = defaults => {
|
||||
return decorateObject(defaults, 'decorateSessionOptions');
|
||||
};
|
||||
|
||||
exports.decorateSessionClass = Session => {
|
||||
return decorateClass(Session, 'decorateSessionClass');
|
||||
};
|
||||
|
||||
exports._toDependencies = toDependencies;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ module.exports = {
|
|||
'onWindow',
|
||||
'onRendererWindow',
|
||||
'onUnload',
|
||||
'decorateSessionClass',
|
||||
'decorateSessionOptions',
|
||||
'middleware',
|
||||
'reduceUI',
|
||||
'reduceSessions',
|
||||
|
|
|
|||
|
|
@ -74,9 +74,17 @@ class DataBatcher extends EventEmitter {
|
|||
}
|
||||
|
||||
module.exports = class Session extends EventEmitter {
|
||||
constructor({uid, rows, cols: columns, cwd, shell, shellArgs}) {
|
||||
const osLocale = require('os-locale');
|
||||
constructor(options) {
|
||||
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(
|
||||
{},
|
||||
process.env,
|
||||
|
|
@ -141,24 +149,39 @@ module.exports = class Session extends EventEmitter {
|
|||
}
|
||||
|
||||
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}) {
|
||||
try {
|
||||
this.pty.resize(cols, rows);
|
||||
} catch (err) {
|
||||
if (this.pty) {
|
||||
try {
|
||||
this.pty.resize(cols, rows);
|
||||
} catch (err) {
|
||||
//eslint-disable-next-line no-console
|
||||
console.error(err.stack);
|
||||
}
|
||||
} else {
|
||||
//eslint-disable-next-line no-console
|
||||
console.error(err.stack);
|
||||
console.warn('Warning: Attempted to resize a session with no pty');
|
||||
}
|
||||
}
|
||||
|
||||
destroy() {
|
||||
try {
|
||||
this.pty.kill();
|
||||
} catch (err) {
|
||||
if (this.pty) {
|
||||
try {
|
||||
this.pty.kill();
|
||||
} catch (err) {
|
||||
//eslint-disable-next-line no-console
|
||||
console.error('exit error', err.stack);
|
||||
}
|
||||
} else {
|
||||
//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.ended = true;
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ const Session = require('../session');
|
|||
const contextMenuTemplate = require('./contextmenu');
|
||||
const {execCommand} = require('../commands');
|
||||
const {setRendererType, unsetRendererType} = require('../utils/renderer-utils');
|
||||
const {decorateSessionOptions, decorateSessionClass} = require('../plugins');
|
||||
|
||||
module.exports = class Window {
|
||||
constructor(options_, cfg, fn) {
|
||||
|
|
@ -89,7 +90,7 @@ module.exports = class Window {
|
|||
function createSession(extraOptions = {}) {
|
||||
const uid = uuid.v4();
|
||||
|
||||
const options = Object.assign(
|
||||
const defaultOptions = Object.assign(
|
||||
{
|
||||
rows: 40,
|
||||
cols: 100,
|
||||
|
|
@ -101,8 +102,9 @@ module.exports = class Window {
|
|||
extraOptions,
|
||||
{uid}
|
||||
);
|
||||
|
||||
const session = new Session(options);
|
||||
const options = decorateSessionOptions(defaultOptions);
|
||||
const DecoratedSession = decorateSessionClass(Session);
|
||||
const session = new DecoratedSession(options);
|
||||
sessions.set(uid, session);
|
||||
return {session, options};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue