From 5ba9f27c5da4034b9ef06d1fa828c54d04d702bd Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Thu, 14 Jul 2016 16:40:15 -0700 Subject: [PATCH] performance improvements --- app/lib/actions/sessions.js | 23 ++++++++++------- app/lib/components/term.js | 6 +++-- app/lib/index.js | 4 +-- app/lib/reducers/sessions.js | 18 +++++++------- app/lib/utils/url-command.js | 48 ++++++++++++++++++++++-------------- app/lib/utils/url-regex.js | 4 +-- index.js | 5 +++- 7 files changed, 64 insertions(+), 44 deletions(-) diff --git a/app/lib/actions/sessions.js b/app/lib/actions/sessions.js index 92aa347c..eab2cc05 100644 --- a/app/lib/actions/sessions.js +++ b/app/lib/actions/sessions.js @@ -19,7 +19,7 @@ import { SESSION_SET_PROCESS_TITLE } from '../constants/sessions'; -export function addSession (uid) { +export function addSession (uid, shell) { return (dispatch, getState) => { const { sessions } = getState(); @@ -31,7 +31,8 @@ export function addSession (uid) { dispatch({ type: SESSION_ADD, - uid + uid, + shell }); }; } @@ -51,13 +52,14 @@ export function requestSession (uid) { } export function addSessionData (uid, data) { - return (dispatch, getState) => { + return function (dispatch, getState) { dispatch({ type: SESSION_ADD_DATA, data, effect () { - const url = getURL(data); - if (null != url) { + const { shell } = getState().sessions.sessions[uid]; + const url = getURL(shell, data); + if (null !== url) { dispatch({ type: SESSION_URL_SET, uid, @@ -101,10 +103,13 @@ export function userExitSession (uid) { type: SESSION_USER_EXIT, uid, effect () { - rpc.emit('exit', { uid }); - const sessions = keys(getState().sessions.sessions); - if (!sessions.length) { - window.close(); + const { sessions } = getState().sessions; + if (sessions[uid]) { + rpc.emit('exit', { uid }); + const sessions = keys(getState().sessions.sessions); + if (!sessions.length) { + window.close(); + } } } }); diff --git a/app/lib/components/term.js b/app/lib/components/term.js index 49c9ca3d..37c2de2e 100644 --- a/app/lib/components/term.js +++ b/app/lib/components/term.js @@ -1,4 +1,4 @@ -/* global Blob,URL */ +/* global Blob,URL,requestAnimationFrame */ import React from 'react'; import hterm from '../hterm'; import Component from '../component'; @@ -73,7 +73,9 @@ export default class Term extends Component { } write (data) { - this.term.io.print(data); + requestAnimationFrame(() => { + this.term.io.print(data); + }); } focus () { diff --git a/app/lib/index.js b/app/lib/index.js index d2714297..9312a8f4 100644 --- a/app/lib/index.js +++ b/app/lib/index.js @@ -41,8 +41,8 @@ rpc.on('ready', () => { store_.dispatch(init()); }); -rpc.on('session add', ({ uid }) => { - store_.dispatch(sessionActions.addSession(uid)); +rpc.on('session add', ({ uid, shell }) => { + store_.dispatch(sessionActions.addSession(uid, shell)); }); rpc.on('session data', ({ uid, data }) => { diff --git a/app/lib/reducers/sessions.js b/app/lib/reducers/sessions.js index 2f63bcb1..ae5d9079 100644 --- a/app/lib/reducers/sessions.js +++ b/app/lib/reducers/sessions.js @@ -25,7 +25,8 @@ function Session (obj) { title: '', write: null, url: null, - cleared: false + cleared: false, + shell: '' }).merge(obj); } @@ -37,14 +38,12 @@ function Write (obj) { } const reducer = (state = initialState, action) => { - // prune the last write to the terminal - if (state.write) { - state = state.set('write', null); - } - switch (action.type) { case SESSION_ADD: - return state.setIn(['sessions', action.uid], Session({ uid: action.uid })); + return state.setIn(['sessions', action.uid], Session({ + uid: action.uid, + shell: action.shell.split('/').pop() + })); case SESSION_URL_SET: return state.setIn(['sessions', action.uid, 'url'], action.url); @@ -65,8 +64,9 @@ const reducer = (state = initialState, action) => { }, { deep: true }); case SESSION_PTY_DATA: - return state.merge({ - write: Write(action), + return state + .set('write', Write(action)) + .merge({ sessions: { [action.uid]: { cleared: false diff --git a/app/lib/utils/url-command.js b/app/lib/utils/url-command.js index 7be1ea36..c8bc2938 100644 --- a/app/lib/utils/url-command.js +++ b/app/lib/utils/url-command.js @@ -1,30 +1,40 @@ import * as regex from './url-regex'; -export default function isUrlCommand (data) { - let match = data.match(regex.bash); - let url; +export const domainRegex = /\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b/; + +export default function isUrlCommand (shell, data) { + const matcher = regex[shell]; + if (undefined === matcher) return null; + let url, i; + + switch (matcher) { + case regex.bash: + i = 5; + break; + + case regex.zsh: + i = 7; + break; + + case regex.fish: + i = 4; + break; + } + + let match = data.match(matcher); if (match) { - url = match[5]; - } else { - match = data.match(regex.zsh); - if (match) { - url = match[7]; - } else { - match = data.match(regex.fish); - if (match) { - url = match[4]; + url = match[i]; + if (url) { + // extract the domain portion from the url + const domain = url.split('/')[0]; + if (domainRegex.test(domain)) { + return toURL(url); } } } - if (url) { - // extract the domain portion from the url - const domain = url.split('/')[0]; - if (regex.domain.test(domain)) { - return toURL(url); - } - } + return null; } function toURL (domain) { diff --git a/app/lib/utils/url-regex.js b/app/lib/utils/url-regex.js index 95831926..4324703c 100644 --- a/app/lib/utils/url-regex.js +++ b/app/lib/utils/url-regex.js @@ -1,4 +1,4 @@ -export const domain = /\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b/; -export const bash = /(ba)?sh: ((https?:\/\/)|(\/\/))?(.*): ((command not found)|(No such file or directory))/; +export const sh = /(ba)?sh: ((https?:\/\/)|(\/\/))?(.*): ((command not found)|(No such file or directory))/; +export const bash = sh; export const zsh = /zsh: ((command not found)|(no such file or directory)): ((https?:\/\/)|(\/\/))?([^\n]+)/; export const fish = /fish: Unknown command '((https?:\/\/)|(\/\/))?([^']+)'/; diff --git a/index.js b/index.js index fd004c19..fa4fdd97 100644 --- a/index.js +++ b/index.js @@ -78,7 +78,10 @@ app.on('ready', () => { rpc.on('new', ({ rows = 40, cols = 100 }) => { initSession({ rows, cols }, (uid, session) => { sessions.set(uid, session); - rpc.emit('session add', { uid }); + rpc.emit('session add', { + uid, + shell: session.shell + }); session.on('data', (data) => { rpc.emit('session data', { uid, data });