From a5309082b6fd81b7f1f03ebc488081194acf337c Mon Sep 17 00:00:00 2001 From: Juan Campa Date: Wed, 19 Dec 2018 21:03:22 -0500 Subject: [PATCH] Optimistic session creation (#3352) * Optimistically creating a session when a window is created to improve launch time * Fix linting issues --- app/ui/window.js | 71 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/app/ui/window.js b/app/ui/window.js index c1c0984f..078ff54c 100644 --- a/app/ui/window.js +++ b/app/ui/window.js @@ -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) { @@ -108,29 +136,32 @@ module.exports = class Window { options ); - const initSession = (opts, fn_) => { - fn_(uuid.v4(), new Session(opts)); - }; + const {uid, session} = initialSession || createSession(); - 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 - }); + 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); - }); + // 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('exit', () => { - rpc.emit('session exit', {uid}); - sessions.delete(uid); - }); + 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}) => {