Skip to content

Commit

Permalink
Optimistic session creation (#3352)
Browse files Browse the repository at this point in the history
* Optimistically creating a session when a window is created to improve launch time

* Fix linting issues
  • Loading branch information
juancampa authored and rauchg committed Dec 20, 2018
1 parent f324a67 commit a530908
Showing 1 changed file with 54 additions and 23 deletions.
77 changes: 54 additions & 23 deletions app/ui/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
let cwd = null;
if (workingDirectory) {
Expand All @@ -108,29 +136,32 @@ module.exports = class Window {
options
);

const initSession = (opts, fn_) => {
fn_(uuid.v4(), new Session(opts));
};

initSession(sessionOpts, (uid, session) => {
sessions.set(uid, session);
rpc.emit('session add', {
rows: sessionOpts.rows,
cols: sessionOpts.cols,
uid,
splitDirection: sessionOpts.splitDirection,
shell: session.shell,
pid: session.pty.pid
});

session.on('data', data => {
rpc.emit('session data', uid + data);
});

session.on('exit', () => {
rpc.emit('session exit', {uid});
sessions.delete(uid);
});
const {uid, session} = initialSession || createSession();

sessions.set(uid, session);
rpc.emit('session add', {
rows: sessionOpts.rows,
cols: sessionOpts.cols,
uid,
splitDirection: sessionOpts.splitDirection,
shell: session.shell,
pid: session.pty.pid
});

// If this is the initial session, flush any events that might have
// occurred while the window was initializing
if (initialSession) {
initialSession.flushEvents();
initialSession = null;
}

session.on('data', data => {
rpc.emit('session data', uid + data);
});

session.on('exit', () => {
rpc.emit('session exit', {uid});
sessions.delete(uid);
});
});
rpc.on('exit', ({uid}) => {
Expand Down

0 comments on commit a530908

Please sign in to comment.