Skip to content

Commit

Permalink
Remember last search and host.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsubrian committed Jul 14, 2015
1 parent 8469394 commit c041d1c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/components/handlers/BrowseHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import autobind from 'autobind-decorator'
import connectToStores from 'alt/utils/connectToStores'
import pureRender from 'pure-render-decorator'
import debounce from '../../utils/debounce'
import throttle from '../../utils/throttle'
import hostsStore from '../../stores/hostsStore'
import browseStore from '../../stores/browseStore'
import browseActions from '../../actions/browseActions'
Expand Down Expand Up @@ -77,7 +77,7 @@ class Browse extends React.Component {
}
}

@debounce(250)
@throttle(250)
fetchKeys (options = {}) {
browseActions.fetchKeys.defer(options)
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/handlers/InfoHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ImmutablePropTypes from 'react-immutable-proptypes'
import autobind from 'autobind-decorator'
import pureRender from 'pure-render-decorator'
import connectToStores from 'alt/utils/connectToStores'
import debounce from '../../utils/debounce'
import throttle from '../../utils/throttle'
import hostsStore from '../../stores/hostsStore'
import hostsActions from '../../actions/hostsActions'
import HostInfo from '../../components/HostInfo'
Expand Down Expand Up @@ -63,7 +63,7 @@ class Info extends React.Component {
}
}

@debounce(250)
@throttle(250)
fetchHostInfo (isRefresh) {
hostsActions.fetchHostInfo.defer(isRefresh)
}
Expand Down
5 changes: 4 additions & 1 deletion src/stores/browseStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import _ from 'underscore'
import Immutable from 'immutable'
import immutable from 'alt/utils/ImmutableUtil'
import regex from '../utils/regex'
import settings from '../utils/settings'
import browseActions from '../actions/browseActions'

@immutable
Expand All @@ -22,7 +23,7 @@ class BrowseStore {
this.finished = false
this.error = null
this.offset = 0
this.match = null
this.match = settings.get('browse:match', null)
this.matchRegExp = null
}

Expand All @@ -40,6 +41,7 @@ class BrowseStore {
this.offset = 0
this.match = null
this.matchRegExp = null
settings.set('browse:match', null)
}

onSetOffset (offset) {
Expand All @@ -53,6 +55,7 @@ class BrowseStore {
} else {
this.match = null
}
settings.set('browse:match', match)
}

onToggleSelectedKey (key) {
Expand Down
7 changes: 6 additions & 1 deletion src/stores/hostsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Immutable from 'immutable'
import immutable from 'alt/utils/ImmutableUtil'
import sshConfig from 'ssh-config'
import hostsActions from '../actions/hostsActions'
import settings from '../utils/settings'

@immutable
class HostsStore {
Expand Down Expand Up @@ -48,7 +49,10 @@ class HostsStore {
Host: 'localhost',
Hostname: 'localhost'
}))
this.activeHost = this.hosts.get(0)

// Set active host.
let name = settings.get('hosts:active', 'localhost')
this.activeHost = this.hosts.find((host) => host.get('Host') === name)
}

/* Connection Lifecycle
Expand All @@ -61,6 +65,7 @@ class HostsStore {
this.hostInfoLoading = false
this.hostInfoError = null
this.hostInfo = null
settings.set('hosts:active', host.get('Host'))
}
}

Expand Down
20 changes: 0 additions & 20 deletions src/utils/debounce.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/utils/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs'
import path from 'path'
import mkdirp from 'mkdirp'
import conf from '../conf'
import throttle from '../utils/throttle'

let basePath = path.join(
process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'],
Expand Down Expand Up @@ -29,6 +30,11 @@ const settings = {

set (key, value) {
cache[key] = value
settings.write()
},

@throttle(1000)
write () {
fs.writeFileSync(settingsPath, JSON.stringify(cache))
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/utils/throttle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import _ from 'underscore'

/**
* Throttle decorator.
*
* Use like:
*
* @throttle(250)
* myFunction () {
* // Probably does something.
* }
*/
function throttle (delay, options = {}) {
return function throttleDecorator (target, name, descriptor) {
descriptor.value = _.throttle(descriptor.value, delay, options)
return descriptor
}
}

export default throttle

0 comments on commit c041d1c

Please sign in to comment.