Optimistic session creation (#3352)

* Optimistically creating a session when a window is created to improve launch time

* Fix linting issues
This commit is contained in:
Juan Campa 2018-12-19 21:03:22 -05:00 committed by Guillermo Rauch
parent f324a67bb9
commit a5309082b6

View file

@ -87,6 +87,34 @@ module.exports = class Window {
} }
}); });
function createSession(options) {
const uid = uuid.v4();
const session = new Session(Object.assign({}, options, {uid}));
sessions.set(uid, session);
return {uid, session};
}
// Optimistically create the initial session so that when the window sends
// the first "new" IPC message, there's a session already warmed up.
function createInitialSession() {
let {session, uid} = createSession({});
const initialEvents = [];
const handleData = data => initialEvents.push(['session data', uid + data]);
const handleExit = () => initialEvents.push(['session exit']);
session.on('data', handleData);
session.on('exit', handleExit);
function flushEvents() {
for (let args of initialEvents) {
rpc.emit(...args);
}
session.removeListener('data', handleData);
session.removeListener('exit', handleExit);
}
return {session, uid, flushEvents};
}
let initialSession = createInitialSession();
rpc.on('new', options => { rpc.on('new', options => {
let cwd = null; let cwd = null;
if (workingDirectory) { if (workingDirectory) {
@ -108,29 +136,32 @@ module.exports = class Window {
options options
); );
const initSession = (opts, fn_) => { const {uid, session} = initialSession || createSession();
fn_(uuid.v4(), new Session(opts));
};
initSession(sessionOpts, (uid, session) => { sessions.set(uid, session);
sessions.set(uid, session); rpc.emit('session add', {
rpc.emit('session add', { rows: sessionOpts.rows,
rows: sessionOpts.rows, cols: sessionOpts.cols,
cols: sessionOpts.cols, uid,
uid, splitDirection: sessionOpts.splitDirection,
splitDirection: sessionOpts.splitDirection, shell: session.shell,
shell: session.shell, pid: session.pty.pid
pid: session.pty.pid });
});
session.on('data', data => { // If this is the initial session, flush any events that might have
rpc.emit('session data', uid + data); // occurred while the window was initializing
}); if (initialSession) {
initialSession.flushEvents();
initialSession = null;
}
session.on('exit', () => { session.on('data', data => {
rpc.emit('session exit', {uid}); rpc.emit('session data', uid + data);
sessions.delete(uid); });
});
session.on('exit', () => {
rpc.emit('session exit', {uid});
sessions.delete(uid);
}); });
}); });
rpc.on('exit', ({uid}) => { rpc.on('exit', ({uid}) => {