Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Speed up search by not invoking apm #1014

Merged
merged 9 commits into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions lib/atom-io-client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,16 @@ class AtomIoClient
options = {
url: "#{@baseURL}#{path}"
headers: {'User-Agent': navigator.userAgent}
json: true
gzip: true
}

request options, (err, res, body) =>
try
data = JSON.parse(body)
catch error
return callback(error)
return callback(err) if err

delete data.versions
delete body.versions
cached =
data: data
data: body
createdOn: Date.now()
localStorage.setItem(@cacheKeyForPath(path), JSON.stringify(cached))
callback(err, cached.data)
Expand Down Expand Up @@ -187,3 +186,33 @@ class AtomIoClient

getCachePath: ->
@cachePath ?= path.join(remote.app.getPath('userData'), 'Cache', 'settings-view')

search: (query, options) ->
qs = {q: query}

if options.themes
qs.filter = 'theme'
else if options.packages
qs.filter = 'package'

options = {
url: "#{@baseURL}packages/search"
headers: {'User-Agent': navigator.userAgent}
qs: qs
json: true
gzip: true
}

new Promise (resolve, reject) ->
request options, (err, res, body) ->
if err
error = new Error("Searching for \u201C#{query}\u201D failed.")
error.stderr = err.message
reject error
else
resolve(
body.filter (pkg) -> pkg.releases?.latest?
.map ({readme, metadata, downloads, stargazers_count}) ->
Object.assign metadata, {readme, downloads, stargazers_count}
.sort (a, b) -> b.downloads - a.downloads
)
12 changes: 5 additions & 7 deletions lib/install-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import path from 'path'
import electron from 'electron'
import _ from 'underscore-plus'
import {CompositeDisposable, TextEditor} from 'atom'
import etch from 'etch'

Expand Down Expand Up @@ -207,12 +206,11 @@ export default class InstallPanel {
this.refs.searchMessage.textContent = `Searching ${this.searchType} for \u201C${query}\u201D\u2026`
this.refs.searchMessage.style.display = ''

const opts = {}
opts[this.searchType] = true
opts['sortBy'] = 'downloads'
const options = {}
options[this.searchType] = true

try {
let packages = (await this.packageManager.search(query, opts)) || []
const packages = (await this.client.search(query, options)) || []
this.refs.resultsContainer.innerHTML = ''
this.refs.searchMessage.style.display = 'none'
if (packages.length === 0) {
Expand All @@ -233,7 +231,7 @@ export default class InstallPanel {
}

highlightExactMatch (container, query, packages) {
const exactMatch = _.filter(packages, pkg => pkg.name === query)[0]
const exactMatch = packages.filter(pkg => pkg.name.toLowerCase() === query)[0]

if (exactMatch) {
this.addPackageCardView(container, this.getPackageCardView(exactMatch))
Expand All @@ -242,7 +240,7 @@ export default class InstallPanel {
}

addCloseMatches (container, query, packages) {
const matches = _.filter(packages, pkg => pkg.name.indexOf(query) >= 0)
const matches = packages.filter(pkg => pkg.name.toLowerCase().indexOf(query) >= 0)

for (let pack of matches) {
this.addPackageCardView(container, this.getPackageCardView(pack))
Expand Down
30 changes: 0 additions & 30 deletions lib/package-manager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -255,36 +255,6 @@ class PackageManager
[version] = version.split('-') if typeof version is 'string'
version

search: (query, options = {}) ->
new Promise (resolve, reject) =>
args = ['search', query, '--json']
if options.themes
args.push '--themes'
else if options.packages
args.push '--packages'
errorMessage = "Searching for \u201C#{query}\u201D failed."

apmProcess = @runCommand args, (code, stdout, stderr) ->
if code is 0
try
packages = JSON.parse(stdout) ? []
if options.sortBy
packages = _.sortBy packages, (pkg) ->
return pkg[options.sortBy]*-1

resolve(packages)
catch parseError
error = createJsonParseError(errorMessage, parseError, stdout)
reject(error)
else
error = new Error(errorMessage)
error.stdout = stdout
error.stderr = stderr
reject(error)

handleProcessErrors apmProcess, errorMessage, (error) ->
reject(error)

update: (pack, newVersion, callback) ->
{name, theme, apmInstallSource} = pack

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"glob": "4.3.1",
"hosted-git-info": "^2.1.4",
"marked": "^0.3.6",
"request": "^2.40",
"request": "^2.83.0",
"roaster": "^1.1.2",
"season": "^6.0.2",
"semver": "^5.3.0",
Expand Down
4 changes: 2 additions & 2 deletions spec/install-panel-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe 'InstallPanel', ->

describe "searching packages", ->
it "highlights exact name matches", ->
spyOn(@packageManager, 'search').andCallFake ->
spyOn(@panel.client, 'search').andCallFake ->
new Promise (resolve, reject) -> resolve([ {name: 'not-first'}, {name: 'first'} ])
spyOn(@panel, 'getPackageCardView').andCallThrough()

Expand All @@ -72,7 +72,7 @@ describe 'InstallPanel', ->
expect(@panel.getPackageCardView.argsForCall[1][0].name).toEqual 'not-first'

it "prioritizes partial name matches", ->
spyOn(@packageManager, 'search').andCallFake ->
spyOn(@panel.client, 'search').andCallFake ->
new Promise (resolve, reject) -> resolve([ {name: 'something else'}, {name: 'partial name match'} ])
spyOn(@panel, 'getPackageCardView').andCallThrough()

Expand Down
1 change: 0 additions & 1 deletion spec/package-manager-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe "PackageManager", ->

it "handle errors spawning apm", ->
noSuchCommandError = if process.platform is 'win32' then ' cannot find the path ' else 'ENOENT'
waitsForPromise shouldReject: true, -> packageManager.search('test')
waitsForPromise shouldReject: true, -> packageManager.getInstalled()
waitsForPromise shouldReject: true, -> packageManager.getOutdated()
waitsForPromise shouldReject: true, -> packageManager.getFeatured()
Expand Down